Search in sources :

Example 6 with ClassOrInterfaceType

use of com.github.antlrjavaparser.api.type.ClassOrInterfaceType in project spring-roo by spring-projects.

the class JavaParserUtils method getResolvedName.

public static Type getResolvedName(final JavaType target, final JavaType current, final CompilationUnitServices compilationUnit) {
    final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(target, compilationUnit.getImports(), current);
    final ClassOrInterfaceType resolvedName = JavaParserUtils.getClassOrInterfaceType(nameExpr);
    if (current.getParameters() != null && current.getParameters().size() > 0) {
        resolvedName.setTypeArgs(new ArrayList<Type>());
        for (final JavaType param : current.getParameters()) {
            resolvedName.getTypeArgs().add(getResolvedName(target, param, compilationUnit));
        }
    }
    if (current.getArray() > 0) {
        // Primitives includes array declaration in resolvedName
        if (!current.isPrimitive()) {
            return new ReferenceType(resolvedName, current.getArray());
        }
    }
    return resolvedName;
}
Also used : WildcardType(com.github.antlrjavaparser.api.type.WildcardType) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) DataType(org.springframework.roo.model.DataType) JdkJavaType(org.springframework.roo.model.JdkJavaType) VoidType(com.github.antlrjavaparser.api.type.VoidType) JavaType(org.springframework.roo.model.JavaType) PrimitiveType(com.github.antlrjavaparser.api.type.PrimitiveType) ReferenceType(com.github.antlrjavaparser.api.type.ReferenceType) Type(com.github.antlrjavaparser.api.type.Type) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) ReferenceType(com.github.antlrjavaparser.api.type.ReferenceType)

Example 7 with ClassOrInterfaceType

use of com.github.antlrjavaparser.api.type.ClassOrInterfaceType in project spring-roo by spring-projects.

the class UpdateCompilationUnitUtils method updateImplements.

/**
 * Update {@code implements} with {@code }
 *
 * @param originalType
 * @param newType
 */
private static void updateImplements(final ClassOrInterfaceDeclaration originalType, final ClassOrInterfaceDeclaration newType) {
    // Getting original an new implements
    List<ClassOrInterfaceType> originalImplements = originalType.getImplements() == null ? new ArrayList<ClassOrInterfaceType>() : originalType.getImplements();
    List<ClassOrInterfaceType> newImplements = newType.getImplements() == null ? new ArrayList<ClassOrInterfaceType>() : newType.getImplements();
    // Check if has the same implements
    if (!originalImplements.equals(newImplements)) {
        // If not, merge implements and add it to original
        List<ClassOrInterfaceType> finalImplements = originalImplements;
        for (ClassOrInterfaceType x : newImplements) {
            if (!finalImplements.contains(x)) {
                finalImplements.add(x);
            }
        }
        originalType.setImplements(finalImplements);
    }
}
Also used : ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType)

Example 8 with ClassOrInterfaceType

use of com.github.antlrjavaparser.api.type.ClassOrInterfaceType in project spring-roo by spring-projects.

the class UpdateCompilationUnitUtils method updateExtends.

/**
 * Update {@code extends} with {@code }
 *
 * @param originalType
 * @param newType
 */
private static void updateExtends(final ClassOrInterfaceDeclaration originalType, final ClassOrInterfaceDeclaration newType) {
    // Getting original an new extends
    List<ClassOrInterfaceType> originalExtends = originalType.getExtends() == null ? new ArrayList<ClassOrInterfaceType>() : originalType.getExtends();
    List<ClassOrInterfaceType> newExtends = newType.getExtends() == null ? new ArrayList<ClassOrInterfaceType>() : newType.getExtends();
    // Check if has the same extends
    if (!originalExtends.equals(newExtends)) {
        // If not, merge extends and add it to original
        List<ClassOrInterfaceType> finalExtends = originalExtends;
        for (ClassOrInterfaceType x : newExtends) {
            if (!finalExtends.contains(x)) {
                finalExtends.add(x);
            }
        }
        originalType.setExtends(finalExtends);
    }
}
Also used : ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType)

Example 9 with ClassOrInterfaceType

use of com.github.antlrjavaparser.api.type.ClassOrInterfaceType in project spring-roo by spring-projects.

