Skip to content

Instantly share code, notes, and snippets.

@kjlubick
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjlubick/2735ee7e640b407459ee to your computer and use it in GitHub Desktop.
Save kjlubick/2735ee7e640b407459ee to your computer and use it in GitHub Desktop.
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