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