use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class SelfAssignment method matchVariable.
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
ExpressionTree initializer = stripCheckNotNull(tree.getInitializer(), state);
Tree parent = state.getPath().getParentPath().getLeaf();
// must be a static class variable with member select initializer
if (initializer == null || initializer.getKind() != MEMBER_SELECT || parent.getKind() != CLASS || !tree.getModifiers().getFlags().contains(STATIC)) {
return Description.NO_MATCH;
}
MemberSelectTree rhs = (MemberSelectTree) initializer;
Symbol rhsClass = ASTHelpers.getSymbol(rhs.getExpression());
Symbol lhsClass = ASTHelpers.getSymbol(parent);
if (rhsClass != null && lhsClass != null && rhsClass.equals(lhsClass) && rhs.getIdentifier().contentEquals(tree.getName())) {
return describeForVarDecl(tree, state);
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class SelfEquals method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (ASSERTION.matches(state.getPath().getParentPath().getLeaf(), state)) {
return NO_MATCH;
}
List<? extends ExpressionTree> args = tree.getArguments();
ExpressionTree toReplace;
if (INSTANCE_MATCHER.matches(tree, state)) {
toReplace = args.get(0);
} else if (STATIC_MATCHER.matches(tree, state)) {
if (args.get(0).getKind() == Kind.IDENTIFIER && args.get(1).getKind() != Kind.IDENTIFIER) {
toReplace = args.get(0);
} else {
toReplace = args.get(1);
}
} else {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
Fix fix = fieldFix(toReplace, state);
if (fix != null) {
description.addFix(fix);
}
return description.build();
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class JUnit3FloatingPointComparisonWithoutDelta method getArgumentTypesWithoutMessage.
/**
* Gets the argument types, excluding the message argument if present.
*/
private List<Type> getArgumentTypesWithoutMessage(MethodInvocationTree methodInvocationTree, VisitorState state) {
List<Type> argumentTypes = new ArrayList<>();
for (ExpressionTree argument : methodInvocationTree.getArguments()) {
JCTree tree = (JCTree) argument;
argumentTypes.add(tree.type);
}
removeMessageArgumentIfPresent(state, argumentTypes);
return argumentTypes;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ProtoFieldNullComparison method isGetListMethodInvocation.
private static boolean isGetListMethodInvocation(ExpressionTree tree, VisitorState state) {
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
MethodInvocationTree method = (MethodInvocationTree) tree;
if (!method.getArguments().isEmpty()) {
return false;
}
if (!returnsListMatcher.matches(method, state)) {
return false;
}
ExpressionTree expressionTree = method.getMethodSelect();
if (expressionTree instanceof JCFieldAccess) {
JCFieldAccess access = (JCFieldAccess) expressionTree;
String methodName = access.sym.getQualifiedName().toString();
return isFieldGetMethod(methodName);
}
return true;
}
return false;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ProtoFieldNullComparison method createReplacement.
/**
* Creates replacements for the following comparisons:
* <pre>
* proto.getField() == null --> !proto.hasField()
* proto.getField() != null --> proto.hasField()
* proto.getList() == null --> proto.getList().isEmpty()
* proto.getList() != null --> !proto.getList().isEmpty()
* <pre>
* Also creates replacements for the Yoda style version of them.
*/
@Nullable
private static String createReplacement(BinaryTree tree, VisitorState state) {
ExpressionTree leftOperand = tree.getLeftOperand();
ExpressionTree rightOperand = tree.getRightOperand();
ExpressionTree methodInvocation;
if (isNull(leftOperand)) {
methodInvocation = rightOperand;
} else {
methodInvocation = leftOperand;
}
if (isGetMethodInvocation(methodInvocation, state)) {
String methodName = getMethodName(methodInvocation);
String hasMethod = methodName.replaceFirst("get", "has");
// proto3 does not generate has methods for scalar types, e.g. ByteString and String.
// Do not provide a replacement in these cases.
Set<MethodSymbol> hasMethods = ASTHelpers.findMatchingMethods(state.getName(hasMethod), NO_ARGS, ASTHelpers.getType(ASTHelpers.getReceiver(methodInvocation)), state.getTypes());
if (hasMethods.isEmpty()) {
return null;
}
String replacement = replaceLast(methodInvocation.toString(), methodName, hasMethod);
replacement = tree.getKind() == Kind.EQUAL_TO ? "!" + replacement : replacement;
return replacement;
} else {
String replacement = methodInvocation + ".isEmpty()";
return tree.getKind() == Kind.EQUAL_TO ? replacement : "!" + replacement;
}
}
Aggregations