the class UpdateCompilationUnitUtils method equals.

/**
 * Compare two {@link Type}
 *
 * @param type
 * @param type2
 * @return
 */
private static boolean equals(final Type type, final Type type2) {
    if (ObjectUtils.equals(type, type2)) {
        return true;
    }
    if (type.getClass() != type2.getClass()) {
        return false;
    }
    if (type instanceof ClassOrInterfaceType) {
        final ClassOrInterfaceType cType = (ClassOrInterfaceType) type;
        final ClassOrInterfaceType cType2 = (ClassOrInterfaceType) type2;
        return cType.getName().equals(cType2.getName());
    } else if (type instanceof PrimitiveType) {
        final PrimitiveType pType = (PrimitiveType) type;
        final PrimitiveType pType2 = (PrimitiveType) type2;
        return pType.getType() == pType2.getType();
    } else if (type instanceof VoidType) {
        return true;
    } else if (type instanceof WildcardType) {
        final WildcardType wType = (WildcardType) type;
        final WildcardType wType2 = (WildcardType) type2;
        return equals(wType.getSuper(), wType2.getSuper()) && equals(wType.getExtends(), wType2.getExtends());
    }
    return false;
}
Also used : VoidType(com.github.antlrjavaparser.api.type.VoidType) WildcardType(com.github.antlrjavaparser.api.type.WildcardType) PrimitiveType(com.github.antlrjavaparser.api.type.PrimitiveType) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType)

Example 10 with ClassOrInterfaceType

use of com.github.antlrjavaparser.api.type.ClassOrInterfaceType in project spring-roo by spring-projects.

the class JavaParserClassOrInterfaceTypeDetailsBuilder method build.

