Search in sources :

Example 31 with ExpressionTree

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

the class ASTHelpers method matchBinaryTree.

/**
   * Given a BinaryTree to match against and a list of two matchers, applies the matchers to the
   * operands in both orders.  If both matchers match, returns a list with the operand that
   * matched each matcher in the corresponding position.
   *
   * @param tree a BinaryTree AST node
   * @param matchers a list of matchers
   * @param state the VisitorState
   * @return a list of matched operands, or null if at least one did not match
   */
public static List<ExpressionTree> matchBinaryTree(BinaryTree tree, List<Matcher<ExpressionTree>> matchers, VisitorState state) {
    ExpressionTree leftOperand = tree.getLeftOperand();
    ExpressionTree rightOperand = tree.getRightOperand();
    if (matchers.get(0).matches(leftOperand, state) && matchers.get(1).matches(rightOperand, state)) {
        return Arrays.asList(leftOperand, rightOperand);
    } else if (matchers.get(0).matches(rightOperand, state) && matchers.get(1).matches(leftOperand, state)) {
        return Arrays.asList(rightOperand, leftOperand);
    }
    return null;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 32 with ExpressionTree

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

the class ClassNewInstance method fixThrows.

// if there wasn't a try/catch to add new catch clauses to, update the enclosing
// method declaration's throws clause to declare the new checked exceptions
private void fixThrows(VisitorState state, SuggestedFix.Builder fix) {
    MethodTree methodTree = state.findEnclosing(MethodTree.class);
    if (methodTree == null || methodTree.getThrows().isEmpty()) {
        return;
    }
    ImmutableMap.Builder<Type, ExpressionTree> thrown = ImmutableMap.builder();
    for (ExpressionTree e : methodTree.getThrows()) {
        thrown.put(ASTHelpers.getType(e), e);
    }
    UnhandledResult<ExpressionTree> result = unhandled(thrown.build(), state);
    if (result.unhandled.isEmpty()) {
        return;
    }
    List<String> newThrows = new ArrayList<>();
    for (Type handle : result.unhandled) {
        newThrows.add(handle.tsym.getSimpleName().toString());
    }
    Collections.sort(newThrows);
    fix.postfixWith(Iterables.getLast(methodTree.getThrows()), ", " + Joiner.on(", ").join(newThrows));
    // the other exceptions are in java.lang
    fix.addImport("java.lang.reflect.InvocationTargetException");
}
Also used : Type(com.sun.tools.javac.code.Type) MethodTree(com.sun.source.tree.MethodTree) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 33 with ExpressionTree

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

the class SuggestedFixes method addValuesToAnnotationArgument.

/**
   * Returns a fix that appends {@code newValues} to the {@code parameterName} argument for {@code
   * annotation}, regardless of whether there is already an argument.
   *
   * <p>N.B.: {@code newValues} are source-code strings, not string literal values.
   */
public static Builder addValuesToAnnotationArgument(AnnotationTree annotation, String parameterName, Collection<String> newValues, VisitorState state) {
    if (annotation.getArguments().isEmpty()) {
        String parameterPrefix = parameterName.equals("value") ? "" : (parameterName + " = ");
        return SuggestedFix.builder().replace(annotation, annotation.toString().replaceFirst("\\(\\)", "(" + parameterPrefix + newArgument(newValues) + ")"));
    }
    Optional<ExpressionTree> maybeExistingArgument = findArgument(annotation, parameterName);
    if (!maybeExistingArgument.isPresent()) {
        return SuggestedFix.builder().prefixWith(annotation.getArguments().get(0), parameterName + " = " + newArgument(newValues) + ", ");
    }
    ExpressionTree existingArgument = maybeExistingArgument.get();
    if (!existingArgument.getKind().equals(NEW_ARRAY)) {
        return SuggestedFix.builder().replace(existingArgument, newArgument(state.getSourceForNode(existingArgument), newValues));
    }
    NewArrayTree newArray = (NewArrayTree) existingArgument;
    if (newArray.getInitializers().isEmpty()) {
        return SuggestedFix.builder().replace(newArray, newArgument(newValues));
    } else {
        return SuggestedFix.builder().postfixWith(getLast(newArray.getInitializers()), ", " + Joiner.on(", ").join(newValues));
    }
}
Also used : NewArrayTree(com.sun.source.tree.NewArrayTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 34 with ExpressionTree

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

the class AnnotationMatcherUtils method getArgument.

/**
   * Gets the value for an argument, or null if the argument does not exist.
   *
   * @param annotationTree the AST node for the annotation
   * @param name the name of the argument whose value to get
   * @return the value of the argument, or null if the argument does not exist
   */
public static ExpressionTree getArgument(AnnotationTree annotationTree, String name) {
    for (ExpressionTree argumentTree : annotationTree.getArguments()) {
        if (argumentTree.getKind() != Tree.Kind.ASSIGNMENT) {
            continue;
        }
        AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
        if (!assignmentTree.getVariable().toString().equals(name)) {
            continue;
        }
        ExpressionTree expressionTree = assignmentTree.getExpression();
        return expressionTree;
    }
    return null;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 35 with ExpressionTree

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

the class Matchers method selectedIsInstance.

/**
   * Returns true if the expression is a member access on an instance, rather than a static type.
   * Supports member method invocations and field accesses.
   */
public static Matcher<ExpressionTree> selectedIsInstance() {
    return new Matcher<ExpressionTree>() {

        @Override
        public boolean matches(ExpressionTree expr, VisitorState state) {
            if (!(expr instanceof JCFieldAccess)) {
                // TODO(cushon): throw IllegalArgumentException?
                return false;
            }
            JCExpression selected = ((JCFieldAccess) expr).getExpression();
            if (selected instanceof JCNewClass) {
                return true;
            }
            Symbol sym = ASTHelpers.getSymbol(selected);
            return sym instanceof VarSymbol;
        }
    };
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) InstanceMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.InstanceMethodMatcher) AnyMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.AnyMethodMatcher) StaticMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.StaticMethodMatcher) ConstructorMatcher(com.google.errorprone.matchers.method.MethodMatchers.ConstructorMatcher) VisitorState(com.google.errorprone.VisitorState) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

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