|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +/* |
| 24 | + * @test |
| 25 | + * @bug 8341408 |
| 26 | + * @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Second Preview) |
| 27 | + * @enablePreview |
| 28 | + */ |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class PrimitiveTypesInTestingContextErasure { |
| 33 | + public static void main(String[] args) { |
| 34 | + erasureSwitch(); |
| 35 | + erasureInstanceofTypeComparisonOperator(); |
| 36 | + erasureInstanceofPatternMatchingOperator(); |
| 37 | + |
| 38 | + pollutedInstanceofPatternMatchingOperatorReference(); |
| 39 | + pollutedInstanceofPatternMatchingOperator(); |
| 40 | + pollutedInstanceofTypeComparisonOperator(); |
| 41 | + pollutedSwitch(); |
| 42 | + } |
| 43 | + |
| 44 | + public static void erasureSwitch() { |
| 45 | + List<Short> ls = List.of((short) 42); |
| 46 | + Short s = 42; |
| 47 | + |
| 48 | + assertTrue(switch(ls.get(0)) { |
| 49 | + case int _ -> true; // Short to int |
| 50 | + default -> false; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + public static void erasureInstanceofTypeComparisonOperator() { |
| 55 | + List<Short> ls = List.of((short) 42); |
| 56 | + |
| 57 | + assertTrue(ls.get(0) instanceof int); // Short to int |
| 58 | + } |
| 59 | + |
| 60 | + public static void erasureInstanceofPatternMatchingOperator() { |
| 61 | + List<Short> ls = List.of((short) 42); |
| 62 | + |
| 63 | + assertTrue(ls.get(0) instanceof int i); // Short to int |
| 64 | + } |
| 65 | + |
| 66 | + public static void pollutedInstanceofPatternMatchingOperator() { |
| 67 | + List<Short> ls = (List) List.of("42"); |
| 68 | + |
| 69 | + assertTrue(!(ls.get(0) instanceof int i)); |
| 70 | + } |
| 71 | + |
| 72 | + public static void pollutedInstanceofTypeComparisonOperator() { |
| 73 | + List<Short> ls = (List) List.of("42"); |
| 74 | + |
| 75 | + assertTrue(!(ls.get(0) instanceof int)); |
| 76 | + } |
| 77 | + |
| 78 | + public static void pollutedInstanceofPatternMatchingOperatorReference() { |
| 79 | + List<Short> ls = (List) List.of("42"); |
| 80 | + |
| 81 | + assertTrue(!(ls.get(0) instanceof Short)); |
| 82 | + } |
| 83 | + |
| 84 | + public static void pollutedSwitch() { |
| 85 | + List<Short> ls = (List) List.of("42"); |
| 86 | + |
| 87 | + try { |
| 88 | + var res = switch(ls.get(0)) { |
| 89 | + case int _ -> true; |
| 90 | + default -> false; |
| 91 | + }; |
| 92 | + throw new AssertionError("Expected: ClassCastException"); |
| 93 | + } catch (ClassCastException e) { |
| 94 | + ; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static void assertTrue(boolean actual) { |
| 99 | + if (!actual) { |
| 100 | + throw new AssertionError("Expected: true, but got false"); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments