1 /*
 2  * Copyright (c) 2023, 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 /*
25  * @test
26  * @bug 8301720
27  * @summary Test expected value of SUPER AccessFlag for pre-ValueClass .class file
28  * @modules java.base/jdk.internal.misc
29  * @compile -source 25 -target 25 SuperAccessFlagTest.java
30  * @run main SuperAccessFlagTest
31  */
32 
33 import java.io.InputStream;
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 import java.lang.classfile.ClassFile;
37 import java.lang.classfile.ClassModel;
38 import java.lang.reflect.AccessFlag;
39 import java.util.Set;
40 
41 import jdk.internal.misc.PreviewFeatures;
42 
43 /*
44  * Test expected value of ACC_SUPER access flag on an earlier release.
45  * We test against class files because core reflection automatically upgrades
46  * outdated access flags to the latest flags.
47  */
48 @ExpectedClassFlags("[PUBLIC, SUPER]")
49 public class SuperAccessFlagTest {
50     public static void main(String... args) throws Exception {
51         checkClass(SuperAccessFlagTest.class);
52         checkClass(ExpectedClassFlags.class);
53     }
54 
55     private static void checkClass(Class<?> clazz) throws Exception {
56         ExpectedClassFlags expected =
57                 clazz.getAnnotation(ExpectedClassFlags.class);
58         if (expected != null) {
59             // Examine stable representation in class files
60             ClassModel cm;
61             try (InputStream is = clazz.getResourceAsStream("/" + clazz.getName().replace('.', '/') + ".class")) {
62                 cm = ClassFile.of().parse(is.readAllBytes());
63             }
64             check(clazz, expected, cm.flags().flags());
65 
66             if (!PreviewFeatures.isEnabled()) {
67                 // Hotspot performs automatic flag translations, so no preview
68                 check(clazz, expected, clazz.accessFlags());
69             }
70         }
71     }
72 
73     private static void check(Class<?> clazz, ExpectedClassFlags expected, Set<AccessFlag> flags) {
74         String actual = flags.toString();
75         if (!expected.value().equals(actual)) {
76             throw new RuntimeException("On " + clazz +
77                     " expected " + expected.value() +
78                     " got " + actual);
79         }
80     }
81 }
82 
83 @Retention(RetentionPolicy.RUNTIME)
84 @ExpectedClassFlags("[INTERFACE, ABSTRACT, ANNOTATION]")
85 @interface ExpectedClassFlags {
86     String value();
87 }