Search in sources :

Example 6 with QualifiedNameExpr

use of com.github.antlrjavaparser.api.expr.QualifiedNameExpr in project spring-roo by spring-projects.

the class JavaParserUtils method importTypeIfRequired.

/**
 * Attempts to import the presented {@link JavaType}.
 * <p>
 * Whether imported or not, the method returns a {@link NameExpr} suitable
 * for subsequent use when referring to that type.
 * <p>
 * If an attempt is made to import a java.lang type, it is ignored.
 * <p>
 * If an attempt is made to import a type without a package, it is ignored.
 * <p>
 * We import every type usage even if the type usage is within the same
 * package and would theoretically not require an import. This is undertaken
 * so that there is no requirement to separately parse every unqualified
 * type usage within the compilation unit so as to refrain from importing
 * subsequently conflicting types.
 *
 * @param targetType the compilation unit target type (required)
 * @param imports the compilation unit's imports (required)
 * @param typeToImport the type to be imported (required)
 * @return the name expression to be used when referring to that type (never
 *         null)
 */
public static NameExpr importTypeIfRequired(final JavaType targetType, final List<ImportDeclaration> imports, final JavaType typeToImport) {
    Validate.notNull(targetType, "Target type is required");
    final JavaPackage compilationUnitPackage = targetType.getPackage();
    Validate.notNull(imports, "Compilation unit imports required");
    Validate.notNull(typeToImport, "Java type to import is required");
    // If it's a primitive, it's really easy
    if (typeToImport.isPrimitive()) {
        return new NameExpr(typeToImport.getNameIncludingTypeParameters());
    }
    // Handle if the type doesn't have a package at all
    if (typeToImport.isDefaultPackage()) {
        return new NameExpr(typeToImport.getSimpleTypeName());
    }
    final JavaPackage typeToImportPackage = typeToImport.getPackage();
    if (typeToImportPackage.equals(compilationUnitPackage)) {
        return new NameExpr(typeToImport.getSimpleTypeName());
    }
    NameExpr typeToImportExpr;
    if (typeToImport.getEnclosingType() == null) {
        typeToImportExpr = new QualifiedNameExpr(new NameExpr(typeToImport.getPackage().getFullyQualifiedPackageName()), typeToImport.getSimpleTypeName());
    } else {
        typeToImportExpr = new QualifiedNameExpr(new NameExpr(typeToImport.getEnclosingType().getFullyQualifiedTypeName()), typeToImport.getSimpleTypeName());
    }
    final ImportDeclaration newImport = new ImportDeclaration(typeToImportExpr, false, false);
    boolean addImport = true;
    boolean useSimpleTypeName = false;
    for (final ImportDeclaration existingImport : imports) {
        if (existingImport.getName().getName().equals(newImport.getName().getName())) {
            // Do not import, as there is already an import with the simple
            // type name
            addImport = false;
            // simple type name
            if (isEqual(existingImport.getName(), newImport.getName())) {
                useSimpleTypeName = true;
                break;
            }
        }
    }
    if (addImport && JdkJavaType.isPartOfJavaLang(typeToImport.getSimpleTypeName())) {
        // This simple type name would be part of java.lang if left as the
        // simple name. We want a fully-qualified name.
        addImport = false;
        useSimpleTypeName = false;
    }
    if (JdkJavaType.isPartOfJavaLang(typeToImport)) {
        // So we would have imported, but we don't need to
        addImport = false;
        // The fact we could have imported means there was no other
        // conflicting simple type names
        useSimpleTypeName = true;
    }
    if (addImport && typeToImport.getPackage().equals(compilationUnitPackage)) {
    // It is not theoretically necessary to add an import for something
    // in the same package,
    // but we elect to explicitly perform an import so future
    // conflicting types are not imported
    // addImport = true;
    // useSimpleTypeName = false;
    }
    if (addImport && targetType.getSimpleTypeName().equals(typeToImport.getSimpleTypeName())) {
        // So we would have imported it, but then it would conflict with the
        // simple name of the type
        addImport = false;
        useSimpleTypeName = false;
    }
    if (addImport) {
        imports.add(newImport);
        useSimpleTypeName = true;
    }
    // (forget imports, though!)
    if (typeToImport.getArgName() != null) {
        return new NameExpr(typeToImport.toString());
    }
    if (useSimpleTypeName) {
        return new NameExpr(typeToImport.getSimpleTypeName());
    }
    return new QualifiedNameExpr(new NameExpr(typeToImport.getPackage().getFullyQualifiedPackageName()), typeToImport.getSimpleTypeName());
}
Also used : QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) ImportDeclaration(com.github.antlrjavaparser.api.ImportDeclaration) JavaPackage(org.springframework.roo.model.JavaPackage)

Example 7 with QualifiedNameExpr

use of com.github.antlrjavaparser.api.expr.QualifiedNameExpr in project spring-roo by spring-projects.

the class JavaParserUtils method getNameExpr.

/**
 * Converts the presented class name into a name expression (either a
 * {@link NameExpr} or {@link QualifiedNameExpr} depending on whether a
 * package was presented).
 *
 * @param className to convert (required; can be fully qualified or simple
 *            name only)
 * @return a compatible expression (never returns null)
 */
public static NameExpr getNameExpr(final String className) {
    Validate.notBlank(className, "Class name required");
    if (className.contains(".")) {
        final int offset = className.lastIndexOf(".");
        final String packageName = className.substring(0, offset);
        final String typeName = className.substring(offset + 1);
        return new QualifiedNameExpr(new NameExpr(packageName), typeName);
    }
    return new NameExpr(className);
}
Also used : QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr)

Aggregations

QualifiedNameExpr (com.github.antlrjavaparser.api.expr.QualifiedNameExpr)7 NameExpr (com.github.antlrjavaparser.api.expr.NameExpr)6 ImportDeclaration (com.github.antlrjavaparser.api.ImportDeclaration)5 JavaPackage (org.springframework.roo.model.JavaPackage)4 JavaType (org.springframework.roo.model.JavaType)4 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)3 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)3 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)3 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)2 ClassOrInterfaceDeclaration (com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration)2 EnumConstantDeclaration (com.github.antlrjavaparser.api.body.EnumConstantDeclaration)2 EnumDeclaration (com.github.antlrjavaparser.api.body.EnumDeclaration)2 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)2 HashSet (java.util.HashSet)2 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)2 ConstructorMetadata (org.springframework.roo.classpath.details.ConstructorMetadata)2 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)2 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)2 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)2 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)2