Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java146
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java312
2 files changed, 456 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java
index 1eb98c5065..a4aaa40919 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java
@@ -3562,7 +3562,6 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
-
}
@@ -4578,6 +4577,151 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
}
}
}
+ @SuppressWarnings("deprecation")
+ public void testSwitchStatement_Bug543720() 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"+
+ " 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());
+
+ AST ast= 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();
+ { // insert statements, replace expression
+ SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);
+
+ SwitchCase caseStatement1= ast.newSwitchCase();
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement1.setExpression(ast.newNumberLiteral("100"));
+ } else {
+ caseStatement1.expressions().add(ast.newNumberLiteral("100"));
+ }
+
+ BreakStatement breakStatement = ast.newBreakStatement();
+ Block block1 = ast.newBlock();
+ block1.statements().add(breakStatement);
+
+ SwitchCase defaultCase = (SwitchCase) switchStmt.statements().get(2);
+ ListRewrite listRewrite= rewrite.getListRewrite(switchStmt, SwitchStatement.STATEMENTS_PROPERTY);
+ listRewrite.insertBefore(caseStatement1, defaultCase, null);
+ listRewrite.insertBefore(block1, defaultCase, 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 100:\n");
+ buf.append(" {\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());
+ }
+
+ // complete removal of statements under switch statement.
+ public void testSwitchStatement_Bug543720_complete_removal() 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"+
+ " 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();
+ { // insert statements, replace expression
+ SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);
+
+ List statements= switchStmt.statements();
+ for (int i = 0, l = statements.size(); i < l; ++i)
+ rewrite.remove((ASTNode) statements.get(i), 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(" }\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());
+ }
public void testSynchronizedStatement() throws Exception {
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
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 ea5df06850..39a87979e7 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
@@ -24,13 +24,17 @@ import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
+import org.eclipse.jdt.core.dom.BreakStatement;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SwitchCase;
+import org.eclipse.jdt.core.dom.SwitchExpression;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.TypeDeclaration;
+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;
@@ -155,5 +159,311 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
-
+ @SuppressWarnings("rawtypes")
+ public void testSwitchExpressions_02_since_12() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" public void foo(int i) {\n");
+ buf.append(" switch (i) {\n");
+ buf.append(" case 1, 2->\n");
+ buf.append(" i= 1;\n");
+ buf.append(" case 3->\n");
+ buf.append(" i= 3;\n");
+ buf.append(" default->\n");
+ buf.append(" i= 4;\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+ MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
+ Block block= methodDecl.getBody();
+ List blockStatements= block.statements();
+ { // insert statements, replace expression
+ SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0);
+
+ SwitchCase caseStatement1= ast.newSwitchCase();
+ caseStatement1.setSwitchLabeledRule(true);
+ caseStatement1.expressions().add(ast.newNumberLiteral("1024"));
+
+ BreakStatement breakStatement = ast.newBreakStatement();
+ breakStatement.setExpression(ast.newNumberLiteral("2048"));
+ ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
+
+ SwitchCase defaultCase = (SwitchCase) switchStatement.statements().get(4);
+ listRewrite.insertBefore(caseStatement1, defaultCase, null);
+ listRewrite.insertBefore(breakStatement, defaultCase, null);
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" public void foo(int i) {\n");
+ buf.append(" switch (i) {\n");
+ buf.append(" case 1, 2->\n");
+ buf.append(" i= 1;\n");
+ buf.append(" case 3->\n");
+ buf.append(" i= 3;\n");
+ buf.append(" case 1024->\n");
+ buf.append(" break 2048;\n");
+ buf.append(" default->\n");
+ buf.append(" i= 4;\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ @SuppressWarnings("rawtypes")
+ public void testSwitchExpressions_03_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 z;\n"+
+ " }\n"+
+ " default -> {\n"+
+ " break 12;\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());
+
+ AST ast= 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();
+ { // insert statements, replace expression
+ VariableDeclarationStatement stmt = (VariableDeclarationStatement) blockStatements.get(0);
+ SwitchExpression switchExpression= (SwitchExpression) ((VariableDeclarationFragment) stmt.fragments().get(0)).getInitializer();
+
+ SwitchCase caseStatement1= ast.newSwitchCase();
+ caseStatement1.setSwitchLabeledRule(true);
+ caseStatement1.expressions().add(ast.newNumberLiteral("100"));
+ caseStatement1.expressions().add(ast.newNumberLiteral("200"));
+
+ SwitchCase caseStatement2= ast.newSwitchCase(); // default
+ caseStatement2.setSwitchLabeledRule(true);
+
+ BreakStatement breakStatement = ast.newBreakStatement();
+ breakStatement.setExpression(ast.newNumberLiteral("2048"));
+ Block block1 = ast.newBlock();
+ block1.statements().add(breakStatement);
+
+ SwitchCase defaultCase = (SwitchCase) switchExpression.statements().get(2);
+ ListRewrite listRewrite= rewrite.getListRewrite(switchExpression, SwitchExpression.STATEMENTS_PROPERTY);
+ listRewrite.insertBefore(caseStatement1, defaultCase, null);
+ listRewrite.insertBefore(block1, defaultCase, 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 z;\n");
+ buf.append(" }\n");
+ buf.append(" case 100, 200->\n");
+ buf.append(" {\n");
+ buf.append(" break 2048;\n");
+ buf.append(" }\n");
+ buf.append(" default -> {\n");
+ buf.append(" break 12;\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());
+ }
+
+ @SuppressWarnings("rawtypes")
+ public void testSwitchStatement_Bug543720_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"+
+ " 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());
+
+ AST ast= 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();
+ { // insert statements, replace expression
+ SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);
+
+ SwitchCase caseStatement1= ast.newSwitchCase();
+ caseStatement1.expressions().add(ast.newNumberLiteral("100"));
+
+ BreakStatement breakStatement = ast.newBreakStatement();
+ Block block1 = ast.newBlock();
+ block1.statements().add(breakStatement);
+
+ SwitchCase defaultCase = (SwitchCase) switchStmt.statements().get(2);
+ ListRewrite listRewrite= rewrite.getListRewrite(switchStmt, SwitchStatement.STATEMENTS_PROPERTY);
+ listRewrite.insertBefore(caseStatement1, defaultCase, null);
+ listRewrite.insertBefore(block1, defaultCase, 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 100:\n");
+ buf.append(" {\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());
+ }
+ @SuppressWarnings("rawtypes")
+ public void testSwitchExpressions_04_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"+
+ " {\n"+
+ " int z = 100;\n"+
+ " break z;\n"+
+ " }\n"+
+ " default -> {\n"+
+ " break 12;\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());
+
+ AST ast= 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();
+ { // insert statements, replace expression
+ VariableDeclarationStatement stmt = (VariableDeclarationStatement) blockStatements.get(0);
+ SwitchExpression switchExpression= (SwitchExpression) ((VariableDeclarationFragment) stmt.fragments().get(0)).getInitializer();
+
+ ASTNode expression= switchExpression.getExpression();
+ SimpleName newExpression= ast.newSimpleName("x");
+ rewrite.replace(expression, newExpression, null);
+
+ List statements= switchExpression.statements();
+
+ // remove statements
+ rewrite.remove((ASTNode) statements.get(0), null);
+ rewrite.remove((ASTNode) statements.get(1), 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 (x) {\n");
+ buf.append(" default -> {\n");
+ buf.append(" break 12;\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());
+ }
+ //Note: complete removal of statements under switch statements now added in ASTRSTest and hence not repeated here.
}

Back to the top