Search in sources :

Example 1 with NewArrayTree

use of com.sun.source.tree.NewArrayTree in project bazel by bazelbuild.

the class TreeUtils method getAssignmentContext.

/**
     * Returns the tree with the assignment context for the treePath
     * leaf node.
     *
     * The assignment context for the treepath is the most enclosing
     * tree of type:
     * <ul>
     *   <li>AssignmentTree </li>
     *   <li>CompoundAssignmentTree </li>
     *   <li>MethodInvocationTree</li>
     *   <li>NewArrayTree</li>
     *   <li>NewClassTree</li>
     *   <li>ReturnTree</li>
     *   <li>VariableTree</li>
     * </ul>
     *
     * @param treePath
     * @return  the assignment context as described.
     */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath path = treePath.getParentPath();
    if (path == null)
        return null;
    Tree node = path.getLeaf();
    if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree))
        return node;
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree) NewClassTree(com.sun.source.tree.NewClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 2 with NewArrayTree

use of com.sun.source.tree.NewArrayTree in project st-js by st-js.

the class TreeUtils method getAssignmentContext.

/**
 * Returns the tree with the assignment context for the treePath leaf node.
 *
 * The assignment context for the treepath is the most enclosing tree of type:
 * <ul>
 * <li>AssignmentTree</li>
 * <li>CompoundAssignmentTree</li>
 * <li>MethodInvocationTree</li>
 * <li>NewArrayTree</li>
 * <li>NewClassTree</li>
 * <li>ReturnTree</li>
 * <li>VariableTree</li>
 * </ul>
 *
 * @param treePath
 *            a {@link com.sun.source.util.TreePath} object.
 * @return the assignment context as described.
 */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath path = treePath.getParentPath();
    if (path == null) {
        return null;
    }
    Tree node = path.getLeaf();
    if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree)) {
        return node;
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree) NewClassTree(com.sun.source.tree.NewClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 3 with NewArrayTree

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

the class AbstractSuppressWarningsMatcher method getSuggestedFix.

protected final Fix getSuggestedFix(AnnotationTree annotationTree) {
    List<String> values = new ArrayList<>();
    for (ExpressionTree argumentTree : annotationTree.getArguments()) {
        AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
        if (assignmentTree.getVariable().toString().equals("value")) {
            ExpressionTree expressionTree = assignmentTree.getExpression();
            switch(expressionTree.getKind()) {
                case STRING_LITERAL:
                    values.add(((String) ((JCTree.JCLiteral) expressionTree).value));
                    break;
                case NEW_ARRAY:
                    NewArrayTree newArrayTree = (NewArrayTree) expressionTree;
                    for (ExpressionTree elementTree : newArrayTree.getInitializers()) {
                        values.add((String) ((JCTree.JCLiteral) elementTree).value);
                    }
                    break;
                default:
                    throw new AssertionError("Unknown kind: " + expressionTree.getKind());
            }
            processSuppressWarningsValues(values);
        } else {
            throw new AssertionError("SuppressWarnings has an element other than value=");
        }
    }
    if (values.isEmpty()) {
        return SuggestedFix.delete(annotationTree);
    } else if (values.size() == 1) {
        return SuggestedFix.replace(annotationTree, "@SuppressWarnings(\"" + values.get(0) + "\")");
    } else {
        return SuggestedFix.replace(annotationTree, "@SuppressWarnings({\"" + Joiner.on("\", \"").join(values) + "\"})");
    }
}
Also used : NewArrayTree(com.sun.source.tree.NewArrayTree) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 4 with NewArrayTree

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

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

the class AnnotationHasArgumentWithValue method matches.

@Override
public boolean matches(AnnotationTree annotationTree, VisitorState state) {
    ExpressionTree expressionTree = AnnotationMatcherUtils.getArgument(annotationTree, element);
    if (expressionTree == null) {
        return false;
    }
    expressionTree = ASTHelpers.stripParentheses(expressionTree);
    if (expressionTree instanceof NewArrayTree) {
        NewArrayTree arrayTree = (NewArrayTree) expressionTree;
        for (ExpressionTree elementTree : arrayTree.getInitializers()) {
            if (valueMatcher.matches(elementTree, state)) {
                return true;
            }
        }
        return false;
    }
    return valueMatcher.matches(expressionTree, state);
}
Also used : NewArrayTree(com.sun.source.tree.NewArrayTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

NewArrayTree (com.sun.source.tree.NewArrayTree)21 ExpressionTree (com.sun.source.tree.ExpressionTree)19 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)12 Tree (com.sun.source.tree.Tree)11 AssignmentTree (com.sun.source.tree.AssignmentTree)10 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)10 MemberSelectTree (com.sun.source.tree.MemberSelectTree)10 BinaryTree (com.sun.source.tree.BinaryTree)9 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)8 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)8 IdentifierTree (com.sun.source.tree.IdentifierTree)8 LiteralTree (com.sun.source.tree.LiteralTree)8 MethodTree (com.sun.source.tree.MethodTree)8 NewClassTree (com.sun.source.tree.NewClassTree)8 TypeCastTree (com.sun.source.tree.TypeCastTree)8 VariableTree (com.sun.source.tree.VariableTree)8 ClassTree (com.sun.source.tree.ClassTree)7 ParameterizedTypeTree (com.sun.source.tree.ParameterizedTypeTree)6 ReturnTree (com.sun.source.tree.ReturnTree)6 ExecutableElement (javax.lang.model.element.ExecutableElement)6