use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ArraysAsListPrimitiveArray method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!ARRAYS_AS_LIST_SINGLE_ARRAY.matches(tree, state)) {
return NO_MATCH;
}
ExpressionTree array = Iterables.getOnlyElement(tree.getArguments());
Type componentType = ((ArrayType) ASTHelpers.getType(array)).getComponentType();
if (!componentType.isPrimitive()) {
return NO_MATCH;
}
String guavaUtils = GUAVA_UTILS.get(componentType.getKind());
if (guavaUtils == null) {
return NO_MATCH;
}
Fix fix = SuggestedFix.builder().addImport("com.google.common.primitives." + guavaUtils).replace(tree.getMethodSelect(), guavaUtils + ".asList").build();
return describeMatch(tree, fix);
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class BadComparable method matchTypeCast.
@Override
public Description matchTypeCast(TypeCastTree tree, VisitorState state) {
// Check for a narrowing match first as its simplest match to test.
if (!matches(tree, state)) {
return Description.NO_MATCH;
}
// Test that the match is in a Comparable.compareTo or Comparator.compare method.
ClassTree declaringClass = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
if (!COMPARABLE_CLASS_MATCHER.matches(declaringClass, state) && !COMPARATOR_CLASS_MATCHER.matches(declaringClass, state)) {
return Description.NO_MATCH;
}
MethodTree method = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
if (method == null) {
return Description.NO_MATCH;
}
if (!COMPARABLE_METHOD_MATCHER.matches(method, state) && !COMPARATOR_METHOD_MATCHER.matches(method, state)) {
return Description.NO_MATCH;
}
// Get the unparenthesized expression.
BinaryTree subtract = (BinaryTree) TreeInfo.skipParens((JCExpression) tree.getExpression());
ExpressionTree lhs = subtract.getLeftOperand();
ExpressionTree rhs = subtract.getRightOperand();
Fix fix;
if (ASTHelpers.getType(lhs).isPrimitive()) {
fix = SuggestedFix.replace(tree, "Long.compare(" + lhs + ", " + rhs + ")");
} else {
fix = SuggestedFix.replace(tree, lhs + ".compareTo(" + rhs + ")");
}
return describeMatch(tree, fix);
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class MyCustomCheck method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!PRINT_METHOD.matches(tree, state)) {
return NO_MATCH;
}
Symbol base = tree.getMethodSelect().accept(new TreeScanner<Symbol, Void>() {
@Override
public Symbol visitIdentifier(IdentifierTree node, Void unused) {
return ASTHelpers.getSymbol(node);
}
@Override
public Symbol visitMemberSelect(MemberSelectTree node, Void unused) {
return super.visitMemberSelect(node, null);
}
}, null);
if (!Objects.equals(base, state.getSymtab().systemType.tsym)) {
return NO_MATCH;
}
ExpressionTree arg = Iterables.getOnlyElement(tree.getArguments());
if (!STRING_FORMAT.matches(arg, state)) {
return NO_MATCH;
}
List<? extends ExpressionTree> formatArgs = ((MethodInvocationTree) arg).getArguments();
return describeMatch(tree, SuggestedFix.builder().replace(((JCTree) tree).getStartPosition(), ((JCTree) formatArgs.get(0)).getStartPosition(), "System.err.printf(").replace(state.getEndPosition((JCTree) getLast(formatArgs)), state.getEndPosition((JCTree) tree), ")").build());
}
Aggregations