@Override
public ClassOrInterfaceTypeDetails build() {
    Validate.notEmpty(compilationUnit.getTypes(), "No types in compilation unit, so unable to continue parsing");
    ClassOrInterfaceDeclaration clazz = null;
    EnumDeclaration enumClazz = null;
    final StringBuilder sb = new StringBuilder(compilationUnit.getPackage().getName().toString());
    if (name.getEnclosingType() != null) {
        sb.append(".").append(name.getEnclosingType().getSimpleTypeName());
    }
    compilationUnitPackage = new JavaPackage(sb.toString());
    // Determine the type name, adding type parameters if possible
    final JavaType newName = JavaParserUtils.getJavaType(compilationUnitServices, typeDeclaration);
    // Revert back to the original type name (thus avoiding unnecessary
    // inferences about java.lang types; see ROO-244)
    name = new JavaType(newName.getFullyQualifiedTypeName(), newName.getEnclosingType(), newName.getArray(), newName.getDataType(), newName.getArgName(), newName.getParameters(), name.getModule());
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId);
    physicalTypeCategory = PhysicalTypeCategory.CLASS;
    if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
        clazz = (ClassOrInterfaceDeclaration) typeDeclaration;
        if (clazz.isInterface()) {
            physicalTypeCategory = PhysicalTypeCategory.INTERFACE;
        }
    } else if (typeDeclaration instanceof EnumDeclaration) {
        enumClazz = (EnumDeclaration) typeDeclaration;
        physicalTypeCategory = PhysicalTypeCategory.ENUMERATION;
    }
    Validate.notNull(physicalTypeCategory, "%s (%s for %s)", UNSUPPORTED_MESSAGE_PREFIX, typeDeclaration.getClass().getSimpleName(), name);
    cidBuilder.setName(name);
    cidBuilder.setPhysicalTypeCategory(physicalTypeCategory);
    imports = compilationUnit.getImports();
    if (imports == null) {
        imports = new ArrayList<ImportDeclaration>();
        compilationUnit.setImports(imports);
    }
    // Verify the package declaration appears to be correct
    if (compilationUnitPackage.equals(name.getPackage()) != true) {
        String warningStr = "[Warning] Compilation unit package '" + compilationUnitPackage + "' unexpected for type '" + name.getPackage() + "', it may be a nested class.";
        LOGGER.log(Level.WARNING, warningStr);
    }
    for (final ImportDeclaration importDeclaration : imports) {
        if (importDeclaration.getName() instanceof QualifiedNameExpr) {
            final String qualifier = ((QualifiedNameExpr) importDeclaration.getName()).getQualifier().toString();
            final String simpleName = importDeclaration.getName().getName();
            final String fullName = qualifier + "." + simpleName;
            // We want to calculate these...
            final JavaType type = new JavaType(fullName);
            final JavaPackage typePackage = importDeclaration.isAsterisk() ? new JavaPackage(fullName) : type.getPackage();
            // Process any comments for the import
            final CommentStructure commentStructure = new CommentStructure();
            JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, importDeclaration);
            final ImportMetadataBuilder newImport = new ImportMetadataBuilder(declaredByMetadataId, 0, typePackage, type, importDeclaration.isStatic(), importDeclaration.isAsterisk());
            newImport.setCommentStructure(commentStructure);
            cidBuilder.add(newImport.build());
        }
    }
    // Convert Java Parser modifier into JDK modifier
    cidBuilder.setModifier(JavaParserUtils.getJdkModifier(typeDeclaration.getModifiers()));
    // Type parameters
    final Set<JavaSymbolName> typeParameterNames = new HashSet<JavaSymbolName>();
    for (final JavaType param : name.getParameters()) {
        final JavaSymbolName arg = param.getArgName();
        // Fortunately type names can only appear at the top-level
        if (arg != null && !JavaType.WILDCARD_NEITHER_ARG.equals(arg) && !JavaType.WILDCARD_EXTENDS_ARG.equals(arg) && !JavaType.WILDCARD_SUPER_ARG.equals(arg)) {
            typeParameterNames.add(arg);
        }
    }
    List<ClassOrInterfaceType> implementsList;
    List<AnnotationExpr> annotationsList = null;
    List<BodyDeclaration> members = null;
    if (clazz != null) {
        final List<ClassOrInterfaceType> extendsList = clazz.getExtends();
        if (extendsList != null) {
            for (final ClassOrInterfaceType candidate : extendsList) {
                final JavaType javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, candidate, typeParameterNames);
                cidBuilder.addExtendsTypes(javaType);
            }
        }
        final List<JavaType> extendsTypes = cidBuilder.getExtendsTypes();
        // Obtain the superclass, if this is a class and one is available
        if (physicalTypeCategory == PhysicalTypeCategory.CLASS && extendsTypes.size() == 1) {
            final JavaType superclass = extendsTypes.get(0);
            final String superclassId = typeLocationService.getPhysicalTypeIdentifier(superclass);
            PhysicalTypeMetadata superPtm = null;
            if (superclassId != null) {
                superPtm = (PhysicalTypeMetadata) metadataService.get(superclassId);
            }
            if (superPtm != null && superPtm.getMemberHoldingTypeDetails() != null) {
                cidBuilder.setSuperclass(superPtm.getMemberHoldingTypeDetails());
            }
        }
        implementsList = clazz.getImplements();
        if (implementsList != null) {
            for (final ClassOrInterfaceType candidate : implementsList) {
                final JavaType javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, candidate, typeParameterNames);
                cidBuilder.addImplementsType(javaType);
            }
        }
        annotationsList = typeDeclaration.getAnnotations();
        members = clazz.getMembers();
    }
    if (enumClazz != null) {
        final List<EnumConstantDeclaration> constants = enumClazz.getEntries();
        if (constants != null) {
            for (final EnumConstantDeclaration enumConstants : constants) {
                cidBuilder.addEnumConstant(new JavaSymbolName(enumConstants.getName()));
            }
        }
        implementsList = enumClazz.getImplements();
        annotationsList = enumClazz.getAnnotations();
        members = enumClazz.getMembers();
    }
    if (annotationsList != null) {
        for (final AnnotationExpr candidate : annotationsList) {
            final AnnotationMetadata md = JavaParserAnnotationMetadataBuilder.getInstance(candidate, compilationUnitServices).build();
            final CommentStructure commentStructure = new CommentStructure();
            JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, candidate);
            md.setCommentStructure(commentStructure);
            cidBuilder.addAnnotation(md);
        }
    }
    if (members != null) {
        // type in the signature of the enclosing type
        for (final BodyDeclaration bodyDeclaration : members) {
            if (bodyDeclaration instanceof TypeDeclaration) {
                // Found a type
                innerTypes.add((TypeDeclaration) bodyDeclaration);
            }
        }
        for (final BodyDeclaration member : members) {
            if (member instanceof FieldDeclaration) {
                final FieldDeclaration castMember = (FieldDeclaration) member;
                for (final VariableDeclarator var : castMember.getVariables()) {
                    final FieldMetadata field = JavaParserFieldMetadataBuilder.getInstance(declaredByMetadataId, castMember, var, compilationUnitServices, typeParameterNames).build();
                    final CommentStructure commentStructure = new CommentStructure();
                    JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
                    field.setCommentStructure(commentStructure);
                    cidBuilder.addField(field);
                }
            }
            if (member instanceof MethodDeclaration) {
                final MethodDeclaration castMember = (MethodDeclaration) member;
                final MethodMetadata method = JavaParserMethodMetadataBuilder.getInstance(declaredByMetadataId, castMember, compilationUnitServices, typeParameterNames).build();
                final CommentStructure commentStructure = new CommentStructure();
                JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
                method.setCommentStructure(commentStructure);
                cidBuilder.addMethod(method);
            }
            if (member instanceof ConstructorDeclaration) {
                final ConstructorDeclaration castMember = (ConstructorDeclaration) member;
                final ConstructorMetadata constructor = JavaParserConstructorMetadataBuilder.getInstance(declaredByMetadataId, castMember, compilationUnitServices, typeParameterNames).build();
                final CommentStructure commentStructure = new CommentStructure();
                JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
                constructor.setCommentStructure(commentStructure);
                cidBuilder.addConstructor(constructor);
            }
            if (member instanceof TypeDeclaration) {
                final TypeDeclaration castMember = (TypeDeclaration) member;
                final JavaType innerType = new JavaType(castMember.getName(), name);
                final String innerTypeMetadataId = PhysicalTypeIdentifier.createIdentifier(innerType, PhysicalTypeIdentifier.getPath(declaredByMetadataId));
                final ClassOrInterfaceTypeDetails cid = new JavaParserClassOrInterfaceTypeDetailsBuilder(compilationUnit, compilationUnitServices, castMember, innerTypeMetadataId, innerType, metadataService, typeLocationService).build();
                cidBuilder.addInnerType(cid);
            }
        }
    }
    return cidBuilder.build();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) JavaPackage(org.springframework.roo.model.JavaPackage) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) FieldDeclaration(com.github.antlrjavaparser.api.body.FieldDeclaration) VariableDeclarator(com.github.antlrjavaparser.api.body.VariableDeclarator) EnumConstantDeclaration(com.github.antlrjavaparser.api.body.EnumConstantDeclaration) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ConstructorDeclaration(com.github.antlrjavaparser.api.body.ConstructorDeclaration) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ImportMetadataBuilder(org.springframework.roo.classpath.details.ImportMetadataBuilder) HashSet(java.util.HashSet) MethodDeclaration(com.github.antlrjavaparser.api.body.MethodDeclaration) EnumDeclaration(com.github.antlrjavaparser.api.body.EnumDeclaration) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) 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) TypeDeclaration(com.github.antlrjavaparser.api.body.TypeDeclaration)

Aggregations

ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)15 JavaType (org.springframework.roo.model.JavaType)12 Type (com.github.antlrjavaparser.api.type.Type)9 NameExpr (com.github.antlrjavaparser.api.expr.NameExpr)8 QualifiedNameExpr (com.github.antlrjavaparser.api.expr.QualifiedNameExpr)7 PrimitiveType (com.github.antlrjavaparser.api.type.PrimitiveType)7 ReferenceType (com.github.antlrjavaparser.api.type.ReferenceType)7 VoidType (com.github.antlrjavaparser.api.type.VoidType)7 WildcardType (com.github.antlrjavaparser.api.type.WildcardType)7 ArrayList (java.util.ArrayList)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)7 DataType (org.springframework.roo.model.DataType)6 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)5 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)5 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)5 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)5 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)5 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)5 TypeParameter (com.github.antlrjavaparser.api.TypeParameter)4 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)4