Search in sources :

Example 1 with ImportMetadata

use of org.springframework.roo.classpath.details.ImportMetadata in project spring-roo by spring-projects.

the class PushInOperationsImpl method pushInClass.

/**
 * Makes push-in of all items defined on a provided class
 *
 * @param klass
 *            class to make the push-in operation
 * @param weiteOnDisk
 *            indicates if pushed elements should be writed on .java file
 * @param force
 *            if some operation will produce several changes, this parameter
 *            should be true.
 *
 * @return list of objects with all the pushed elements.
 */
public List<Object> pushInClass(JavaType klass, boolean writeOnDisk, boolean force) {
    List<Object> pushedElements = new ArrayList<Object>();
    // Check if current klass exists
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    // Getting class details
    ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(klass);
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    // String builder where changes will be registered
    StringBuilder changesToApply = new StringBuilder();
    // Getting member details
    MemberDetails memberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
    List<MemberHoldingTypeDetails> memberHoldingTypes = memberDetails.getDetails();
    // Return if the class has not associated ITD's
    if (memberHoldingTypes.size() == 1 && memberHoldingTypes.get(0).getPhysicalTypeCategory() != PhysicalTypeCategory.ITD) {
        return pushedElements;
    }
    // Check if the provided class is a test to be able to select valid
    // class path
    Path path = classDetails.getAnnotation(RooJavaType.ROO_JPA_UNIT_TEST) == null ? Path.SRC_MAIN_JAVA : Path.SRC_TEST_JAVA;
    // Getting current class .java file metadata ID
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(klass, getPathResolver().getPath(klass.getModule(), path));
    // Getting detailsBuilder
    ClassOrInterfaceTypeDetailsBuilder detailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(classDetails);
    // Getting all details
    for (final MemberHoldingTypeDetails memberHoldingTypeDetails : memberDetails.getDetails()) {
        // this .java file
        if (!memberHoldingTypeDetails.getType().equals(classDetails.getType())) {
            continue;
        }
        // Getting all declared methods (including declared on ITDs
        // and .java files)
        List<MethodMetadata> allDeclaredMethods = memberHoldingTypeDetails.getMethods();
        // Checking if is necessary to make push-in for all declared methods
        for (MethodMetadata method : allDeclaredMethods) {
            // If method exists on .aj file, add it!
            if (method.getDeclaredByMetadataId().split("\\?").length > 1 && method.getDeclaredByMetadataId().split("\\?")[1].equals(klass.getFullyQualifiedTypeName()) && !method.getDeclaredByMetadataId().equals(declaredByMetadataId)) {
                // Add method to .java file
                MethodMetadata newMethod = getNewMethod(declaredByMetadataId, method);
                detailsBuilder.addMethod(newMethod);
                // Save changes on pushed elements list
                pushedElements.add(newMethod);
                changesToApply.append(String.format("Method '%s' will be pushed on '%s.java' class. \n", method.getMethodName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all declared fields (including declared on ITDs
        // and .java files)
        List<? extends FieldMetadata> allDeclaredFields = memberHoldingTypeDetails.getDeclaredFields();
        // Checking if is necessary to make push-in for all declared fields
        for (FieldMetadata field : allDeclaredFields) {
            // If field exists on .aj file, add it!
            if (field.getDeclaredByMetadataId().split("\\?").length > 1 && field.getDeclaredByMetadataId().split("\\?")[1].equals(klass.getFullyQualifiedTypeName()) && !field.getDeclaredByMetadataId().equals(declaredByMetadataId)) {
                // Add field to .java file
                FieldMetadata newField = getNewField(declaredByMetadataId, field);
                detailsBuilder.addField(newField);
                // Save changes on pushed elements list
                pushedElements.add(newField);
                changesToApply.append(String.format("Field '%s' will be pushed on '%s.java' class. \n", field.getFieldName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all declared constructors (including declared on ITDs and
        // .java files)
        List<? extends ConstructorMetadata> allDeclaredConstructors = memberHoldingTypeDetails.getDeclaredConstructors();
        // constructors
        for (ConstructorMetadata constructor : allDeclaredConstructors) {
            // Check if current constructor exists on .java file
            classDetails = getTypeLocationService().getTypeDetails(detailsBuilder.build().getType());
            List<JavaType> parameterTypes = new ArrayList<JavaType>();
            for (AnnotatedJavaType type : constructor.getParameterTypes()) {
                parameterTypes.add(type.getJavaType());
            }
            ConstructorMetadata javaDeclaredConstructor = classDetails.getDeclaredConstructor(parameterTypes);
            // If not exists, add it!
            if (javaDeclaredConstructor == null) {
                // Add constructor to .java file
                detailsBuilder.addConstructor(constructor);
                // Save changes on pushed elements list
                pushedElements.add(constructor);
                String constructorParametersNames = "";
                for (JavaSymbolName paramName : constructor.getParameterNames()) {
                    constructorParametersNames = constructorParametersNames.concat(paramName.getSymbolName()).concat(", ");
                    changesToApply.append(String.format("Constructor with parameters '%s' will be pushed on '%s.java' class. \n", constructorParametersNames.substring(0, constructorParametersNames.length() - 2), klass.getSimpleTypeName()));
                }
            }
        }
        // Getting all declared annotations (including declared on ITDs
        // and .java files)
        List<AnnotationMetadata> allDeclaredAnnotations = memberHoldingTypeDetails.getAnnotations();
        for (AnnotationMetadata annotation : allDeclaredAnnotations) {
            // Check if current annotation exists on .java file
            classDetails = getTypeLocationService().getTypeDetails(detailsBuilder.build().getType());
            List<AnnotationMetadata> javaDeclaredAnnotations = classDetails.getAnnotations();
            boolean annotationExists = false;
            for (AnnotationMetadata javaAnnotation : javaDeclaredAnnotations) {
                if (javaAnnotation.getAnnotationType().getFullyQualifiedTypeName().equals(annotation.getAnnotationType().getFullyQualifiedTypeName())) {
                    annotationExists = true;
                }
            }
            // If not exists, add it!
            if (!annotationExists) {
                // Add annotation to .java file
                detailsBuilder.addAnnotation(annotation);
                // Save changes on pushed elements list
                pushedElements.add(annotation);
                changesToApply.append(String.format("Annotation '%s' will be pushed on '%s.java' class. \n", annotation.getAnnotationType().getSimpleTypeName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all extends registered on .aj file to move to .java file
        List<JavaType> allExtendsTypes = memberHoldingTypeDetails.getExtendsTypes();
        for (JavaType extendsType : allExtendsTypes) {
            // If extends exists on .aj file, add it!
            if (!detailsBuilder.getExtendsTypes().contains(extendsType)) {
                detailsBuilder.addExtendsTypes(extendsType);
                // Save changes on pushed elements list
                pushedElements.add(extendsType);
                changesToApply.append(String.format("Extends type '%s' will be pushed on '%s.java' class. \n", extendsType.getSimpleTypeName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all implements registered on .aj file to move to .java
        // file
        List<JavaType> allImplementsTypes = memberHoldingTypeDetails.getImplementsTypes();
        for (JavaType implementsType : allImplementsTypes) {
            if (!detailsBuilder.getImplementsTypes().contains(implementsType)) {
                detailsBuilder.addImplementsType(implementsType);
                // Save changes on pushed elements list
                pushedElements.add(implementsType);
                changesToApply.append(String.format("Implements type '%s' will be pushed on '%s.java' class. \n", implementsType.getSimpleTypeName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all imports registered on .aj file to move to .java file
        Set<ImportMetadata> allRegisteredImports = memberHoldingTypeDetails.getImports();
        detailsBuilder.addImports(allRegisteredImports);
        // Save changes on pushed elements list
        pushedElements.add(allRegisteredImports);
    }
    // Updating .java file
    if (!force) {
        // Show message to be able to know which changes will be applied
        if (changesToApply.length() > 0) {
            LOGGER.log(Level.INFO, changesToApply.toString());
        }
    } else if (writeOnDisk) {
        getTypeManagementService().createOrUpdateTypeOnDisk(detailsBuilder.build());
    }
    return pushedElements;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) Path(org.springframework.roo.project.Path) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ImportMetadata(org.springframework.roo.classpath.details.ImportMetadata)

Example 2 with ImportMetadata

use of org.springframework.roo.classpath.details.ImportMetadata in project spring-roo by spring-projects.

the class JavaParserTypeParsingService method updateOutput.

/**
 * 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);
    }
    // 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 = "= ".concat(cid.getType().getSimpleTypeName()).concat("\n \nTODO Auto-generated class documentation");
        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);
}
Also used : TypeParameter(com.github.antlrjavaparser.api.TypeParameter) PhysicalTypeCategory(org.springframework.roo.classpath.PhysicalTypeCategory) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) ArrayList(java.util.ArrayList) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) JavaPackage(org.springframework.roo.model.JavaPackage) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) EnumConstantDeclaration(com.github.antlrjavaparser.api.body.EnumConstantDeclaration) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Comment(com.github.antlrjavaparser.api.Comment) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) EnumDeclaration(com.github.antlrjavaparser.api.body.EnumDeclaration) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) ImportDeclaration(com.github.antlrjavaparser.api.ImportDeclaration) BodyDeclaration(com.github.antlrjavaparser.api.body.BodyDeclaration) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) ImportMetadata(org.springframework.roo.classpath.details.ImportMetadata) TypeDeclaration(com.github.antlrjavaparser.api.body.TypeDeclaration)

Example 3 with ImportMetadata

use of org.springframework.roo.classpath.details.ImportMetadata in project spring-roo by spring-projects.

the class PushInOperationsImpl method pushInMethod.

/**
 * Makes push-in of a method defined on a provided class
 *
 * @param klass
 *            class to make the push-in operation
 * @param weiteOnDisk
 *            indicates if pushed elements should be writed on .java file
 *
 * @return list of objects with all the pushed elements.
 */
public List<Object> pushInMethod(JavaType klass, MethodMetadata method, boolean writeOnDisk) {
    List<Object> pushedElements = new ArrayList<Object>();
    // Check if current klass exists
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    // Getting class details
    ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(klass);
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    Validate.notNull(method, "ERROR: You must provide a valid method");
    // Getting member details
    MemberDetails memberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
    // Check if the provided class is a test to be able to select valid
    // class path
    Path path = classDetails.getAnnotation(RooJavaType.ROO_JPA_UNIT_TEST) == null ? Path.SRC_MAIN_JAVA : Path.SRC_TEST_JAVA;
    // Getting current class .java file metadata ID
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(klass, getPathResolver().getPath(klass.getModule(), path));
    // Getting detailsBuilder
    ClassOrInterfaceTypeDetailsBuilder detailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(classDetails);
    // Avoid AspectJ error when push-in from *RepositoryImpl classes
    AnnotationMetadata rooRepositoryCustomImplAnnotation = classDetails.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA_CUSTOM_IMPL);
    JavaType relatedRepositoryCustom = null;
    if (rooRepositoryCustomImplAnnotation != null) {
        AnnotationAttributeValue<Object> attribute = rooRepositoryCustomImplAnnotation.getAttribute("repository");
        Validate.notNull(attribute, "Unable to find 'repository' attribute of @RooJpaRepositoryCustomImpl on '%s'", classDetails.getType().getSimpleTypeName());
        relatedRepositoryCustom = (JavaType) attribute.getValue();
    }
    // Getting all details
    for (final MemberHoldingTypeDetails memberHoldingTypeDetails : memberDetails.getDetails()) {
        // Avoid AspectJ error when push-in from *RepositoryImpl classes
        if (rooRepositoryCustomImplAnnotation != null && memberHoldingTypeDetails.getImplementsTypes().contains(relatedRepositoryCustom)) {
            detailsBuilder.addImplementsType(relatedRepositoryCustom);
            pushedElements.add(relatedRepositoryCustom);
        }
        // this .java file
        if (!memberHoldingTypeDetails.getType().equals(classDetails.getType())) {
            continue;
        }
        // Getting all declared methods (including declared on ITDs
        // and .java files)
        List<MethodMetadata> allDeclaredMethods = memberHoldingTypeDetails.getMethods();
        // Checking if is necessary to make push-in for all declared methods
        for (MethodMetadata declaredMethod : allDeclaredMethods) {
            // If method exists on .aj file, add it!
            if (!method.getDeclaredByMetadataId().equals(declaredByMetadataId) && declaredMethod.equals(method)) {
                // Add method to .java file
                MethodMetadata newMethod = getNewMethod(declaredByMetadataId, method);
                detailsBuilder.addMethod(newMethod);
                // Save changes to be pushed
                pushedElements.add(newMethod);
            }
        }
        // Getting all imports registered on .aj file to move to .java file
        Set<ImportMetadata> allRegisteredImports = memberHoldingTypeDetails.getImports();
        detailsBuilder.addImports(allRegisteredImports);
        // Save imports to be pushed only if some method has been pushed
        if (!pushedElements.isEmpty()) {
            pushedElements.addAll(allRegisteredImports);
        }
    }
    // Updating .java file if write on disdk
    if (writeOnDisk) {
        getTypeManagementService().createOrUpdateTypeOnDisk(detailsBuilder.build());
    }
    return pushedElements;
}
Also used : Path(org.springframework.roo.project.Path) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ImportMetadata(org.springframework.roo.classpath.details.ImportMetadata)

Aggregations

ArrayList (java.util.ArrayList)3 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)3 ImportMetadata (org.springframework.roo.classpath.details.ImportMetadata)3 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)3 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)3 JavaType (org.springframework.roo.model.JavaType)3 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)2 ConstructorMetadata (org.springframework.roo.classpath.details.ConstructorMetadata)2 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)2 MemberHoldingTypeDetails (org.springframework.roo.classpath.details.MemberHoldingTypeDetails)2 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)2 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2 RooJavaType (org.springframework.roo.model.RooJavaType)2 Path (org.springframework.roo.project.Path)2 Comment (com.github.antlrjavaparser.api.Comment)1 ImportDeclaration (com.github.antlrjavaparser.api.ImportDeclaration)1 TypeParameter (com.github.antlrjavaparser.api.TypeParameter)1 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)1 ClassOrInterfaceDeclaration (com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration)1