diff options
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 4534410de6..6c9e4d6039 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 4ab18c14f3..7a1b204fdc 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 |