Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarika Sinha2019-02-26 10:35:21 -0500
committerSarika Sinha2019-03-07 04:34:28 -0500
commitd9dd4c4df7ad218a8571d028ac3719cc0f3a7a95 (patch)
treed99de60c3d5f61ffd39dd22c10320f70486623af
parente610552c6fa74d3273565496cd9138987ee68fc6 (diff)
downloadeclipse.jdt.core-d9dd4c4df7ad218a8571d028ac3719cc0f3a7a95.tar.gz
eclipse.jdt.core-d9dd4c4df7ad218a8571d028ac3719cc0f3a7a95.tar.xz
eclipse.jdt.core-d9dd4c4df7ad218a8571d028ac3719cc0f3a7a95.zip
Bug 543720 - [12][ast rewrite] AST Rewrite Support for SwitchY20190307-1015
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java87
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java159
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java75
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java28
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java142
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java68
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java9
8 files changed, 497 insertions, 73 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java
index 1b39e0b558..59b92d6ea6 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java
@@ -127,7 +127,7 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases {
System.out.println(System.getProperties());
fail("Unexpected java vm "+javaVersion+" "+vmName);
}
- System.out.println("Recognized Java 9 version '"+javaVersion+"' with vm.name '"+vmName+"'");
+ System.out.println("Recognized Java version '"+javaVersion+"' with vm.name '"+vmName+"'");
}
}
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 fc9b8be5d8..1eb98c5065 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -7,6 +7,10 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
+ *
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
*
* Contributors:
* IBM Corporation - initial API and implementation
@@ -3607,12 +3611,19 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
assertTrue("Number of statements not 0", statements.size() == 0);
SwitchCase caseStatement1= ast.newSwitchCase();
- caseStatement1.setExpression(ast.newNumberLiteral("1"));
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement1.setExpression(ast.newNumberLiteral("1"));
+ }
+ else {
+ caseStatement1.expressions().add(ast.newNumberLiteral("1"));
+ }
Statement statement1= ast.newReturnStatement();
SwitchCase caseStatement2= ast.newSwitchCase(); // default
- caseStatement2.setExpression(null);
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement2.setExpression(null);
+ }
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
listRewrite.insertLast(caseStatement1, null);
@@ -3635,14 +3646,25 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
// change case statement
SwitchCase caseStatement= (SwitchCase) statements.get(3);
Expression newCaseExpression= ast.newNumberLiteral("10");
- rewrite.replace(caseStatement.getExpression(), newCaseExpression, null);
+ if (this.apiLevel < AST.JLS12) {
+ rewrite.replace(caseStatement.getExpression(), newCaseExpression, null);
+ } else {
+ List expressions = caseStatement.expressions();
+ ListRewrite listRewrite= rewrite.getListRewrite(caseStatement, SwitchCase.EXPRESSIONS2_PROPERTY);
+ listRewrite.replace((Expression)expressions.get(0), newCaseExpression, null);
+ }
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
{
// insert case statement
SwitchCase caseStatement2= ast.newSwitchCase();
- caseStatement2.setExpression(ast.newNumberLiteral("11"));
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement2.setExpression(ast.newNumberLiteral("11"));
+ }
+ else {
+ caseStatement2.expressions().add(ast.newNumberLiteral("11"));
+ }
listRewrite.insertFirst(caseStatement2, null);
// insert statement
@@ -3653,7 +3675,12 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
{
// insert case statement
SwitchCase caseStatement2= ast.newSwitchCase();
- caseStatement2.setExpression(ast.newNumberLiteral("12"));
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement2.setExpression(ast.newNumberLiteral("12"));
+ }
+ else {
+ caseStatement2.expressions().add(ast.newNumberLiteral("12"));
+ }
listRewrite.insertLast(caseStatement2, null);
// insert statement
@@ -3743,12 +3770,19 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
assertTrue("Number of statements not 0", statements.size() == 0);
SwitchCase caseStatement1= ast.newSwitchCase();
- caseStatement1.setExpression(ast.newNumberLiteral("1"));
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement1.setExpression(ast.newNumberLiteral("1"));
+ }
+ else {
+ caseStatement1.expressions().add(ast.newNumberLiteral("1"));
+ }
Statement statement1= ast.newReturnStatement();
SwitchCase caseStatement2= ast.newSwitchCase(); // default
- caseStatement2.setExpression(null);
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement2.setExpression(null);
+ }
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
listRewrite.insertLast(caseStatement1, null);
@@ -3771,14 +3805,26 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
// change case statement
SwitchCase caseStatement= (SwitchCase) statements.get(3);
Expression newCaseExpression= ast.newNumberLiteral("10");
- rewrite.replace(caseStatement.getExpression(), newCaseExpression, null);
+ if (this.apiLevel < AST.JLS12) {
+ rewrite.replace(caseStatement.getExpression(), newCaseExpression, null);
+ } else {
+ List expressions = caseStatement.expressions();
+ ListRewrite listRewrite2= rewrite.getListRewrite(caseStatement, SwitchCase.EXPRESSIONS2_PROPERTY);
+ listRewrite2.replace((Expression)expressions.get(0), newCaseExpression, null);
+ }
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
{
// insert case statement
SwitchCase caseStatement2= ast.newSwitchCase();
- caseStatement2.setExpression(ast.newNumberLiteral("11"));
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement2.setExpression(ast.newNumberLiteral("11"));
+ }
+ else {
+ caseStatement2.expressions().add(ast.newNumberLiteral("11"));
+
+ }
listRewrite.insertFirst(caseStatement2, null);
// insert statement
@@ -3789,7 +3835,12 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
{
// insert case statement
SwitchCase caseStatement2= ast.newSwitchCase();
- caseStatement2.setExpression(ast.newNumberLiteral("12"));
+ if (this.apiLevel < AST.JLS12) {
+ caseStatement2.setExpression(ast.newNumberLiteral("12"));
+ }
+ else {
+ caseStatement2.expressions().add(ast.newNumberLiteral("12"));
+ }
listRewrite.insertLast(caseStatement2, null);
// insert statement
@@ -4095,7 +4146,12 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
ExpressionStatement assignment = (ExpressionStatement)statements.get(1); // i= 1;:
SwitchCase switchCase = ast.newSwitchCase();
- switchCase.setExpression(ast.newNumberLiteral("2"));
+ if (this.apiLevel < AST.JLS12) {
+ switchCase.setExpression(ast.newNumberLiteral("2"));
+ }
+ else {
+ switchCase.expressions().add(ast.newNumberLiteral("2"));
+ }
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
listRewrite.replace(assignment, switchCase, null);
@@ -4238,7 +4294,12 @@ public class ASTRewritingStatementsTest extends ASTRewritingTest {
ExpressionStatement assignment = (ExpressionStatement)statements.get(1); // i= 1;
SwitchCase switchCase = ast.newSwitchCase();
- switchCase.setExpression(ast.newNumberLiteral("2"));
+ if (this.apiLevel < AST.JLS12) {
+ switchCase.setExpression(ast.newNumberLiteral("2"));
+ }
+ else {
+ switchCase.expressions().add(ast.newNumberLiteral("2"));
+ }
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
listRewrite.remove(assignment, 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
new file mode 100644
index 0000000000..ea5df06850
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
@@ -0,0 +1,159 @@
+/*******************************************************************************
+ * Copyright (c) 2019 IBM Corporation and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jdt.core.tests.rewrite.describing;
+
+import java.util.List;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+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.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.SwitchStatement;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+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 {
+
+
+ public ASTRewritingSwitchExpressionsTest(String name) {
+ super(name, 12);
+ }
+
+ public ASTRewritingSwitchExpressionsTest(String name, int apiLevel) {
+ super(name, apiLevel);
+ }
+
+ public static Test suite() {
+ return createSuite(ASTRewritingSwitchExpressionsTest.class);
+ }
+
+ @SuppressWarnings("rawtypes")
+ public void testSwitchExpressions_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(" }\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();
+ assertTrue("Number of statements not 2", blockStatements.size() == 2);
+ { // insert statements, replace expression
+ SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0);
+
+ ASTNode expression= switchStatement.getExpression();
+ SimpleName newExpression= ast.newSimpleName("x");
+ rewrite.replace(expression, newExpression, null);
+
+ List statements= switchStatement.statements();
+ assertTrue("Number of statements not 0", statements.size() == 0);
+
+ SwitchCase caseStatement1= ast.newSwitchCase();
+ caseStatement1.setSwitchLabeledRule(true);
+ caseStatement1.expressions().add(ast.newNumberLiteral("1"));
+ caseStatement1.expressions().add(ast.newNumberLiteral("2"));
+
+
+ Statement statement1= ast.newReturnStatement();
+
+ SwitchCase caseStatement2= ast.newSwitchCase(); // default
+ caseStatement2.setSwitchLabeledRule(true);
+
+
+ ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
+ listRewrite.insertLast(caseStatement1, null);
+ listRewrite.insertLast(statement1, null);
+ listRewrite.insertLast(caseStatement2, null);
+ }
+
+ { // insert, remove, replace statements, change case statements
+ SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(1);
+
+ List statements= switchStatement.statements();
+ assertTrue("Number of statements not 6", statements.size() == 6);
+
+ // remove statements
+
+ rewrite.remove((ASTNode) statements.get(0), null);
+ rewrite.remove((ASTNode) statements.get(1), null);
+
+ // change case statement
+ SwitchCase caseStatement= (SwitchCase) statements.get(2);
+ ListRewrite listRewrite= rewrite.getListRewrite(caseStatement, SwitchCase.EXPRESSIONS2_PROPERTY);
+
+ {
+ listRewrite.insertFirst(ast.newNumberLiteral("10"), null);
+ listRewrite.insertLast(ast.newNumberLiteral("12"), 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 (x) {\n");
+ buf.append(" case 1, 2->\n");
+ buf.append(" return;\n");
+ buf.append(" default->\n");
+ buf.append(" }\n");
+ buf.append(" switch (i) {\n");
+ buf.append(" case 10, 3, 12->\n");
+ buf.append(" i= 3;\n");
+ buf.append(" default->\n");
+ buf.append(" i= 4;\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+}
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java
index bf162e4e9c..c3ca55807c 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java
@@ -1,10 +1,14 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
+ *
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
*
* SPDX-License-Identifier: EPL-2.0
*
@@ -16,21 +20,33 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
-import org.eclipse.jdt.core.dom.*;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
+import org.eclipse.jdt.core.dom.BodyDeclaration;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.FieldDeclaration;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.eclipse.jdt.core.dom.Modifier;
+import org.eclipse.jdt.core.dom.PrimitiveType;
+import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
+import org.eclipse.jdt.core.dom.Type;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jdt.core.tests.model.AbstractJavaModelTests;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.TextEdit;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
/**
* Tests for ASTRewrite. Subclasses must have 2 constructors that forward to
* constructors with the same signature as this class's constructors.
@@ -70,8 +86,10 @@ public class ASTRewritingTest extends AbstractJavaModelTests {
/** @deprecated using deprecated code */
private final static int JLS10_INTERNAL = AST.JLS10;
+
+ private final static int JLS12_INTERNAL = AST.JLS12;
- private final static int[] JLS_LEVELS = { JLS2_INTERNAL, JLS3_INTERNAL, JLS4_INTERNAL, JLS8_INTERNAL, JLS9_INTERNAL, JLS10_INTERNAL };
+ private final static int[] JLS_LEVELS = { JLS2_INTERNAL, JLS3_INTERNAL, JLS4_INTERNAL, JLS8_INTERNAL, JLS9_INTERNAL, JLS10_INTERNAL, JLS12_INTERNAL };
private static final String ONLY_AST_STRING = "_only";
private static final String SINCE_AST_STRING = "_since";
@@ -108,26 +126,31 @@ public class ASTRewritingTest extends AbstractJavaModelTests {
public static Test suite() {
TestSuite suite= new TestSuite(ASTRewritingTest.class.getName());
- suite.addTest(ASTRewritingExpressionsTest.suite());
- suite.addTest(ASTRewritingInsertBoundTest.suite());
- suite.addTest(ASTRewritingMethodDeclTest.suite());
- suite.addTest(ASTRewritingMoveCodeTest.suite());
- suite.addTest(ASTRewritingStatementsTest.suite());
- suite.addTest(ASTRewritingTrackingTest.suite());
- suite.addTest(ASTRewritingJavadocTest.suite());
- suite.addTest(ASTRewritingTypeAnnotationsTest.suite());
- suite.addTest(ASTRewritingTypeDeclTest.suite());
- suite.addTest(ASTRewritingGroupNodeTest.suite());
- suite.addTest(ASTRewritingRevertTest.suite());
- suite.addTest(LineCommentOffsetsTest.suite());
- suite.addTest(ASTRewritingWithStatementsRecoveryTest.suite());
- suite.addTest(ASTRewritePropertyTest.suite());
- suite.addTest(ASTRewritingPackageDeclTest.suite());
- suite.addTest(ASTRewritingLambdaExpressionTest.suite());
- suite.addTest(ASTRewritingReferenceExpressionTest.suite());
- suite.addTest(SourceModifierTest.suite());
- suite.addTest(ImportRewriteTest.suite());
- suite.addTest(ImportRewrite18Test.suite());
+
+
+ suite.addTest(ASTRewritingExpressionsTest.suite());
+ suite.addTest(ASTRewritingInsertBoundTest.suite());
+ suite.addTest(ASTRewritingMethodDeclTest.suite());
+ suite.addTest(ASTRewritingMoveCodeTest.suite());
+ suite.addTest(ASTRewritingStatementsTest.suite());
+ suite.addTest(ASTRewritingSwitchExpressionsTest.suite());
+
+ suite.addTest(ASTRewritingTrackingTest.suite());
+ suite.addTest(ASTRewritingJavadocTest.suite());
+ suite.addTest(ASTRewritingTypeAnnotationsTest.suite());
+ suite.addTest(ASTRewritingTypeDeclTest.suite());
+ suite.addTest(ASTRewritingGroupNodeTest.suite());
+ suite.addTest(ASTRewritingRevertTest.suite());
+ suite.addTest(LineCommentOffsetsTest.suite());
+ suite.addTest(ASTRewritingWithStatementsRecoveryTest.suite());
+ suite.addTest(ASTRewritePropertyTest.suite());
+ suite.addTest(ASTRewritingPackageDeclTest.suite());
+ suite.addTest(ASTRewritingLambdaExpressionTest.suite());
+ suite.addTest(ASTRewritingReferenceExpressionTest.suite());
+ suite.addTest(SourceModifierTest.suite());
+ suite.addTest(ImportRewriteTest.suite());
+ suite.addTest(ImportRewrite18Test.suite());
+
return suite;
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
index ec82e2e4c0..001fc1fd7f 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
@@ -1500,30 +1500,16 @@ public class NaiveASTFlattener extends ASTVisitor {
@Override
public boolean visit(SwitchCase node) {
if (node.getAST().apiLevel() >= JLS12) {
- if (!node.expressions().isEmpty()) {
+ if (node.isDefault()) {
+ this.buffer.append("default");//$NON-NLS-1$
+ this.buffer.append(node.isSwitchLabeledRule() ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
+ } else {
+ this.buffer.append("case ");//$NON-NLS-1$
for (Iterator it = node.expressions().iterator(); it.hasNext(); ) {
Expression t = (Expression) it.next();
- if (node.isDefault()) {
- if (node.isSwitchLabeledRule()) {
- this.buffer.append("default ->\n");//$NON-NLS-1$
- }
- else {
- this.buffer.append("default :\n");//$NON-NLS-1$
- }
- } else {
- this.buffer.append("case ");//$NON-NLS-1$
t.accept(this);
- if (it.hasNext()) {
- this.buffer.append(", ");//$NON-NLS-1$
- } else {
- if (node.isSwitchLabeledRule()) {
- this.buffer.append("->\n");//$NON-NLS-1$
- }
- else {
- this.buffer.append(":\n");//$NON-NLS-1$
- }
- }
- }
+ this.buffer.append(it.hasNext() ? ", " : //$NON-NLS-1$
+ node.isSwitchLabeledRule() ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
}
}
}
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 db4f964e6a..ead61a2cc7 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
@@ -135,6 +135,8 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
/** @deprecated using deprecated code */
private static final int JLS9_INTERNAL = AST.JLS9;
+
+ private static final int JLS12_INTERNAL = AST.JLS12;
TextEdit currentEdit;
@@ -1039,6 +1041,60 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
}
return pos;
}
+
+ private int rewriteExpressionOptionalQualifier(SwitchCase parent, StructuralPropertyDescriptor property, int startPos) {
+ RewriteEvent event= getEvent(parent, property);
+ if (event != null) {
+ switch (event.getChangeKind()) {
+ case RewriteEvent.INSERTED: {
+ ASTNode node= (ASTNode) event.getNewValue();
+ TextEditGroup editGroup= getEditGroup(event);
+ doTextInsert(startPos, node, getIndent(startPos), true, editGroup);
+ doTextInsert(startPos, ".", editGroup); //$NON-NLS-1$
+ return startPos;
+ }
+ case RewriteEvent.REMOVED: {
+ try {
+ ASTNode node= (ASTNode) event.getOriginalValue();
+ TextEditGroup editGroup= getEditGroup(event);
+ int dotEnd= getScanner().getTokenEndOffset(TerminalTokens.TokenNameCOLON, node.getStartPosition() + node.getLength());
+ doTextRemoveAndVisit(startPos, dotEnd - startPos, node, editGroup);
+ return dotEnd;
+ } catch (CoreException e) {
+ handleException(e);
+ }
+ break;
+ }
+ case RewriteEvent.REPLACED: {
+ ASTNode node= (ASTNode) event.getOriginalValue();
+ TextEditGroup editGroup= getEditGroup(event);
+ SourceRange range= getExtendedRange(node);
+ int offset= range.getStartPosition();
+ int length= range.getLength();
+
+ doTextRemoveAndVisit(offset, length, node, editGroup);
+ doTextInsert(offset, (ASTNode) event.getNewValue(), getIndent(startPos), true, editGroup);
+ try {
+ return getScanner().getTokenEndOffset(TerminalTokens.TokenNameCOLON, offset + length);
+ } catch (CoreException e) {
+ handleException(e);
+ }
+ break;
+ }
+ }
+ }
+ Object node= getOriginalValue(parent, property);
+ if (node == null) {
+ return startPos;
+ }
+ int pos= doVisit((ASTNode) node);
+ try {
+ return getScanner().getTokenEndOffset(TerminalTokens.TokenNameCOLON, pos);
+ } catch (CoreException e) {
+ handleException(e);
+ }
+ return pos;
+ }
class ParagraphListRewriter extends ListRewriter {
@@ -1546,7 +1602,48 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
}
}
+ private int rewriteExpression2(ASTNode node, ChildListPropertyDescriptor property, int pos) {
+ RewriteEvent event= getEvent(node, property);
+ if (event == null || event.getChangeKind() == RewriteEvent.UNCHANGED) {
+ return doVisit(node, property, pos);
+ }
+ RewriteEvent[] children= event.getChildren();
+ boolean isAllInsert= isAllOfKind(children, RewriteEvent.INSERTED);
+ boolean isAllRemove= isAllOfKind(children, RewriteEvent.REMOVED);
+ String keyword= Util.EMPTY_STRING;
+ if (((SwitchCase)node).isSwitchLabeledRule()) {
+ keyword = "->"; //$NON-NLS-1$
+ } else {
+ keyword = ":"; //$NON-NLS-1$
+ }
+
+ Prefix formatterPrefix = this.formatter.CASE_SEPARATION;
+ int endPos= new ModifierRewriter(formatterPrefix).rewriteList(node, property, pos, keyword, " "); //$NON-NLS-1$
+
+ try {
+ int nextPos= getScanner().getNextStartOffset(endPos, false);
+ RewriteEvent lastChild = children[children.length - 1];
+ boolean lastUnchanged= lastChild.getChangeKind() != RewriteEvent.UNCHANGED;
+
+ if (isAllRemove) {
+ doTextRemove(endPos, nextPos - endPos, getEditGroup(lastChild));
+ return nextPos;
+ } else if (isAllInsert || (nextPos == endPos && lastUnchanged)){
+ String separator;
+ if (lastChild.getNewValue() instanceof Annotation) {
+ separator= formatterPrefix.getPrefix(getIndent(pos));
+ } else {
+ separator= String.valueOf(' '); //$NON-NLS-1$
+ }
+ doTextInsert(endPos, separator, getEditGroup(lastChild));
+ }
+ } catch (CoreException e) {
+ handleException(e);
+ }
+ return endPos;
+ }
+
private int rewriteModifiers2(ASTNode node, ChildListPropertyDescriptor property, int pos) {
RewriteEvent event= getEvent(node, property);
if (event == null || event.getChangeKind() == RewriteEvent.UNCHANGED) {
@@ -3416,7 +3513,19 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
}
// dont allow switching from case to default or back. New statements should be created.
- rewriteRequiredNode(node, INTERNAL_SWITCH_EXPRESSION_PROPERTY);
+ 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$
+ }
+ rewriteNodeList(node, SwitchCase.EXPRESSIONS2_PROPERTY, node.getStartPosition(), keyword, ", "); //$NON-NLS-1$
+ } else {
+ rewriteExpressionOptionalQualifier(node, SwitchCase.EXPRESSION_PROPERTY, node.getStartPosition());
+ }
+
return false;
}
@@ -3515,6 +3624,37 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
}
@Override
+ public boolean visit(SwitchExpression node) {
+ if (!hasChildrenChanges(node)) {
+ return doVisitUnchangedChildren(node);
+ }
+
+ int pos= rewriteRequiredNode(node, SwitchExpression.EXPRESSION_PROPERTY);
+
+ ChildListPropertyDescriptor property= SwitchExpression.STATEMENTS_PROPERTY;
+ if (getChangeKind(node, property) != RewriteEvent.UNCHANGED) {
+ try {
+ pos= getScanner().getTokenEndOffset(TerminalTokens.TokenNameLBRACE, pos);
+ int insertIndent= getIndent(node.getStartPosition());
+ if (DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH))) {
+ insertIndent++;
+ }
+
+ ParagraphListRewriter listRewriter= new SwitchListRewriter(insertIndent);
+ StringBuffer leadString= new StringBuffer();
+ leadString.append(getLineDelimiter());
+ leadString.append(createIndentString(insertIndent));
+ listRewriter.rewriteList(node, property, pos, leadString.toString());
+ } catch (CoreException e) {
+ handleException(e);
+ }
+ } else {
+ voidVisit(node, SwitchExpression.STATEMENTS_PROPERTY);
+ }
+ return false;
+ }
+
+ @Override
public boolean visit(SwitchStatement node) {
if (!hasChildrenChanges(node)) {
return doVisitUnchangedChildren(node);
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 117b09530b..8a380d2c4c 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -8,11 +8,16 @@
*
* SPDX-License-Identifier: EPL-2.0
*
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
+ *
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.core.dom.rewrite;
+import java.util.Iterator;
import java.util.List;
import org.eclipse.jdt.core.dom.*;
@@ -92,6 +97,13 @@ public class ASTRewriteFlattener extends ASTVisitor {
/** @deprecated using deprecated code */
private static final int JLS9_INTERNAL = AST.JLS9;
+
+ /**
+ * Internal synonym for {@link AST#JLS12}. Use to alleviate
+ * deprecation warnings.
+ * @since 3.17 BETA_JAVA_12
+ */
+ private static final int JLS12 = AST.JLS12;
public static String asString(ASTNode node, RewriteEventStore store) {
@@ -962,25 +974,61 @@ public class ASTRewriteFlattener extends ASTVisitor {
@Override
public boolean visit(SwitchCase node) {
- ASTNode expression= getChildNode(node, INTERNAL_SWITCH_EXPRESSION_PROPERTY);
- if (expression == null) {
- this.result.append("default"); //$NON-NLS-1$
+ 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$
+ } 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$
+ }
+ }
} else {
- this.result.append("case "); //$NON-NLS-1$
- expression.accept(this);
+ ASTNode expression= getChildNode(node, SwitchCase.EXPRESSION_PROPERTY);
+ if (expression == null) {
+ this.result.append("default"); //$NON-NLS-1$
+ } else {
+ this.result.append("case "); //$NON-NLS-1$
+ expression.accept(this);
+ }
+ this.result.append(':');
}
- this.result.append(':');
return false;
}
@Override
- public boolean visit(SwitchStatement node) {
+ public boolean visit(SwitchExpression node) {
+ visitSwitchNode(node);
+ return false;
+ }
+
+ private void visitSwitchNode(ASTNode node) {
this.result.append("switch ("); //$NON-NLS-1$
- getChildNode(node, SwitchStatement.EXPRESSION_PROPERTY).accept(this);
+ if (node instanceof SwitchExpression) {
+ getChildNode(node, SwitchExpression.EXPRESSION_PROPERTY).accept(this);
+ }
+ else if (node instanceof SwitchStatement) {
+ getChildNode(node, SwitchStatement.EXPRESSION_PROPERTY).accept(this);
+ }
this.result.append(')');
this.result.append('{');
- visitList(node, SwitchStatement.STATEMENTS_PROPERTY, null);
+ if (node instanceof SwitchExpression) {
+ visitList(node, SwitchExpression.STATEMENTS_PROPERTY, null);
+ }
+ else if (node instanceof SwitchStatement) {
+ visitList(node, SwitchStatement.STATEMENTS_PROPERTY, null);
+ }
+
this.result.append('}');
+ }
+
+ @Override
+ public boolean visit(SwitchStatement node) {
+ visitSwitchNode(node);
return false;
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java
index ad53806b93..6e936b1a0a 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java
@@ -1,10 +1,15 @@
/*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
+ *
+ *
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
*
* SPDX-License-Identifier: EPL-2.0
*
@@ -574,6 +579,8 @@ public final class ASTRewriteFormatter {
public final BlockContext IF_BLOCK_NO_ELSE= new BlockFormattingPrefix("if (true)", 8); //$NON-NLS-1$
public final BlockContext ELSE_AFTER_STATEMENT= new BlockFormattingPrefix("if (true) foo();else ", 15); //$NON-NLS-1$
public final BlockContext ELSE_AFTER_BLOCK= new BlockFormattingPrefix("if (true) {}else ", 11); //$NON-NLS-1$
+
+ public final Prefix CASE_SEPARATION= new FormattingPrefix("case A, B", "A" , CodeFormatter.K_EXPRESSION); //$NON-NLS-1$ //$NON-NLS-2$
public final BlockContext FOR_BLOCK= new BlockFormattingPrefix("for (;;) ", 7); //$NON-NLS-1$
public final BlockContext WHILE_BLOCK= new BlockFormattingPrefix("while (true)", 11); //$NON-NLS-1$

Back to the top