Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManoj Palat2019-03-15 11:16:51 -0400
committerManoj Palat2019-03-15 12:35:57 -0400
commitb330307e74ac6c58b263854b69c229782b954bf6 (patch)
tree9ff65bf3f478db6e14494511f61f4b076450844c
parentd21549b5b3197f76572d4374c2560bac0290abc8 (diff)
downloadeclipse.jdt.core-b330307e74ac6c58b263854b69c229782b954bf6.tar.gz
eclipse.jdt.core-b330307e74ac6c58b263854b69c229782b954bf6.tar.xz
eclipse.jdt.core-b330307e74ac6c58b263854b69c229782b954bf6.zip
Boolean node rewrite in Bug 543720 - [12][ast rewrite] AST Rewrite
Support for Switch Expressions Change-Id: Ide2a0f9397d25b113e86e4732eaf7de790befd16
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java314
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java34
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java6
3 files changed, 340 insertions, 14 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
index 39a87979e7..d35c6cbb1c 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
@@ -37,7 +37,6 @@ import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
-
import junit.framework.Test;
public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
@@ -466,4 +465,317 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
assertEqualString(preview, buf.toString());
}
//Note: complete removal of statements under switch statements now added in ASTRSTest and hence not repeated here.
+
+ // replacing colon by ->
+ @SuppressWarnings("rawtypes")
+ public void testSwitchStatement_Bug543720_05_since_12() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ String s =
+ "package test1;\n"+
+ "public class X {\n"+
+ " static int foo(int i) {\n"+
+ " int tw = 0;\n"+
+ " switch (i) {\n"+
+ " case 1 : {\n"+
+ " int z = 100;\n"+
+ " break;\n"+
+ " }\n"+
+ " case 2 : {\n"+
+ " break;\n"+
+ " }\n"+
+ " default : {\n"+
+ " break;\n"+
+ " }\n"+
+ " }\n"+
+ " return tw;\n"+
+ " }\n"+
+ " public static void main(String[] args) {\n"+
+ " System.out.print(foo(1));\n"+
+ " }\n"+
+ "}\n";
+ StringBuffer buf = new StringBuffer(s);
+ ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "X");
+ MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
+ Block block= methodDecl.getBody();
+ List blockStatements= block.statements();
+ { // set switch labeled rule
+ SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);
+ for (int i = 0, l = switchStmt.statements().size(); i < l; ++i) {
+ Statement stmt = (Statement) switchStmt.statements().get(i);
+ if (stmt instanceof SwitchCase) {
+ SwitchCase switchCase = (SwitchCase) stmt;
+ assertTrue("Switch case has arrow", switchCase.isSwitchLabeledRule() == false);
+ rewrite.set(switchCase, SwitchCase.SWITCH_LABELED_RULE_PROPERTY, Boolean.TRUE, null);
+ }
+ }
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class X {\n");
+ buf.append(" static int foo(int i) {\n");
+ buf.append(" int tw = 0;\n");
+ buf.append(" switch (i) {\n");
+ buf.append(" case 1 -> {\n");
+ buf.append(" int z = 100;\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" case 2 -> {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" default -> {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append(" return tw;\n");
+ buf.append(" }\n");
+ buf.append(" public static void main(String[] args) {\n");
+ buf.append(" System.out.print(foo(1));\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ // replacing colon by ->
+ @SuppressWarnings("rawtypes")
+ public void testSwitchStatement_Bug543720_06_since_12() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ String s =
+ "package test1;\n"+
+ "public class X {\n"+
+ " static int foo(int i) {\n"+
+ " int tw = 0;\n"+
+ " switch (i) {\n"+
+ " case 1 -> {\n"+
+ " int z = 100;\n"+
+ " break;\n"+
+ " }\n"+
+ " case 2 -> {\n"+
+ " break;\n"+
+ " }\n"+
+ " default -> {\n"+
+ " break;\n"+
+ " }\n"+
+ " }\n"+
+ " return tw;\n"+
+ " }\n"+
+ " public static void main(String[] args) {\n"+
+ " System.out.print(foo(1));\n"+
+ " }\n"+
+ "}\n";
+ StringBuffer buf = new StringBuffer(s);
+ ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "X");
+ MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
+ Block block= methodDecl.getBody();
+ List blockStatements= block.statements();
+ { // set switch labeled rule
+ SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);
+ for (int i = 0, l = switchStmt.statements().size(); i < l; ++i) {
+ Statement stmt = (Statement) switchStmt.statements().get(i);
+ if (stmt instanceof SwitchCase) {
+ SwitchCase switchCase = (SwitchCase) stmt;
+ assertTrue("Switch case has colon", switchCase.isSwitchLabeledRule() == true);
+ rewrite.set(switchCase, SwitchCase.SWITCH_LABELED_RULE_PROPERTY, Boolean.FALSE, null);
+ }
+ }
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class X {\n");
+ buf.append(" static int foo(int i) {\n");
+ buf.append(" int tw = 0;\n");
+ buf.append(" switch (i) {\n");
+ buf.append(" case 1 : {\n");
+ buf.append(" int z = 100;\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" case 2 : {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" default : {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append(" return tw;\n");
+ buf.append(" }\n");
+ buf.append(" public static void main(String[] args) {\n");
+ buf.append(" System.out.print(foo(1));\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+ // replacing colon by ->
+ @SuppressWarnings("rawtypes")
+ public void testSwitchExpression_Bug543720_07_since_12() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ String s =
+ "package test1;\n"+
+ "public class X {\n"+
+ " static int foo(int i) {\n"+
+ " int tw =\n"+
+ " switch (i) {\n"+
+ " case 1 : {\n"+
+ " int z = 100;\n"+
+ " break;\n"+
+ " }\n"+
+ " case 2 : {\n"+
+ " break;\n"+
+ " }\n"+
+ " default : {\n"+
+ " break;\n"+
+ " }\n"+
+ " };\n"+
+ " return tw;\n"+
+ " }\n"+
+ " public static void main(String[] args) {\n"+
+ " System.out.print(foo(1));\n"+
+ " }\n"+
+ "}\n";
+ StringBuffer buf = new StringBuffer(s);
+ ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "X");
+ MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
+ Block block= methodDecl.getBody();
+ List blockStatements= block.statements();
+ {
+ VariableDeclarationStatement stmt = (VariableDeclarationStatement) blockStatements.get(0);
+ SwitchExpression switchExpression= (SwitchExpression) ((VariableDeclarationFragment) stmt.fragments().get(0)).getInitializer();
+ for (int i = 0, l = switchExpression.statements().size(); i < l; ++i) {
+ Statement stmt1 = (Statement) switchExpression.statements().get(i);
+ if (stmt1 instanceof SwitchCase) {
+ SwitchCase switchCase = (SwitchCase) stmt1;
+ assertTrue("Switch case has arrow", switchCase.isSwitchLabeledRule() == false);
+ rewrite.set(switchCase, SwitchCase.SWITCH_LABELED_RULE_PROPERTY, Boolean.TRUE, null);
+ }
+ }
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class X {\n");
+ buf.append(" static int foo(int i) {\n");
+ buf.append(" int tw =\n");
+ buf.append(" switch (i) {\n");
+ buf.append(" case 1 -> {\n");
+ buf.append(" int z = 100;\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" case 2 -> {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" default -> {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" };\n");
+ buf.append(" return tw;\n");
+ buf.append(" }\n");
+ buf.append(" public static void main(String[] args) {\n");
+ buf.append(" System.out.print(foo(1));\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ // replacing colon by ->
+ @SuppressWarnings("rawtypes")
+ public void testSwitchExpression_Bug543720_08_since_12() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ String s =
+ "package test1;\n"+
+ "public class X {\n"+
+ " static int foo(int i) {\n"+
+ " int tw =\n"+
+ " switch (i) {\n"+
+ " case 1 -> {\n"+
+ " int z = 100;\n"+
+ " break;\n"+
+ " }\n"+
+ " case 2 -> {\n"+
+ " break;\n"+
+ " }\n"+
+ " default -> {\n"+
+ " break;\n"+
+ " }\n"+
+ " };\n"+
+ " return tw;\n"+
+ " }\n"+
+ " public static void main(String[] args) {\n"+
+ " System.out.print(foo(1));\n"+
+ " }\n"+
+ "}\n";
+ StringBuffer buf = new StringBuffer(s);
+ ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "X");
+ MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
+ Block block= methodDecl.getBody();
+ List blockStatements= block.statements();
+ {
+ VariableDeclarationStatement stmt = (VariableDeclarationStatement) blockStatements.get(0);
+ SwitchExpression switchExpression= (SwitchExpression) ((VariableDeclarationFragment) stmt.fragments().get(0)).getInitializer();
+ for (int i = 0, l = switchExpression.statements().size(); i < l; ++i) {
+ Statement stmt1 = (Statement) switchExpression.statements().get(i);
+ if (stmt1 instanceof SwitchCase) {
+ SwitchCase switchCase = (SwitchCase) stmt1;
+ assertTrue("Switch case has colon", switchCase.isSwitchLabeledRule() == true);
+ rewrite.set(switchCase, SwitchCase.SWITCH_LABELED_RULE_PROPERTY, Boolean.FALSE, null);
+ }
+ }
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class X {\n");
+ buf.append(" static int foo(int i) {\n");
+ buf.append(" int tw =\n");
+ buf.append(" switch (i) {\n");
+ buf.append(" case 1 : {\n");
+ buf.append(" int z = 100;\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" case 2 : {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" default : {\n");
+ buf.append(" break;\n");
+ buf.append(" }\n");
+ buf.append(" };\n");
+ buf.append(" return tw;\n");
+ buf.append(" }\n");
+ buf.append(" public static void main(String[] args) {\n");
+ buf.append(" System.out.print(foo(1));\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
index cd4ec90f7f..14be191f08 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
@@ -3521,23 +3521,37 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
// dont allow switching from case to default or back. New statements should be created.
if (node.getAST().apiLevel() >= JLS12_INTERNAL) {
- // rewriteExpression2(node, SwitchCase.EXPRESSIONS2_PROPERTY, node.getStartPosition());
- String keyword = Util.EMPTY_STRING;
- if (node.isSwitchLabeledRule()) {
- keyword = "->"; //$NON-NLS-1$
- } else {
- keyword = ":"; //$NON-NLS-1$
+ int pos = node.expressions().size() == 0 ? node.getStartPosition() :
+ rewriteNodeList(node, SwitchCase.EXPRESSIONS2_PROPERTY, node.getStartPosition(), Util.EMPTY_STRING, ", "); //$NON-NLS-1$
+ if (isChanged(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY)) {
+ TextEditGroup editGroup = getEditGroup(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY);
+ try {
+ int tokenEnd, oldToken;
+ String newVal;
+ if (getNewValue(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY).equals(Boolean.TRUE)) {
+ oldToken = TerminalTokens.TokenNameCOLON;
+ newVal = "->"; //$NON-NLS-1$
+ } else {
+ oldToken = TerminalTokens.TokenNameARROW;
+ newVal = ":"; //$NON-NLS-1$
+ }
+ pos = getScanner().getTokenStartOffset(oldToken, pos);
+ tokenEnd = getScanner().getTokenEndOffset(oldToken, pos);
+ doTextRemove(pos, tokenEnd - pos, editGroup);
+ doTextInsert(pos, newVal, editGroup);
+ pos = tokenEnd;
+ } catch (CoreException e) {
+ handleException(e);
+ }
}
- rewriteNodeList(node, SwitchCase.EXPRESSIONS2_PROPERTY, node.getStartPosition(), keyword, ", "); //$NON-NLS-1$
} else {
rewriteExpressionOptionalQualifier(node, INTERNAL_SWITCH_EXPRESSION_PROPERTY, node.getStartPosition());
- }
-
+ }
return false;
}
class SwitchListRewriter extends ParagraphListRewriter {
-
+
private boolean indentSwitchStatementsCompareToCases;
public SwitchListRewriter(int initialIndent) {
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
index f2a1c89b95..98f0f4807e 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
@@ -987,15 +987,15 @@ public class ASTRewriteFlattener extends ASTVisitor {
if (node.getAST().apiLevel() >= JLS12) {
if (node.isDefault()) {
this.result.append("default");//$NON-NLS-1$
- this.result.append(node.isSwitchLabeledRule() ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
+ this.result.append(getBooleanAttribute(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY) ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
} else {
this.result.append("case ");//$NON-NLS-1$
for (Iterator it = node.expressions().iterator(); it.hasNext(); ) {
Expression t = (Expression) it.next();
t.accept(this);
- this.result.append(it.hasNext() ? ", " : //$NON-NLS-1$
- node.isSwitchLabeledRule() ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
+ this.result.append(it.hasNext() ? ", " : ""); //$NON-NLS-1$ //$NON-NLS-2$
}
+ this.result.append(getBooleanAttribute(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY) ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
}
} else {
ASTNode expression= getChildNode(node, INTERNAL_SWITCH_EXPRESSION_PROPERTY);

Back to the top