1 /*
  2  * Copyright (c) 2016, 2025, 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 id=NoPreview
 26  * @bug 8161013
 27  * @summary Verify that anonymous class binaries have the correct flags set
 28  * @modules java.base/jdk.internal.misc
 29  * @comment Ensure that that this test is skipped if the test is run on a preview enabled
 30             VM as the compiled test class has not been forced into preview mode.
 31             Valhalla affects the outcome.
 32  * @requires !java.enablePreview
 33  * @run main AnonymousClassFlags
 34  */
 35 
 36 /*
 37  * @test id=Preview
 38  * @bug 8161013
 39  * @summary Verify that anonymous class binaries have the correct flags set
 40  * @modules java.base/jdk.internal.misc
 41  * @enablePreview
 42  * @compile -XDforcePreview AnonymousClassFlags.java
 43  * @run main AnonymousClassFlags
 44  */
 45 
 46 import java.util.*;
 47 import java.nio.file.Path;
 48 import java.nio.file.Paths;
 49 
 50 import java.lang.classfile.*;
 51 import java.lang.classfile.attribute.InnerClassInfo;
 52 import java.lang.classfile.attribute.InnerClassesAttribute;
 53 
 54 import jdk.internal.misc.PreviewFeatures;
 55 
 56 public class AnonymousClassFlags {
 57     public static void main(String[] args) throws Exception {
 58         new AnonymousClassFlags().test(System.getProperty("test.classes", "."));
 59     }
 60 
 61     AnonymousClassFlags() {
 62         System.currentTimeMillis();
 63         super(); // Triggers force preview
 64     }
 65 
 66     // ACC_SUPER does not exist in InnerClasses before Value Objects
 67     private static final int EXPECTED_ACCESS_FLAGS = PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0;
 68 
 69     /** Maps names of anonymous classes to their expected inner_class_access_flags */
 70     private static Map<String, Integer> anonClasses = new LinkedHashMap<>();
 71 
 72     // ******* TEST CASES ********
 73 
 74     static Object o1 = new Object() {
 75         { anonClasses.put(getClass().getName(), EXPECTED_ACCESS_FLAGS); }
 76     };
 77 
 78     static void staticMethod() {
 79         Object o2 = new Object() {
 80             { anonClasses.put(getClass().getName(), EXPECTED_ACCESS_FLAGS); }
 81         };
 82     }
 83 
 84     static {
 85         staticMethod();
 86 
 87         Object o3 = new Object() {
 88             { anonClasses.put(getClass().getName(), EXPECTED_ACCESS_FLAGS); }
 89         };
 90     }
 91 
 92     Object o4 = new Object() {
 93         { anonClasses.put(getClass().getName(), EXPECTED_ACCESS_FLAGS); }
 94     };
 95 
 96     void instanceMethod() {
 97         Object o5 = new Object() {
 98             { anonClasses.put(getClass().getName(), EXPECTED_ACCESS_FLAGS); }
 99         };
100     }
101 
102     {
103         instanceMethod();
104 
105         Object o6 = new Object() {
106             { anonClasses.put(getClass().getName(), EXPECTED_ACCESS_FLAGS); }
107         };
108     }
109 
110     // ******* TEST IMPLEMENTATION ********
111 
112     void test(String classesDir) throws Exception {
113         staticMethod();
114         instanceMethod();
115 
116         Path outerFile = Paths.get(classesDir, getClass().getName() + ".class");
117         ClassModel outerClass = ClassFile.of().parse(outerFile);
118         for (Map.Entry<String,Integer> entry : anonClasses.entrySet()) {
119             Path innerFile = Paths.get(classesDir, entry.getKey() + ".class");
120             ClassModel innerClass = ClassFile.of().parse(innerFile);
121             String name = entry.getKey();
122             int expected = entry.getValue();
123             assertInnerFlags(outerClass, name, expected);
124             assertClassFlags(innerClass, name, expected);
125             assertInnerFlags(innerClass, name, expected);
126         }
127     }
128 
129     static void assertClassFlags(ClassModel classFile, String name, int expected) {
130         int mask = ClassFile.ACC_PUBLIC | ClassFile.ACC_FINAL | ClassFile.ACC_INTERFACE | ClassFile.ACC_ABSTRACT |
131                    ClassFile.ACC_SYNTHETIC | ClassFile.ACC_ANNOTATION | ClassFile.ACC_ENUM | ClassFile.ACC_IDENTITY;
132         int classExpected = (expected & mask) | ClassFile.ACC_SUPER;
133         int classActual = classFile.flags().flagsMask();
134         if (classActual != classExpected) {
135             throw new AssertionError("Incorrect access_flags for class " + name +
136                                      ": expected=" + classExpected + ", actual=" + classActual);
137         }
138 
139     }
140 
141     static void assertInnerFlags(ClassModel classFile, String name, int expected) {
142         int innerActual = lookupInnerFlags(classFile, name);
143         if (innerActual != expected) {
144             throw new AssertionError("Incorrect inner_class_access_flags for class " + name +
145                                      " in class " + classFile.thisClass().asInternalName() +
146                                      ": expected=" + expected + ", actual=" + innerActual);
147         }
148     }
149 
150     private static int lookupInnerFlags(ClassModel classFile, String innerName) {
151         InnerClassesAttribute inners = classFile.findAttribute(Attributes.innerClasses()).orElse(null);
152         if (inners == null) {
153             throw new AssertionError("InnerClasses attribute missing in class " + classFile.thisClass().asInternalName());
154         }
155         for (InnerClassInfo info: inners.classes()) {
156             String entryName = info.innerClass().asInternalName();
157             if (innerName.equals(entryName)) {
158                 return info.flagsMask();
159             }
160         }
161         throw new AssertionError("No InnerClasses entry in class " + classFile.thisClass().asInternalName() + " for class " + innerName);
162     }
163 
164 }