Search in sources :

Example 6 with ParseException

use of com.github.antlrjavaparser.ParseException 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 7 with ParseException

use of com.github.antlrjavaparser.ParseException in project spring-roo by spring-projects.

the class JavaParserTypeResolutionService method getPackage.

@Override
public final JavaPackage getPackage(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()));
        if (compilationUnit == null || compilationUnit.getPackage() == null) {
            return null;
        }
        return new JavaPackage(compilationUnit.getPackage().getName().toString());
    } 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) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ParseException(com.github.antlrjavaparser.ParseException) File(java.io.File) JavaPackage(org.springframework.roo.model.JavaPackage)

Aggregations

ParseException (com.github.antlrjavaparser.ParseException)7 CompilationUnit (com.github.antlrjavaparser.api.CompilationUnit)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)5 JavaType (org.springframework.roo.model.JavaType)4 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)3 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)3 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)3 Type (com.github.antlrjavaparser.api.type.Type)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)3 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)3 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)3 TypeParameter (com.github.antlrjavaparser.api.TypeParameter)2 Parameter (com.github.antlrjavaparser.api.body.Parameter)2 VariableDeclaratorId (com.github.antlrjavaparser.api.body.VariableDeclaratorId)2 NameExpr (com.github.antlrjavaparser.api.expr.NameExpr)2 BlockStmt (com.github.antlrjavaparser.api.stmt.BlockStmt)2