summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoopur Gupta2013-04-11 08:45:05 (EDT)
committerDani Megert2013-04-11 08:45:05 (EDT)
commitc47cfd066ed2625d7d5bcb86255bd873a5058b8b (patch)
treea807d4c1ab47c360b668dac48f36a7ffe29a8b09
parent4a050305939c6d797c481566c3915e524c770989 (diff)
downloadeclipse.jdt.ui-c47cfd066ed2625d7d5bcb86255bd873a5058b8b.zip
eclipse.jdt.ui-c47cfd066ed2625d7d5bcb86255bd873a5058b8b.tar.gz
eclipse.jdt.ui-c47cfd066ed2625d7d5bcb86255bd873a5058b8b.tar.bz2
Fixed bug 405096: [save actions][clean up] Use parentheses in expressions: Only if necessary, removes required parentheses
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java109
-rw-r--r--org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/NecessaryParenthesesChecker.java20
2 files changed, 128 insertions, 1 deletions
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 4534410..6c9e4d6 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
@@ -6915,6 +6915,115 @@ public class CleanUpTest extends CleanUpTestCase {
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
}
+ public void testRemoveParenthesesBug405096_1() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" final Short cache[] = new Short[-(-128) + 1];\n");
+ buf.append("}\n");
+ ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
+
+ assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+ }
+
+ public void testRemoveParenthesesBug405096_2() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" int a= 10\n");
+ buf.append(" final Short cache[] = new Short[-(-a) + 1];\n");
+ buf.append("}\n");
+ ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
+
+ assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+ }
+
+ public void testRemoveParenthesesBug405096_3() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" int a= 10\n");
+ buf.append(" final Short cache[] = new Short[-(--a) + 1];\n");
+ buf.append("}\n");
+ ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
+
+ assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+ }
+
+ public void testRemoveParenthesesBug405096_4() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" int a= 10\n");
+ buf.append(" final Short cache[] = new Short[+(+a) + 1];\n");
+ buf.append("}\n");
+ ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
+
+ assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+ }
+
+ public void testRemoveParenthesesBug405096_5() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" int a= 10\n");
+ buf.append(" final Short cache[] = new Short[+(++a) + +(-127)];\n");
+ buf.append("}\n");
+ ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
+
+ buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" int a= 10\n");
+ buf.append(" final Short cache[] = new Short[+(++a) + +-127];\n");
+ buf.append("}\n");
+ String expected1= buf.toString();
+
+ assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+ }
+
+ public void testRemoveParenthesesBug405096_6() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" final Short cache[] = new Short[+(+128) + ~(-127)];\n");
+ buf.append("}\n");
+ ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
+ enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
+
+ buf= new StringBuffer();
+ buf.append("package test;\n");
+ buf.append("public class E {\n");
+ buf.append(" final Short cache[] = new Short[+(+128) + ~-127];\n");
+ buf.append("}\n");
+ String expected1= buf.toString();
+
+ assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+ }
+
public void testRemoveQualifier01() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf= new StringBuffer();
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/NecessaryParenthesesChecker.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/NecessaryParenthesesChecker.java
index 4ab18c1..7a1b204 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/NecessaryParenthesesChecker.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/NecessaryParenthesesChecker.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011 IBM Corporation and others.
+ * Copyright (c) 2011, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -27,6 +27,7 @@ import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
+import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
@@ -294,9 +295,26 @@ public class NecessaryParenthesesChecker {
return true;
}
+ if (parentExpression instanceof PrefixExpression && expression instanceof PrefixExpression) { // see bug 405096
+ return needsParenthesesInPrefixExpression(((PrefixExpression) parentExpression).getOperator(),
+ ((PrefixExpression) expression).getOperator());
+ }
+
return false;
}
return true;
}
+
+ private static boolean needsParenthesesInPrefixExpression(PrefixExpression.Operator parentOperator, PrefixExpression.Operator expressionOperator) {
+ if (parentOperator == PrefixExpression.Operator.PLUS &&
+ (expressionOperator == PrefixExpression.Operator.PLUS || expressionOperator == PrefixExpression.Operator.INCREMENT)) {
+ return true;
+ }
+ if (parentOperator == PrefixExpression.Operator.MINUS &&
+ (expressionOperator == PrefixExpression.Operator.MINUS || expressionOperator == PrefixExpression.Operator.DECREMENT)) {
+ return true;
+ }
+ return false;
+ }
} \ No newline at end of file