diff options
author | Jay Arthanareeswaran | 2019-01-29 10:18:43 +0000 |
---|---|---|
committer | Jay Arthanareeswaran | 2019-01-29 10:19:12 +0000 |
commit | c7ec8a255fd31106dd76f769daecdf116a3095c0 (patch) | |
tree | dbbc462e6fa739cb626c9f6579dd9ba9c630aff4 | |
parent | 60010dfa88a963a8b16209676ac7277e5606027e (diff) | |
download | eclipse.jdt.core-c7ec8a255fd31106dd76f769daecdf116a3095c0.tar.gz eclipse.jdt.core-c7ec8a255fd31106dd76f769daecdf116a3095c0.tar.xz eclipse.jdt.core-c7ec8a255fd31106dd76f769daecdf116a3095c0.zip |
Bug 542560 - [12][completion] Code completion Support for Switch
Expressions
Change-Id: Id0101c52abbeb80bb04e32daaacbb532f9192102
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
7 files changed, 770 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.model/JCL/jclMin12.jar b/org.eclipse.jdt.core.tests.model/JCL/jclMin12.jar Binary files differnew file mode 100644 index 0000000000..6f2af93c90 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/JCL/jclMin12.jar diff --git a/org.eclipse.jdt.core.tests.model/JCL/jclMin12src.zip b/org.eclipse.jdt.core.tests.model/JCL/jclMin12src.zip Binary files differnew file mode 100644 index 0000000000..37447f944f --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/JCL/jclMin12src.zip 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 7724561164..face2acc8e 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 @@ -3112,7 +3112,10 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases { newJclLibString = "JCL18_FULL"; newJclSrcString = "JCL18_SRC"; // Use the same source } else { - if (compliance.equals("11")) { + if (compliance.equals("12")) { + newJclLibString = "JCL12_LIB"; + newJclSrcString = "JCL12_SRC"; + } else if (compliance.equals("11")) { newJclLibString = "JCL11_LIB"; newJclSrcString = "JCL11_SRC"; } else if (compliance.equals("10")) { @@ -3169,10 +3172,11 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases { IPath jcl9Lib = new Path("JCL19_LIB"); IPath jcl10Lib = new Path("JCL10_LIB"); IPath jcl11Lib = new Path("JCL11_LIB"); + IPath jcl12Lib = new Path("JCL12_LIB"); IPath jclFull = new Path("JCL18_FULL"); return path.equals(jclLib) || path.equals(jcl5Lib) || path.equals(jcl8Lib) || path.equals(jcl9Lib) - || path.equals(jcl10Lib) || path.equals(jcl11Lib) || path.equals(jclFull); + || path.equals(jcl10Lib) || path.equals(jcl11Lib) || path.equals(jcl12Lib) || path.equals(jclFull); } public void setUpJCLClasspathVariables(String compliance) throws JavaModelException, IOException { setUpJCLClasspathVariables(compliance, false); @@ -3235,6 +3239,14 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases { new IPath[] {getExternalJCLPath("11"), getExternalJCLSourcePath("11"), getExternalJCLRootSourcePath()}, null); } + } else if ("12".equals(compliance)) { + if (JavaCore.getClasspathVariable("JCL12_LIB") == null) { + setupExternalJCL("jclMin12"); + JavaCore.setClasspathVariables( + new String[] {"JCL12_LIB", "JCL12_SRC", "JCL_SRCROOT"}, + new IPath[] {getExternalJCLPath("12"), getExternalJCLSourcePath("12"), getExternalJCLRootSourcePath()}, + null); + } } else { if (JavaCore.getClasspathVariable("JCL_LIB") == null) { setupExternalJCL("jclMin"); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests12.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests12.java new file mode 100644 index 0000000000..f30f27672d --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests12.java @@ -0,0 +1,716 @@ +/******************************************************************************* + * Copyright (c) 2019 IBM 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 + * + *******************************************************************************/ +package org.eclipse.jdt.core.tests.model; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.internal.codeassist.RelevanceConstants; + +import junit.framework.Test; + +public class CompletionTests12 extends AbstractJavaModelCompletionTests { + + static { +// TESTS_NAMES = new String[]{"test018e"}; + } + + public CompletionTests12(String name) { + super(name); + } + public void setUpSuite() throws Exception { + if (COMPLETION_PROJECT == null) { + COMPLETION_PROJECT = setUpJavaProject("Completion", "12"); + } else { + setUpProjectCompliance(COMPLETION_PROJECT, "12"); + } + super.setUpSuite(); + } + public static Test suite() { + return buildModelTestSuite(CompletionTests12.class); + } + public void test001() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (day) {\n" + + " case SATURDAY, SUN ->\n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "SATURDAY, SUN"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "SUNDAY[FIELD_REF]{SUNDAY, LMyDay;, LMyDay;, SUNDAY, null, "+ + (RelevanceConstants.R_DEFAULT + RelevanceConstants.R_RESOLVED + RelevanceConstants.R_INTERESTING+ + RelevanceConstants.R_CASE + RelevanceConstants.R_EXACT_EXPECTED_TYPE + + RelevanceConstants.R_ENUM + RelevanceConstants.R_ENUM_CONSTANT + + RelevanceConstants.R_UNQUALIFIED + RelevanceConstants.R_NON_RESTRICTED)+ "}", + requestor.getResults()); + } + public void test002() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (day) {\n" + + " case SATURDAY, SUN :\n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "SATURDAY, SUN"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "SUNDAY[FIELD_REF]{SUNDAY, LMyDay;, LMyDay;, SUNDAY, null, "+ + (RelevanceConstants.R_DEFAULT + RelevanceConstants.R_RESOLVED + RelevanceConstants.R_INTERESTING+ + RelevanceConstants.R_CASE + RelevanceConstants.R_EXACT_EXPECTED_TYPE + + RelevanceConstants.R_ENUM + RelevanceConstants.R_ENUM_CONSTANT + + RelevanceConstants.R_UNQUALIFIED + RelevanceConstants.R_NON_RESTRICTED)+ "}", + requestor.getResults()); + } + public void test003() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (day) {\n" + + " case SATU -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "SATU"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "SATURDAY[FIELD_REF]{SATURDAY, LMyDay;, LMyDay;, SATURDAY, null, "+ + (RelevanceConstants.R_DEFAULT + RelevanceConstants.R_RESOLVED + RelevanceConstants.R_INTERESTING+ + RelevanceConstants.R_CASE + RelevanceConstants.R_EXACT_EXPECTED_TYPE + + RelevanceConstants.R_ENUM + RelevanceConstants.R_ENUM_CONSTANT + + RelevanceConstants.R_UNQUALIFIED + RelevanceConstants.R_NON_RESTRICTED)+ "}", + requestor.getResults()); + } + public void test004() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (day) {\n" + + " case SATURDAY, SUNDAY, MOND -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY, MONDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "MOND"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "MONDAY[FIELD_REF]{MONDAY, LMyDay;, LMyDay;, MONDAY, null, "+ + (RelevanceConstants.R_DEFAULT + RelevanceConstants.R_RESOLVED + RelevanceConstants.R_INTERESTING+ + RelevanceConstants.R_CASE + RelevanceConstants.R_EXACT_EXPECTED_TYPE + + RelevanceConstants.R_ENUM + RelevanceConstants.R_ENUM_CONSTANT + + RelevanceConstants.R_UNQUALIFIED + RelevanceConstants.R_NON_RESTRICTED)+ "}", + requestor.getResults()); + } + public void test005() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (day) {\n" + + " case SATURDAY, SUND, MONDAY -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY, MONDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "SUND"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "SUNDAY[FIELD_REF]{SUNDAY, LMyDay;, LMyDay;, SUNDAY, null, "+ + (RelevanceConstants.R_DEFAULT + RelevanceConstants.R_RESOLVED + RelevanceConstants.R_INTERESTING+ + RelevanceConstants.R_CASE + RelevanceConstants.R_EXACT_EXPECTED_TYPE + + RelevanceConstants.R_ENUM + RelevanceConstants.R_ENUM_CONSTANT + + RelevanceConstants.R_UNQUALIFIED + RelevanceConstants.R_NON_RESTRICTED)+ "}", + requestor.getResults()); + } + public void test006() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (day.toS) {\n" + + " case \"SATURDAY\" -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "day.toS"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "toString[METHOD_REF]{toString(), Ljava.lang.Object;, ()Ljava.lang.String;, toString, null, 60}", + requestor.getResults()); + } + public void test007() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (day.o) {\n" + + " case 0 -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "day.o"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "ordinal[METHOD_REF]{ordinal(), Ljava.lang.Enum<LMyDay;>;, ()I, ordinal, null, 60}", + requestor.getResults()); + } + public void test008() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (1 + day.o) {\n" + + " case 0 -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "day.o"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "ordinal[METHOD_REF]{ordinal(), Ljava.lang.Enum<LMyDay;>;, ()I, ordinal, null, 90}", + requestor.getResults()); + } + public void test009() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (1 + da) {\n" + + " case 0 -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "1 + da"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "day[LOCAL_VARIABLE_REF]{day, null, LMyDay;, day, null, 52}", + requestor.getResults()); + } + public void test010() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(MyDay day) {\n" + + " switch (1 + da + 1) {\n" + + " case 0 -> \n" + + " System.out.println(day.toString());\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n" + + "enum MyDay { SATURDAY, SUNDAY}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "1 + da"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "day[LOCAL_VARIABLE_REF]{day, null, LMyDay;, day, null, 52}", + requestor.getResults()); + } + public void test011() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(int arg0 day) {\n" + + " foo(\n" + + " switch (arg) {\n" + + " case 1 -> 1;\n" + + " default -> 0;\n" + + " }\n" + + " });\n" + + " public static void foo(int arg0) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "switch (arg"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", + requestor.getResults()); + } + public void test012() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(int arg0 day) {\n" + + " foo(\n" + + " switch (arg0) {\n" + + " case 1 -> arg;\n" + + " default -> 0;\n" + + " }\n" + + " });\n" + + " public static void foo(int arg0) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "-> arg"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", + requestor.getResults()); + } + public void test013() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/Switch.java", + "public class Switch {\n" + + " public static void bar(int arg0 day) {\n" + + " foo(\n" + + " switch (0 + arg) {\n" + + " case 1 -> 1;\n" + + " default -> 0;\n" + + " }\n" + + " });\n" + + " public static void foo(int arg0) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "0 + arg"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 82}", + requestor.getResults()); + } + public void test014() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " public static void bar(int arg0 day) {\n" + + " foo(\n" + + " swi);\n" + + " public static void foo(int arg0) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "swi"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "switch[KEYWORD]{switch, null, null, switch, null, 49}", + requestor.getResults()); + } + public void test015() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class Switch {\n" + + " public static void bar(int arg0 day) {\n" + + " foo(\n" + + " switch (0 + arg0) {\n" + + " case 1 -> {break ar;}\n" + + " default -> 0;\n" + + " }\n" + + " });\n" + + " public static void foo(int arg0) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "break ar"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", + requestor.getResults()); + } + public void test016() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class Switch {\n" + + " public static void bar(int arg0 day) {\n" + + " foo(\n" + + " switch (0 + arg0) {\n" + + " case 1 -> {break 1;}\n" + + " default -> ar;\n" + + " }\n" + + " });\n" + + " public static void foo(int arg0) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "-> ar"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", + requestor.getResults()); + } + public void test017() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class Switch {\n" + + " public static void bar(int arg0 day) {\n" + + " foo(\n" + + " switch (0 + arg0) {\n" + + " case 1 -> 1;\n" + + " default -> {break ar;}\n" + + " }\n" + + " });\n" + + " public static void foo(int arg0) {\n" + + " bar(MyDay.SUNDAY);\n" + + " }\n" + + "}\n"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "break ar"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", + requestor.getResults()); + } + public void test018a() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "import org.eclipse.jdt.annotation.*;\n" + + "import java.util.function.*;\n" + + "interface IN0 {} \n" + + "interface IN1 extends IN0 {} \n" + + "interface IN2 extends IN0 {}\n" + + "public class X {\n" + + " @NonNull IN1 n_1() { return new IN1() {}; } \n" + + " IN2 n_2() { return null; } \n" + + " <M> void m(@NonNull Supplier<@NonNull M> m2) { } \n" + + " void testSw(int i) { \n" + + " m(switch(i) { \n" + + " case 1 -> this::n_; \n" + + " case 2 -> () -> n1(); \n" + + " case 3 -> null; \n" + + " case 4 -> () -> n2(); \n" + + " default -> this::n2; }); \n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "this::n_"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "n_1[METHOD_NAME_REFERENCE]{n_1, LX;, ()LIN1;, n_1, null, 60}\n" + + "n_2[METHOD_NAME_REFERENCE]{n_2, LX;, ()LIN2;, n_2, null, 60}", + requestor.getResults()); + } + public void test018b() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "import org.eclipse.jdt.annotation.*;\n" + + "interface IN0 {} \n" + + "interface IN1 extends IN0 {} \n" + + "interface IN2 extends IN0 {}\n" + + "public class X {\n" + + " @NonNull IN1 n_1() { return new IN1() {}; } \n" + + " IN2 n_2() { return null; } \n" + + " <M> void m(@NonNull Supplier<@NonNull M> m2) { } \n" + + " void testSw(int i) { \n" + + " m(switch(i) { \n" + + " case 2 -> () -> n_; \n" + + " }\n" + + "}\n" + + "interface Supplier<T> {\n" + + " T get();\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "-> n_"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "n_1[METHOD_REF]{n_1(), LX;, ()LIN1;, n_1, null, 52}\n" + + "n_2[METHOD_REF]{n_2(), LX;, ()LIN2;, n_2, null, 52}", + requestor.getResults()); + } + public void test018c() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "import org.eclipse.jdt.annotation.*;\n" + + "interface IN0 {} \n" + + "interface IN1 extends IN0 {} \n" + + "interface IN2 extends IN0 {}\n" + + "public class X {\n" + + " @NonNull IN1 n_1() { return new IN1() {}; } \n" + + " IN2 n_2() { return null; } \n" + + " <M> void m(@NonNull Supplier<@NonNull M> m2) { } \n" + + " void testSw(int i) { \n" + + " m(switch(i) { \n" + + " default -> this::n_; }); \n" + + " }\n" + + "}\n" + + "interface Supplier<T> {\n" + + " T get();\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "this::n_"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "n_1[METHOD_NAME_REFERENCE]{n_1, LX;, ()LIN1;, n_1, null, 60}\n" + + "n_2[METHOD_NAME_REFERENCE]{n_2, LX;, ()LIN2;, n_2, null, 60}", + requestor.getResults()); + } + public void test018d() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "import org.eclipse.jdt.annotation.*;\n" + + "interface IN0 {} \n" + + "interface IN1 extends IN0 {} \n" + + "interface IN2 extends IN0 {}\n" + + "public class X {\n" + + " @NonNull IN1 n_1() { return new IN1() {}; } \n" + + " IN2 n_2() { return null; } \n" + + " <M> void m(@NonNull Supplier<@NonNull M> m2) { } \n" + + " void testSw(int i) { \n" + + " m(switch(i) { \n" + + " default -> () -> n_; }); \n" + + " }\n" + + "}\n" + + "interface Supplier<T> {\n" + + " T get();\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "() -> n_"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "n_1[METHOD_REF]{n_1(), LX;, ()LIN1;, n_1, null, 52}\n" + + "n_2[METHOD_REF]{n_2(), LX;, ()LIN2;, n_2, null, 52}", + requestor.getResults()); + } + public void test018e() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "import org.eclipse.jdt.annotation.*;\n" + + "interface IN0 {} \n" + + "interface IN1 extends IN0 {} \n" + + "interface IN2 extends IN0 {}\n" + + "public class X {\n" + + " @NonNull IN1 n_1() { return new IN1() {}; } \n" + + " IN2 n_2() { return null; } \n" + + " <M> void m(@NonNull Supplier<@NonNull M> m2) { } \n" + + " void testSw(int i) { \n" + + " m(switch(i) { \n" + + " case 1 -> this::n_1; \n" + + " case 2 -> () -> n_; \n" + + " }\n" + + "}\n" + + "interface Supplier<T> {\n" + + " T get();\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "() -> n_"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "n_1[LOCAL_VARIABLE_REF]{n_1, null, Ljava.lang.Object;, n_1, null, 51}\n" + + "n_1[METHOD_REF]{n_1(), LX;, ()LIN1;, n_1, null, 52}\n" + + "n_2[METHOD_REF]{n_2(), LX;, ()LIN2;, n_2, null, 52}", + requestor.getResults()); + } + public void test018f() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "import org.eclipse.jdt.annotation.*;\n" + + "interface IN0 {} \n" + + "interface IN1 extends IN0 {} \n" + + "interface IN2 extends IN0 {}\n" + + "public class X {\n" + + " @NonNull IN1 n_1() { return new IN1() {}; } \n" + + " IN2 n_2() { return null; } \n" + + " <M> void m(@NonNull Supplier<@NonNull M> m2) { } \n" + + " void testSw(int i) { \n" + + " m(switch(i) { \n" + + " case 1 -> () -> n_1; \n" + + " case 2 -> this::n_; \n" + + " }\n" + + "}\n" + + "interface Supplier<T> {\n" + + " T get();\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "this::n_"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "n_1[METHOD_NAME_REFERENCE]{n_1, LX;, ()LIN1;, n_1, null, 60}\n" + + "n_2[METHOD_NAME_REFERENCE]{n_2, LX;, ()LIN2;, n_2, null, 60}", + requestor.getResults()); + } + public void test019() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "import org.eclipse.jdt.annotation.*;\n" + + " <M> void m(@NonNull Supplier<@NonNull M> m2) { } \n" + + "public class X {\n" + + " void testSw(int i) { \n" + + " m(swi);\n" + + "}\n" + + "interface Supplier<T> {\n" + + " T get();\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "swi"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "switch[KEYWORD]{switch, null, null, switch, null, 49}", + requestor.getResults()); + } +} diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java index 0aa6f16a4b..c1562f0019 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java @@ -6,6 +6,10 @@ * 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 * * Contributors: @@ -2512,6 +2516,14 @@ protected void consumeCompilationUnit() { super.consumeCompilationUnit(); } @Override +protected void consumeSwitchExpression() { + super.consumeSwitchExpression(); + if (this.assistNode != null) { + SwitchExpression expr = (SwitchExpression) this.astStack[0]; + expr.resolveAll = true; + } +} +@Override protected void consumeConditionalExpression(int op) { popElement(K_CONDITIONAL_OPERATOR); super.consumeConditionalExpression(op); @@ -4268,6 +4280,16 @@ protected void consumeToken(int token) { pushOnElementStack(K_CONDITIONAL_OPERATOR, QUESTION); } break; + case TokenNameARROW: + switch (topKnownElementKind(COMPLETION_OR_ASSIST_PARSER)) { + case K_BETWEEN_CASE_AND_COLON: + popElement(K_BETWEEN_CASE_AND_COLON); + break; + case K_BETWEEN_DEFAULT_AND_COLON: + popElement(K_BETWEEN_DEFAULT_AND_COLON); + break; + } + break; case TokenNameCOLON: switch (topKnownElementKind(COMPLETION_OR_ASSIST_PARSER)) { case K_CONDITIONAL_OPERATOR: @@ -4314,6 +4336,16 @@ protected void consumeToken(int token) { case TokenNamecase : pushOnElementStack(K_BETWEEN_CASE_AND_COLON); break; + case TokenNameCOMMA : + switch (topKnownElementKind(COMPLETION_OR_ASSIST_PARSER)) { + // for multi constant case stmt + // case MONDAY, FRI + // if there's a comma, ignore the previous expression (constant) + // Which doesn't matter for completing the next constant + case K_BETWEEN_CASE_AND_COLON: + this.expressionPtr--; + } + break; case TokenNamedefault : pushOnElementStack(K_BETWEEN_DEFAULT_AND_COLON); break; @@ -4885,6 +4917,9 @@ public NameReference createSingleAssistNameReference(char[] assistName, long pos if (kind == K_LOCAL_INITIALIZER_DELIMITER && this.options.complianceLevel >= ClassFileConstants.JDK11) { keywords[count++]= Keywords.VAR; } + if (kind == K_SELECTOR_QUALIFIER && this.options.complianceLevel >= ClassFileConstants.JDK12) { + keywords[count++] = Keywords.SWITCH; + } keywords[count++]= Keywords.TRUE; keywords[count++]= Keywords.FALSE; keywords[count++]= Keywords.NULL; diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java index 88e75f2a52..0b9830cb55 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java @@ -54,6 +54,7 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression private int nullStatus = FlowInfo.UNKNOWN; public List<Expression> resultExpressions; + public boolean resolveAll; /* package */ List<Integer> resultExpressionNullStatus; private static Map<TypeBinding, TypeBinding[]> type_map; @@ -389,6 +390,10 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression this.finalValueResultExpressionTypes[i] = this.originalValueResultExpressionTypes[i] = resultExpr.resolveTypeExpecting(upperScope, this.expectedType); } + // This is a kludge and only way completion can tell this node to resolve all + // resultExpressions. Ideal solution is to remove all other expressions except + // the one that contain the completion node. + if (this.resolveAll) continue; if (resultExpr.resolvedType == null || !resultExpr.resolvedType.isValidBinding()) return this.resolvedType = null; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java index 18ff40132c..02200bb2cc 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java @@ -9527,10 +9527,8 @@ protected void consumeSwitchExpression() { SwitchExpression s = (SwitchExpression) this.astStack[this.astPtr--]; if (!this.parsingJava12Plus) { -// problemReporter().switchExpressionsNotBelow12(s); problemReporter().previewFeatureNotSupported(s.sourceStart, s.sourceEnd, "Switch Expression", CompilerOptions.VERSION_12); //$NON-NLS-1$ } else if (!this.options.enablePreviewFeatures) { - //problemReporter().switchExpressionIsPreview(s); problemReporter().previewFeatureNotEnabled(s.sourceStart, s.sourceEnd, "Switch Expression"); //$NON-NLS-1$ } else { if (this.options.isAnyEnabled(IrritantSet.PREVIEW)) { |