Search in sources :

Example 36 with VarSymbol

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

the class StrictFormatStringValidation method validate.

@Nullable
public static ValidationResult validate(ExpressionTree formatStringTree, List<? extends ExpressionTree> args, VisitorState state) {
    String formatStringValue = ASTHelpers.constValue(formatStringTree, String.class);
    // so don't bother with annotations and just check if the parameters match the format string.
    if (formatStringValue != null) {
        return FormatStringValidation.validate(ImmutableList.<ExpressionTree>builder().add(formatStringTree).addAll(args).build(), state);
    }
    // The format string is not a compile time constant. Check if it is an @FormatString method
    // parameter or is in an @FormatMethod method.
    Symbol formatStringSymbol = ASTHelpers.getSymbol(formatStringTree);
    if (!(formatStringSymbol instanceof VarSymbol)) {
        return ValidationResult.create(null, String.format("Format strings must be either a literal or a variable. Other expressions" + " are not valid.\n" + "Invalid format string: %s", formatStringTree));
    }
    if ((formatStringSymbol.flags() & (Flags.FINAL | Flags.EFFECTIVELY_FINAL)) == 0) {
        return ValidationResult.create(null, "All variables passed as @FormatString must be final or effectively final");
    }
    if (formatStringSymbol.getKind() == ElementKind.PARAMETER) {
        return validateFormatStringParamter(formatStringTree, formatStringSymbol, args, state);
    } else {
        // works with the format arguments.
        return validateFormatStringVariable(formatStringTree, formatStringSymbol, args, state);
    }
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree) FormatString(com.google.errorprone.annotations.FormatString) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Nullable(javax.annotation.Nullable)

Example 37 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 38 with VarSymbol

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

the class MockitoCast method matchCompilationUnit.

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, final VisitorState state) {
    Symbol mockitoSym = state.getSymbolFromString(MOCKITO_CLASS);
    if (mockitoSym == null) {
        // fast path if mockito isn't being used
        return Description.NO_MATCH;
    }
    // collect variable symbols for standard Answer constants that don't support generics
    final Set<Symbol> badAnswers = new LinkedHashSet<>();
    for (Symbol member : mockitoSym.members().getSymbols(LookupKind.NON_RECURSIVE)) {
        if (member.getKind() != ElementKind.FIELD) {
            continue;
        }
        if (BAD_ANSWER_STRATEGIES.contains(member.getSimpleName().toString())) {
            badAnswers.add(member);
        }
    }
    // collect mocks that are initialized in this compilation unit using a bad answer strategy
    final Set<VarSymbol> mockVariables = MockInitializationScanner.scan(state, badAnswers);
    // check for when(...) calls on mocks using a bad answer strategy
    new WhenNeedsCastScanner(mockVariables, state).scan(state.getPath(), null);
    // errors are reported in WhenNeedsCastScanner
    return Description.NO_MATCH;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 39 with VarSymbol

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

the class AbstractToString method checkToString.

/**
   * Tests if the given expression is converted to a String by its parent (i.e. its parent
   * is a string concat expression, {@code String.format}, or {@code println(Object)}).
   */
private Description checkToString(ExpressionTree tree, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (!(sym instanceof VarSymbol || sym instanceof MethodSymbol)) {
        return NO_MATCH;
    }
    Type type = ASTHelpers.getType(tree);
    if (type instanceof MethodType) {
        type = type.getReturnType();
    }
    Tree parent = state.getPath().getParentPath().getLeaf();
    ToStringKind toStringKind = isToString(parent, tree, state);
    if (toStringKind == ToStringKind.NONE) {
        return NO_MATCH;
    }
    Optional<Fix> fix;
    switch(toStringKind) {
        case IMPLICIT:
            fix = implicitToStringFix(tree, state);
            break;
        case EXPLICIT:
            fix = toStringFix(parent, tree, state);
            break;
        default:
            throw new AssertionError(toStringKind);
    }
    if (!typePredicate().apply(type, state)) {
        return NO_MATCH;
    }
    return maybeFix(tree, fix);
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Fix(com.google.errorprone.fixes.Fix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 40 with VarSymbol

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

the class SynchronizeOnNonFinalField method matchSynchronized.

@Override
public Description matchSynchronized(SynchronizedTree tree, VisitorState state) {
    Symbol symbol = ASTHelpers.getSymbol(TreeInfo.skipParens((JCExpression) tree.getExpression()));
    if (!(symbol instanceof VarSymbol)) {
        return Description.NO_MATCH;
    }
    // TODO(cushon): check that the receiver doesn't contain mutable state.
    // Currently 'this.locks[i].mu' is accepted if 'mu' is final but 'locks' is non-final.
    VarSymbol varSymbol = (VarSymbol) symbol;
    if (varSymbol.isLocal() || varSymbol.isStatic() || (varSymbol.flags() & Flags.FINAL) != 0) {
        return Description.NO_MATCH;
    }
    return describeMatch(tree.getExpression());
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Aggregations

VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 Symbol (com.sun.tools.javac.code.Symbol)24 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)21 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)16 Type (com.sun.tools.javac.code.Type)12 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)11 ExpressionTree (com.sun.source.tree.ExpressionTree)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)10 Tree (com.sun.source.tree.Tree)9 IdentifierTree (com.sun.source.tree.IdentifierTree)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)7 ClassType (com.sun.tools.javac.code.Type.ClassType)7 ClassTree (com.sun.source.tree.ClassTree)6 NewClassTree (com.sun.source.tree.NewClassTree)6 VariableTree (com.sun.source.tree.VariableTree)6 BlockTree (com.sun.source.tree.BlockTree)5 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)5 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)5 ForLoopTree (com.sun.source.tree.ForLoopTree)5