Search in sources :

Example 51 with ExpressionTree

use of com.sun.source.tree.ExpressionTree 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 52 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class SizeGreaterThanOrEqualsZero method matchBinary.

@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
    // Easy stuff: needs to be a binary expression of the form foo >= 0 or 0 <= foo
    ExpressionType expressionType = isGreaterThanEqualToZero(tree);
    if (expressionType == ExpressionType.MISMATCH) {
        return Description.NO_MATCH;
    }
    ExpressionTree operand = expressionType == ExpressionType.GREATER_THAN_EQUAL ? tree.getLeftOperand() : tree.getRightOperand();
    if (operand instanceof MethodInvocationTree) {
        MethodInvocationTree callToSize = (MethodInvocationTree) operand;
        if (INSTANCE_METHOD_MATCHER.matches(callToSize, state)) {
            return provideReplacementForMethodInvocation(tree, callToSize, state, expressionType);
        } else if (STATIC_METHOD_MATCHER.matches(callToSize, state)) {
            return provideReplacementForStaticMethodInvocation(tree, callToSize, state, expressionType);
        }
    } else if (operand instanceof MemberSelectTree) {
        if (ARRAY_LENGTH_MATCHER.matches((MemberSelectTree) operand, state)) {
            return removeEqualsFromComparison(tree, state, expressionType);
        }
    }
    return Description.NO_MATCH;
}
Also used : MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 53 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class SizeGreaterThanOrEqualsZero method provideReplacementForStaticMethodInvocation.

private Description provideReplacementForStaticMethodInvocation(BinaryTree tree, MethodInvocationTree callToSize, final VisitorState state, ExpressionType expressionType) {
    ExpressionTree classToken = ASTHelpers.getReceiver(callToSize);
    if (HAS_EMPTY_METHOD.matches(classToken, state)) {
        List<CharSequence> argumentSourceValues = Lists.transform(callToSize.getArguments(), new Function<ExpressionTree, CharSequence>() {

            @Override
            public CharSequence apply(ExpressionTree expressionTree) {
                return state.getSourceForNode((JCTree) expressionTree);
            }
        });
        String argumentString = Joiner.on(',').join(argumentSourceValues);
        return describeMatch(tree, SuggestedFix.replace(tree, "!" + state.getSourceForNode((JCTree) classToken) + ".isEmpty(" + argumentString + ")"));
    } else {
        return removeEqualsFromComparison(tree, state, expressionType);
    }
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 54 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class TruthConstantAsserts method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
    if (methodInvocationTree.getArguments().isEmpty()) {
        return Description.NO_MATCH;
    }
    if (!TRUTH_SUBJECT_CALL.matches(methodInvocationTree, state)) {
        return Description.NO_MATCH;
    }
    ExpressionTree rec = ASTHelpers.getReceiver(methodInvocationTree);
    if (rec == null) {
        return Description.NO_MATCH;
    }
    if (!ASSERT_THAT.matches(rec, state)) {
        return Description.NO_MATCH;
    }
    ExpressionTree expr = getOnlyElement(((MethodInvocationTree) rec).getArguments());
    if (expr == null) {
        return Description.NO_MATCH;
    }
    // check that argument of assertThat is a constant
    if (ASTHelpers.constValue(expr) == null) {
        return Description.NO_MATCH;
    }
    // check that expectation isn't a constant
    ExpressionTree expectation = getOnlyElement(methodInvocationTree.getArguments());
    if (ASTHelpers.constValue(expectation) != null) {
        return Description.NO_MATCH;
    }
    SuggestedFix fix = SuggestedFix.swap(expr, expectation);
    return buildDescription(methodInvocationTree).addFix(fix).build();
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 55 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class TruthSelfEquals method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
    if (methodInvocationTree.getArguments().isEmpty()) {
        return Description.NO_MATCH;
    }
    Description.Builder description = buildDescription(methodInvocationTree);
    ExpressionTree toReplace = methodInvocationTree.getArguments().get(0);
    if (EQUALS_MATCHER.matches(methodInvocationTree, state)) {
        description.setMessage(generateSummary(ASTHelpers.getSymbol(methodInvocationTree).getSimpleName().toString(), "passes")).addFix(suggestEqualsTesterFix(methodInvocationTree, toReplace));
    } else if (NOT_EQUALS_MATCHER.matches(methodInvocationTree, state)) {
        description.setMessage(generateSummary(ASTHelpers.getSymbol(methodInvocationTree).getSimpleName().toString(), "fails"));
    } else {
        return Description.NO_MATCH;
    }
    Fix fix = SelfEquals.fieldFix(toReplace, state);
    if (fix != null) {
        description.addFix(fix);
    }
    return description.build();
}
Also used : Description(com.google.errorprone.matchers.Description) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

ExpressionTree (com.sun.source.tree.ExpressionTree)78 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)27 Tree (com.sun.source.tree.Tree)18 Type (com.sun.tools.javac.code.Type)18 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)16 Symbol (com.sun.tools.javac.code.Symbol)14 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)14 JCTree (com.sun.tools.javac.tree.JCTree)14 Fix (com.google.errorprone.fixes.Fix)13 VariableTree (com.sun.source.tree.VariableTree)13 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)13 IdentifierTree (com.sun.source.tree.IdentifierTree)12 MemberSelectTree (com.sun.source.tree.MemberSelectTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)11 VisitorState (com.google.errorprone.VisitorState)9 AssignmentTree (com.sun.source.tree.AssignmentTree)9 BinaryTree (com.sun.source.tree.BinaryTree)9 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)8 MethodTree (com.sun.source.tree.MethodTree)8 TreePath (com.sun.source.util.TreePath)8