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 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 AmbiguousMethodReference method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol origin = getSymbol(tree);
Types types = state.getTypes();
Iterable<Symbol> members = types.membersClosure(getType(tree), /*skipInterface=*/
false).getSymbols();
// collect declared and inherited methods, grouped by reference descriptor
Map<String, List<MethodSymbol>> methods = stream(members.spliterator(), false).filter(MethodSymbol.class::isInstance).map(MethodSymbol.class::cast).filter(m -> m.isConstructor() || m.owner.equals(origin)).collect(groupingBy(m -> methodReferenceDescriptor(types, m), toCollection(ArrayList::new)));
// look for groups of ambiguous method references
for (Tree member : tree.getMembers()) {
if (!(member instanceof MethodTree)) {
continue;
}
MethodSymbol msym = getSymbol((MethodTree) member);
if (isSuppressed(msym)) {
continue;
}
List<MethodSymbol> clash = methods.remove(methodReferenceDescriptor(types, msym));
if (clash == null) {
continue;
}
clash.remove(msym);
// ignore overridden inherited methods and hidden interface methods
clash.removeIf(m -> types.isSubSignature(msym.type, m.type));
if (clash.isEmpty()) {
continue;
}
String message = String.format("This method's reference is ambiguous, its name and functional interface type" + " are the same as: %s", clash.stream().map(m -> Signatures.prettyMethodSignature(origin, m)).collect(joining(", ")));
state.reportMatch(buildDescription(member).setMessage(message).build());
}
return NO_MATCH;
}
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 checker-framework by typetools.
the class TreeUtils method isTreeInStaticScope.
/**
* Returns whether or not the leaf of the tree path is in a static scope.
*
* @param path TreePath whose leaf may or may not be in static scope
* @return returns whether or not the leaf of the tree path is in a static scope
*/
public static boolean isTreeInStaticScope(TreePath path) {
MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
if (enclosingMethod != null) {
return enclosingMethod.getModifiers().getFlags().contains(Modifier.STATIC);
}
// no enclosing method, check for static or initializer block
BlockTree block = enclosingTopLevelBlock(path);
if (block != null) {
return block.isStatic();
}
// check if its in a variable initializer
Tree t = enclosingVariable(path);
if (t != null) {
return ((VariableTree) t).getModifiers().getFlags().contains((Modifier.STATIC));
}
ClassTree classTree = enclosingClass(path);
if (classTree != null) {
return classTree.getModifiers().getFlags().contains((Modifier.STATIC));
}
return false;
}
Aggregations