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