Fix fix = null;
ExpressionTree leftOperand = tree.getLeftOperand();
ExpressionTree rightOperand = tree.getRightOperand();
Type leftType = ((JCTree) leftOperand).type;
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
/**
* Try to figure out what they were trying to do.
* Cases:
* 1) (foo == foo) ==> (foo == other.foo)
* 2) (foo == this.foo) ==> (other.foo == this.foo)
* 3) (this.foo == foo) ==> (this.foo == other.foo)
* 4) (this.foo == this.foo) ==> (this.foo == other.foo)
*/
// Choose argument to replace.
ExpressionTree toReplace;
if (rightOperand.getKind() == Kind.IDENTIFIER) {
toReplace = rightOperand;
} else if (leftOperand.getKind() == Kind.IDENTIFIER) {
toReplace = leftOperand;
} else {
// If we don't have a good reason to replace one or the other, replace the second.
toReplace = rightOperand;
}
// Find containing block
TreePath path = state.getPath();
while (path.getLeaf() != null && path.getLeaf().getKind() != Kind.CLASS
&& path.getLeaf().getKind() != Kind.BLOCK) {
path = path.getParentPath();
}
if (path.getLeaf() != null) {
List<? extends JCTree> members;
// Must be block or class
if (path.getLeaf().getKind() == Kind.CLASS) {
members = ((JCClassDecl) path.getLeaf()).getMembers();
} else {
members = ((JCBlock) path.getLeaf()).getStatements();
}
for (JCTree jcTree : members) {
if (jcTree.getKind() == Kind.VARIABLE) {
JCVariableDecl declaration = (JCVariableDecl) jcTree;
TypeSymbol variableTypeSymbol = declaration.getType().type.tsym;
if (ASTHelpers.getSymbol(toReplace).isMemberOf(variableTypeSymbol, state.getTypes())) {
if (toReplace.getKind() == Kind.IDENTIFIER) {
fix = SuggestedFix.prefixWith(toReplace, declaration.getName() + ".");
} else {
fix = SuggestedFix.replace(
((JCFieldAccess) toReplace).getExpression(), declaration.getName().toString());
}
}
}
}
}
if (fix == null) {
// No good replacement, let's try something else!
// For floats or doubles, y!=y -> isNaN(y)
if (tree.getKind() == Tree.Kind.EQUAL_TO) {
fixedExpression.append("!");
}
if (types.isSameType(leftType, symtab.floatType)) {
fixedExpression.append("Float.isNaN(" + leftOperand + ")");
fix = SuggestedFix.replace(tree, fixedExpression.toString());
} else if (types.isSameType(leftType, symtab.doubleType)) {
fixedExpression.append("Double.isNaN(" + leftOperand + ")");
fix = SuggestedFix.replace(tree, fixedExpression.toString());
} else {
// last resort, just replace with true or false