Search in sources :

Example 56 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class DoubleCheckedLocking method findDCL.

/**
 * Matches an instance of DCL. The canonical pattern is:
 *
 * <pre>{@code
 * if ($X == null) {
 *   synchronized (...) {
 *     if ($X == null) {
 *       ...
 *     }
 *     ...
 *   }
 * }
 * }</pre>
 *
 * Gaps before the synchronized or inner 'if' statement are ignored, and the operands in the
 * null-checks are accepted in either order.
 */
@Nullable
static DCLInfo findDCL(IfTree outerIf) {
    // TODO(cushon): Optional.ifPresent...
    ExpressionTree outerIfTest = getNullCheckedExpression(outerIf.getCondition());
    if (outerIfTest == null) {
        return null;
    }
    SynchronizedTree synchTree = getChild(outerIf.getThenStatement(), SynchronizedTree.class);
    if (synchTree == null) {
        return null;
    }
    IfTree innerIf = getChild(synchTree.getBlock(), IfTree.class);
    if (innerIf == null) {
        return null;
    }
    ExpressionTree innerIfTest = getNullCheckedExpression(innerIf.getCondition());
    if (innerIfTest == null) {
        return null;
    }
    Symbol outerSym = ASTHelpers.getSymbol(outerIfTest);
    if (!Objects.equals(outerSym, ASTHelpers.getSymbol(innerIfTest))) {
        return null;
    }
    if (!(outerSym instanceof VarSymbol)) {
        return null;
    }
    VarSymbol var = (VarSymbol) outerSym;
    return DCLInfo.create(outerIf, synchTree, innerIf, var);
}
Also used : SynchronizedTree(com.sun.source.tree.SynchronizedTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) ExpressionTree(com.sun.source.tree.ExpressionTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) IfTree(com.sun.source.tree.IfTree) Nullable(javax.annotation.Nullable)

Example 57 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class EqualsReference method matchMethod.

@Override
public Description matchMethod(MethodTree methodTree, VisitorState visitorState) {
    if (EQUALS_MATCHER.matches(methodTree, visitorState)) {
        VariableTree variableTree = methodTree.getParameters().get(0);
        VarSymbol varSymbol = ASTHelpers.getSymbol(variableTree);
        TreeScannerEquals treeScannerEquals = new TreeScannerEquals(methodTree);
        treeScannerEquals.scan(methodTree.getBody(), varSymbol);
        if (treeScannerEquals.hasIllegalEquals) {
            return describeMatch(methodTree);
        }
    }
    return Description.NO_MATCH;
}
Also used : VariableTree(com.sun.source.tree.VariableTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 58 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class CatchFail method catchVariableIsUsed.

private boolean catchVariableIsUsed(CatchTree c) {
    VarSymbol sym = ASTHelpers.getSymbol(c.getParameter());
    boolean[] found = { false };
    c.getBlock().accept(new TreeScanner<Void, Void>() {

        @Override
        public Void visitIdentifier(IdentifierTree node, Void aVoid) {
            if (Objects.equals(sym, ASTHelpers.getSymbol(node))) {
                found[0] = true;
            }
            return super.visitIdentifier(node, aVoid);
        }
    }, null);
    return found[0];
}
Also used : IdentifierTree(com.sun.source.tree.IdentifierTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 59 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class HidingField method matchClass.

@Override
public Description matchClass(ClassTree classTree, VisitorState visitorState) {
    List<VariableTree> originalClassMembers = classTree.getMembers().stream().filter(mem -> mem instanceof VariableTree).map(mem -> (VariableTree) mem).filter(mem -> !isSuppressed(ASTHelpers.getSymbol(mem)) && !isIgnoredType(mem) && !isStatic(mem)).collect(toCollection(ArrayList::new));
    ClassSymbol classSymbol = ASTHelpers.getSymbol(classTree);
    while (!Objects.equals(classSymbol.getSuperclass(), Type.noType)) {
        TypeSymbol parentSymbol = classSymbol.getSuperclass().asElement();
        List<Symbol> parentElements = parentSymbol.getEnclosedElements();
        Map<Name, VarSymbol> parentMembers = parentElements.stream().filter(mem -> (mem instanceof VarSymbol)).map(mem -> (VarSymbol) mem).filter(mem -> (!mem.isPrivate() && !mem.getModifiers().contains(Modifier.STATIC))).collect(Collectors.toMap(Symbol::getSimpleName, mem -> mem));
        checkForHiddenFields(originalClassMembers, parentMembers, parentSymbol.getSimpleName(), classTree, visitorState);
        classSymbol = (ClassSymbol) parentSymbol;
    }
    return Description.NO_MATCH;
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) Modifier(javax.lang.model.element.Modifier) VariableTree(com.sun.source.tree.VariableTree) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ArrayList(java.util.ArrayList) Collectors.toCollection(java.util.stream.Collectors.toCollection) VisitorState(com.google.errorprone.VisitorState) Map(java.util.Map) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) ClassTree(com.sun.source.tree.ClassTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Name(javax.lang.model.element.Name) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) Symbol(com.sun.tools.javac.code.Symbol) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Description(com.google.errorprone.matchers.Description) Builder(com.google.errorprone.matchers.Description.Builder) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) VariableTree(com.sun.source.tree.VariableTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Name(javax.lang.model.element.Name)

Example 60 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class DateFormatConstant method matchVariable.

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
    if (tree.getInitializer() == null) {
        return NO_MATCH;
    }
    VarSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym == null || sym.getKind() != ElementKind.FIELD) {
        return NO_MATCH;
    }
    String name = sym.getSimpleName().toString();
    if (!(sym.isStatic() && sym.getModifiers().contains(Modifier.FINAL))) {
        return NO_MATCH;
    }
    if (!name.equals(name.toUpperCase())) {
        return NO_MATCH;
    }
    if (!isSubtype(getType(tree), state.getTypeFromString("java.text.DateFormat"), state)) {
        return NO_MATCH;
    }
    return buildDescription(tree).addFix(threadLocalFix(tree, state, sym)).addFix(renameVariable(tree, CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tree.getName().toString()), state)).build();
}
Also used : VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Aggregations

VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)68 Symbol (com.sun.tools.javac.code.Symbol)29 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)23 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)22 Type (com.sun.tools.javac.code.Type)20 ExpressionTree (com.sun.source.tree.ExpressionTree)15 Tree (com.sun.source.tree.Tree)15 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)11 VariableTree (com.sun.source.tree.VariableTree)11 ClassTree (com.sun.source.tree.ClassTree)10 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MemberSelectTree (com.sun.source.tree.MemberSelectTree)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)10 NewClassTree (com.sun.source.tree.NewClassTree)9 ArrayList (java.util.ArrayList)9 VisitorState (com.google.errorprone.VisitorState)7 MethodTree (com.sun.source.tree.MethodTree)7 TreePath (com.sun.source.util.TreePath)7 ClassType (com.sun.tools.javac.code.Type.ClassType)7