Search in sources :

Example 11 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol 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();
}
Also used : Filter(com.sun.tools.javac.util.Filter) HashMap(java.util.HashMap) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 12 with VarSymbol

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

the class CompileTimeConstantChecker method handleMatch.

/**
   * If the non-constant variable is annotated with @CompileTimeConstant, it must have been
   * non-final. Suggest making it final in the error message.
   */
private Description handleMatch(ExpressionTree actualParam, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(actualParam);
    if (!(sym instanceof VarSymbol)) {
        return describeMatch(actualParam);
    }
    VarSymbol var = (VarSymbol) sym;
    if (!hasCompileTimeConstantAnnotation(state, var)) {
        return describeMatch(actualParam);
    }
    return buildDescription(actualParam).setMessage(this.message() + String.format(DID_YOU_MEAN_FINAL_FMT_MESSAGE, var.getSimpleName())).build();
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 13 with VarSymbol

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

the class FuturesGetCheckedIllegalExceptionType method canBeUsedByGetChecked.

private static boolean canBeUsedByGetChecked(MethodSymbol constructor, VisitorState state) {
    Type stringType = state.getSymtab().stringType;
    Type throwableType = state.getSymtab().throwableType;
    // TODO(cpovirk): Check visibility of enclosing types (assuming that it matters to getChecked).
    if (!constructor.getModifiers().contains(PUBLIC)) {
        return false;
    }
    for (VarSymbol param : constructor.getParameters()) {
        if (!isSameType(param.asType(), stringType, state) && !isSameType(param.asType(), throwableType, state)) {
            return false;
        }
    }
    return true;
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) ASTHelpers.isSameType(com.google.errorprone.util.ASTHelpers.isSameType) Type(com.sun.tools.javac.code.Type) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 14 with VarSymbol

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

the class ASTHelpers method getRootAssignable.

/**
   * Find the root assignable expression of a chain of field accesses.  If there is no root
   * (i.e, a bare method call or a static method call), return null.
   *
   * <p>Examples:
   * <pre>
   * {@code
   *    a.trim().intern() ==> a
   *    a.b.trim().intern() ==> a.b
   *    this.intValue.foo() ==> this.intValue
   *    this.foo() ==> this
   *    intern() ==> null
   *    String.format() ==> null
   *    java.lang.String.format() ==> null
   * }
   * </pre>
   */
public static ExpressionTree getRootAssignable(MethodInvocationTree methodInvocationTree) {
    if (!(methodInvocationTree instanceof JCMethodInvocation)) {
        throw new IllegalArgumentException("Expected type to be JCMethodInvocation, but was " + methodInvocationTree.getClass());
    }
    // Check for bare method call, e.g. intern().
    if (((JCMethodInvocation) methodInvocationTree).getMethodSelect() instanceof JCIdent) {
        return null;
    }
    // Unwrap the field accesses until you get to an identifier.
    ExpressionTree expr = methodInvocationTree;
    while (expr instanceof JCMethodInvocation) {
        expr = ((JCMethodInvocation) expr).getMethodSelect();
        if (expr instanceof JCFieldAccess) {
            expr = ((JCFieldAccess) expr).getExpression();
        }
    }
    // We only want assignable identifiers.
    Symbol sym = getSymbol(expr);
    if (sym instanceof VarSymbol) {
        return expr;
    }
    return null;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 15 with VarSymbol

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

the class AbstractArgumentParameterChecker method findReplacements.

private Description findReplacements(List<? extends ExpressionTree> args, com.sun.tools.javac.util.List<VarSymbol> params, boolean isVarArgs, VisitorState state, Tree tree) {
    if (args.isEmpty()) {
        return Description.NO_MATCH;
    }
    ImmutableSet<PotentialReplacement> potentialReplacements = potentialReplacementsFunction.apply(state.withPath(new TreePath(state.getPath(), args.get(0))));
    SuggestedFix.Builder fix = SuggestedFix.builder();
    // Don't suggest for the varargs parameter.
    // TODO(eaftan): Reconsider this, especially if the argument is of array type or is itself
    // a varargs parameter.
    int maxArg = isVarArgs ? params.size() - 1 : params.size();
    for (int i = 0; i < maxArg; i++) {
        ExpressionTree arg = args.get(i);
        VarSymbol param = params.get(i);
        if (!validKinds.contains(arg.getKind()) || !parameterPredicate.test(param)) {
            continue;
        }
        String extractedArgumentName = extractArgumentName(arg);
        if (extractedArgumentName == null) {
            continue;
        }
        double currSimilarity = similarityMetric.applyAsDouble(extractedArgumentName, param.getSimpleName().toString());
        if (1.0 - currSimilarity < beta) {
            // No way for any replacement to be at least BETA better than the current argument
            continue;
        }
        ReplacementWithSimilarity bestReplacement = potentialReplacements.stream().filter(replacement -> !replacement.sym().equals(ASTHelpers.getSymbol(arg))).filter(replacement -> isSubtypeHandleCompletionFailures(replacement.sym(), param, state)).map(replacement -> ReplacementWithSimilarity.create(replacement, similarityMetric.applyAsDouble(replacement.argumentName(), param.getSimpleName().toString()))).max(Comparator.comparingDouble(ReplacementWithSimilarity::similarity)).orElse(null);
        if ((bestReplacement != null) && (bestReplacement.similarity() - currSimilarity >= beta)) {
            fix.replace(arg, bestReplacement.replacement().replacementString());
        }
    }
    if (fix.isEmpty()) {
        return Description.NO_MATCH;
    } else {
        return describeMatch(tree, fix.build());
    }
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Function(java.util.function.Function) NewClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Kind(com.sun.source.tree.Tree.Kind) NewClassTree(com.sun.source.tree.NewClassTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) Nullable(javax.annotation.Nullable) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) MethodInvocationTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher) TreePath(com.sun.source.util.TreePath) ImmutableSet(com.google.common.collect.ImmutableSet) ExpressionTree(com.sun.source.tree.ExpressionTree) Predicate(java.util.function.Predicate) Symbol(com.sun.tools.javac.code.Symbol) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ToDoubleBiFunction(java.util.function.ToDoubleBiFunction) List(java.util.List) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) Description(com.google.errorprone.matchers.Description) AutoValue(com.google.auto.value.AutoValue) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Comparator(java.util.Comparator) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) TreePath(com.sun.source.util.TreePath) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ExpressionTree(com.sun.source.tree.ExpressionTree) 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