Search in sources :

Example 11 with Symbol

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

the class StaticQualifiedUsingExpression method matchMemberSelect.

@Override
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
    if (!staticAccessedFromInstanceMatcher.matches(tree, state)) {
        return Description.NO_MATCH;
    }
    // Is the static member being accessed a method or a variable?
    Symbol staticMemberSym = ASTHelpers.getSymbol(tree);
    if (staticMemberSym == null) {
        return Description.NO_MATCH;
    }
    boolean isMethod = staticMemberSym instanceof MethodSymbol;
    // Is the static member defined in this class?
    Symbol ownerSym = staticMemberSym.owner;
    Symbol whereAccessedSym = ASTHelpers.getSymbol(ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), ClassTree.class));
    if (!(ownerSym instanceof ClassSymbol && whereAccessedSym instanceof ClassSymbol)) {
        return Description.NO_MATCH;
    }
    boolean staticMemberDefinedHere = whereAccessedSym.equals(ownerSym);
    SuggestedFix.Builder fix = SuggestedFix.builder();
    String replacement;
    if (staticMemberDefinedHere && isMethod) {
        // If the static member is defined in the enclosing class and the member is a method, then
        // just use the bare method name. Don't do this for fields, because they may share a simple
        // name with a local in the same scope.
        // TODO(eaftan): If we had access to name resolution info, we could do this in all applicable
        // cases.  Investigate Scope.Entry for this.
        replacement = tree.getIdentifier().toString();
    } else {
        // Replace the operand of the field access expression with the simple name of the class.
        replacement = ownerSym.getSimpleName() + "." + tree.getIdentifier();
        // Don't import implicitly imported packages (java.lang.* and current package).
        // TODO(cushon): move this logic into addImport?
        Symbol packageSym = ownerSym.packge();
        if (!packageSym.toString().equals("java.lang") && !packageSym.equals(whereAccessedSym.packge())) {
            fix.addImport(ownerSym.toString());
        }
    }
    fix.replace(tree, replacement);
    // Compute strings to interpolate into diagnostic message.
    String memberName = staticMemberSym.getSimpleName().toString();
    String methodOrVariable = isMethod ? "method" : "variable";
    String customDiagnosticMessage = String.format(MESSAGE_TEMPLATE, methodOrVariable, memberName, replacement);
    return buildDescription(tree).setMessage(customDiagnosticMessage).addFix(fix.build()).build();
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassTree(com.sun.source.tree.ClassTree)

Example 12 with Symbol

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

the class DoubleCheckedLocking method handleLocal.

/**
   * Report a diagnostic for an instance of DCL on a local variable. A match is only reported
   * if a non-volatile field is written to the variable after acquiring the lock and before
   * the second null-check on the local.
   *
   * <p>e.g.
   *
   * <pre>
   * {@code
   * if ($X == null) {
   *   synchronized (...) {
   *     $X = myNonVolatileField;
   *     if ($X == null) {
   *       ...
   *     }
   *     ...
   *   }
   * }
   * }
   * </pre>
   */
private Description handleLocal(DCLInfo info, VisitorState state) {
    JCExpressionStatement expr = getChild(info.synchTree().getBlock(), JCExpressionStatement.class);
    if (expr.getStartPosition() > ((JCTree) info.innerIf()).getStartPosition()) {
        return Description.NO_MATCH;
    }
    if (!(expr.getExpression() instanceof JCAssign)) {
        return Description.NO_MATCH;
    }
    JCAssign assign = (JCAssign) expr.getExpression();
    if (!Objects.equals(ASTHelpers.getSymbol(assign.getVariable()), info.sym())) {
        return Description.NO_MATCH;
    }
    Symbol sym = ASTHelpers.getSymbol(assign.getExpression());
    if (!(sym instanceof VarSymbol)) {
        return Description.NO_MATCH;
    }
    VarSymbol fvar = (VarSymbol) sym;
    if (fvar.getKind() != ElementKind.FIELD) {
        return Description.NO_MATCH;
    }
    return handleField(info.outerIf(), fvar, state);
}
Also used : JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement)

Example 13 with Symbol

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

the class GuardedByChecker method isRWLock.

/**
   * Returns true if the lock expression corresponds to a
   * {@code java.util.concurrent.locks.ReadWriteLock}.
   */
private static boolean isRWLock(GuardedByExpression guard, VisitorState state) {
    Type guardType = guard.type();
    if (guardType == null) {
        return false;
    }
    Symbol rwLockSymbol = state.getSymbolFromString(JUC_READ_WRITE_LOCK);
    if (rwLockSymbol == null) {
        return false;
    }
    return state.getTypes().isSubtype(guardType, rwLockSymbol.type);
}
Also used : Type(com.sun.tools.javac.code.Type) Symbol(com.sun.tools.javac.code.Symbol)

Example 14 with Symbol

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

the class GuardedBySymbolResolver method resolveType.

/**
   * Resolves a simple name as a type. Considers super classes, lexically enclosing classes, and
   * then arbitrary types available in the current environment.
   */
private Symbol resolveType(String name, SearchSuperTypes searchSuperTypes) {
    Symbol type = null;
    if (searchSuperTypes == SearchSuperTypes.YES) {
        type = getSuperType(enclosingClass, name);
    }
    if (enclosingClass.getSimpleName().contentEquals(name)) {
        type = enclosingClass;
    }
    if (type == null) {
        type = getLexicallyEnclosing(enclosingClass, name);
    }
    if (type == null) {
        type = attribIdent(name);
    }
    checkGuardedBy(!(type instanceof Symbol.PackageSymbol), "All we could find for '%s' was a package symbol.", name);
    return type;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Example 15 with Symbol

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

the class GuardedBySymbolResolver method resolveIdentifier.

@Override
public Symbol resolveIdentifier(IdentifierTree node) {
    String name = node.getName().toString();
    if (name.equals("this")) {
        return enclosingClass;
    }
    // isn't legal java.
    if (name.equals("itself")) {
        Symbol sym = ASTHelpers.getSymbol(decl);
        if (sym == null) {
            throw new IllegalGuardedBy(decl.getClass().toString());
        }
        return sym;
    }
    Symbol.VarSymbol field = getField(enclosingClass, name);
    if (field != null) {
        return field;
    }
    Symbol type = resolveType(name, SearchSuperTypes.YES);
    if (type != null) {
        return type;
    }
    throw new IllegalGuardedBy(name);
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Aggregations

Symbol (com.sun.tools.javac.code.Symbol)195 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)56 Type (com.sun.tools.javac.code.Type)54 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)53 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)36 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)29 JCTree (com.sun.tools.javac.tree.JCTree)28 ClassType (com.sun.tools.javac.code.Type.ClassType)18 Tree (com.sun.source.tree.Tree)17 ExpressionTree (com.sun.source.tree.ExpressionTree)15 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)15 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)15 ClassTree (com.sun.source.tree.ClassTree)14 MethodTree (com.sun.source.tree.MethodTree)14 Name (com.sun.tools.javac.util.Name)14 IdentifierTree (com.sun.source.tree.IdentifierTree)13 ArrayType (com.sun.tools.javac.code.Type.ArrayType)12 MethodType (com.sun.tools.javac.code.Type.MethodType)12 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)12