Search in sources :

Example 11 with TypeDeclaration

use of com.github.antlrjavaparser.api.body.TypeDeclaration in project spring-roo by spring-projects.

the class JavaParserTypeResolutionService method getJavaType.

@Override
public final JavaType getJavaType(final String fileIdentifier) {
    Validate.notBlank(fileIdentifier, "Compilation unit path required");
    Validate.isTrue(new File(fileIdentifier).exists(), "The file doesn't exist");
    Validate.isTrue(new File(fileIdentifier).isFile(), "The identifier doesn't represent a file");
    try {
        final File file = new File(fileIdentifier);
        String typeContents = "";
        try {
            typeContents = FileUtils.readFileToString(file);
        } catch (final IOException ignored) {
        }
        if (StringUtils.isBlank(typeContents)) {
            return null;
        }
        final CompilationUnit compilationUnit = JavaParser.parse(new ByteArrayInputStream(typeContents.getBytes()));
        final String typeName = fileIdentifier.substring(fileIdentifier.lastIndexOf(File.separator) + 1, fileIdentifier.lastIndexOf("."));
        for (final TypeDeclaration typeDeclaration : compilationUnit.getTypes()) {
            if (typeName.equals(typeDeclaration.getName())) {
                return new JavaType(compilationUnit.getPackage().getName().getName() + "." + typeDeclaration.getName());
            }
        }
        return null;
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } catch (final ParseException e) {
        throw new IllegalStateException("Failed to parse " + fileIdentifier + " : " + e.getMessage());
    }
}
Also used : CompilationUnit(com.github.antlrjavaparser.api.CompilationUnit) JavaType(org.springframework.roo.model.JavaType) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ParseException(com.github.antlrjavaparser.ParseException) File(java.io.File) TypeDeclaration(com.github.antlrjavaparser.api.body.TypeDeclaration)

Example 12 with TypeDeclaration

use of com.github.antlrjavaparser.api.body.TypeDeclaration in project spring-roo by spring-projects.

the class JavaParserUtils method locateTypeDeclaration.

/**
 * Searches a compilation unit and locates the declaration with the given
 * type's simple name.
 *
 * @param compilationUnit to scan (required)
 * @param javaType the target to locate (required)
 * @return the located type declaration or null if it could not be found
 */
public static TypeDeclaration locateTypeDeclaration(final CompilationUnit compilationUnit, final JavaType javaType) {
    Validate.notNull(compilationUnit, "Compilation unit required");
    Validate.notNull(javaType, "Java type to search for required");
    if (compilationUnit.getTypes() == null) {
        return null;
    }
    for (final TypeDeclaration candidate : compilationUnit.getTypes()) {
        if (javaType.getSimpleTypeName().equals(candidate.getName())) {
            // We have the required type declaration
            return candidate;
        }
    }
    return null;
}
Also used : TypeDeclaration(com.github.antlrjavaparser.api.body.TypeDeclaration)

Aggregations

TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)12 ArrayList (java.util.ArrayList)7 JavaType (org.springframework.roo.model.JavaType)7 CompilationUnit (com.github.antlrjavaparser.api.CompilationUnit)6 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)6 ParseException (com.github.antlrjavaparser.ParseException)5 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)5 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)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 ImportDeclaration (com.github.antlrjavaparser.api.ImportDeclaration)4 EnumDeclaration (com.github.antlrjavaparser.api.body.EnumDeclaration)4 NameExpr (com.github.antlrjavaparser.api.expr.NameExpr)4 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)4 TypeParameter (com.github.antlrjavaparser.api.TypeParameter)3 QualifiedNameExpr (com.github.antlrjavaparser.api.expr.QualifiedNameExpr)3 Type (com.github.antlrjavaparser.api.type.Type)3