1 /* 2 * Copyright (c) 2022, 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 /** 26 * @test 27 * @bug 8291360 8293448 28 * @summary Test getting a class's raw access flags using java.lang.Class API 29 * @modules java.base/java.lang:open 30 * @enablePreview 31 * @compile classAccessFlagsRaw.jcod 32 * @run main/othervm ClassAccessFlagsRawTest 33 */ 34 35 import java.lang.classfile.ClassFile; 36 import java.lang.reflect.*; 37 import java.util.Set; 38 39 public class ClassAccessFlagsRawTest { 40 41 static Method m; 42 43 public static void testIt(String className, int expectedResult) throws Exception { 44 Class<?> testClass = Class.forName(className); 45 int flags = (int)m.invoke(testClass); 46 if (flags != expectedResult) { 47 throw new RuntimeException( 48 "expected 0x" + Integer.toHexString(expectedResult) + ", got 0x" + Integer.toHexString(flags) + " for class " + className); 49 } 50 } 51 52 public static void main(String argv[]) throws Throwable { 53 Class<?> cl = java.lang.Class.class; 54 m = cl.getDeclaredMethod("getClassFileAccessFlags", new Class[0]); 55 m.setAccessible(true); 56 57 testIt("SUPERset", Modifier.PUBLIC | ClassFile.ACC_IDENTITY); 58 // Because of the repurposing of ACC_SUPER into ACC_IDENTITY by JEP 401, the VM now fixes missing ACC_IDENTITY flags in old class files 59 testIt("SUPERnotset", Modifier.PUBLIC | ClassFile.ACC_IDENTITY); 60 61 // Test that primitive should return ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC. 62 int[] arr = new int[3]; 63 if (!arr.getClass().getComponentType().isPrimitive()) { 64 throw new RuntimeException("not primitive"); 65 } 66 int flags = (int)m.invoke(arr.getClass().getComponentType()); 67 if (flags != (Modifier.ABSTRACT | Modifier.FINAL | Modifier.PUBLIC)) { 68 throw new RuntimeException( 69 "expected 0x411, got 0x" + Integer.toHexString(flags) + " for primitive type"); 70 } 71 72 // Test that primitive array raw access flags return 0. 73 flags = (int)m.invoke(arr.getClass()); 74 if (flags != 0) { 75 throw new RuntimeException( 76 "expected 0x0 got 0x" + Integer.toHexString(flags) + " for primitive array"); 77 } 78 79 // Test that the modifier flags return element type flags. 80 flags = (int)arr.getClass().getModifiers(); 81 if (flags != (Modifier.ABSTRACT | Modifier.FINAL | Modifier.PUBLIC | ClassFile.ACC_IDENTITY)) { 82 throw new RuntimeException( 83 "expected 0x431, got 0x" + Integer.toHexString(flags) + " for primitive type"); 84 } 85 86 // Test that AccessFlags set will return element type access flags. 87 Set<AccessFlag> aacc = arr.getClass().accessFlags(); 88 if (!aacc.containsAll(Set.of(AccessFlag.FINAL, AccessFlag.ABSTRACT, AccessFlag.PUBLIC))) { 89 throw new RuntimeException( 90 "AccessFlags should contain FINAL, ABSTRACT and PUBLIC for primitive type"); 91 } 92 93 // Test object array. Raw access flags are 0 for arrays. 94 flags = (int)m.invoke((new SUPERnotset[2]).getClass()); 95 if (flags != 0) { 96 throw new RuntimeException( 97 "expected 0x0, got 0x" + Integer.toHexString(flags) + " for object array"); 98 } 99 100 // Test object array component type. 101 flags = (int)m.invoke((new SUPERnotset[2]).getClass().getComponentType()); 102 // Because of the repurposing of ACC_SUPER into ACC_IDENTITY by JEP 401, the VM now fixes missing ACC_IDENTITY flags in old class files 103 if (flags != (Modifier.PUBLIC | ClassFile.ACC_IDENTITY)) { 104 throw new RuntimeException( 105 "expected 0x1, got 0x" + Integer.toHexString(flags) + " for object array"); 106 } 107 108 // test multi-dimensional object array. should return flags of component. 109 flags = (int)m.invoke((new SUPERnotset[4][2]).getClass()); 110 if (flags != 0) { 111 throw new RuntimeException( 112 "expected 0x0, got 0x" + Integer.toHexString(flags) + " for object array"); 113 } 114 } 115 } --- EOF ---