use of com.github.antlrjavaparser.api.CompilationUnit in project spring-roo by spring-projects.
the class JavaParserTypeParsingService method getTypeFromString.
@Override
public ClassOrInterfaceTypeDetails getTypeFromString(final String fileContents, final String declaredByMetadataId, final JavaType typeName) {
if (StringUtils.isBlank(fileContents)) {
return null;
}
Validate.notBlank(declaredByMetadataId, "Declaring metadata ID required");
Validate.notNull(typeName, "Java type to locate required");
try {
final CompilationUnit compilationUnit = JavaParser.parse(new ByteArrayInputStream(fileContents.getBytes()));
final TypeDeclaration typeDeclaration = JavaParserUtils.locateTypeDeclaration(compilationUnit, typeName);
if (typeDeclaration == null) {
return null;
}
return JavaParserClassOrInterfaceTypeDetailsBuilder.getInstance(compilationUnit, null, typeDeclaration, declaredByMetadataId, typeName, metadataService, typeLocationService).build();
} catch (final IOException e) {
throw new IllegalStateException(e);
} catch (final ParseException e) {
throw new IllegalStateException("Failed to parse " + typeName + " : " + e.getMessage());
}
}
use of com.github.antlrjavaparser.api.CompilationUnit 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());
}
}
use of com.github.antlrjavaparser.api.CompilationUnit 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());
}
}
Aggregations