1
1
/*
2
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
/**
25
25
* @test
26
- * @bug 8291769 8301858 8304694
26
+ * @bug 8291769 8301858 8304694 8304883
27
27
* @summary Verify more complex switches work properly
28
28
* @compile --enable-preview -source ${jdk.version} DeconstructionDesugaring.java
29
29
* @run main/othervm --enable-preview DeconstructionDesugaring
30
30
*/
31
31
32
+ import java .util .Objects ;
32
33
import java .util .function .ToIntFunction ;
33
34
public class DeconstructionDesugaring {
34
35
@@ -45,8 +46,21 @@ private void test() {
45
46
assertEquals (runCheckExpressionWithUnconditional1 (new R5 (null )), 3 );
46
47
assertEquals (runCheckExpressionWithUnconditionalAndParams (new R1 (42 )), 1 );
47
48
assertEquals (runCheckExpressionWithUnconditionalAndParams (new R1 (new Object ())), 2 );
48
- assertEquals (runFallThrough (new R6 (1 , 1 )), 0 );
49
- assertEquals (runFallThrough (new R6 (0 , 0 )), 1 );
49
+ assertEquals (switchNullable1 (new R6 (0 , 0 )), "int: 0, int: 0" );
50
+ assertEquals (switchNullable1 (new R6 (0L , 0 )), "obj: 0, obj: 0" );
51
+ assertEquals (switchNullable2 (new R6 (0 , 0 )), "int: 0, int: 0" );
52
+ assertEquals (switchNullable2 (new R6 (0L , 0 )), "obj: 0, int: 0" );
53
+ assertEquals (switchNullable2 (new R6 (0 , 0L )), "int: 0, obj: 0" );
54
+ assertEquals (switchNullable2 (new R6 (0L , 0L )), "obj: 0, obj: 0" );
55
+ assertEquals (switchNullableNPE (new R6 (1 , 1 )), "obj: 1, obj: 1" );
56
+ try {
57
+ switchNullableNPE (new R6 (null , 1 ));
58
+ throw new AssertionError ("Expected NPE, but got none." );
59
+ } catch (NullPointerException ex ) {
60
+ //expected.
61
+ }
62
+ assertEquals (runFallThrough (new R7 (1 , 1 )), 0 );
63
+ assertEquals (runFallThrough (new R7 (0 , 0 )), 1 );
50
64
}
51
65
52
66
private void test (ToIntFunction <Object > task ) {
@@ -116,10 +130,33 @@ case R1(Object o):
116
130
}
117
131
}
118
132
119
- public static int runFallThrough (R6 r ) {
133
+ private String switchNullable1 (R6 r ) {
134
+ return switch (r ) {
135
+ case R6 (Integer i1 , Integer i2 ) -> "int: " + i1 + ", int: " + i2 ;
136
+ case R6 (Object o1 , Object o2 ) -> "obj: " + o1 + ", obj: " + o2 ;
137
+ };
138
+ }
139
+
140
+ private String switchNullable2 (R6 r ) {
141
+ return switch (r ) {
142
+ case R6 (Integer i1 , Integer i2 ) -> "int: " + i1 + ", int: " + i2 ;
143
+ case R6 (Integer i1 , Object o2 ) -> "int: " + i1 + ", obj: " + o2 ;
144
+ case R6 (Object o1 , Integer i2 ) -> "obj: " + o1 + ", int: " + i2 ;
145
+ case R6 (Object o1 , Object o2 ) -> "obj: " + o1 + ", obj: " + o2 ;
146
+ };
147
+ }
148
+
149
+ private String switchNullableNPE (R6 r ) {
150
+ return switch (r ) {
151
+ case R6 (Object o1 , Object o2 ) when ((int ) o1 ) == 0 && ((int ) o2 ) == 0 -> "int: " + o1 + ", int: " + o2 ;
152
+ case R6 (Object o1 , Object o2 ) -> "obj: " + o1 + ", obj: " + o2 ;
153
+ };
154
+ }
155
+
156
+ public static int runFallThrough (R7 r ) {
120
157
switch (r ) {
121
- case R6 (var v1 , var v2 ) when v1 != 0 : return 0 ;
122
- case R6 (var v1 , var v2 ):
158
+ case R7 (var v1 , var v2 ) when v1 != 0 : return 0 ;
159
+ case R7 (var v1 , var v2 ):
123
160
}
124
161
return 1 ;
125
162
}
@@ -134,6 +171,13 @@ private void assertEquals(int expected, int actual) {
134
171
}
135
172
}
136
173
174
+ private void assertEquals (String expected , String actual ) {
175
+ if (!Objects .equals (expected , actual )) {
176
+ throw new AssertionError ("expected: " + expected + ", " +
177
+ "actual: " + actual );
178
+ }
179
+ }
180
+
137
181
record R1 (Object o ) {}
138
182
record R2 (Object o ) {}
139
183
record R3 (Object o ) {}
@@ -145,5 +189,6 @@ final class Sub3 extends Super {}
145
189
146
190
record R4 (Super o ) {}
147
191
record R5 (R4 o ) {}
148
- record R6 (int i1 , int i2 ) {}
192
+ record R6 (Object o1 , Object o2 ) {}
193
+ record R7 (int i1 , int i2 ) {}
149
194
}
0 commit comments