From 50190a1918e66769e0037542054645add1ff8204 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Thu, 24 Mar 2022 15:16:29 +0700 Subject: Upgrade to latest JDT Core Java 18 changes, activate J18 preview tests Signed-off-by: Alexander Kriegisch --- .../java18/SwitchPatternPreview2Aspect.aj | 30 ++++--- .../java18/SwitchPatternPreview2Error.java | 31 ------- .../java18/SwitchPatternPreview2Error1.java | 13 +++ .../java18/SwitchPatternPreview2Error2.java | 21 +++++ .../java18/SwitchPatternPreview2OK.java | 95 +++++++++++++++++++--- .../ajc199/Java18PreviewFeaturesTests.java | 20 ++--- .../org/aspectj/systemtest/ajc199/ajc199.xml | 33 +++++++- 7 files changed, 170 insertions(+), 73 deletions(-) delete mode 100644 tests/features199/java18/SwitchPatternPreview2Error.java create mode 100644 tests/features199/java18/SwitchPatternPreview2Error1.java create mode 100644 tests/features199/java18/SwitchPatternPreview2Error2.java (limited to 'tests') diff --git a/tests/features199/java18/SwitchPatternPreview2Aspect.aj b/tests/features199/java18/SwitchPatternPreview2Aspect.aj index ee5dbbd68..4170b4d15 100644 --- a/tests/features199/java18/SwitchPatternPreview2Aspect.aj +++ b/tests/features199/java18/SwitchPatternPreview2Aspect.aj @@ -1,28 +1,26 @@ -import java.util.Locale; - aspect SwitchPatternPreview2Aspect { - Object around(Object o): execution(* doSomethingWithObject(*)) && args(o) { + Object around(Integer i): execution(* doSomethingWithInteger(*)) && args(i) { System.out.println( - switch (o) { - case -1, 1 -> "special case:" + o; - case Integer i && i > 0 -> "positive integer: " + o; - case Integer i -> "other integer: " + o; - default -> "non-integer: " + o; + switch (i) { + case null -> "value unavailable: " + i; + case -1, 1 -> "absolute value 1: " + i; + case Integer value && value > 0 -> "positive integer: " + i; + default -> "other integer: " + i; } ); - return proceed(o); + return proceed(i); } } class Application { public static void main(String[] args) { - doSomethingWithObject(-1); - doSomethingWithObject(0); - doSomethingWithObject(42); - doSomethingWithObject(-99); - doSomethingWithObject("test"); - doSomethingWithObject(null); + doSomethingWithInteger(-1); + doSomethingWithInteger(0); + doSomethingWithInteger(42); + doSomethingWithInteger(-99); + doSomethingWithInteger(Integer.valueOf(123)); + doSomethingWithInteger(null); } - public static Object doSomethingWithObject(Object o) { return o; } + public static Object doSomethingWithInteger(Integer o) { return o; } } diff --git a/tests/features199/java18/SwitchPatternPreview2Error.java b/tests/features199/java18/SwitchPatternPreview2Error.java deleted file mode 100644 index b1652aad3..000000000 --- a/tests/features199/java18/SwitchPatternPreview2Error.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Inspired by examples in https://openjdk.java.net/jeps/420 - */ -public class SwitchPatternPreview2Error { - static void constantLabelMustAppearBeforePattern1(Object o) { - switch (o) { - case Integer i && i > 0 -> System.out.println("positive integer: " + o); - case -1, 1 -> System.out.println("special case:" + o); - case Integer i -> System.out.println("other integer: " + o); - default -> System.out.println("non-integer: " + o); - } - } - - static void constantLabelMustAppearBeforePattern2(Object o) { - switch (o) { - case -1, 1 -> System.out.println("special case:" + o); - case Integer i -> System.out.println("other integer: " + o); - case Integer i && i > 0 -> System.out.println("positive integer: " + o); - default -> System.out.println("non-integer: " + o); - } - } - - static void constantLabelMustAppearBeforePattern3(Object o) { - switch (o) { - case Integer i && i > 0 -> System.out.println("positive integer: " + o); - case Integer i -> System.out.println("other integer: " + o); - case -1, 1 -> System.out.println("special case:" + o); - default -> System.out.println("non-integer: " + o); - } - } -} diff --git a/tests/features199/java18/SwitchPatternPreview2Error1.java b/tests/features199/java18/SwitchPatternPreview2Error1.java new file mode 100644 index 000000000..342b848aa --- /dev/null +++ b/tests/features199/java18/SwitchPatternPreview2Error1.java @@ -0,0 +1,13 @@ +/** + * Inspired by examples in https://openjdk.java.net/jeps/420 + */ +public class SwitchPatternPreview2Error1 { + static void constantLabelsMustAppearBeforePatterns1(Integer i) { + switch (i) { + case null -> System.out.println("value unavailable: " + i); + case Integer value && value > 0 -> System.out.println("positive integer: " + i); + case -1, 1 -> System.out.println("absolute value 1: " + i); + default -> System.out.println("other integer: " + i); + } + } +} diff --git a/tests/features199/java18/SwitchPatternPreview2Error2.java b/tests/features199/java18/SwitchPatternPreview2Error2.java new file mode 100644 index 000000000..3cee435e0 --- /dev/null +++ b/tests/features199/java18/SwitchPatternPreview2Error2.java @@ -0,0 +1,21 @@ +/** + * Inspired by examples in https://openjdk.java.net/jeps/420 + */ +public class SwitchPatternPreview2Error2 { + static void constantLabelsMustAppearBeforePatterns2(Object o) { + switch (o) { + case null -> System.out.println("value unavailable: " + o); + // This seems to be a bug in JEP 420 implementation. Those constants should be compatible with 'Object'. + // case -1, 1 -> System.out.println("absolute value 1: " + o); + // case "hello" -> System.out.println("string value: " + o); + + // 'Integer value' dominates the next two, more specific ones -> error + case Integer value -> System.out.println("other integer: " + o); + case Integer value && (value == 1 || value == -1) -> System.out.println("absolute value 1: " + o); + case Integer value && value > 0 -> System.out.println("positive integer: " + o); + + case String value && value.startsWith("hello") -> System.out.println("greeting: " + o); + default -> System.out.println("other type: " + o); + } + } +} diff --git a/tests/features199/java18/SwitchPatternPreview2OK.java b/tests/features199/java18/SwitchPatternPreview2OK.java index 859583b6d..b36b26bf2 100644 --- a/tests/features199/java18/SwitchPatternPreview2OK.java +++ b/tests/features199/java18/SwitchPatternPreview2OK.java @@ -1,30 +1,104 @@ -import java.util.List; - /** * Inspired by examples in https://openjdk.java.net/jeps/420 */ public class SwitchPatternPreview2OK { public static void main(String[] args) { - constantLabelMustAppearBeforePattern(-1); - constantLabelMustAppearBeforePattern(0); - constantLabelMustAppearBeforePattern(42); - constantLabelMustAppearBeforePattern(-99); - constantLabelMustAppearBeforePattern(Integer.valueOf(123)); - constantLabelMustAppearBeforePattern(null); +// constantLabelMustAppearBeforePattern(-1); +// constantLabelMustAppearBeforePattern(0); +// constantLabelMustAppearBeforePattern(42); +// constantLabelMustAppearBeforePattern(-99); +// constantLabelMustAppearBeforePattern(Integer.valueOf(123)); +// constantLabelMustAppearBeforePattern(null); + + constantLabelMustAppearBeforePatternInteger(-1); + constantLabelMustAppearBeforePatternInteger(0); + constantLabelMustAppearBeforePatternInteger(42); + constantLabelMustAppearBeforePatternInteger(-99); + constantLabelMustAppearBeforePatternInteger(Integer.valueOf(123)); + constantLabelMustAppearBeforePatternInteger(null); - System.out.println(testGenericSealedExhaustive(new B())); +// System.out.println(testGenericSealedExhaustive(new B())); } + /** + * According to an example from JEP 420, this should work, but it does not, neither with Javac nor ECJ. + * + * See: + * https://openjdk.java.net/jeps/420#1b--Dominance-of-pattern-labels + * https://bugs.openjdk.java.net/browse/JDK-8273326 + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579355 + * + * TODO: reactivate when implemented or move to preview 3 with Java 19, Eclipse 4.24. + */ + /* static String constantLabelMustAppearBeforePattern(Object o) { switch (o) { + case null -> System.out.println("value unavailable: " + i); case -1, 1 -> System.out.println("special case:" + o); case Integer i && i > 0 -> System.out.println("positive integer: " + o); case Integer i -> System.out.println("other integer: " + o); default -> System.out.println("non-integer: " + o); } - return o.toString(); + return i == null ? "null" : i.toString(); + } + */ + + static String constantLabelMustAppearBeforePatternInteger(Integer i) { + switch (i) { + case null -> System.out.println("value unavailable: " + i); + case -1, 1 -> System.out.println("absolute value 1: " + i); + case Integer value && value > 0 -> System.out.println("positive integer: " + i); + default -> System.out.println("other integer: " + i); + } + return i == null ? "null" : i.toString(); + } + + static void nullCanAppearAfterConstantLabel(Integer i) { + switch (i) { + case -1, 1 -> System.out.println("absolute value 1: " + i); + case null -> System.out.println("value unavailable: " + i); + case Integer value && value > 0 -> System.out.println("positive integer: " + i); + default -> System.out.println("other integer: " + i); + } + } + + static void defaultCanAppearBeforePattern(Integer i) { + switch (i) { + case null -> System.out.println("value unavailable: " + i); + case -1, 1 -> System.out.println("absolute value 1: " + i); + default -> System.out.println("other integer: " + i); + case Integer value && value > 0 -> System.out.println("positive integer: " + i); + } + } + + static void defaultCanAppearBeforeNull(Integer i) { + switch (i) { + case -1, 1 -> System.out.println("absolute value 1: " + i); + default -> System.out.println("other integer: " + i); + case null -> System.out.println("value unavailable: " + i); + case Integer value && value > 0 -> System.out.println("positive integer: " + i); + } + } + + static void defaultCanAppearBeforeConstantLabel(Integer i) { + switch (i) { + case null -> System.out.println("value unavailable: " + i); + default -> System.out.println("other integer: " + i); + case -1, 1 -> System.out.println("absolute value 1: " + i); + case Integer value && value > 0 -> System.out.println("positive integer: " + i); + } } + /** + * According to an example from JEP 420, this should work, and it does with Javac, but not with ECJ. + * + * See: + * https://openjdk.java.net/jeps/420#2--Exhaustiveness-of-switch-expressions-and-statements + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579360 + * + * TODO: reactivate when implemented or move to preview 3 with Java 19, Eclipse 4.24. + */ + /* sealed interface I permits A, B {} final static class A implements I {} final static class B implements I {} @@ -35,4 +109,5 @@ public class SwitchPatternPreview2OK { case B bi -> 42; }; } + */ } diff --git a/tests/src/test/java/org/aspectj/systemtest/ajc199/Java18PreviewFeaturesTests.java b/tests/src/test/java/org/aspectj/systemtest/ajc199/Java18PreviewFeaturesTests.java index 8d586b440..dbe6f733a 100644 --- a/tests/src/test/java/org/aspectj/systemtest/ajc199/Java18PreviewFeaturesTests.java +++ b/tests/src/test/java/org/aspectj/systemtest/ajc199/Java18PreviewFeaturesTests.java @@ -34,28 +34,24 @@ public class Java18PreviewFeaturesTests extends XMLBasedAjcTestCaseForJava18Only checkVersion("S", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); } - // TODO: - // JDT Core does not seem to have implemented JEP 420 yet, - // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=576746. - // Therefore, deactivate the special tests for now. -/* - public void testSwitchPatternMatchingPreview2Error() { - runTest("switch pattern matching preview 2 error"); + public void testSwitchPatternMatchingPreview2Error1() { + runTest("switch pattern matching preview 2 error 1"); + } + + public void testSwitchPatternMatchingPreview2Error2() { + runTest("switch pattern matching preview 2 error 2"); } public void testSwitchPatternMatchingPreview2Java() { runTest("switch pattern matching preview 2 java"); - checkVersion("SwitchPatternOK", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); + checkVersion("SwitchPatternPreview2OK", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); } public void testSwitchPatternMatchingPreview2Aspect() { runTest("switch pattern matching preview 2 aspect"); - checkVersion("SwitchPatternAspect", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); + checkVersion("SwitchPatternPreview2Aspect", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); checkVersion("Application", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); - checkVersion("Shape", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); - checkVersion("S", Constants.MAJOR_18, Constants.PREVIEW_MINOR_VERSION); } -*/ public static Test suite() { return XMLBasedAjcTestCase.loadSuite(Java18PreviewFeaturesTests.class); diff --git a/tests/src/test/resources/org/aspectj/systemtest/ajc199/ajc199.xml b/tests/src/test/resources/org/aspectj/systemtest/ajc199/ajc199.xml index f0510e79a..7baeea086 100644 --- a/tests/src/test/resources/org/aspectj/systemtest/ajc199/ajc199.xml +++ b/tests/src/test/resources/org/aspectj/systemtest/ajc199/ajc199.xml @@ -58,18 +58,34 @@ - - + + - + + + + + + + + + - + + + + + + + + + @@ -91,6 +108,13 @@ + + + + + + + -- cgit v1.2.3