public void createModule( final JavaPackage topLevelPackage, final String name, final GAV parent, final PackagingType packagingType) { Assert.isTrue(isCreateModuleAvailable(), "Cannot create modules at this time"); final String moduleName = StringUtils.defaultIfEmpty(name, topLevelPackage.getLastElement()); final GAV module = new GAV(topLevelPackage.getFullyQualifiedPackageName(), moduleName, parent.getVersion()); final ProjectMetadata project = getProjectMetadata(); // TODO create or update "modules" element of parent module's POM // Create the new module's directory, named by its artifactId (Maven standard practice) fileManager.createDirectory(moduleName); // Focus the new module so that artifacts created below go to the correct path(s) focus(module); packagingType.createArtifacts(topLevelPackage, name, "${java.version}", parent); }
/** {@inheritDoc} */ public void annotateType(final JavaType type, final JavaPackage javaPackage) { // TODO check for existing controller // Use Roo's Assert type for null checks Validate.notNull(type, "Java type required"); Validate.notNull(javaPackage, "Java package required, web mvc all command package required"); // Obtain ClassOrInterfaceTypeDetails for this java type ClassOrInterfaceTypeDetails entityDetails = typeLocationService.getTypeDetails(type); // Test if the annotation already exists on the target type if (entityDetails != null && MemberFindingUtils.getAnnotationOfType( entityDetails.getAnnotations(), new JavaType(RooEnvers.class.getName())) == null) { ClassOrInterfaceTypeDetailsBuilder classOrInterfaceTypeDetailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(entityDetails); // Create JavaType instance for the add-ons trigger annotation JavaType rooEnvers = new JavaType(RooEnvers.class.getName()); // Create Annotation metadata AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(rooEnvers); // Add annotation to target type classOrInterfaceTypeDetailsBuilder.addAnnotation(annotationBuilder.build()); // Save changes to disk typeManagementService.createOrUpdateTypeOnDisk(classOrInterfaceTypeDetailsBuilder.build()); } // Get details for existing controller JavaType typeController = new JavaType( javaPackage.getFullyQualifiedPackageName() + "." + type.getSimpleTypeName() + "Controller"); ClassOrInterfaceTypeDetails typeControllerDetails = typeLocationService.getTypeDetails(typeController); // Add annotation @RooEnversController to existing controller ClassOrInterfaceTypeDetailsBuilder classOrInterfaceTypeDetailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(typeControllerDetails); JavaType rooEnversController = new JavaType("de.eightbitboy.roo.envers.controller.RooEnversController"); final List<AnnotationAttributeValue<?>> rooEnversControllerAttributes = new ArrayList<AnnotationAttributeValue<?>>(); rooEnversControllerAttributes.add(new ClassAttributeValue(new JavaSymbolName("type"), type)); AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(rooEnversController, rooEnversControllerAttributes); classOrInterfaceTypeDetailsBuilder.addAnnotation(annotationBuilder.build()); typeManagementService.createOrUpdateTypeOnDisk(classOrInterfaceTypeDetailsBuilder.build()); }
/** * Appends the presented class to the end of the presented body declarations. The body * declarations appear within the presented compilation unit. This is used to progressively build * inner types. * * @param compilationUnit the work-in-progress compilation unit (required) * @param enclosingCompilationUnitServices * @param cid the new class to add (required) * @param parent the class body declarations a subclass should be added to (may be null, which * denotes a top-level type within the compilation unit) */ private void updateOutput( final CompilationUnit compilationUnit, CompilationUnitServices enclosingCompilationUnitServices, final ClassOrInterfaceTypeDetails cid, final List<BodyDeclaration> parent) { // Append the new imports this class declares Validate.notNull( compilationUnit.getImports(), "Compilation unit imports should be non-null when producing type '%s'", cid.getName()); for (final ImportMetadata importType : cid.getRegisteredImports()) { ImportDeclaration importDeclaration; if (!importType.isAsterisk()) { NameExpr typeToImportExpr; if (importType.getImportType().getEnclosingType() == null) { typeToImportExpr = new QualifiedNameExpr( new NameExpr( importType.getImportType().getPackage().getFullyQualifiedPackageName()), importType.getImportType().getSimpleTypeName()); } else { typeToImportExpr = new QualifiedNameExpr( new NameExpr( importType.getImportType().getEnclosingType().getFullyQualifiedTypeName()), importType.getImportType().getSimpleTypeName()); } importDeclaration = new ImportDeclaration(typeToImportExpr, importType.isStatic(), false); } else { importDeclaration = new ImportDeclaration( new NameExpr(importType.getImportPackage().getFullyQualifiedPackageName()), importType.isStatic(), importType.isAsterisk()); } JavaParserCommentMetadataBuilder.updateCommentsToJavaParser( importDeclaration, importType.getCommentStructure()); compilationUnit.getImports().add(importDeclaration); } // Create a class or interface declaration to represent this actual type final int javaParserModifier = JavaParserUtils.getJavaParserModifier(cid.getModifier()); TypeDeclaration typeDeclaration; ClassOrInterfaceDeclaration classOrInterfaceDeclaration; // Implements handling final List<ClassOrInterfaceType> implementsList = new ArrayList<ClassOrInterfaceType>(); for (final JavaType current : cid.getImplementsTypes()) { implementsList.add(JavaParserUtils.getResolvedName(cid.getName(), current, compilationUnit)); } if (cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE || cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS) { final boolean isInterface = cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE; if (parent == null) { // Top level type typeDeclaration = new ClassOrInterfaceDeclaration( javaParserModifier, isInterface, cid.getName() .getNameIncludingTypeParameters() .replace(cid.getName().getPackage().getFullyQualifiedPackageName() + ".", "")); classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration; } else { // Inner type typeDeclaration = new ClassOrInterfaceDeclaration( javaParserModifier, isInterface, cid.getName().getSimpleTypeName()); classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration; if (cid.getName().getParameters().size() > 0) { classOrInterfaceDeclaration.setTypeParameters(new ArrayList<TypeParameter>()); for (final JavaType param : cid.getName().getParameters()) { NameExpr pNameExpr = JavaParserUtils.importTypeIfRequired( cid.getName(), compilationUnit.getImports(), param); final String tempName = StringUtils.replace(pNameExpr.toString(), param.getArgName() + " extends ", "", 1); pNameExpr = new NameExpr(tempName); final ClassOrInterfaceType pResolvedName = JavaParserUtils.getClassOrInterfaceType(pNameExpr); classOrInterfaceDeclaration .getTypeParameters() .add( new TypeParameter( param.getArgName().getSymbolName(), Collections.singletonList(pResolvedName))); } } } // Superclass handling final List<ClassOrInterfaceType> extendsList = new ArrayList<ClassOrInterfaceType>(); for (final JavaType current : cid.getExtendsTypes()) { if (!OBJECT.equals(current)) { extendsList.add(JavaParserUtils.getResolvedName(cid.getName(), current, compilationUnit)); } } if (extendsList.size() > 0) { classOrInterfaceDeclaration.setExtends(extendsList); } // Implements handling if (implementsList.size() > 0) { classOrInterfaceDeclaration.setImplements(implementsList); } } else { typeDeclaration = new EnumDeclaration(javaParserModifier, cid.getName().getSimpleTypeName()); } typeDeclaration.setMembers(new ArrayList<BodyDeclaration>()); Validate.notNull( typeDeclaration.getName(), "Missing type declaration name for '%s'", cid.getName()); // If adding a new top-level type, must add it to the compilation unit // types Validate.notNull( compilationUnit.getTypes(), "Compilation unit types must not be null when attempting to add '%s'", cid.getName()); if (parent == null) { // Top-level class compilationUnit.getTypes().add(typeDeclaration); } else { // Inner class parent.add(typeDeclaration); } // If the enclosing CompilationUnitServices was not provided a default // CompilationUnitServices needs to be created if (enclosingCompilationUnitServices == null) { // Create a compilation unit so that we can use JavaType*Metadata // static methods directly enclosingCompilationUnitServices = new CompilationUnitServices() { @Override public JavaPackage getCompilationUnitPackage() { return cid.getName().getPackage(); } @Override public JavaType getEnclosingTypeName() { return cid.getName(); } @Override public List<ImportDeclaration> getImports() { return compilationUnit.getImports(); } @Override public List<TypeDeclaration> getInnerTypes() { return compilationUnit.getTypes(); } @Override public PhysicalTypeCategory getPhysicalTypeCategory() { return cid.getPhysicalTypeCategory(); } }; } final CompilationUnitServices finalCompilationUnitServices = enclosingCompilationUnitServices; // A hybrid CompilationUnitServices must be provided that references the // enclosing types imports and package final CompilationUnitServices compilationUnitServices = new CompilationUnitServices() { @Override public JavaPackage getCompilationUnitPackage() { return finalCompilationUnitServices.getCompilationUnitPackage(); } @Override public JavaType getEnclosingTypeName() { return cid.getName(); } @Override public List<ImportDeclaration> getImports() { return finalCompilationUnitServices.getImports(); } @Override public List<TypeDeclaration> getInnerTypes() { return compilationUnit.getTypes(); } @Override public PhysicalTypeCategory getPhysicalTypeCategory() { return cid.getPhysicalTypeCategory(); } }; // Add type annotations final List<AnnotationExpr> annotations = new ArrayList<AnnotationExpr>(); typeDeclaration.setAnnotations(annotations); for (final AnnotationMetadata candidate : cid.getAnnotations()) { JavaParserAnnotationMetadataBuilder.addAnnotationToList( compilationUnitServices, annotations, candidate); } // ROO-3834: Generating default Javadoc inside class if class doesn't contains JavaDoc List<Comment> classComments = compilationUnit.getComments(); if (classComments == null || classComments.isEmpty()) { CommentStructure defaultCommentStructure = new CommentStructure(); String defaultComment = "/**\n * = " .concat(cid.getType().getSimpleTypeName()) .concat("\n *\n * TODO Auto-generated class documentation\n *\n */\n"); defaultCommentStructure.addComment( new JavadocComment(defaultComment), CommentLocation.BEGINNING); if (annotations.isEmpty()) { JavaParserCommentMetadataBuilder.updateCommentsToJavaParser( typeDeclaration, defaultCommentStructure); } else { // If exists some annotation, include comment before the existing annotations AnnotationExpr firstAnnotation = annotations.get(0); JavaParserCommentMetadataBuilder.updateCommentsToJavaParser( firstAnnotation, defaultCommentStructure); } } // Add enum constants and interfaces if (typeDeclaration instanceof EnumDeclaration && cid.getEnumConstants().size() > 0) { final EnumDeclaration enumDeclaration = (EnumDeclaration) typeDeclaration; final List<EnumConstantDeclaration> constants = new ArrayList<EnumConstantDeclaration>(); enumDeclaration.setEntries(constants); for (final JavaSymbolName constant : cid.getEnumConstants()) { addEnumConstant(constants, constant); } // Implements handling if (implementsList.size() > 0) { enumDeclaration.setImplements(implementsList); } } // Add fields for (final FieldMetadata candidate : cid.getDeclaredFields()) { JavaParserFieldMetadataBuilder.addField( compilationUnitServices, typeDeclaration.getMembers(), candidate); } // Add constructors for (final ConstructorMetadata candidate : cid.getDeclaredConstructors()) { JavaParserConstructorMetadataBuilder.addConstructor( compilationUnitServices, typeDeclaration.getMembers(), candidate, null); } // Add methods for (final MethodMetadata candidate : cid.getDeclaredMethods()) { JavaParserMethodMetadataBuilder.addMethod( compilationUnitServices, typeDeclaration.getMembers(), candidate, null); } // Add inner types for (final ClassOrInterfaceTypeDetails candidate : cid.getDeclaredInnerTypes()) { updateOutput( compilationUnit, compilationUnitServices, candidate, typeDeclaration.getMembers()); } final HashSet<String> imported = new HashSet<String>(); final ArrayList<ImportDeclaration> imports = new ArrayList<ImportDeclaration>(); for (final ImportDeclaration importDeclaration : compilationUnit.getImports()) { JavaPackage importPackage = null; JavaType importType = null; if (importDeclaration.isAsterisk()) { importPackage = new JavaPackage(importDeclaration.getName().toString()); } else { importType = new JavaType(importDeclaration.getName().toString()); importPackage = importType.getPackage(); } if (importPackage.equals(cid.getName().getPackage()) && importDeclaration.isAsterisk()) { continue; } if (importPackage.equals(cid.getName().getPackage()) && importType != null && importType.getEnclosingType() == null) { continue; } if (importType != null && importType.equals(cid.getName())) { continue; } if (!imported.contains(importDeclaration.getName().toString())) { imports.add(importDeclaration); imported.add(importDeclaration.getName().toString()); } } Collections.sort( imports, new Comparator<ImportDeclaration>() { @Override public int compare( final ImportDeclaration importDeclaration, final ImportDeclaration importDeclaration1) { return importDeclaration .getName() .toString() .compareTo(importDeclaration1.getName().toString()); } }); compilationUnit.setImports(imports); }
private void reverseEngineer(final Database database) { final Set<ClassOrInterfaceTypeDetails> managedEntities = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(ROO_DB_MANAGED); // Determine whether to create "active record" CRUD methods database.setActiveRecord(isActiveRecord(database, managedEntities)); // Lookup the relevant destination package if not explicitly given final JavaPackage destinationPackage = getDestinationPackage(database, managedEntities); // Set the destination package in the database database.setDestinationPackage(destinationPackage); // Get tables from database final Set<Table> tables = new LinkedHashSet<Table>(database.getTables()); // Manage existing entities with @RooDbManaged annotation for (final ClassOrInterfaceTypeDetails managedEntity : managedEntities) { // Remove table from set as each managed entity is processed. // The tables that remain in the set will be used for creation of // new entities later final Table table = updateOrDeleteManagedEntity(managedEntity, database); if (table != null) { tables.remove(table); } } // Create new entities from tables final List<ClassOrInterfaceTypeDetails> newEntities = new ArrayList<ClassOrInterfaceTypeDetails>(); for (final Table table : tables) { // Don't create types from join tables in many-to-many associations if (!table.isJoinTable()) { JavaPackage schemaPackage = destinationPackage; if (database.hasMultipleSchemas()) { schemaPackage = new JavaPackage( destinationPackage.getFullyQualifiedPackageName() + "." + DbreTypeUtils.suggestPackageName(table.getSchema().getName())); } final JavaType javaType = DbreTypeUtils.suggestTypeNameForNewTable(table.getName(), schemaPackage); final boolean activeRecord = database.isActiveRecord() && !database.isRepository(); if (typeLocationService.getTypeDetails(javaType) == null) { table.setIncludeNonPortableAttributes(database.isIncludeNonPortableAttributes()); table.setDisableVersionFields(database.isDisableVersionFields()); table.setDisableGeneratedIdentifiers(database.isDisableGeneratedIdentifiers()); newEntities.add(createNewManagedEntityFromTable(javaType, table, activeRecord)); } } } // Create repositories if required if (database.isRepository()) { for (final ClassOrInterfaceTypeDetails entity : newEntities) { final JavaType type = entity.getType(); repositoryJpaOperations.setupRepository( new JavaType(type.getFullyQualifiedTypeName() + "Repository"), type); } } // Create services if required if (database.isService()) { for (final ClassOrInterfaceTypeDetails entity : newEntities) { final JavaType type = entity.getType(); final String typeName = type.getFullyQualifiedTypeName(); serviceOperations.setupService( new JavaType(typeName + "Service"), new JavaType(typeName + "ServiceImpl"), type, false, "", false, false); } } // Create integration tests if required if (database.isTestAutomatically()) { for (final ClassOrInterfaceTypeDetails entity : newEntities) { integrationTestOperations.newIntegrationTest(entity.getType()); } } // Notify final List<ClassOrInterfaceTypeDetails> allEntities = new ArrayList<ClassOrInterfaceTypeDetails>(); allEntities.addAll(newEntities); allEntities.addAll(managedEntities); notify(allEntities); }