|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 | + * @summary Verify enhanced variable declarations when sealed hierarchy changes after compilation. |
| 26 | + * @enablePreview |
| 27 | + * @compile EnhancedVariableDeclSealedTypeChanges.java |
| 28 | + * @compile EnhancedVariableDeclSealedTypeChanges2.java |
| 29 | + * @run main EnhancedVariableDeclSealedTypeChanges |
| 30 | + */ |
| 31 | + |
| 32 | +import java.util.function.Consumer; |
| 33 | + |
| 34 | +public class EnhancedVariableDeclSealedTypeChanges { |
| 35 | + public static void main(String... args) throws Exception { |
| 36 | + new EnhancedVariableDeclSealedTypeChanges().run(); |
| 37 | + } |
| 38 | + |
| 39 | + void run() throws Exception { |
| 40 | + doRun(this::enhancedVariableDeclStatement); |
| 41 | + doRun(this::enhancedVariableDeclStatementNoComponentAccess); |
| 42 | + } |
| 43 | + |
| 44 | + <T> void doRun(Consumer<T> consumer) throws Exception { |
| 45 | + consumer.accept((T) new A(1, 2)); |
| 46 | + |
| 47 | + try { |
| 48 | + consumer.accept((T) Class.forName("EnhancedVariableDeclSealedTypeChangesClass") |
| 49 | + .getDeclaredConstructor() |
| 50 | + .newInstance()); |
| 51 | + throw new AssertionError("Expected an exception, but none thrown."); |
| 52 | + } catch (Throwable ex) { |
| 53 | + validateMatchException(ex); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + void validateMatchException(Throwable t) { |
| 58 | + if (!(t instanceof MatchException)) { |
| 59 | + throw new AssertionError("Unexpected exception kind", t); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + void enhancedVariableDeclStatement(EnhancedVariableDeclSealedTypeChangesIntf obj) { |
| 64 | + A(Integer x, Integer y) = obj; |
| 65 | + if (x + y < 0) { |
| 66 | + throw new AssertionError("unreachable"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void enhancedVariableDeclStatementNoComponentAccess(EnhancedVariableDeclSealedTypeChangesIntf obj) { |
| 71 | + A(var x, var y) = obj; |
| 72 | + if (obj == null) { |
| 73 | + throw new AssertionError("unreachable"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + record A(Integer x, Integer y) implements EnhancedVariableDeclSealedTypeChangesIntf {} |
| 78 | +} |
| 79 | + |
| 80 | +sealed interface EnhancedVariableDeclSealedTypeChangesIntf |
| 81 | + permits EnhancedVariableDeclSealedTypeChanges.A {} |
0 commit comments