Search in sources :

Example 6 with ExpressionTree

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

the class FormatStringValidation method validate.

@Nullable
public static ValidationResult validate(Collection<? extends ExpressionTree> arguments, final VisitorState state) {
    Deque<ExpressionTree> args = new ArrayDeque<ExpressionTree>(arguments);
    String formatString = ASTHelpers.constValue(args.removeFirst(), String.class);
    if (formatString == null) {
        return null;
    }
    // Bail out, since we don't know what the actual argument types are.
    if (args.size() == 1) {
        Type type = ASTHelpers.getType(Iterables.getOnlyElement(args));
        if (type instanceof Type.ArrayType && ASTHelpers.isSameType(((Type.ArrayType) type).elemtype, state.getSymtab().objectType, state)) {
            return null;
        }
    }
    Iterable<Object> instances = Iterables.transform(args, new Function<ExpressionTree, Object>() {

        @Override
        public Object apply(ExpressionTree input) {
            try {
                return getInstance(input, state);
            } catch (Throwable t) {
                // ignore symbol completion failures
                return null;
            }
        }
    });
    return validate(formatString, instances);
}
Also used : Type(com.sun.tools.javac.code.Type) ExpressionTree(com.sun.source.tree.ExpressionTree) ArrayDeque(java.util.ArrayDeque) Nullable(javax.annotation.Nullable)

Example 7 with ExpressionTree

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

the class StrictFormatStringValidation method validateFormatStringParamter.

/** Helps {@code validate()} validate a format string that is declared as a method parameter. */
private static ValidationResult validateFormatStringParamter(ExpressionTree formatStringTree, Symbol formatStringSymbol, List<? extends ExpressionTree> args, VisitorState state) {
    if (!isFormatStringParameter(formatStringSymbol, state)) {
        return ValidationResult.create(null, String.format("Format strings must be compile time constant or parameters annotated " + "@FormatString: %s", formatStringTree));
    }
    List<VarSymbol> ownerParams = ((MethodSymbol) formatStringSymbol.owner).getParameters();
    int ownerFormatStringIndex = ownerParams.indexOf(formatStringSymbol);
    ImmutableList.Builder<Type> ownerFormatArgTypesBuilder = ImmutableList.builder();
    for (VarSymbol paramSymbol : ownerParams.subList(ownerFormatStringIndex + 1, ownerParams.size())) {
        ownerFormatArgTypesBuilder.add(paramSymbol.type);
    }
    ImmutableList<Type> ownerFormatArgTypes = ownerFormatArgTypesBuilder.build();
    Types types = state.getTypes();
    ImmutableList.Builder<Type> calleeFormatArgTypesBuilder = ImmutableList.builder();
    for (ExpressionTree formatArgExpression : args) {
        calleeFormatArgTypesBuilder.add(types.erasure(((JCExpression) formatArgExpression).type));
    }
    ImmutableList<Type> calleeFormatArgTypes = calleeFormatArgTypesBuilder.build();
    if (ownerFormatArgTypes.size() != calleeFormatArgTypes.size()) {
        return ValidationResult.create(null, String.format("The number of format arguments passed " + "with an @FormatString must match the number of format arguments in the " + "@FormatMethod header where the format string was declared.\n\t" + "Format args passed: %d\n\tFormat args expected: %d", calleeFormatArgTypes.size(), ownerFormatArgTypes.size()));
    } else {
        for (int i = 0; i < calleeFormatArgTypes.size(); i++) {
            if (!ASTHelpers.isSameType(ownerFormatArgTypes.get(i), calleeFormatArgTypes.get(i), state)) {
                return ValidationResult.create(null, String.format("The format argument types passed " + "with an @FormatString must match the types of the format arguments in " + "the @FormatMethod header where the format string was declared.\n\t" + "Format arg types passed: %s\n\tFormat arg types expected: %s", calleeFormatArgTypes.toArray(), ownerFormatArgTypes.toArray()));
            }
        }
    }
    // Format string usage was valid.
    return null;
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ImmutableList(com.google.common.collect.ImmutableList) ExpressionTree(com.sun.source.tree.ExpressionTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 8 with ExpressionTree

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

the class SizeGreaterThanOrEqualsZero method isGreaterThanEqualToZero.

private ExpressionType isGreaterThanEqualToZero(BinaryTree tree) {
    ExpressionTree literalOperand;
    ExpressionType returnType;
    switch(tree.getKind()) {
        case GREATER_THAN_EQUAL:
            literalOperand = tree.getRightOperand();
            returnType = ExpressionType.GREATER_THAN_EQUAL;
            break;
        case LESS_THAN_EQUAL:
            literalOperand = tree.getLeftOperand();
            returnType = ExpressionType.LESS_THAN_EQUAL;
            break;
        default:
            return ExpressionType.MISMATCH;
    }
    if (literalOperand.getKind() != Kind.INT_LITERAL) {
        return ExpressionType.MISMATCH;
    }
    if (!((LiteralTree) literalOperand).getValue().equals(0)) {
        return ExpressionType.MISMATCH;
    }
    return returnType;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) LiteralTree(com.sun.source.tree.LiteralTree)

Example 9 with ExpressionTree

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

the class StringBuilderInitWithChar method matchNewClass.

@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
    if (ASTHelpers.isSameType(state.getSymtab().stringBuilderType, ASTHelpers.getType(tree.getIdentifier()), state) && tree.getArguments().size() == 1) {
        ExpressionTree argument = tree.getArguments().get(0);
        Type type = ((JCTree) argument).type;
        if (type.getKind() == TypeKind.CHAR) {
            if (argument.getKind() == Kind.CHAR_LITERAL) {
                char ch = (Character) ((LiteralTree) argument).getValue();
                return describeMatch(tree, SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\""));
            } else {
                return describeMatch(tree, SuggestedFix.replace(tree, "new StringBuilder().append(" + state.getSourceForNode((JCTree) argument) + ")"));
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : Type(com.sun.tools.javac.code.Type) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 10 with ExpressionTree

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

the class TryFailThrowable method getMessageSnippet.

private static String getMessageSnippet(StatementTree failStatement, VisitorState state, HasOtherParameters hasOtherParameters) {
    ExpressionTree expression = ((ExpressionStatementTree) failStatement).getExpression();
    MethodSymbol sym = (MethodSymbol) getSymbol(expression);
    String tail = hasOtherParameters == HasOtherParameters.TRUE ? ", " : "";
    // The above casts were checked earlier by failOrAssert.
    return hasInitialStringParameter(sym, state) ? state.getSourceForNode(((MethodInvocationTree) expression).getArguments().get(0)) + tail : "";
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree)

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