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;
}
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;
}
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) + "\"})");
}
}
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));
}
}
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);
}
Aggregations