Bug 558635 - "Push down negation" cleanup creates wrong code
- fix PushDownNegationCleanup.PushDownNegationInInFixExpressionOperation
doRewriteAST method to handle InstanceofExpressions
- add tests to CleanUpTest
Change-Id: Ibf51a0a731d361b800ef791a223976045c28ff08
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
index 1b04555..6de93f0 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
@@ -4367,6 +4367,58 @@
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { sample });
}
+ public void testPushDownNegationReplaceInstanceofNegationWithInfixAndOperator() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ String sample= "" //
+ + "package test1;\n" //
+ + "\n" //
+ + "public class E1 {\n" //
+ + " public boolean replaceNegationWithInfixAndOperator(boolean b1, boolean b2) {\n" //
+ + " return !(b1 && b2 instanceof String); // another refactoring removes the parentheses\n" //
+ + " }\n" //
+ + "}\n";
+ ICompilationUnit cu1= pack1.createCompilationUnit("E1.java", sample, false, null);
+
+ enable(CleanUpConstants.PUSH_DOWN_NEGATION);
+
+ sample= "" //
+ + "package test1;\n" //
+ + "\n" //
+ + "public class E1 {\n" //
+ + " public boolean replaceNegationWithInfixAndOperator(boolean b1, boolean b2) {\n" //
+ + " return (!b1 || !(b2 instanceof String)); // another refactoring removes the parentheses\n" //
+ + " }\n" //
+ + "}\n";
+
+ assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { sample });
+ }
+
+ public void testPushDownNegationReplaceInstanceofNegationWithInfixOrOperator() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ String sample= "" //
+ + "package test1;\n" //
+ + "\n" //
+ + "public class E1 {\n" //
+ + " public boolean replaceNegationWithInfixOrOperator(boolean b1, boolean b2) {\n" //
+ + " return !(b1 instanceof String || b2); // another refactoring removes the parentheses\n" //
+ + " }\n" //
+ + "}\n";
+ ICompilationUnit cu1= pack1.createCompilationUnit("E1.java", sample, false, null);
+
+ enable(CleanUpConstants.PUSH_DOWN_NEGATION);
+
+ sample= "" //
+ + "package test1;\n" //
+ + "\n" //
+ + "public class E1 {\n" //
+ + " public boolean replaceNegationWithInfixOrOperator(boolean b1, boolean b2) {\n" //
+ + " return (!(b1 instanceof String) && !b2); // another refactoring removes the parentheses\n" //
+ + " }\n" //
+ + "}\n";
+
+ assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { sample });
+ }
+
public void testPushDownNegationReplaceNegationWithEqualOperator() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String sample= "" //
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/PushDownNegationCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/PushDownNegationCleanUp.java
index 5a1b856..4940f86 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/PushDownNegationCleanUp.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/PushDownNegationCleanUp.java
@@ -30,6 +30,7 @@
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
+import org.eclipse.jdt.core.dom.InstanceofExpression;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
@@ -277,7 +278,13 @@
} else {
PrefixExpression prefixExpression= ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
- prefixExpression.setOperand((Expression) rewrite.createCopyTarget(anOperand));
+ if (anOperand instanceof InstanceofExpression) {
+ ParenthesizedExpression parenExpression= ast.newParenthesizedExpression();
+ parenExpression.setExpression((Expression) rewrite.createCopyTarget(anOperand));
+ prefixExpression.setOperand(parenExpression);
+ } else {
+ prefixExpression.setOperand((Expression) rewrite.createCopyTarget(anOperand));
+ }
it.set(prefixExpression);
}