Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarika Sinha2019-01-07 09:03:15 +0000
committerSarika Sinha2019-02-05 12:07:10 +0000
commit8a1071fd0c22ca461a7868b29e9af8a866b80cd2 (patch)
tree2b7436d39e5e745f3b261db299d5c4d002b97778
parentbea7ce71afb7673b638922f4f0689b6bcdc614cf (diff)
parent3d00de0bddc2b0d1c6a82db101dc7fdf733db143 (diff)
downloadeclipse.jdt.core-8a1071fd0c22ca461a7868b29e9af8a866b80cd2.tar.gz
eclipse.jdt.core-8a1071fd0c22ca461a7868b29e9af8a866b80cd2.tar.xz
eclipse.jdt.core-8a1071fd0c22ca461a7868b29e9af8a866b80cd2.zip
Bug 542558 - [12][dom ast] DOM AST Support for Switch Expressions
-rw-r--r--org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.jarbin0 -> 22094 bytes
-rw-r--r--org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.zipbin0 -> 12320 bytes
-rw-r--r--org.eclipse.jdt.core.tests.model/JCL/converterJclMin12src.zipbin0 -> 12320 bytes
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava12Tests.java91
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunOnlyJava12Tests.java80
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java112
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/AbstractASTTests.java10
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java17
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java1
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java8
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Converter12/.classpath6
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Converter12/.project17
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Converter12/src/X.java4
-rw-r--r--org.eclipse.jdt.core/.settings/.api_filters16
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java58
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java45
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java38
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTNode.java32
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java7
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTVisitor.java40
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java86
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultASTVisitor.java14
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java7
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Expression.java7
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java117
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchExpression.java240
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java92
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java1
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java2
29 files changed, 1095 insertions, 53 deletions
diff --git a/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.jar b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.jar
new file mode 100644
index 0000000000..eed2c4df78
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.jar
Binary files differ
diff --git a/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.zip b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.zip
new file mode 100644
index 0000000000..35e5589d58
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12.zip
Binary files differ
diff --git a/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12src.zip b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12src.zip
new file mode 100644
index 0000000000..35e5589d58
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin12src.zip
Binary files differ
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava12Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava12Tests.java
new file mode 100644
index 0000000000..55ada0ae80
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava12Tests.java
@@ -0,0 +1,91 @@
+/*******************************************************************************
+ * 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;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.eclipse.jdt.core.tests.dom.ASTConverter12Test;
+import org.eclipse.jdt.core.tests.dom.ConverterTestSetup;
+import org.eclipse.jdt.core.tests.util.AbstractCompilerTest;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+@SuppressWarnings({"rawtypes", "unchecked"})
+public class RunAllJava12Tests extends TestCase {
+
+ public RunAllJava12Tests(String name) {
+ super(name);
+ }
+ public static Class[] getAllTestClasses() {
+ return new Class[] {
+ // to be filled after other tests are added
+ };
+ }
+
+ public static Class[] getConverterTestClasses() {
+ return new Class[] {
+ ASTConverter12Test.class,
+ };
+ }
+
+ public static Class[] getCompilerClasses() {
+ return new Class[] {
+ org.eclipse.jdt.core.tests.eval.TestAll.class,
+ org.eclipse.jdt.core.tests.compiler.regression.TestAll.class,
+ };
+ }
+
+ public static Test suite() {
+ TestSuite ts = new TestSuite(RunAllJava12Tests.class.getName());
+
+ Class[] testClasses = getAllTestClasses();
+ addTestsToSuite(ts, testClasses);
+ testClasses = getConverterTestClasses();
+ addTestsToSuite(ts, testClasses);
+
+ AbstractCompilerTest.setpossibleComplianceLevels(AbstractCompilerTest.F_11);
+ addTestsToSuite(ts, getCompilerClasses());
+ // ComplianceDiagnoseTest is already added to the test suite through getTestSuite
+ ts.addTest(org.eclipse.jdt.core.tests.compiler.parser.TestAll.getTestSuite(false));
+ return ts;
+ }
+ public static void addTestsToSuite(TestSuite suite, Class[] testClasses) {
+
+ for (int i = 0; i < testClasses.length; i++) {
+ Class testClass = testClasses[i];
+ // call the suite() method and add the resulting suite to the suite
+ try {
+ Method suiteMethod = testClass.getDeclaredMethod("suite", new Class[0]); //$NON-NLS-1$
+ Test test = (Test)suiteMethod.invoke(null, new Object[0]);
+ suite.addTest(test);
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ e.getTargetException().printStackTrace();
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ protected void tearDown() throws Exception {
+ ConverterTestSetup.PROJECT_SETUP = false;
+ super.tearDown();
+ }
+} \ No newline at end of file
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunOnlyJava12Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunOnlyJava12Tests.java
new file mode 100644
index 0000000000..46a8dff16a
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunOnlyJava12Tests.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * 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;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.eclipse.jdt.core.tests.dom.ASTConverter12Test;
+import org.eclipse.jdt.core.tests.dom.ConverterTestSetup;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+@SuppressWarnings({"rawtypes", "unchecked"})
+public class RunOnlyJava12Tests extends TestCase {
+
+ public RunOnlyJava12Tests(String name) {
+ super(name);
+ }
+ public static Class[] getAllTestClasses() {
+ return new Class[] {
+ // to be filled after other tests are added
+ };
+ }
+
+ public static Class[] getConverterTestClasses() {
+ return new Class[] {
+ ASTConverter12Test.class
+ };
+ }
+ public static Test suite() {
+ TestSuite ts = new TestSuite(RunOnlyJava12Tests.class.getName());
+
+ Class[] testClasses = getAllTestClasses();
+ addTestsToSuite(ts, testClasses);
+ testClasses = getConverterTestClasses();
+ ConverterTestSetup.TEST_SUITES = new ArrayList(Arrays.asList(testClasses));
+ addTestsToSuite(ts, testClasses);
+ return ts;
+ }
+ public static void addTestsToSuite(TestSuite suite, Class[] testClasses) {
+
+ for (int i = 0; i < testClasses.length; i++) {
+ Class testClass = testClasses[i];
+ // call the suite() method and add the resulting suite to the suite
+ try {
+ Method suiteMethod = testClass.getDeclaredMethod("suite", new Class[0]); //$NON-NLS-1$
+ Test test = (Test)suiteMethod.invoke(null, new Object[0]);
+ suite.addTest(test);
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ e.getTargetException().printStackTrace();
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ protected void tearDown() throws Exception {
+ ConverterTestSetup.PROJECT_SETUP = false;
+ super.tearDown();
+ }
+}
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
new file mode 100644
index 0000000000..e2bdf8a5b6
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * 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.dom;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.IBinding;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.eclipse.jdt.core.dom.Type;
+import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
+
+import junit.framework.Test;
+
+public class ASTConverter12Test extends ConverterTestSetup {
+
+ ICompilationUnit workingCopy;
+
+ public void setUpSuite() throws Exception {
+ super.setUpSuite();
+ this.ast = AST.newAST(getAST12());
+ }
+
+ public ASTConverter12Test(String name) {
+ super(name);
+ }
+
+ static {
+// TESTS_NUMBERS = new int[] { 19 };
+// TESTS_RANGE = new int[] { 1, -1 };
+// TESTS_NAMES = new String[] {"test0001"};
+ }
+ public static Test suite() {
+ return buildModelTestSuite(ASTConverter12Test.class);
+ }
+
+ static int getAST12() {
+ return AST.JLS12;
+ }
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ if (this.workingCopy != null) {
+ this.workingCopy.discardWorkingCopy();
+ this.workingCopy = null;
+ }
+ }
+
+ public void test0001() throws JavaModelException {
+ String contents =
+ " public class X {\n" +
+ " enum Day\n" +
+ " {\n" +
+ " SUNDAY, MONDAY, TUESDAY, WEDNESDAY,\n" +
+ " THURSDAY, FRIDAY, SATURDAY;\n" +
+ " }\n" +
+ " public static void main(String[] args) {\n" +
+ " Day day = Day.SUNDAY;\n" +
+ " int k = switch (day) {\n" +
+ " case MONDAY -> throw new NullPointerException();\n" +
+ " case TUESDAY -> 1;\n" +
+ "\n" +
+ " case WEDNESDAY -> {break 10;}\n" +
+ " default -> {\n" +
+ " int g = day.toString().length();\n" +
+ " break g;\n" +
+ " }};\n" +
+ " System.out.println(k);\n" +
+ " }\n" +
+ "}" ;
+ this.workingCopy = getWorkingCopy("/Converter12/src/X.java", true/*resolve*/);
+ IJavaProject javaProject = this.workingCopy.getJavaProject();
+ String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
+ try {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
+ ASTNode node = buildAST(
+ contents,
+ this.workingCopy);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+ assertProblemsSize(compilationUnit, 0);
+ node = getASTNode(compilationUnit, 0, 1);
+ MethodDeclaration methodDeclaration = (MethodDeclaration) node;
+ VariableDeclarationStatement vStmt1 = (VariableDeclarationStatement) methodDeclaration.getBody().statements().get(1);
+ Type type = vStmt1.getType();
+ IBinding binding = type.resolveBinding();
+ assertTrue("null binding", binding != null);
+ assertTrue("binding incorrect", binding.getName().equals("int"));
+
+ } finally {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
+ }
+
+ }
+}
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/AbstractASTTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/AbstractASTTests.java
index b6fafe483c..8b895c14be 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/AbstractASTTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/AbstractASTTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2018 IBM Corporation and others.
+ * Copyright (c) 2004, 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,6 +8,10 @@
*
* 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
*******************************************************************************/
@@ -493,7 +497,9 @@ public class AbstractASTTests extends ModifyingResourceTests implements DefaultM
String option = cu.getJavaProject().getOption(JavaCore.COMPILER_COMPLIANCE, true);
long jdkLevel = CompilerOptions.versionToJdkLevel(option);
int JLSLevel = AST_INTERNAL_JLS3;
- if (jdkLevel >= ClassFileConstants.getComplianceLevelForJavaVersion(ClassFileConstants.MAJOR_VERSION_11)) {
+ if (jdkLevel >= ClassFileConstants.getComplianceLevelForJavaVersion(ClassFileConstants.MAJOR_VERSION_12)) {
+ JLSLevel = AST_INTERNAL_JLS12;
+ } else if (jdkLevel >= ClassFileConstants.getComplianceLevelForJavaVersion(ClassFileConstants.MAJOR_VERSION_11)) {
JLSLevel = AST_INTERNAL_JLS11;
} else if (jdkLevel >= ClassFileConstants.JDK10) {
JLSLevel = AST_INTERNAL_JLS10;
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java
index f34d73e568..513a512a7f 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.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,6 +8,10 @@
*
* 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
*******************************************************************************/
@@ -105,6 +109,7 @@ public abstract class ConverterTestSetup extends AbstractASTTests {
this.deleteProject("Converter9"); //$NON-NLS-1$
this.deleteProject("Converter10"); //$NON-NLS-1$
this.deleteProject("Converter11"); //$NON-NLS-1$
+ this.deleteProject("Converter12"); //$NON-NLS-1$
PROJECT_SETUP = false;
} else {
TEST_SUITES.remove(getClass());
@@ -117,6 +122,7 @@ public abstract class ConverterTestSetup extends AbstractASTTests {
this.deleteProject("Converter9"); //$NON-NLS-1$
this.deleteProject("Converter10"); //$NON-NLS-1$
this.deleteProject("Converter11"); //$NON-NLS-1$
+ this.deleteProject("Converter12"); //$NON-NLS-1$
PROJECT_SETUP = false;
}
}
@@ -178,6 +184,14 @@ public abstract class ConverterTestSetup extends AbstractASTTests {
new IPath[] {getConverterJCLPath("11"), getConverterJCLSourcePath("11"), getConverterJCLRootSourcePath()},
null);
}
+ } else if ("12".equals(compliance)) {
+ if (JavaCore.getClasspathVariable("CONVERTER_JCL12_LIB") == null) {
+ setupExternalJCL("converterJclMin12");
+ JavaCore.setClasspathVariables(
+ new String[] {"CONVERTER_JCL11_LIB", "CONVERTER_JCL11_SRC", "CONVERTER_JCL11_SRCROOT"},
+ new IPath[] {getConverterJCLPath("12"), getConverterJCLSourcePath("12"), getConverterJCLRootSourcePath()},
+ null);
+ }
} else if (JavaCore.getClasspathVariable("CONVERTER_JCL_LIB") == null) {
setupExternalJCL("converterJclMin");
JavaCore.setClasspathVariables(
@@ -202,6 +216,7 @@ public abstract class ConverterTestSetup extends AbstractASTTests {
setUpJavaProject("Converter9", "9"); //$NON-NLS-1$ //$NON-NLS-2$
setUpJavaProject("Converter10", "10"); //$NON-NLS-1$ //$NON-NLS-2$
setUpJavaProject("Converter11", "11"); //$NON-NLS-1$ //$NON-NLS-2$
+ setUpJavaProject("Converter12", "12"); //$NON-NLS-1$ //$NON-NLS-2$
waitUntilIndexesReady(); // needed to find secondary types
PROJECT_SETUP = true;
}
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java
index 028e170023..f12cc67bab 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java
@@ -52,6 +52,7 @@ public static Class[] getAllTestClasses() {
TypeAnnotationsConverterTest.class,
ASTConverter18Test.class,
ASTConverter9Test.class,
+ ASTConverter12Test.class,
};
}
public static Test suite() {
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 f73f342df1..0c699a9eaa 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
@@ -137,11 +137,16 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases {
protected static final int AST_INTERNAL_JLS10 = AST.JLS10;
/**
+ * Internal synonym for constant AST.JSL12
+ * to alleviate deprecation warnings once AST.JLS12 is deprecated in future.
+ */
+ protected static final int AST_INTERNAL_JLS12 = AST.JLS12;
+
+ /**
* Internal synonym for constant AST.JSL11
* to alleviate deprecation warnings once AST.JLS11 is deprecated in future.
*/
protected static final int AST_INTERNAL_JLS11 = AST.JLS11;
-
public static class BasicProblemRequestor implements IProblemRequestor {
public void acceptProblem(IProblem problem) {}
public void beginReporting() {}
@@ -3111,6 +3116,7 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases {
javaProject.setOption(JavaCore.COMPILER_PB_FIELD_HIDING, JavaCore.IGNORE);
javaProject.setOption(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.IGNORE);
javaProject.setOption(JavaCore.COMPILER_PB_TYPE_PARAMETER_HIDING, JavaCore.IGNORE);
+// javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
return javaProject;
}
protected void setUpProjectCompliance(IJavaProject javaProject, String compliance) throws JavaModelException, IOException {
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Converter12/.classpath b/org.eclipse.jdt.core.tests.model/workspace/Converter12/.classpath
new file mode 100644
index 0000000000..3522bc0c3f
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Converter12/.classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="var" path="CONVERTER_JCL18_LIB" sourcepath="CONVERTER_JCL18_SRC" rootpath="CONVERTER_JCL_SRCROOT"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Converter12/.project b/org.eclipse.jdt.core.tests.model/workspace/Converter12/.project
new file mode 100644
index 0000000000..663fb8d747
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Converter12/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>Converter18</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Converter12/src/X.java b/org.eclipse.jdt.core.tests.model/workspace/Converter12/src/X.java
new file mode 100644
index 0000000000..5a1a8880f1
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Converter12/src/X.java
@@ -0,0 +1,4 @@
+public class X {
+ public void foo(X this) {
+ }
+}
diff --git a/org.eclipse.jdt.core/.settings/.api_filters b/org.eclipse.jdt.core/.settings/.api_filters
index f892b0c388..03e2085432 100644
--- a/org.eclipse.jdt.core/.settings/.api_filters
+++ b/org.eclipse.jdt.core/.settings/.api_filters
@@ -22,6 +22,22 @@
</message_arguments>
</filter>
</resource>
+ <resource path="dom/org/eclipse/jdt/core/dom/BreakStatement.java" type="org.eclipse.jdt.core.dom.BreakStatement">
+ <filter comment="For Java 12 support." id="336658481">
+ <message_arguments>
+ <message_argument value="org.eclipse.jdt.core.dom.BreakStatement"/>
+ <message_argument value="EXPRESSION_PROPERTY"/>
+ </message_arguments>
+ </filter>
+ </resource>
+ <resource path="dom/org/eclipse/jdt/core/dom/SwitchCase.java" type="org.eclipse.jdt.core.dom.SwitchCase">
+ <filter comment="For Java 12 Switch Expression support" id="336658481">
+ <message_arguments>
+ <message_argument value="org.eclipse.jdt.core.dom.SwitchCase"/>
+ <message_argument value="EXPRESSION_2_PROPERTY"/>
+ </message_arguments>
+ </filter>
+ </resource>
<resource path="compiler/org/eclipse/jdt/core/compiler/IProblem.java" type="org.eclipse.jdt.core.compiler.IProblem">
<filter id="388194388">
<message_arguments>
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
index ce3f49443b..6004f8005a 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.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
@@ -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
@@ -270,6 +274,28 @@ public final class AST {
* @since 3.14
*/
/*package*/ static final int JLS11_INTERNAL = JLS11;
+
+ /**
+ * Constant for indicating the AST API that handles JLS12.
+ * <p>
+ * This API is capable of handling all constructs in the
+ * Java language as described in the Java Language
+ * Specification, Java SE 12 Edition (JLS12).
+ * JLS12 is a superset of all earlier versions of the
+ * Java language, and the JLS12 API can be used to manipulate
+ * programs written in all versions of the Java language
+ * up to and including Java SE 12 (aka JDK 12).
+ * </p>
+ *
+ * @since 3.17 BETA_JAVA_12
+ */
+ public static final int JLS12 = 12;
+ /**
+ * Internal synonym for {@link #JLS11}. Use to alleviate
+ * deprecation warnings once JLS11 is deprecated
+ * @since 3.16
+ */
+ static final int JLS12_INTERNAL = JLS12;
/*
* Must not collide with a value for ICompilationUnit constants
@@ -850,7 +876,21 @@ public final class AST {
null/*taskTag*/,
null/*taskPriorities*/,
true/*taskCaseSensitive*/);
- break;
+ break;
+ case JLS12_INTERNAL :
+ this.apiLevel = level;
+ // initialize a scanner
+ compliance = ClassFileConstants.getComplianceLevelForJavaVersion(ClassFileConstants.MAJOR_VERSION_12);
+ this.scanner = new Scanner(
+ true /*comment*/,
+ true /*whitespace*/,
+ false /*nls*/,
+ compliance /*sourceLevel*/,
+ compliance /*complianceLevel*/,
+ null/*taskTag*/,
+ null/*taskPriorities*/,
+ true/*taskCaseSensitive*/);
+ break;
default:
throw new IllegalArgumentException("Unsupported JLS level"); //$NON-NLS-1$
}
@@ -2539,6 +2579,19 @@ public final class AST {
}
/**
+ * Creates and returns a new unparented switch expression node
+ * owned by this AST. By default, the expression is unspecified, but legal,
+ * and there are no statements or switch cases.
+ *
+ * @return a new unparented labeled switch expression node
+ * @since 3.17 BETA_JAVA_12
+ */
+ public SwitchExpression newSwitchExpression() {
+ SwitchExpression result = new SwitchExpression(this);
+ return result;
+ }
+
+ /**
* Creates a new unparented switch case statement node owned by
* this AST. By default, the expression is unspecified, but legal.
*
@@ -2548,7 +2601,6 @@ public final class AST {
return new SwitchCase(this);
}
-
/**
* Creates a new unparented switch statement node owned by this AST.
* By default, the expression is unspecified, but legal, and there are
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java
index 2d59d086ff..22b150370c 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.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
@@ -1289,6 +1289,10 @@ class ASTConverter {
retrieveIdentifierAndSetPositions(statement.sourceStart, statement.sourceEnd, name);
breakStatement.setLabel(name);
}
+ else if (statement.expression != null && this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ final Expression expression= convert(statement.expression);
+ breakStatement.setExpression(expression);
+ }
return breakStatement;
}
@@ -1301,6 +1305,7 @@ class ASTConverter {
} else {
switchCase.setExpression(convert(constantExpression));
}
+ switchCase.setIsExpr(statement.isExpr);
switchCase.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
retrieveColonPosition(switchCase);
return switchCase;
@@ -2693,14 +2698,7 @@ class ASTConverter {
return result;
}
- public Expression convert(org.eclipse.jdt.internal.compiler.ast.SwitchExpression expression) {
- // TODO: Entry point for dom ast conversion.
- // Replace the code below with actual implementation
- NullLiteral nullLiteral = new NullLiteral(this.ast);
- nullLiteral.setFlags(nullLiteral.getFlags() | ASTNode.MALFORMED);
- nullLiteral.setSourceRange(expression.sourceStart, expression.sourceEnd - expression.sourceStart + 1);
- return nullLiteral;
- }
+
public ReturnStatement convert(org.eclipse.jdt.internal.compiler.ast.ReturnStatement statement) {
final ReturnStatement returnStatement = new ReturnStatement(this.ast);
returnStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
@@ -2842,6 +2840,33 @@ class ASTConverter {
return literal;
}
+ public Expression convert(org.eclipse.jdt.internal.compiler.ast.SwitchExpression expression) {
+ if (this.ast.apiLevel < AST.JLS12_INTERNAL) {
+ return createFakeNullLiteral(expression);
+ }
+ SwitchExpression switchExpression = new SwitchExpression(this.ast);
+ if (this.resolveBindings) {
+ recordNodes(switchExpression, expression);
+ }
+ switchExpression.setSourceRange(expression.sourceStart, expression.sourceEnd - expression.sourceStart + 1);
+ switchExpression.setExpression(convert(expression.expression));
+ org.eclipse.jdt.internal.compiler.ast.Statement[] statements = expression.statements;
+ if (statements != null) {
+ int statementsLength = statements.length;
+ for (int i = 0; i < statementsLength; i++) {
+ if (statements[i] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) {
+ checkAndAddMultipleLocalDeclaration(statements, i, switchExpression.statements());
+ } else {
+ final Statement currentStatement = convert(statements[i]);
+ if (currentStatement != null) {
+ switchExpression.statements().add(currentStatement);
+ }
+ }
+ }
+ }
+ return switchExpression;
+ }
+
public SwitchStatement convert(org.eclipse.jdt.internal.compiler.ast.SwitchStatement statement) {
SwitchStatement switchStatement = new SwitchStatement(this.ast);
switchStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
@@ -4241,7 +4266,7 @@ class ASTConverter {
* Warning: Callers of this method must ensure that the fake literal node is not recorded in
* {@link #recordNodes(ASTNode, org.eclipse.jdt.internal.compiler.ast.ASTNode)}, see bug 403444!
*/
- protected Expression createFakeNullLiteral(org.eclipse.jdt.internal.compiler.ast.FunctionalExpression expression) {
+ protected Expression createFakeNullLiteral(org.eclipse.jdt.internal.compiler.ast.Expression expression) {
if (this.referenceContext != null) {
this.referenceContext.setFlags(this.referenceContext.getFlags() | ASTNode.MALFORMED);
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java
index ff2d234843..167141dd4c 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * 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
@@ -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
@@ -484,7 +488,9 @@ public class ASTMatcher {
return false;
}
BreakStatement o = (BreakStatement) other;
- return safeSubtreeMatch(node.getLabel(), o.getLabel());
+ return ( node.getAST().apiLevel >= AST.JLS12_INTERNAL && node.getExpression() != null
+ ? safeSubtreeMatch(node.getExpression(), o.getExpression())
+ : safeSubtreeMatch(node.getLabel(), o.getLabel()));
}
/**
@@ -2267,7 +2273,33 @@ public class ASTMatcher {
return false;
}
SwitchCase o = (SwitchCase) other;
- return safeSubtreeMatch(node.getExpression(), o.getExpression());
+ return ( node.getAST().apiLevel >= AST.JLS12_INTERNAL
+ ? safeSubtreeListMatch(node.getExpression2(), o.getExpression2())
+ : safeSubtreeMatch(node.getExpression(), o.getExpression()));
+ }
+
+ /**
+ * Returns whether the given node and the other object match.
+ * <p>
+ * The default implementation provided by this class tests whether the
+ * other object is a node of the same type with structurally isomorphic
+ * child subtrees. Subclasses may override this method as needed.
+ * </p>
+ *
+ * @param node the node
+ * @param other the other object, or <code>null</code>
+ * @return <code>true</code> if the subtree matches, or
+ * <code>false</code> if they do not match or the other object has a
+ * different node type or is <code>null</code>
+ * @since 3.17 BETA_JAVA_12
+ */
+ public boolean match(SwitchExpression node, Object other) {
+ if (!(other instanceof SwitchExpression)) {
+ return false;
+ }
+ SwitchExpression o = (SwitchExpression) other;
+ return safeSubtreeMatch(node.getExpression(), o.getExpression())
+ && safeSubtreeListMatch(node.statements(), o.statements());
}
/**
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTNode.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTNode.java
index ac6d7103a7..11135b6f45 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTNode.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTNode.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
@@ -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
@@ -954,6 +958,14 @@ public abstract class ASTNode {
* @since 3.14
*/
public static final int MODULE_MODIFIER = 99;
+
+ /**
+ * Node type constant indicating a node of type
+ * <code>SwitchExpression</code>.
+ * @see SwitchExpression
+ * @since 3.17 BETA_JAVA_12
+ */
+ public static final int SWITCH_EXPRESSION = 100;
/**
* Returns the node class for the corresponding node type.
@@ -1129,6 +1141,8 @@ public abstract class ASTNode {
return SwitchCase.class;
case SWITCH_STATEMENT :
return SwitchStatement.class;
+ case SWITCH_EXPRESSION :
+ return SwitchExpression.class;
case SYNCHRONIZED_STATEMENT :
return SynchronizedStatement.class;
case TAG_ELEMENT :
@@ -2071,6 +2085,22 @@ public abstract class ASTNode {
throw new UnsupportedOperationException("Operation only supported in ASTs with level JLS11 and above"); //$NON-NLS-1$
}
}
+
+ /**
+ * Checks that this AST operation is not used when
+ * building JLS2, JLS3, JLS4, JLS8, JLS9,JLS10 or JLS11 level ASTs.
+ * <p>
+ * Use this method to prevent access to new properties that have been added in JLS12
+ * </p>
+ *
+ * @exception UnsupportedOperationException if this operation is used below JLS12
+ * @since 3.16
+ */
+ final void unsupportedBelow12() {
+ if (this.ast.apiLevel < AST.JLS12_INTERNAL) {
+ throw new UnsupportedOperationException("Operation only supported in ASTs with level JLS12 and above"); //$NON-NLS-1$
+ }
+ }
/**
* Checks that this AST operation is only used when
* building JLS2 level ASTs.
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java
index c202eb295a..af3682f5fa 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2018 IBM Corporation and others.
+ * Copyright (c) 2004, 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
@@ -234,6 +238,7 @@ public class ASTParser {
case AST.JLS9_INTERNAL:
case AST.JLS10_INTERNAL:
case AST.JLS11_INTERNAL:
+ case AST.JLS12_INTERNAL:
break;
default:
throw new IllegalArgumentException();
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTVisitor.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTVisitor.java
index b31c3a7c82..b88b354bab 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTVisitor.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTVisitor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * 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
@@ -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
@@ -1521,7 +1525,24 @@ public abstract class ASTVisitor {
public boolean visit(SwitchCase node) {
return true;
}
-
+
+ /**
+ * Visits the given type-specific AST node.
+ * <p>
+ * The default implementation does nothing and return true.
+ * Subclasses may reimplement.
+ * </p>
+ *
+ * @param node the node to visit
+ * @return <code>true</code> if the children of this node should be
+ * visited, and <code>false</code> if the children of this node should
+ * be skipped
+ * @since 3.17 BETA_JAVA_12
+ */
+ public boolean visit(SwitchExpression node) {
+ return true;
+ }
+
/**
* Visits the given type-specific AST node.
* <p>
@@ -2355,7 +2376,7 @@ public abstract class ASTVisitor {
public void endVisit(LambdaExpression node) {
// default implementation: do nothing
}
-
+
/**
* End of visit the given type-specific AST node.
* <p>
@@ -2830,6 +2851,19 @@ public abstract class ASTVisitor {
* </p>
*
* @param node the node to visit
+ * @since 3.17 BETA_JAVA_12
+ */
+ public void endVisit(SwitchExpression node) {
+ // default implementation: do nothing
+ }
+
+ /**
+ * End of visit the given type-specific AST node.
+ * <p>
+ * The default implementation does nothing. Subclasses may reimplement.
+ * </p>
+ *
+ * @param node the node to visit
*/
public void endVisit(SwitchStatement node) {
// default implementation: do nothing
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java
index 4cebdc6ca0..32b9f8e586 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 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
@@ -23,6 +27,9 @@ import java.util.List;
* <pre>
* BreakStatement:
* <b>break</b> [ Identifier ] <b>;</b>
+ *
+ * Break statement allows expression as part of Java 12 preview feature (JEP 325)
+ * <b>break</b> <b>{ Identifier | Expression }</b>
* </pre>
*
* @since 2.0
@@ -37,6 +44,13 @@ public class BreakStatement extends Statement {
*/
public static final ChildPropertyDescriptor LABEL_PROPERTY =
new ChildPropertyDescriptor(BreakStatement.class, "label", SimpleName.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$
+
+ /**
+ * The "expression" structural property of this node type (child type: {@link Expression}). (added in JEP 325).
+ * @since 3.17 BETA_JAVA_12
+ */
+ public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
+ new ChildPropertyDescriptor(BreakStatement.class, "expression", Expression.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$);
/**
* A list of property descriptors (element type:
@@ -44,12 +58,25 @@ public class BreakStatement extends Statement {
* or null if uninitialized.
*/
private static final List PROPERTY_DESCRIPTORS;
+
+ /**
+ * A list of property descriptors (element type:
+ * {@link StructuralPropertyDescriptor}),
+ * or null if uninitialized.
+ */
+ private static final List PROPERTY_DESCRIPTORS_12;
static {
List properyList = new ArrayList(2);
createPropertyList(BreakStatement.class, properyList);
addProperty(LABEL_PROPERTY, properyList);
PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
+
+ List properyList_12 = new ArrayList(2);
+ createPropertyList(BreakStatement.class, properyList_12);
+ addProperty(LABEL_PROPERTY, properyList_12);
+ addProperty(EXPRESSION_PROPERTY, properyList_12);
+ PROPERTY_DESCRIPTORS_12 = reapPropertyList(properyList_12);
}
/**
@@ -64,6 +91,9 @@ public class BreakStatement extends Statement {
* @since 3.0
*/
public static List propertyDescriptors(int apiLevel) {
+ if (apiLevel >= AST.JLS12_INTERNAL) {
+ return PROPERTY_DESCRIPTORS_12;
+ }
return PROPERTY_DESCRIPTORS;
}
@@ -71,6 +101,11 @@ public class BreakStatement extends Statement {
* The label, or <code>null</code> if none; none by default.
*/
private SimpleName optionalLabel = null;
+
+ /**
+ * The expression; <code>null</code> for none
+ */
+ private Expression optionalExpression = null;
/**
* Creates a new unparented break statement node owned by the given
@@ -99,6 +134,14 @@ public class BreakStatement extends Statement {
setLabel((SimpleName) child);
return null;
}
+ }
+ if (property == EXPRESSION_PROPERTY) {
+ if (get) {
+ return getExpression();
+ } else {
+ setExpression((Expression) child);
+ return null;
+ }
}
// allow default implementation to flag the error
return super.internalGetSetChildProperty(property, get, child);
@@ -115,6 +158,9 @@ public class BreakStatement extends Statement {
result.setSourceRange(getStartPosition(), getLength());
result.copyLeadingComment(this);
result.setLabel((SimpleName) ASTNode.copySubtree(target, getLabel()));
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ result.setExpression((Expression) ASTNode.copySubtree(target, getLabel()));
+ }
return result;
}
@@ -128,6 +174,9 @@ public class BreakStatement extends Statement {
void accept0(ASTVisitor visitor) {
boolean visitChildren = visitor.visit(this);
if (visitChildren) {
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ acceptChild(visitor, getExpression());
+ }
acceptChild(visitor, getLabel());
}
visitor.endVisit(this);
@@ -160,17 +209,48 @@ public class BreakStatement extends Statement {
this.optionalLabel = label;
postReplaceChild(oldChild, label, LABEL_PROPERTY);
}
+
+ /**
+ * Returns the expression of this break statement, or <code>null</code> if
+ * there is none.
+ *
+ * @return the expression, or <code>null</code> if there is none
+ * @since 3.17 BETA_JAVA_12
+ */
+ public Expression getExpression() {
+ return this.optionalExpression;
+ }
+
+ /**
+ * Sets or clears the expression of this break statement.
+ *
+ * @param expression the expression, or <code>null</code> if
+ * there is none
+ * @exception IllegalArgumentException if:
+ * <ul>
+ * <li>the node belongs to a different AST</li>
+ * <li>the node already has a parent</li>
+ * </ul>
+ * @since 3.17 BETA_JAVA_12
+ */
+ public void setExpression(Expression expression) {
+ ASTNode oldChild = this.optionalExpression;
+ preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
+ this.optionalExpression = expression;
+ postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
+ }
@Override
int memSize() {
- return super.memSize() + 1 * 4;
+ return super.memSize() + 2 * 4;
}
@Override
int treeSize() {
return
memSize()
- + (this.optionalLabel == null ? 0 : getLabel().treeSize());
+ + (this.optionalLabel == null ? 0 : getLabel().treeSize())
+ + (this.optionalExpression == null ? 0 : getExpression().treeSize());
}
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultASTVisitor.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultASTVisitor.java
index e50e553ef8..f926654178 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultASTVisitor.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultASTVisitor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * 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
@@ -8,6 +8,9 @@
*
* 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
*******************************************************************************/
@@ -334,6 +337,10 @@ class DefaultASTVisitor extends ASTVisitor {
endVisitNode(node);
}
@Override
+ public void endVisit(SwitchExpression node) {
+ endVisitNode(node);
+ }
+ @Override
public void endVisit(SwitchCase node) {
endVisitNode(node);
}
@@ -731,6 +738,11 @@ class DefaultASTVisitor extends ASTVisitor {
public boolean visit(SwitchCase node) {
return visitNode(node);
}
+
+ @Override
+ public boolean visit(SwitchExpression node) {
+ return visitNode(node);
+ }
@Override
public boolean visit(SwitchStatement node) {
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java
index 341e16ea7a..851500e532 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.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
@@ -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
@@ -742,6 +746,7 @@ class DefaultBindingResolver extends BindingResolver {
case ASTNode.METHOD_INVOCATION :
case ASTNode.SUPER_METHOD_INVOCATION :
case ASTNode.CONDITIONAL_EXPRESSION :
+ case ASTNode.SWITCH_EXPRESSION :
case ASTNode.MARKER_ANNOTATION :
case ASTNode.NORMAL_ANNOTATION :
case ASTNode.SINGLE_MEMBER_ANNOTATION :
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Expression.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Expression.java
index 13d96883a7..de760b945a 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Expression.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Expression.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 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
@@ -48,6 +52,7 @@ package org.eclipse.jdt.core.dom;
* {@link SuperFieldAccess},
* {@link SuperMethodInvocation},
* {@link SuperMethodReference},
+ * {@link SwitchExpression},
* {@link ThisExpression},
* {@link TypeLiteral},
* {@link TypeMethodReference},
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java
index c805ceac10..a11d668f73 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 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,6 +8,10 @@
*
* 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
*******************************************************************************/
@@ -23,8 +27,12 @@ import java.util.List;
* <p>
* <pre>
* SwitchCase:
- * <b>case</b> Expression <b>:</b>
- * <b>default</b> <b>:</b>
+ * <b>case</b> Expression <b>:</b>
+ * <b>default</b> <b>:</b>
+ *
+ * Switch case allows multiple expressions and '->' as part of Java 12 preview feature (JEP 325)
+ * <b>case</b> [ Expression { <b>,</b> Expression } ] <b>{ : | ->}</b>
+ * <b>default</b> <b>{ : | ->}</b>
* </pre>
* </p>
*
@@ -42,17 +50,37 @@ public class SwitchCase extends Statement {
new ChildPropertyDescriptor(SwitchCase.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
/**
+ * The "expression" structural property of this node type (child type: {@link Expression}). (added in JEP 325).
+ * @since 3.17 BETA_JAVA_12
+ */
+ public static final ChildListPropertyDescriptor EXPRESSION_2_PROPERTY =
+ new ChildListPropertyDescriptor(SwitchCase.class, "expression", Expression.class, CYCLE_RISK); //$NON-NLS-1$);
+
+
+ /**
* A list of property descriptors (element type:
* {@link StructuralPropertyDescriptor}),
* or null if uninitialized.
*/
private static final List PROPERTY_DESCRIPTORS;
+
+ /**
+ * A list of property descriptors (element type:
+ * {@link StructuralPropertyDescriptor}),
+ * or null if uninitialized.
+ */
+ private static final List PROPERTY_DESCRIPTORS_12;
static {
List propertyList = new ArrayList(2);
createPropertyList(SwitchCase.class, propertyList);
addProperty(EXPRESSION_PROPERTY, propertyList);
PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
+
+ propertyList = new ArrayList(2);
+ createPropertyList(SwitchCase.class, propertyList);
+ addProperty(EXPRESSION_2_PROPERTY, propertyList);
+ PROPERTY_DESCRIPTORS_12 = reapPropertyList(propertyList);
}
/**
@@ -66,6 +94,9 @@ public class SwitchCase extends Statement {
* @since 3.0
*/
public static List propertyDescriptors(int apiLevel) {
+ if (apiLevel >= AST.JLS12_INTERNAL) {
+ return PROPERTY_DESCRIPTORS_12;
+ }
return PROPERTY_DESCRIPTORS;
}
@@ -75,6 +106,19 @@ public class SwitchCase extends Statement {
* @see #expressionInitialized
*/
private Expression optionalExpression = null;
+
+ /**
+ * The expression; <code>null</code> for none; lazily initialized (but
+ * does <b>not</b> default to none).
+ * @see #expressionInitialized
+ */
+ private boolean isExpr = false;
+
+
+ /**
+ * The expression; <code>empty</code> for none;
+ */
+ private ASTNode.NodeList expression = null;
/**
* Indicates whether <code>optionalExpression</code> has been initialized.
@@ -89,6 +133,9 @@ public class SwitchCase extends Statement {
*/
SwitchCase(AST ast) {
super(ast);
+ if (ast.apiLevel >= AST.JLS12_INTERNAL) {
+ this.expression = new ASTNode.NodeList(EXPRESSION_2_PROPERTY);
+ }
}
@Override
@@ -111,17 +158,32 @@ public class SwitchCase extends Statement {
}
@Override
+ final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
+ if (property == EXPRESSION_2_PROPERTY) {
+ return getExpression2();
+ }
+ // allow default implementation to flag the error
+ return super.internalGetChildListProperty(property);
+ }
+
+ @Override
final int getNodeType0() {
return SWITCH_CASE;
}
+ @SuppressWarnings("unchecked")
@Override
ASTNode clone0(AST target) {
SwitchCase result = new SwitchCase(target);
result.setSourceRange(getStartPosition(), getLength());
result.copyLeadingComment(this);
- result.setExpression(
- (Expression) ASTNode.copySubtree(target, getExpression()));
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ result.getExpression2().addAll(
+ ASTNode.copySubtrees(target, getExpression2()));
+ } else {
+ result.setExpression(
+ (Expression) ASTNode.copySubtree(target, getExpression()));
+ }
return result;
}
@@ -135,7 +197,11 @@ public class SwitchCase extends Statement {
void accept0(ASTVisitor visitor) {
boolean visitChildren = visitor.visit(this);
if (visitChildren) {
- acceptChild(visitor, getExpression());
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ acceptChildren(visitor, this.expression);
+ } else {
+ acceptChild(visitor, getExpression());
+ }
}
visitor.endVisit(this);
}
@@ -160,6 +226,20 @@ public class SwitchCase extends Statement {
}
return this.optionalExpression;
}
+
+ /**
+ * Returns the expression of this switch case, or
+ * <code>empty</code> if there is none (the "default:" case).
+ *
+ * @return the expression node, or <code>expression</code> if there is none
+ * @since 3.17 BETA_JAVA_12
+ */
+ public List getExpression2() {
+ if (this.expression == null) {
+ unsupportedBelow12();
+ }
+ return this.expression;
+ }
/**
* Sets the expression of this switch case, or clears it (turns it into
@@ -181,18 +261,41 @@ public class SwitchCase extends Statement {
this.expressionInitialized = true;
postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
}
+
+ /**
+ * Sets the isExpr of this switch case as <code>true</code> or </false>.
+ *
+ * @param isExpr <code>true</code> or </false>
+ * @since 3.17 BETA_JAVA_12
+ */
+ public void setIsExpr(boolean isExpr) {
+ this.isExpr = isExpr;
+ }
+
+ /**
+ * Gets the isExpr of this switch case as <code>true</code> or </false>.
+ *
+ * @return isExpr <code>true</code> or </false>
+ * @since 3.17 BETA_JAVA_12
+ */
+ public boolean isExpr() {
+ return this.isExpr;
+ }
/**
* Returns whether this switch case represents the "default:" case.
* <p>
* This convenience method is equivalent to
- * <code>getExpression() == null</code>.
+ * <code>getExpression() == null</code> or <code>getExpression2().isEmpty()</code>.
* </p>
*
* @return <code>true</code> if this is the default switch case, and
* <code>false</code> if this is a non-default switch case
*/
public boolean isDefault() {
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ return getExpression2().isEmpty();
+ }
return getExpression() == null;
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchExpression.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchExpression.java
new file mode 100644
index 0000000000..97df44e724
--- /dev/null
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchExpression.java
@@ -0,0 +1,240 @@
+/*******************************************************************************
+ * 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.dom;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Switch expression AST node type (added in JEP 325).
+ * <p>
+ * <pre>
+ * SwitchExpression:
+ * <b>switch</b> <b>(</b> Expression <b>)</b>
+ * <b>{</b> { SwitchCase | Statement } <b>}</b>
+ * SwitchCase:
+ * <b>case</b> [ Expression { <b>,</b> Expression } ] <b>{ : | ->}</b>
+ * <b>default</b> <b>{ : | ->}</b>
+ * </pre>
+ * <code>SwitchCase</code> nodes are treated as a kind of
+ * <code>Expression</code>.
+ * </p>
+ *
+ * @since 3.17 BETA_JAVA_12
+ * @noinstantiate This class is not intended to be instantiated by clients.
+ */
+@SuppressWarnings({"rawtypes", "unchecked"})
+public class SwitchExpression extends Expression {
+
+ /**
+ * The "expression" structural property of this node type (child type: {@link Expression}).
+ * @since 3.16
+ */
+ public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
+ new ChildPropertyDescriptor(SwitchExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
+
+ /**
+ * The "statements" structural property of this node type (element type: {@link Statement}).
+ * @since 3.16
+ */
+ public static final ChildListPropertyDescriptor STATEMENTS_PROPERTY =
+ new ChildListPropertyDescriptor(SwitchExpression.class, "statements", Statement.class, CYCLE_RISK); //$NON-NLS-1$
+
+ /**
+ * A list of property descriptors (element type:
+ * {@link StructuralPropertyDescriptor}),
+ * or null if uninitialized.
+ */
+ private static final List PROPERTY_DESCRIPTORS_12_0;
+
+ static {
+ List propertyList = new ArrayList(3);
+ createPropertyList(SwitchExpression.class, propertyList);
+ addProperty(EXPRESSION_PROPERTY, propertyList);
+ addProperty(STATEMENTS_PROPERTY, propertyList);
+ PROPERTY_DESCRIPTORS_12_0 = reapPropertyList(propertyList);
+ }
+
+ /**
+ * Returns a list of structural property descriptors for this node type.
+ * Clients must not modify the result.
+ *
+ * @param apiLevel the API level; one of the
+ * <code>AST.JLS*</code> constants
+ * @return a list of property descriptors (element type:
+ * {@link StructuralPropertyDescriptor})
+ * @since 3.16
+ */
+ public static List propertyDescriptors(int apiLevel) {
+ return PROPERTY_DESCRIPTORS_12_0;
+ }
+
+ /**
+ * The expression; lazily initialized; defaults to a unspecified, but legal,
+ * expression.
+ */
+ private Expression expression = null;
+
+ /**
+ * The statements and SwitchCase nodes
+ * (element type: {@link Statement}).
+ * Defaults to an empty list.
+ */
+ private ASTNode.NodeList statements =
+ new ASTNode.NodeList(STATEMENTS_PROPERTY);
+
+ /**
+ * Creates a new unparented switch statement node owned by the given
+ * AST. By default, the swicth statement has an unspecified, but legal,
+ * expression, and an empty list of switch groups.
+ * <p>
+ * N.B. This constructor is package-private.
+ * </p>
+ *
+ * @param ast the AST that is to own this node
+ */
+ SwitchExpression(AST ast) {
+ super(ast);
+ unsupportedBelow12();
+ }
+
+ @Override
+ final List internalStructuralPropertiesForType(int apiLevel) {
+ return propertyDescriptors(apiLevel);
+ }
+
+ @Override
+ final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
+ if (property == EXPRESSION_PROPERTY) {
+ if (get) {
+ return getExpression();
+ } else {
+ setExpression((Expression) child);
+ return null;
+ }
+ }
+ // allow default implementation to flag the error
+ return super.internalGetSetChildProperty(property, get, child);
+ }
+
+ @Override
+ final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
+ if (property == STATEMENTS_PROPERTY) {
+ return statements();
+ }
+ // allow default implementation to flag the error
+ return super.internalGetChildListProperty(property);
+ }
+
+ @Override
+ final int getNodeType0() {
+ return SWITCH_EXPRESSION;
+ }
+
+ @Override
+ ASTNode clone0(AST target) {
+ SwitchExpression result = new SwitchExpression(target);
+ result.setSourceRange(getStartPosition(), getLength());
+ result.setExpression((Expression) getExpression().clone(target));
+ result.statements().addAll(ASTNode.copySubtrees(target, statements()));
+ return result;
+ }
+
+ @Override
+ final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
+ // dispatch to correct overloaded match method
+ return matcher.match(this, other);
+ }
+
+ @Override
+ void accept0(ASTVisitor visitor) {
+ boolean visitChildren = visitor.visit(this);
+ if (visitChildren) {
+ // visit children in normal left to right reading order
+ acceptChild(visitor, getExpression());
+ acceptChildren(visitor, this.statements);
+ }
+ visitor.endVisit(this);
+ }
+
+ /**
+ * Returns the expression of this switch statement.
+ *
+ * @return the expression node
+ */
+ public Expression getExpression() {
+ if (this.expression == null) {
+ // lazy init must be thread-safe for readers
+ synchronized (this) {
+ if (this.expression == null) {
+ preLazyInit();
+ this.expression = new SimpleName(this.ast);
+ postLazyInit(this.expression, EXPRESSION_PROPERTY);
+ }
+ }
+ }
+ return this.expression;
+ }
+
+ /**
+ * Sets the expression of this switch statement.
+ *
+ * @param expression the new expression node
+ * @exception IllegalArgumentException if:
+ * <ul>
+ * <li>the node belongs to a different AST</li>
+ * <li>the node already has a parent</li>
+ * <li>a cycle in would be created</li>
+ * </ul>
+ */
+ public void setExpression(Expression expression) {
+ if (expression == null) {
+ throw new IllegalArgumentException();
+ }
+ ASTNode oldChild = this.expression;
+ preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
+ this.expression = expression;
+ postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
+ }
+
+ /**
+ * Returns the live ordered list of statements for this switch statement.
+ * Within this list, <code>SwitchCase</code> nodes mark the start of
+ * the switch groups.
+ *
+ * @return the live list of statement nodes
+ * (element type: {@link Statement})
+ */
+ public List statements() {
+ return this.statements;
+ }
+
+ @Override
+ int memSize() {
+ return BASE_NODE_SIZE + 3 * 4;
+ }
+
+ @Override
+ int treeSize() {
+ return
+ memSize()
+ + (this.expression == null ? 0 : getExpression().treeSize())
+ + this.statements.listSize();
+ }
+}
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 7f2c004e17..5dda5ce5bd 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
@@ -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
@@ -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
@@ -81,6 +85,14 @@ public class NaiveASTFlattener extends ASTVisitor {
* @since 3.14
*/
private static final int JLS9 = 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;
+
/**
* The string buffer into which the serialized representation of the AST is
@@ -1487,32 +1499,84 @@ public class NaiveASTFlattener extends ASTVisitor {
@Override
public boolean visit(SwitchCase node) {
- if (node.isDefault()) {
- this.buffer.append("default :\n");//$NON-NLS-1$
- } else {
- this.buffer.append("case ");//$NON-NLS-1$
- node.getExpression().accept(this);
- this.buffer.append(":\n");//$NON-NLS-1$
+ if (node.getAST().apiLevel() >= JLS12) {
+ if (!node.getExpression2().isEmpty()) {
+ for (Iterator it = node.getExpression2().iterator(); it.hasNext(); ) {
+ Expression t = (Expression) it.next();
+ if (node.isDefault()) {
+ if (node.isExpr()) {
+ 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.isExpr()) {
+ this.buffer.append("->\n");//$NON-NLS-1$
+ }
+ else {
+ this.buffer.append(":\n");//$NON-NLS-1$
+ }
+ }
+ }
+ }
+ }
+ }
+ else {
+ if (node.isDefault()) {
+ this.buffer.append("default :\n");//$NON-NLS-1$
+ } else {
+ this.buffer.append("case ");//$NON-NLS-1$
+ node.getExpression().accept(this);
+ this.buffer.append(":\n");//$NON-NLS-1$
+ }
}
this.indent++; //decremented in visit(SwitchStatement)
return false;
}
- @Override
- public boolean visit(SwitchStatement node) {
+ private void visitSwitchNode(ASTNode node) {
this.buffer.append("switch (");//$NON-NLS-1$
- node.getExpression().accept(this);
+ if (node instanceof SwitchExpression) {
+ ((SwitchExpression)node).getExpression().accept(this);
+ } else if (node instanceof SwitchStatement) {
+ ((SwitchStatement)node).getExpression().accept(this);
+ }
this.buffer.append(") ");//$NON-NLS-1$
this.buffer.append("{\n");//$NON-NLS-1$
this.indent++;
- for (Iterator it = node.statements().iterator(); it.hasNext(); ) {
- Statement s = (Statement) it.next();
- s.accept(this);
- this.indent--; // incremented in visit(SwitchCase)
+ if (node instanceof SwitchExpression) {
+ for (Iterator it = ((SwitchExpression)node).statements().iterator(); it.hasNext(); ) {
+ Statement s = (Statement) it.next();
+ s.accept(this);
+ this.indent--; // incremented in visit(SwitchCase)
+ }
+ } else if (node instanceof SwitchStatement) {
+ for (Iterator it = ((SwitchStatement)node).statements().iterator(); it.hasNext(); ) {
+ Statement s = (Statement) it.next();
+ s.accept(this);
+ this.indent--; // incremented in visit(SwitchCase)
+ }
}
this.indent--;
printIndent();
this.buffer.append("}\n");//$NON-NLS-1$
+
+ }
+ @Override
+ public boolean visit(SwitchExpression node) {
+ visitSwitchNode(node);
+ return false;
+ }
+
+ @Override
+ public boolean visit(SwitchStatement node) {
+ visitSwitchNode(node);
return false;
}
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java
index 8390c3a88b..4dde0c415e 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java
@@ -1,3 +1,4 @@
+
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java
index 7a7cac40fc..73e2344885 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java
@@ -3058,7 +3058,7 @@ public final class JavaCore extends Plugin {
public static final String VERSION_11 = "11"; //$NON-NLS-1$
/**
* Configurable option value: {@value}.
- * @since 3.17
+ * @since 3.17 BETA_JAVA_12
* @category OptionValue
*/
public static final String VERSION_12 = "12"; //$NON-NLS-1$

Back to the top