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