use of com.sun.source.tree.CompilationUnitTree in project bazel by bazelbuild.
the class JavaSource2CFGDOT method getMethodTreeAndCompilationUnit.
/**
* @return The AST of a specific method in a specific class as well as the
* {@link CompilationUnitTree} in a specific file (or null they do
* not exist).
*/
public static Entry</*@Nullable*/
MethodTree, /*@Nullable*/
CompilationUnitTree> getMethodTreeAndCompilationUnit(String file, final String method, String clas) {
final Holder<MethodTree> m = new Holder<>();
final Holder<CompilationUnitTree> c = new Holder<>();
BasicTypeProcessor typeProcessor = new BasicTypeProcessor() {
@Override
protected TreePathScanner<?, ?> createTreePathScanner(CompilationUnitTree root) {
c.value = root;
return new TreePathScanner<Void, Void>() {
@Override
public Void visitMethod(MethodTree node, Void p) {
ExecutableElement el = TreeUtils.elementFromDeclaration(node);
if (el.getSimpleName().contentEquals(method)) {
m.value = node;
// compilation).
throw new RuntimeException();
}
return null;
}
};
}
};
Context context = new Context();
JavaCompiler javac = new JavaCompiler(context);
javac.attrParseOnly = true;
JavacFileManager fileManager = (JavacFileManager) context.get(JavaFileManager.class);
JavaFileObject l = fileManager.getJavaFileObjectsFromStrings(List.of(file)).iterator().next();
PrintStream err = System.err;
try {
// redirect syserr to nothing (and prevent the compiler from issuing
// warnings about our exception.
System.setErr(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
}
}));
javac.compile(List.of(l), List.of(clas), List.of(typeProcessor));
} catch (Throwable e) {
// ok
} finally {
System.setErr(err);
}
return new Entry<MethodTree, CompilationUnitTree>() {
@Override
public CompilationUnitTree setValue(CompilationUnitTree value) {
return null;
}
@Override
public CompilationUnitTree getValue() {
return c.value;
}
@Override
public MethodTree getKey() {
return m.value;
}
};
}
use of com.sun.source.tree.CompilationUnitTree in project j2objc by google.
the class JavacParser method processAnnotations.
@Override
public ProcessingResult processAnnotations(Iterable<String> fileArgs, List<ProcessingContext> inputs) {
final List<ProcessingContext> generatedInputs = Lists.newArrayList();
PathClassLoader loader = new PathClassLoader(options.fileUtil().getClassPathEntries());
loader.addPaths(options.getProcessorPathEntries());
Iterator<Processor> serviceIterator = ServiceLoader.load(Processor.class, loader).iterator();
if (serviceIterator.hasNext()) {
List<File> inputFiles = new ArrayList<>();
for (ProcessingContext input : inputs) {
inputFiles.add(new File(input.getFile().getAbsolutePath()));
}
try {
JavacEnvironment env = createEnvironment(inputFiles, null, true);
List<CompilationUnitTree> units = new ArrayList<>();
for (CompilationUnitTree unit : env.task().parse()) {
units.add(unit);
}
// JavacTaskImpl.enter() parses and runs annotation processing, but
// not type checking and attribution (that's done by analyze()).
env.task().enter();
processDiagnostics(env.diagnostics());
// The source output directory is created and set in createEnvironment().
File sourceOutputDirectory = env.fileManager().getLocation(StandardLocation.SOURCE_OUTPUT).iterator().next();
collectGeneratedInputs(sourceOutputDirectory, "", generatedInputs);
return new JavacProcessingResult(generatedInputs, sourceOutputDirectory);
} catch (IOException e) {
ErrorUtil.fatalError(e, "javac file manager error");
}
}
// No annotation processors on classpath, or processing errors reported.
return new JavacProcessingResult(generatedInputs, null);
}
use of com.sun.source.tree.CompilationUnitTree in project error-prone by google.
the class TemplateIntegrationTest method extractRefasterRule.
private CodeTransformer extractRefasterRule(JavaFileObject object) {
compile(object);
ClassTree classTree = Iterables.getOnlyElement(FluentIterable.from(compilationUnits).transformAndConcat(new Function<CompilationUnitTree, Iterable<? extends Tree>>() {
@Override
public Iterable<? extends Tree> apply(CompilationUnitTree input) {
return input.getTypeDecls();
}
}).filter(ClassTree.class));
return Iterables.getOnlyElement(RefasterRuleBuilderScanner.extractRules(classTree, context));
}
use of com.sun.source.tree.CompilationUnitTree in project error-prone by google.
the class EmptyTopLevelDeclaration method matchCompilationUnit.
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
List<Tree> toDelete = new ArrayList<>();
for (Tree member : tree.getTypeDecls()) {
if (member.getKind() == Tree.Kind.EMPTY_STATEMENT) {
toDelete.add(member);
}
}
if (toDelete.isEmpty()) {
return Description.NO_MATCH;
}
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
for (Tree member : toDelete) {
fixBuilder.delete(member);
}
return describeMatch(toDelete.get(0), fixBuilder.build());
}
use of com.sun.source.tree.CompilationUnitTree in project vertx-docgen by vert-x3.
the class Helper method readSource.
/**
* Read the source code of the provided element, this returns the source of the entire related compilation unit.
*
* @param elt the element to load
* @return the source
*/
String readSource(Element elt) {
CompilationUnitTree unit = docTrees.getPath(elt).getCompilationUnit();
StringBuilder source = new StringBuilder();
try (Reader reader = unit.getSourceFile().openReader(true)) {
char[] buffer = new char[256];
while (true) {
int len = reader.read(buffer);
if (len == -1) {
break;
}
source.append(buffer, 0, len);
}
return source.toString();
} catch (IOException e) {
throw new DocGenException(elt, "Could not read source code of element " + elt);
}
}
Aggregations