use of com.sun.source.tree.ClassTree in project error-prone by google.
the class RefasterRuleCompilerAnalyzer method finished.
@Override
public void finished(TaskEvent taskEvent) {
if (taskEvent.getKind() != Kind.ANALYZE) {
return;
}
if (JavaCompiler.instance(context).errorCount() > 0) {
return;
}
ClassTree tree = JavacTrees.instance(context).getTree(taskEvent.getTypeElement());
if (tree == null) {
return;
}
Collection<? extends CodeTransformer> rules = RefasterRuleBuilderScanner.extractRules(tree, context);
try (ObjectOutputStream output = new ObjectOutputStream(Files.newOutputStream(destinationPath))) {
output.writeObject(CompositeCodeTransformer.compose(rules));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.sun.source.tree.ClassTree 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.ClassTree in project error-prone by google.
the class ImmutableAnalysis method areFieldsImmutable.
/**
* Check a single class' fields for immutability.
*
* @param immutableTyParams the in-scope immutable type parameters
* @param classType the type to check the fields of
*/
Violation areFieldsImmutable(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType classType) {
ClassSymbol classSym = (ClassSymbol) classType.tsym;
if (classSym.members() == null) {
return Violation.absent();
}
Filter<Symbol> instanceFieldFilter = new Filter<Symbol>() {
@Override
public boolean accepts(Symbol symbol) {
return symbol.getKind() == ElementKind.FIELD && !symbol.isStatic();
}
};
Map<Symbol, Tree> declarations = new HashMap<>();
if (tree.isPresent()) {
for (Tree member : tree.get().getMembers()) {
Symbol sym = ASTHelpers.getSymbol(member);
if (sym != null) {
declarations.put(sym, member);
}
}
}
// javac gives us members in reverse declaration order
// handling them in declaration order leads to marginally better diagnostics
List<Symbol> members = ImmutableList.copyOf(classSym.members().getSymbols(instanceFieldFilter)).reverse();
for (Symbol member : members) {
Optional<Tree> memberTree = Optional.fromNullable(declarations.get(member));
Violation info = isFieldImmutable(memberTree, immutableTyParams, classSym, classType, (VarSymbol) member);
if (info.isPresent()) {
return info;
}
}
return Violation.absent();
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class RefasterScanner method visitClass.
@Override
public Void visitClass(ClassTree node, Context context) {
Symbol sym = ASTHelpers.getSymbol(node);
if (sym == null || !sym.getQualifiedName().contentEquals(rule().qualifiedTemplateClass())) {
ListBuffer<JCStatement> statements = new ListBuffer<>();
for (Tree tree : node.getMembers()) {
if (tree instanceof JCStatement) {
statements.append((JCStatement) tree);
} else {
tree.accept(this, context);
}
}
scan(TreeMaker.instance(context).Block(0, statements.toList()), context);
}
return null;
}
use of com.sun.source.tree.ClassTree in project ceylon-compiler by ceylon.
the class T6404194 method main.
public static void main(String... args) throws IOException {
class MyFileObject extends SimpleJavaFileObject {
MyFileObject() {
super(URI.create("myfo:///Test.java"), SOURCE);
}
@Override
public String getCharContent(boolean ignoreEncodingErrors) {
// 01234567890123456 7890 123456789012345
return "@SuppressWarning(\"foo\") @Deprecated class Test { Test() { } }";
}
}
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject());
JavacTask task = (JavacTask) javac.getTask(null, null, null, null, null, compilationUnits);
Trees trees = Trees.instance(task);
CompilationUnitTree toplevel = task.parse().iterator().next();
ClassTree classTree = (ClassTree) toplevel.getTypeDecls().get(0);
List<? extends Tree> annotations = classTree.getModifiers().getAnnotations();
Tree tree1 = annotations.get(0);
Tree tree2 = annotations.get(1);
long pos = trees.getSourcePositions().getStartPosition(toplevel, tree1);
if (pos != 0)
throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree1, pos));
pos = trees.getSourcePositions().getEndPosition(toplevel, tree1);
if (pos != 23)
throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree1, pos));
pos = trees.getSourcePositions().getStartPosition(toplevel, tree2);
if (pos != 24)
throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree2, pos));
pos = trees.getSourcePositions().getEndPosition(toplevel, tree2);
if (pos != 35)
throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree2, pos));
}
Aggregations