use of com.sun.source.tree.ClassTree in project error-prone by google.
the class EqualsHashCode method matchClass.
@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
TypeSymbol symbol = ASTHelpers.getSymbol(classTree);
if (symbol.getKind() != ElementKind.CLASS) {
return Description.NO_MATCH;
}
MethodTree equals = null;
for (Tree member : classTree.getMembers()) {
if (!(member instanceof MethodTree)) {
continue;
}
MethodTree methodTree = (MethodTree) member;
if (EQUALS_MATCHER.matches(methodTree, state)) {
equals = methodTree;
}
}
if (equals == null) {
return Description.NO_MATCH;
}
MethodSymbol hashCodeSym = ASTHelpers.resolveExistingMethod(state, symbol, state.getName("hashCode"), ImmutableList.<Type>of(), ImmutableList.<Type>of());
if (hashCodeSym.owner.equals(state.getSymtab().objectType.tsym)) {
return describeMatch(equals);
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class FindIdentifiers method findAllIdents.
/**
* Finds the set of all bare variable identifiers in scope at the current location. Identifiers
* are ordered by ascending distance/scope count from the current location to match shadowing
* rules. That is, if two variables with the same simple names appear in the set, the one that
* appears first in iteration order is the one you get if you use the bare name in the source
* code.
*
* <p>We do not report variables that would require a qualfied access. We also do not handle
* wildcard imports.
*/
public static LinkedHashSet<VarSymbol> findAllIdents(VisitorState state) {
ImmutableSet.Builder<VarSymbol> result = new ImmutableSet.Builder<>();
Tree prev = state.getPath().getLeaf();
for (Tree curr : state.getPath().getParentPath()) {
switch(curr.getKind()) {
case BLOCK:
for (StatementTree stmt : ((BlockTree) curr).getStatements()) {
if (stmt.equals(prev)) {
break;
}
addIfVariable(stmt, result);
}
break;
case METHOD:
for (VariableTree param : ((MethodTree) curr).getParameters()) {
result.add(ASTHelpers.getSymbol(param));
}
break;
case CATCH:
result.add(ASTHelpers.getSymbol(((CatchTree) curr).getParameter()));
break;
case CLASS:
case INTERFACE:
case ENUM:
case ANNOTATION_TYPE:
// field is referred to by qualified name, but we don't support that.
for (Tree member : ((ClassTree) curr).getMembers()) {
if (member.equals(prev)) {
break;
}
addIfVariable(member, result);
}
// Collect inherited fields.
Type classType = ASTHelpers.getType(curr);
com.sun.tools.javac.util.List<Type> superTypes = state.getTypes().closure(classType).tail;
for (Type type : superTypes) {
Scope scope = type.tsym.members();
ImmutableList.Builder<VarSymbol> varsList = new ImmutableList.Builder<VarSymbol>();
for (Symbol var : scope.getSymbols(VarSymbol.class::isInstance)) {
varsList.add((VarSymbol) var);
}
result.addAll(varsList.build().reverse());
}
break;
case FOR_LOOP:
addAllIfVariable(((ForLoopTree) curr).getInitializer(), result);
break;
case ENHANCED_FOR_LOOP:
result.add(ASTHelpers.getSymbol(((EnhancedForLoopTree) curr).getVariable()));
break;
case TRY:
TryTree tryTree = (TryTree) curr;
boolean inResources = false;
for (Tree resource : tryTree.getResources()) {
if (resource.equals(prev)) {
inResources = true;
break;
}
}
if (inResources) {
// Case 1: we're in one of the resource declarations
for (Tree resource : tryTree.getResources()) {
if (resource.equals(prev)) {
break;
}
addIfVariable(resource, result);
}
} else if (tryTree.getBlock().equals(prev)) {
// Case 2: We're in the block (not a catch or finally)
addAllIfVariable(tryTree.getResources(), result);
}
break;
case COMPILATION_UNIT:
for (ImportTree importTree : ((CompilationUnitTree) curr).getImports()) {
if (importTree.isStatic() && importTree.getQualifiedIdentifier().getKind() == Kind.MEMBER_SELECT) {
MemberSelectTree memberSelectTree = (MemberSelectTree) importTree.getQualifiedIdentifier();
Scope scope = state.getTypes().membersClosure(ASTHelpers.getType(memberSelectTree.getExpression()), /*skipInterface*/
false);
for (Symbol var : scope.getSymbols(sym -> sym instanceof VarSymbol && sym.getSimpleName().equals(memberSelectTree.getIdentifier()))) {
result.add((VarSymbol) var);
}
}
}
break;
default:
// other node types don't introduce variables
break;
}
prev = curr;
}
// TODO(eaftan): switch out collector for ImmutableSet.toImmutableSet()
return result.build().stream().filter(var -> isVisible(var, state.getPath())).collect(Collectors.toCollection(LinkedHashSet::new));
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class ReferenceEquality method matchArgument.
@Override
protected boolean matchArgument(ExpressionTree tree, VisitorState state) {
Type type = ASTHelpers.getType(tree);
if (!type.isReference()) {
return false;
}
ClassTree classTree = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
if (classTree == null) {
return false;
}
Type classType = ASTHelpers.getType(classTree);
if (classType == null) {
return false;
}
if (inEqualsOrCompareTo(classType, type, state)) {
return false;
}
if (ASTHelpers.isSubtype(type, state.getSymtab().enumSym.type, state)) {
return false;
}
if (ASTHelpers.isSubtype(type, state.getSymtab().classType, state)) {
return false;
}
if (!implementsEquals(type, state)) {
return false;
}
return true;
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class Util method addPrivateConstructor.
// TODO(dpb): Account for indentation level.
private static SuggestedFix.Builder addPrivateConstructor(ClassTree classTree) {
SuggestedFix.Builder fix = SuggestedFix.builder();
String indent = " ";
for (Tree member : classTree.getMembers()) {
if (member.getKind().equals(METHOD) && !isGeneratedConstructor((MethodTree) member)) {
fix.prefixWith(member, indent + "private " + classTree.getSimpleName() + "() {} // no instances\n" + indent);
break;
}
if (!member.getKind().equals(METHOD)) {
indent = "";
}
}
return fix;
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class AutoFactoryAtInject method matchAnnotation.
@Override
public final Description matchAnnotation(AnnotationTree annotationTree, VisitorState state) {
if (!IS_APPLICATION_OF_AT_INJECT.matches(annotationTree, state)) {
return Description.NO_MATCH;
}
Tree annotatedTree = getCurrentlyAnnotatedNode(state);
if (!annotatedTree.getKind().equals(METHOD) || !methodIsConstructor().matches((MethodTree) annotatedTree, state)) {
return Description.NO_MATCH;
}
ClassTree classTree = findEnclosingNode(state.getPath(), ClassTree.class);
ImmutableList<Tree> potentiallyAnnotatedTrees = new ImmutableList.Builder<Tree>().add(classTree).addAll(getConstructors(classTree)).build();
for (Tree potentiallyAnnotatedTree : potentiallyAnnotatedTrees) {
if (HAS_AUTO_FACTORY_ANNOTATION.matches(potentiallyAnnotatedTree, state)) {
return describeMatch(annotationTree, SuggestedFix.delete(annotationTree));
}
}
return Description.NO_MATCH;
}
Aggregations