You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SwitchPatternPreview2OK.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Inspired by examples in https://openjdk.java.net/jeps/420
  3. */
  4. public class SwitchPatternPreview2OK {
  5. public static void main(String[] args) {
  6. // constantLabelMustAppearBeforePattern(-1);
  7. // constantLabelMustAppearBeforePattern(0);
  8. // constantLabelMustAppearBeforePattern(42);
  9. // constantLabelMustAppearBeforePattern(-99);
  10. // constantLabelMustAppearBeforePattern(Integer.valueOf(123));
  11. // constantLabelMustAppearBeforePattern(null);
  12. constantLabelMustAppearBeforePatternInteger(-1);
  13. constantLabelMustAppearBeforePatternInteger(0);
  14. constantLabelMustAppearBeforePatternInteger(42);
  15. constantLabelMustAppearBeforePatternInteger(-99);
  16. constantLabelMustAppearBeforePatternInteger(Integer.valueOf(123));
  17. constantLabelMustAppearBeforePatternInteger(null);
  18. // System.out.println(testGenericSealedExhaustive(new B<Integer>()));
  19. }
  20. /**
  21. * According to an example from JEP 420, this should work, but it does not, neither with Javac nor ECJ.
  22. *
  23. * See:
  24. * https://openjdk.java.net/jeps/420#1b--Dominance-of-pattern-labels
  25. * https://bugs.openjdk.java.net/browse/JDK-8273326
  26. * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579355
  27. *
  28. * TODO: reactivate when implemented or move to preview 3 with Java 19, Eclipse 4.24.
  29. */
  30. /*
  31. static String constantLabelMustAppearBeforePattern(Object o) {
  32. switch (o) {
  33. case null -> System.out.println("value unavailable: " + i);
  34. case -1, 1 -> System.out.println("special case:" + o);
  35. case Integer i && i > 0 -> System.out.println("positive integer: " + o);
  36. case Integer i -> System.out.println("other integer: " + o);
  37. default -> System.out.println("non-integer: " + o);
  38. }
  39. return i == null ? "null" : i.toString();
  40. }
  41. */
  42. static String constantLabelMustAppearBeforePatternInteger(Integer i) {
  43. switch (i) {
  44. case null -> System.out.println("value unavailable: " + i);
  45. case -1, 1 -> System.out.println("absolute value 1: " + i);
  46. case Integer value && value > 0 -> System.out.println("positive integer: " + i);
  47. default -> System.out.println("other integer: " + i);
  48. }
  49. return i == null ? "null" : i.toString();
  50. }
  51. static void nullCanAppearAfterConstantLabel(Integer i) {
  52. switch (i) {
  53. case -1, 1 -> System.out.println("absolute value 1: " + i);
  54. case null -> System.out.println("value unavailable: " + i);
  55. case Integer value && value > 0 -> System.out.println("positive integer: " + i);
  56. default -> System.out.println("other integer: " + i);
  57. }
  58. }
  59. static void defaultCanAppearBeforePattern(Integer i) {
  60. switch (i) {
  61. case null -> System.out.println("value unavailable: " + i);
  62. case -1, 1 -> System.out.println("absolute value 1: " + i);
  63. default -> System.out.println("other integer: " + i);
  64. case Integer value && value > 0 -> System.out.println("positive integer: " + i);
  65. }
  66. }
  67. static void defaultCanAppearBeforeNull(Integer i) {
  68. switch (i) {
  69. case -1, 1 -> System.out.println("absolute value 1: " + i);
  70. default -> System.out.println("other integer: " + i);
  71. case null -> System.out.println("value unavailable: " + i);
  72. case Integer value && value > 0 -> System.out.println("positive integer: " + i);
  73. }
  74. }
  75. static void defaultCanAppearBeforeConstantLabel(Integer i) {
  76. switch (i) {
  77. case null -> System.out.println("value unavailable: " + i);
  78. default -> System.out.println("other integer: " + i);
  79. case -1, 1 -> System.out.println("absolute value 1: " + i);
  80. case Integer value && value > 0 -> System.out.println("positive integer: " + i);
  81. }
  82. }
  83. /**
  84. * According to an example from JEP 420, this should work with preview 2 (Java 18), and it does with Javac,
  85. * but not with ECJ for Java 18 and 19.
  86. *
  87. * See:
  88. * https://openjdk.java.net/jeps/420#2--Exhaustiveness-of-switch-expressions-and-statements
  89. * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579360
  90. * https://github.com/eclipse-jdt/eclipse.jdt.core/issues/587
  91. */
  92. /*
  93. sealed interface I<T> permits A, B {}
  94. final static class A<X> implements I<String> {}
  95. final static class B<Y> implements I<Y> {}
  96. static int testGenericSealedExhaustive(I<Integer> i) {
  97. return switch (i) {
  98. // Exhaustive as no A case possible!
  99. case B<Integer> bi -> 42;
  100. };
  101. }
  102. */
  103. }