Create a gist now

Instantly share code, notes, and snippets.

Alternate version of Literal String Comparison quickfix, with types. See http://kjlubick.github.io/#/blog/post/3?building-your-first-eclipse-quick-fix
//make sure resolveBindings() returns true in the Resolution class
@Override
@SuppressWarnings("unchecked")
public boolean visit(MethodInvocation node) {
if (this.lscMethodInvocation != null) {
return false;
}
// for checking the type of the receiver. Although it is tempting to try
// node.resolveTypeBinding(), that refers to the return value.
ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
if (comparisonMethods.contains(node.getName().getIdentifier()) && //check the method name
"java.lang.String".equals(typeBinding.getQualifiedName())) {
List<Expression> arguments = (List<Expression>) node.arguments();
if (arguments.size() == 1) { // I doubt this could be anything other than 1
// if this was a constant string, resolveConstantExpressionValue() will be nonnull
Expression argument = arguments.get(0);
if (argument.resolveConstantExpressionValue() != null) {
this.lscMethodInvocation = node;
this.stringLiteralExpression = argument;
this.stringVariableExpression = node.getExpression();
return false;
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment