public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
// the statement that is the parent of the self-assignment expression
Tree parent = state.getPath().getParentPath().getLeaf();
ExpressionTree lhs = ASTHelpers.getReceiver(methodInvocationTree);
ExpressionTree rhs = methodInvocationTree.getArguments().get(0);
// default fix for methods
Fix fix = SuggestedFix.delete(parent);
if (methodSelect(instanceMethod(Matchers.<ExpressionTree>anything(), "removeAll"))
.matches(methodInvocationTree, state)) {
fix = SuggestedFix.replace(methodInvocationTree, lhs + ".clear()");
}
if (lhs.getKind() == MEMBER_SELECT) {
// find a method parameter of the same type and similar name and suggest it
// as the rhs
// rhs should be either identifier or field access
assert(rhs.getKind() == IDENTIFIER || rhs.getKind() == MEMBER_SELECT);
// get current name of rhs
String rhsName = null;
if (rhs.getKind() == IDENTIFIER) {
rhsName = ((JCIdent) rhs).name.toString();
} else if (rhs.getKind() == MEMBER_SELECT) {
rhsName = ((JCFieldAccess) rhs).name.toString();
}
// find method parameters of the type "Collection"
TreePath path = state.getPath();
while (path != null && path.getLeaf().getKind() != METHOD) {
path = path.getParentPath();
}
JCMethodDecl method = (JCMethodDecl) path.getLeaf();
int minEditDistance = Integer.MAX_VALUE;
String replacement = null;
for (JCVariableDecl var : method.params) {
if (variableType(isSubtypeOf("java.util.Collection")).matches(var, state)) {
int editDistance = EditDistance.getEditDistance(rhsName, var.name.toString());
if (editDistance < minEditDistance) {
// pick one with minimum edit distance
minEditDistance = editDistance;
replacement = var.name.toString();
}
}
}
if (replacement != null) {
// suggest replacing rhs with the parameter
fix = SuggestedFix.replace(rhs, replacement);
}
} else if (rhs.getKind() == IDENTIFIER) {
// find a field of the same type and similar name and suggest it as the lhs
// lhs should be identifier
assert(lhs.getKind() == IDENTIFIER);