File: Unnamed.java

package info (click to toggle)
openjdk-25 25.0.1%2B8-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (343 lines) | stat: -rw-r--r-- 10,977 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8304246
 * @summary Compiler Implementation for Unnamed patterns and variables
 * @compile Unnamed.java
 * @run main Unnamed
 */

import java.util.Objects;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class Unnamed {
    public static void main(String[] args) throws Throwable {
        new Unnamed().run();
    }

    public void run() {
        assertEquals(1, testMultiValuesTopLevel(new R1()));
        assertEquals(2, testMultiValuesTopLevel(new R3()));
        assertEquals(1, testMultiValuesTopLevel2(new R1()));
        assertEquals(2, testMultiValuesTopLevel2(new R2()));
        assertEquals(2, testMultiValuesTopLevel2(new R4()));
        assertEquals(1, testMultiValuesNested(new Box<>(new R1())));
        assertEquals(1, testMultiValuesNested(new Box<>(new R2())));
        assertEquals(2, testMultiValuesNested(new Box<>(new R3())));
        assertEquals(3, testMultiValuesNested(new Box<>(new R4())));
        assertEquals(1, testMultiValuesNestedUnnamedVarAndPattern(new Box<>(new R1())));
        assertEquals(2, testMultiValuesNestedUnnamedVarAndPattern(new Box<>(new R4())));
        assertEquals(1, testMultiValuesNestedMix(new Box<>(new R1())));
        assertEquals(1, testMultiValuesNestedMix(new Box2<>(new R1())));
        assertEquals(1, testMultiValuesNestedMix2(new Box<>(new R1())));
        assertEquals(1, testMultiValuesNestedMix2("BOX"));
        assertEquals(2, testMultiValuesNestedMix2(new Box2<>(new R1())));
        assertEquals(1, testMultiValuesStatementBlock(42));
        assertEquals(1, testMultiValuesStatementBlock(42.0f));
        assertEquals(2, testMultiValuesStatementBlock("BOX"));
        assertEquals(1, testMultiValuesStatementBlock2(new Box<>(new R1())));
        assertEquals(1, testMultiValuesStatementBlock2("BOX"));
        assertEquals(2, testMultiValuesStatementBlock2(new Box2<>(new R1())));
        assertEquals(2, testMultiValuesGuards(new R3(), 1));
        assertEquals(3, testMultiValuesGuards(new R4(), 42));
        assertEquals(3, testMultiValuesGuards(new R3(), 42));
        assertEquals(1, testMultiValuesNestedGuards(new Box(new R2()), 42));
        assertEquals(2, testMultiValuesNestedGuards(new Box(new R3()), 1));
        assertEquals(1, testMixUnconditionalAndConditional(new R1()));
        assertEquals(2, testMixUnconditionalAndConditional(new R2()));
        assertEquals(2, testMixUnconditionalAndConditional(new R3()));
        assertEquals(1, testMultipleExpr(new Box<>(new R1())));
        assertEquals(1, testUnrolledExpr(new Box<>(new R1())));
        assertEquals(1, testMultipleStat(new Box<>(new R1())));
        assertEquals(1, testUnrolledStat(new Box<>(new R1())));
        assertEquals(2, testMixVarWithExplicit(new Box<>(new R2())));
        assertEquals("binding", unnamedGuardAddsBindings("match1", "binding"));
        assertEquals("any", unnamedGuardAddsBindings(42, 42));
        assertEquals(true, testUnnamedPrimitiveAndExhaustiveness(new Prim1(4)));
        assertEquals(false, testUnnamedPrimitiveAndExhaustiveness(new Prim2(5)));

        unnamedTest();
    }

    private void unnamedTest() {
        int _ = 0;
        int _ = 1;
        try (Lock _ = null) {
            try (Lock _ = null) {
            } catch (Exception _) {
                try {
                } catch (Exception _) {}
            }
        }
        try (final Lock _ = null) { }
        try (@Foo Lock _ = null) { }

        try (Lock _ = null) { }
        catch (Exception | Error _) { }

        String[] strs = new String[] { "str1", "str2" };
        for (var _ : strs) {
            for (var _ : strs) {
            }
        }
        TwoParams p1 = (_, _) -> {};
        TwoParams p2 = (var _, var _) -> {};
        TwoIntParams p3 = (int _, int b) -> {};
        TwoIntParams p4 = (int _, int _) -> {};
        TwoIntParamsIntRet p5 = (int _, int _) -> { return 1; };

        p1.run(1, 2);
        p2.run(1, 2);
        p3.run(1, 2);
        p4.run(1, 2);
        p5.run(1, 2);

        R r = new R(null);
        if (r instanceof R _) {}
        if (r instanceof R(_)) {}
        for (int _ = 0, _ = 1, x = 1; x <= 1 ; x++) {}
    }

    int testMultiValuesTopLevel(Object o) {
        return switch (o) {
            case R1 _, R2 _ -> 1;
            default -> 2;
        };
    }

    int testMultiValuesTopLevel2(Base o) {
        return switch (o) {
            case R1 r -> 1;
            case R2 _, R3 _, R4 _ -> 2;
        };
    }

    int testMultiValuesNested(Box<?> b) {
        return switch (b) {
            case Box(R1 _), Box(R2 _) -> 1;
            case Box(R3 _) -> 2;
            case Box(_)  -> 3;
        };
    }

    int testMultiValuesNestedUnnamedVarAndPattern(Box<?> b) {
        return switch (b) {
            case Box(R1 _), Box(R2 _) -> 1;
            case Box(R3 _), Box(_) -> 2;
        };
    }

    int testMultiValuesNestedMix(Object b) {
        return switch (b) {
            case Box(_), Box2(_) -> 1;
            default -> 2;
        };
    }

    int testMultiValuesNestedMix2(Object b) {
        return switch (b) {
            case Box(_), String _ -> 1;
            default -> 2;
        };
    }

    int testMultiValuesStatementBlock(Object o) {
        switch (o) {
            case Integer _:
            case Number _:
                return 1;
            default:
                return 2;
        }
    }

    int testMultiValuesStatementBlock2(Object o) {
        switch (o) {
            case Box(_):
            case String _:
                return 1;
            default:
                return 2;
        }
    }

    int testMultiValuesGuards(Base b, int x) {
        return switch (b) {
            case R1 r -> 1;
            case R2 _, R3 _, R4 _ when x == 1 -> 2;
            case R2 _, R3 _, R4 _ -> 3;
        };
    }

    int testMultiValuesNestedGuards(Box<?> b, int x) {
        return switch (b) {
            case Box(R1 _), Box(R2 _) -> 1;
            case Box(R3 _), Box(_) when x == 1 -> 2;
            case Box(_) -> 3;
        };
    }

    int testMixUnconditionalAndConditional(Base t) {
        return switch(t) {
            case R1 _ -> 1;
            case R2 _, Base _-> 2;
        };
    }

    int testMultipleExpr(Box<?> t) {
        return switch(t) {
            case Box(R1 _), Box(R2 _) -> 1;
            default -> -2;
        };
    }

    int testUnrolledExpr(Box<?> t) {
        return switch(t) {
            case Box(R1 _) -> 1;
            case Box(R2 _) -> 0;
            default -> -2;
        };
    }

    int testMultipleStat(Box<?> t) {
        int ret = -1;
        switch(t) {
            case Box(R1 _), Box(R2 _):
                ret = 1;
                break;
            default:
                ret = -2;
        }
        return ret;
    }

    int testUnrolledStat(Box<?> t) {
        int ret = -1;
        switch(t) {
            case Box(R1 _):
                ret = 1;
                break;
            case Box(R2 _):
                ret = 0;
                break;
            default:
                ret = -2;
        }
        return ret;
    }

    int testMixVarWithExplicit(Box<?> t) {
        int success = -1;
        success = switch(t) {
            case Box(R1 _) : {
                yield 1;
            }
            case Box(R2 _), Box(var _) : {
                yield 2;
            }
            default : {
                yield -2;
            }
        };
        return success;
    }

    String unnamedGuardAddsBindings(Object o1, Object o2) {
        return switch (o1) {
            case String _, Object _ when o2 instanceof String s: yield s;
            case Object _: yield "any";
        };
    }

    boolean testUnnamedPrimitiveAndExhaustiveness(RecordWithPrimitive a) {
        boolean r1 = switch (a) {
            case Prim1(var _) -> true;
            case Prim2(_) -> false;
        };

        boolean r2 = switch (a) {
            case Prim1(var _) -> true;
            case Prim2(var _) -> false;
        };

        boolean r3 = switch (a) {
            case Prim1(_) -> true;
            case Prim2(_) -> false;
        };

        return r1 && r2 && r3;
    }

    sealed interface RecordWithPrimitive permits Prim1, Prim2 {};
    record Prim1(int n1) implements RecordWithPrimitive {};
    record Prim2(int n2) implements RecordWithPrimitive {};

    // JEP 443 examples
    record Point(int x, int y) { }
    enum Color { RED, GREEN, BLUE }
    record ColoredPoint(Point p, Color c) { }

    void jep443examples(ColoredPoint r) {
        if (r instanceof ColoredPoint(Point(int x, int y), _)) { }
        if (r instanceof ColoredPoint(_, Color c)) { }
        if (r instanceof ColoredPoint(Point(int x, _), _)) { }
        if (r instanceof ColoredPoint(Point(int x, int _), Color _)) { }
        if (r instanceof ColoredPoint _) { }
    }

    class Lock implements AutoCloseable {
        @Override
        public void close() {}
    }
    interface TwoParams {
        public void run(Object o1, Object o2);
    }
    interface TwoIntParams {
        public void run(int o1, int o2);
    }
    interface TwoIntParamsIntRet {
        public int run(int a, int b);
    }
    record R(Object o) {}
    @Target(ElementType.LOCAL_VARIABLE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Foo { }

    sealed abstract class Base permits R1, R2, R3, R4 { }
    final  class R1  extends Base { }
    final  class R2  extends Base { }
    final  class R3  extends Base { }
    final  class R4  extends Base { }
    record Box<T extends Base>(T content) { }
    record Box2<T extends Base>(T content) { }
    void assertEquals(Object expected, Object actual) {
        if (!Objects.equals(expected, actual)) {
            throw new AssertionError("Expected: " + expected + ", but got: " + actual);
        }
    }
}