下面列出了怎么用org.eclipse.jdt.core.dom.FileASTRequestor的API类实例代码及写法,或者点击链接到github查看源代码。
private UMLModelASTReader(File rootFolder, ASTParser parser, List<String> javaFiles, Set<String> repositoryDirectories) {
this.umlModel = new UMLModel(repositoryDirectories);
this.projectRoot = rootFolder.getPath();
this.parser = parser;
final String[] emptyArray = new String[0];
String[] filesArray = new String[javaFiles.size()];
for (int i = 0; i < filesArray.length; i++) {
filesArray[i] = rootFolder + File.separator + javaFiles.get(i).replaceAll("/", systemFileSeparator);
}
FileASTRequestor fileASTRequestor = new FileASTRequestor() {
@Override
public void acceptAST(String sourceFilePath, CompilationUnit ast) {
String relativePath = sourceFilePath.substring(projectRoot.length() + 1).replaceAll(systemFileSeparator, "/");
processCompilationUnit(relativePath, ast);
}
};
this.parser.createASTs((String[]) filesArray, null, emptyArray, fileASTRequestor, null);
}
private static void parseAST(String[] srcFiles, Charset srcCharset,
String[] classPathEntries, FileASTRequestor requestor) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setEnvironment(classPathEntries, null, null, true);
String[] srcEncodings = new String[srcFiles.length];
for (int i = 0; i < srcEncodings.length; i++) {
srcEncodings[i] = srcCharset.name();
}
parser.createASTs(
srcFiles, srcEncodings, new String[]{}, requestor, null);
}
private Set<Slice> generateExamples() {
Set<Slice> slices = new HashSet<>();
File srcFile = new File(srcPath);
String[] testPaths = getTestFiles(srcFile).parallelStream().map(File::getAbsolutePath).toArray(String[]::new);
String[] folderPaths = getAllFiles(srcFile).parallelStream().map(File::getParentFile).map(File::getAbsolutePath).toArray(String[]::new);
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setEnvironment(null, folderPaths, null, true);
parser.setResolveBindings(true);
Map<String, String> options = new Hashtable<>();
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
parser.setCompilerOptions(options);
parser.setBindingsRecovery(true);
parser.createASTs(testPaths, null, new String[]{}, new FileASTRequestor() {
@Override
public void acceptAST(String sourceFilePath, CompilationUnit javaUnit) {
APIMethodVisitor visitor = new APIMethodVisitor();
javaUnit.accept(visitor);
slices.addAll(visitor.getSlices());
}
}, null);
return slices;
}
protected static String parseJava(final String content) {
final StringBuilder sb = new StringBuilder();
final FileASTRequestor r = new FileASTRequestor() {
@Override
public void acceptAST(String sourceFilePath, CompilationUnit cu) {
final ASTRoot.Builder ast = ASTRoot.newBuilder();
try {
ast.addNamespaces(visitor.getNamespaces(cu));
} catch (final Exception e) {
System.err.println(e);
e.printStackTrace();
}
sb.append(JsonFormat.printToString(ast.build()));
}
};
Map<String, String> fileContents = new HashMap<String, String>();
fileContents.put("", content);
@SuppressWarnings("rawtypes")
Map options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_COMPLIANCE, javaVersion);
options.put(JavaCore.COMPILER_SOURCE, javaVersion);
ASTParser parser = ASTParser.newParser(astLevel);
parser.setCompilerOptions(options);
parser.setEnvironment(new String[0], new String[]{}, new String[]{}, true);
parser.setResolveBindings(true);
parser.createASTs(fileContents, new String[]{""}, null, new String[0], r, null);
return FileIO.normalizeEOL(sb.toString());
}
/** Returns a map from file paths to compilation units after JDT parsing. */
public CompilationUnitsAndTypeBindings parseFiles(
List<FileInfo> filePaths, boolean useTargetPath) {
// Parse and create a compilation unit for every file.
ASTParser parser = newASTParser(true);
// The map must be ordered because it will be iterated over later and if it was not ordered then
// our output would be unstable
final Map<String, CompilationUnit> compilationUnitsByFilePath = new LinkedHashMap<>();
final List<ITypeBinding> wellKnownTypeBindings = new ArrayList<>();
final Map<String, String> targetPathBySourcePath =
filePaths.stream().collect(Collectors.toMap(FileInfo::sourcePath, FileInfo::targetPath));
FileASTRequestor astRequestor =
new FileASTRequestor() {
@Override
public void acceptAST(String filePath, CompilationUnit compilationUnit) {
if (compilationHasErrors(filePath, compilationUnit)) {
return;
}
String filePathKey = filePath;
if (useTargetPath) {
filePathKey = targetPathBySourcePath.get(filePath);
}
compilationUnitsByFilePath.put(filePathKey, compilationUnit);
}
@Override
public void acceptBinding(String bindingKey, IBinding binding) {
wellKnownTypeBindings.add((ITypeBinding) binding);
}
};
parser.createASTs(
filePaths.stream().map(f -> f.sourcePath()).toArray(String[]::new),
getEncodings(filePaths.size()),
FrontendConstants.WELL_KNOWN_CLASS_NAMES.stream()
.map(BindingKey::createTypeBindingKey)
.toArray(String[]::new),
astRequestor,
null);
return new CompilationUnitsAndTypeBindings(compilationUnitsByFilePath, wellKnownTypeBindings);
}
public static ElementInfoPool parse(String srcDir) {
ElementInfoPool elementInfoPool = new ElementInfoPool(srcDir);
Collection<File> javaFiles = FileUtils.listFiles(new File(srcDir), new String[]{"java"}, true);
Set<String> srcPathSet = new HashSet<>();
Set<String> srcFolderSet = new HashSet<>();
for (File javaFile : javaFiles) {
String srcPath = javaFile.getAbsolutePath();
String srcFolderPath = javaFile.getParentFile().getAbsolutePath();
srcPathSet.add(srcPath);
srcFolderSet.add(srcFolderPath);
}
String[] srcPaths = new String[srcPathSet.size()];
srcPathSet.toArray(srcPaths);
NameResolver.setSrcPathSet(srcPathSet);
String[] srcFolderPaths = new String[srcFolderSet.size()];
srcFolderSet.toArray(srcFolderPaths);
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setEnvironment(null, srcFolderPaths, null, true);
parser.setResolveBindings(true);
Map<String, String> options = new Hashtable<>();
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
parser.setCompilerOptions(options);
parser.setBindingsRecovery(true);
System.out.println(javaFiles.size());
parser.createASTs(srcPaths, null, new String[]{}, new FileASTRequestor() {
@Override
public void acceptAST(String sourceFilePath, CompilationUnit javaUnit) {
try {
javaUnit.accept(new JavaASTVisitor(elementInfoPool, FileUtils.readFileToString(new File(sourceFilePath))));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, null);
return elementInfoPool;
}
@Override
protected FileASTRequestor getRequestor(JavaFiles allJavaFiles) {
return new AstRequestor(this, allJavaFiles);
}
/**
* Process the given batch of files and pass the results back to the listener as each file is processed.
*/
public static BatchASTFuture analyze(final BatchASTListener listener, final WildcardImportResolver importResolver,
final Set<String> libraryPaths,
final Set<String> sourcePaths, Set<Path> sourceFiles)
{
final String[] encodings = null;
final String[] bindingKeys = new String[0];
final ExecutorService executor = WindupExecutors.newFixedThreadPool(WindupExecutors.getDefaultThreadCount());
final FileASTRequestor requestor = new FileASTRequestor()
{
@Override
public void acceptAST(String sourcePath, CompilationUnit ast)
{
try
{
/*
* This super() call doesn't do anything, but we call it just to be nice, in case that ever changes.
*/
super.acceptAST(sourcePath, ast);
ReferenceResolvingVisitor visitor = new ReferenceResolvingVisitor(importResolver, ast, sourcePath);
ast.accept(visitor);
listener.processed(Paths.get(sourcePath), visitor.getJavaClassReferences());
}
catch (WindupStopException ex)
{
throw ex;
}
catch (Throwable t)
{
listener.failed(Paths.get(sourcePath), t);
}
}
};
List<List<String>> batches = createBatches(sourceFiles);
for (final List<String> batch : batches)
{
executor.submit(new Callable<Void>()
{
@Override
public Void call() throws Exception
{
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setBindingsRecovery(false);
parser.setResolveBindings(true);
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
// these options seem to slightly reduce the number of times that JDT aborts on compilation errors
options.put(JavaCore.CORE_INCOMPLETE_CLASSPATH, "warning");
options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, "warning");
options.put(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, "warning");
options.put(JavaCore.CORE_CIRCULAR_CLASSPATH, "warning");
options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, "warning");
options.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_VIOLATION, "warning");
options.put(JavaCore.CORE_JAVA_BUILD_INVALID_CLASSPATH, "ignore");
options.put(JavaCore.COMPILER_PB_NULL_ANNOTATION_INFERENCE_CONFLICT, "warning");
options.put(JavaCore.CORE_OUTPUT_LOCATION_OVERLAPPING_ANOTHER_SOURCE, "warning");
options.put(JavaCore.CORE_JAVA_BUILD_DUPLICATE_RESOURCE, "warning");
parser.setCompilerOptions(options);
parser.setEnvironment(libraryPaths.toArray(new String[libraryPaths.size()]),
sourcePaths.toArray(new String[sourcePaths.size()]),
null,
true);
parser.createASTs(batch.toArray(new String[batch.size()]), encodings, bindingKeys, requestor, null);
return null;
}
});
}
executor.shutdown();
return new BatchASTFuture()
{
@Override
public boolean isDone()
{
return executor.isTerminated();
}
};
}
protected abstract FileASTRequestor getRequestor(JavaFiles allJavaFiles);