1 /*
2 * Copyright (c) 1994, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
53 import java.lang.reflect.TypeVariable;
54 import java.lang.constant.Constable;
55 import java.net.URL;
56 import java.security.AllPermission;
57 import java.security.Permissions;
58 import java.security.ProtectionDomain;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.Collection;
62 import java.util.HashMap;
63 import java.util.LinkedHashMap;
64 import java.util.LinkedHashSet;
65 import java.util.List;
66 import java.util.Map;
67 import java.util.Objects;
68 import java.util.Optional;
69 import java.util.Set;
70 import java.util.stream.Collectors;
71
72 import jdk.internal.constant.ConstantUtils;
73 import jdk.internal.loader.BootLoader;
74 import jdk.internal.loader.BuiltinClassLoader;
75 import jdk.internal.misc.Unsafe;
76 import jdk.internal.module.Resources;
77 import jdk.internal.reflect.CallerSensitive;
78 import jdk.internal.reflect.CallerSensitiveAdapter;
79 import jdk.internal.reflect.ConstantPool;
80 import jdk.internal.reflect.Reflection;
81 import jdk.internal.reflect.ReflectionFactory;
82 import jdk.internal.util.ModifiedUtf;
83 import jdk.internal.vm.annotation.AOTRuntimeSetup;
84 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
85 import jdk.internal.vm.annotation.IntrinsicCandidate;
86 import jdk.internal.vm.annotation.Stable;
87
88 import sun.invoke.util.BytecodeDescriptor;
89 import sun.invoke.util.Wrapper;
90 import sun.reflect.generics.factory.CoreReflectionFactory;
91 import sun.reflect.generics.factory.GenericsFactory;
92 import sun.reflect.generics.repository.ClassRepository;
93 import sun.reflect.generics.scope.ClassScope;
94 import sun.reflect.annotation.*;
95
96 /**
97 * Instances of the class {@code Class} represent classes and
98 * interfaces in a running Java application. An enum class and a record
99 * class are kinds of class; an annotation interface is a kind of
228 private static final int ENUM = 0x00004000;
229 private static final int SYNTHETIC = 0x00001000;
230
231 private static native void registerNatives();
232 static {
233 runtimeSetup();
234 }
235
236 /// No significant static final fields; [#resetArchivedStates()] handles
237 /// prevents storing [#reflectionFactory] into AOT image.
238 @AOTRuntimeSetup
239 private static void runtimeSetup() {
240 registerNatives();
241 }
242
243 /*
244 * Private constructor. Only the Java Virtual Machine creates Class objects.
245 * This constructor is not used and prevents the default constructor being
246 * generated.
247 */
248 private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim, char flags) {
249 // Initialize final field for classLoader. The initialization value of non-null
250 // prevents future JIT optimizations from assuming this final field is null.
251 // The following assignments are done directly by the VM without calling this constructor.
252 classLoader = loader;
253 componentType = arrayComponentType;
254 modifiers = mods;
255 protectionDomain = pd;
256 primitive = isPrim;
257 classFileAccessFlags = flags;
258 }
259
260 /**
261 * Converts the object to a string. The string representation is the
262 * string "class" or "interface", followed by a space, and then by the
263 * name of the class in the format returned by {@code getName}.
264 * If this {@code Class} object represents a primitive type,
265 * this method returns the name of the primitive type. If
266 * this {@code Class} object represents void this method returns
267 * "void". If this {@code Class} object represents an array type,
268 * this method returns "class " followed by {@code getName}.
269 *
270 * @return a string representation of this {@code Class} object.
271 */
272 public String toString() {
273 String kind = isInterface() ? "interface " : isPrimitive() ? "" : "class ";
274 return kind.concat(getName());
275 }
276
308 *
309 * @since 1.8
310 */
311 public String toGenericString() {
312 if (isPrimitive()) {
313 return toString();
314 } else {
315 StringBuilder sb = new StringBuilder();
316 Class<?> component = this;
317 int arrayDepth = 0;
318
319 if (isArray()) {
320 do {
321 arrayDepth++;
322 component = component.getComponentType();
323 } while (component.isArray());
324 sb.append(component.getName());
325 } else {
326 // Class modifiers are a superset of interface modifiers
327 int modifiers = getModifiers() & Modifier.classModifiers();
328 if (modifiers != 0) {
329 sb.append(Modifier.toString(modifiers));
330 sb.append(' ');
331 }
332
333 // A class cannot be strictfp and sealed/non-sealed so
334 // it is sufficient to check for sealed-ness after all
335 // modifiers are printed.
336 addSealingInfo(modifiers, sb);
337
338 if (isAnnotation()) {
339 sb.append('@');
340 }
341 if (isInterface()) { // Note: all annotation interfaces are interfaces
342 sb.append("interface");
343 } else {
344 if (isEnum())
345 sb.append("enum");
346 else if (isRecord())
347 sb.append("record");
348 else
349 sb.append("class");
350 }
351 sb.append(' ');
352 sb.append(getName());
353 }
354
355 TypeVariable<?>[] typeparms = component.getTypeParameters();
356 if (typeparms.length > 0) {
357 sb.append(Arrays.stream(typeparms)
358 .map(Class::typeVarBounds)
359 .collect(Collectors.joining(",", "<", ">")));
360 }
361
362 if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
363
364 return sb.toString();
365 }
366 }
367
368 private void addSealingInfo(int modifiers, StringBuilder sb) {
369 // A class can be final XOR sealed XOR non-sealed.
598 *
599 * @jls 12.2 Loading of Classes and Interfaces
600 * @jls 12.3 Linking of Classes and Interfaces
601 * @since 9
602 */
603 public static Class<?> forName(Module module, String name) {
604 Objects.requireNonNull(module);
605 Objects.requireNonNull(name);
606 if (!ModifiedUtf.isValidLengthInConstantPool(name)) {
607 return null;
608 }
609
610 ClassLoader cl = module.getClassLoader();
611 if (cl != null) {
612 return cl.loadClass(module, name);
613 } else {
614 return BootLoader.loadClass(module, name);
615 }
616 }
617
618 /**
619 * {@return the {@code Class} object associated with the
620 * {@linkplain #isPrimitive() primitive type} of the given name}
621 * If the argument is not the name of a primitive type, {@code
622 * null} is returned.
623 *
624 * @param primitiveName the name of the primitive type to find
625 *
626 * @jls 4.2 Primitive Types and Values
627 * @jls 15.8.2 Class Literals
628 * @since 22
629 */
630 public static Class<?> forPrimitiveName(String primitiveName) {
631 return switch(primitiveName) {
632 // Integral types
633 case "int" -> int.class;
634 case "long" -> long.class;
635 case "short" -> short.class;
636 case "char" -> char.class;
637 case "byte" -> byte.class;
1001 *
1002 * @since 9
1003 */
1004 public Module getModule() {
1005 return module;
1006 }
1007
1008 // set by VM
1009 @Stable
1010 private transient Module module;
1011
1012 // Initialized in JVM not by private constructor
1013 // This field is filtered from reflection access, i.e. getDeclaredField
1014 // will throw NoSuchFieldException
1015 private final ClassLoader classLoader;
1016
1017 private transient Object classData; // Set by VM
1018 private transient Object[] signers; // Read by VM, mutable
1019 private final transient char modifiers; // Set by the VM
1020 private final transient char classFileAccessFlags; // Set by the VM
1021 private final transient boolean primitive; // Set by the VM if the Class is a primitive type.
1022
1023 // package-private
1024 Object getClassData() {
1025 return classData;
1026 }
1027
1028 /**
1029 * Returns an array of {@code TypeVariable} objects that represent the
1030 * type variables declared by the generic declaration represented by this
1031 * {@code GenericDeclaration} object, in declaration order. Returns an
1032 * array of length 0 if the underlying generic declaration declares no type
1033 * variables.
1034 *
1035 * @return an array of {@code TypeVariable} objects that represent
1036 * the type variables declared by this generic declaration
1037 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
1038 * signature of this generic declaration does not conform to
1039 * the format specified in section {@jvms 4.7.9} of
1040 * <cite>The Java Virtual Machine Specification</cite>
1041 * @since 1.5
1321 }
1322 return c;
1323 }
1324
1325 /**
1326 * Returns the Java language modifiers for this class or interface, encoded
1327 * in an integer. The modifiers consist of the Java Virtual Machine's
1328 * constants for {@code public}, {@code protected},
1329 * {@code private}, {@code final}, {@code static},
1330 * {@code abstract} and {@code interface}; they should be decoded
1331 * using the methods of class {@code Modifier}.
1332 *
1333 * <p> If the underlying class is an array class:
1334 * <ul>
1335 * <li> its {@code public}, {@code private} and {@code protected}
1336 * modifiers are the same as those of its component type
1337 * <li> its {@code abstract} and {@code final} modifiers are always
1338 * {@code true}
1339 * <li> its interface modifier is always {@code false}, even when
1340 * the component type is an interface
1341 * </ul>
1342 * If this {@code Class} object represents a primitive type or
1343 * void, its {@code public}, {@code abstract}, and {@code final}
1344 * modifiers are always {@code true}.
1345 * For {@code Class} objects representing void, primitive types, and
1346 * arrays, the values of other modifiers are {@code false} other
1347 * than as specified above.
1348 *
1349 * <p> The modifier encodings are defined in section {@jvms 4.1}
1350 * of <cite>The Java Virtual Machine Specification</cite>.
1351 *
1352 * @return the {@code int} representing the modifiers for this class
1353 * @see java.lang.reflect.Modifier
1354 * @see #accessFlags()
1355 * @see <a
1356 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1357 * programming language and JVM modeling in core reflection</a>
1358 * @since 1.1
1359 * @jls 8.1.1 Class Modifiers
1360 * @jls 9.1.1 Interface Modifiers
1361 * @jvms 4.1 The {@code ClassFile} Structure
1362 */
1363 public int getModifiers() { return modifiers; }
1364
1365 /**
1366 * {@return an unmodifiable set of the {@linkplain AccessFlag access
1367 * flags} for this class, possibly empty}
1368 *
1369 * <p> If the underlying class is an array class:
1370 * <ul>
1371 * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1372 * access flags are the same as those of its component type
1373 * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1374 * <li> its {@code INTERFACE} flag is absent, even when the
1375 * component type is an interface
1376 * </ul>
1377 * If this {@code Class} object represents a primitive type or
1378 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1379 * {@code FINAL}.
1380 * For {@code Class} objects representing void, primitive types, and
1381 * arrays, access flags are absent other than as specified above.
1382 *
1383 * @see #getModifiers()
1384 * @jvms 4.1 The ClassFile Structure
1385 * @jvms 4.7.6 The InnerClasses Attribute
1386 * @since 20
1387 */
1388 public Set<AccessFlag> accessFlags() {
1389 // Location.CLASS allows SUPER and AccessFlag.MODULE which
1390 // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1391 // and STATIC, which are not allowed on Location.CLASS.
1392 // Use getClassFileAccessFlags to expose SUPER status.
1393 // Arrays need to use PRIVATE/PROTECTED from its component modifiers.
1394 var location = (isMemberClass() || isLocalClass() ||
1395 isAnonymousClass() || isArray()) ?
1396 AccessFlag.Location.INNER_CLASS :
1397 AccessFlag.Location.CLASS;
1398 return getReflectionFactory().parseAccessFlags((location == AccessFlag.Location.CLASS) ?
1399 getClassFileAccessFlags() : getModifiers(), location, this);
1400 }
1401
1402 /**
1403 * Gets the signers of this class.
1404 *
1405 * @return the signers of this class, or null if there are no signers. In
1406 * particular, this method returns null if this {@code Class} object represents
1407 * a primitive type or void.
1408 * @since 1.1
1409 */
1410 public Object[] getSigners() {
1411 var signers = this.signers;
1412 return signers == null ? null : signers.clone();
1413 }
1414
1415 /**
1416 * Set the signers of this class.
1417 */
1418 void setSigners(Object[] signers) {
1419 if (!isPrimitive() && !isArray()) {
|
1 /*
2 * Copyright (c) 1994, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
53 import java.lang.reflect.TypeVariable;
54 import java.lang.constant.Constable;
55 import java.net.URL;
56 import java.security.AllPermission;
57 import java.security.Permissions;
58 import java.security.ProtectionDomain;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.Collection;
62 import java.util.HashMap;
63 import java.util.LinkedHashMap;
64 import java.util.LinkedHashSet;
65 import java.util.List;
66 import java.util.Map;
67 import java.util.Objects;
68 import java.util.Optional;
69 import java.util.Set;
70 import java.util.stream.Collectors;
71
72 import jdk.internal.constant.ConstantUtils;
73 import jdk.internal.javac.PreviewFeature;
74 import jdk.internal.loader.BootLoader;
75 import jdk.internal.loader.BuiltinClassLoader;
76 import jdk.internal.misc.PreviewFeatures;
77 import jdk.internal.misc.Unsafe;
78 import jdk.internal.module.Resources;
79 import jdk.internal.reflect.AccessFlagSet;
80 import jdk.internal.reflect.CallerSensitive;
81 import jdk.internal.reflect.CallerSensitiveAdapter;
82 import jdk.internal.reflect.ConstantPool;
83 import jdk.internal.reflect.PreviewAccessFlags;
84 import jdk.internal.reflect.Reflection;
85 import jdk.internal.reflect.ReflectionFactory;
86 import jdk.internal.util.ModifiedUtf;
87 import jdk.internal.vm.annotation.AOTRuntimeSetup;
88 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
89 import jdk.internal.vm.annotation.IntrinsicCandidate;
90 import jdk.internal.vm.annotation.Stable;
91
92 import sun.invoke.util.BytecodeDescriptor;
93 import sun.invoke.util.Wrapper;
94 import sun.reflect.generics.factory.CoreReflectionFactory;
95 import sun.reflect.generics.factory.GenericsFactory;
96 import sun.reflect.generics.repository.ClassRepository;
97 import sun.reflect.generics.scope.ClassScope;
98 import sun.reflect.annotation.*;
99
100 /**
101 * Instances of the class {@code Class} represent classes and
102 * interfaces in a running Java application. An enum class and a record
103 * class are kinds of class; an annotation interface is a kind of
232 private static final int ENUM = 0x00004000;
233 private static final int SYNTHETIC = 0x00001000;
234
235 private static native void registerNatives();
236 static {
237 runtimeSetup();
238 }
239
240 /// No significant static final fields; [#resetArchivedStates()] handles
241 /// prevents storing [#reflectionFactory] into AOT image.
242 @AOTRuntimeSetup
243 private static void runtimeSetup() {
244 registerNatives();
245 }
246
247 /*
248 * Private constructor. Only the Java Virtual Machine creates Class objects.
249 * This constructor is not used and prevents the default constructor being
250 * generated.
251 */
252 private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim, boolean isIdentity, char flags) {
253 // Initialize final field for classLoader. The initialization value of non-null
254 // prevents future JIT optimizations from assuming this final field is null.
255 // The following assignments are done directly by the VM without calling this constructor.
256 classLoader = loader;
257 componentType = arrayComponentType;
258 modifiers = mods;
259 protectionDomain = pd;
260 primitive = isPrim;
261 identity = isIdentity;
262 classFileAccessFlags = flags;
263 }
264
265 /**
266 * Converts the object to a string. The string representation is the
267 * string "class" or "interface", followed by a space, and then by the
268 * name of the class in the format returned by {@code getName}.
269 * If this {@code Class} object represents a primitive type,
270 * this method returns the name of the primitive type. If
271 * this {@code Class} object represents void this method returns
272 * "void". If this {@code Class} object represents an array type,
273 * this method returns "class " followed by {@code getName}.
274 *
275 * @return a string representation of this {@code Class} object.
276 */
277 public String toString() {
278 String kind = isInterface() ? "interface " : isPrimitive() ? "" : "class ";
279 return kind.concat(getName());
280 }
281
313 *
314 * @since 1.8
315 */
316 public String toGenericString() {
317 if (isPrimitive()) {
318 return toString();
319 } else {
320 StringBuilder sb = new StringBuilder();
321 Class<?> component = this;
322 int arrayDepth = 0;
323
324 if (isArray()) {
325 do {
326 arrayDepth++;
327 component = component.getComponentType();
328 } while (component.isArray());
329 sb.append(component.getName());
330 } else {
331 // Class modifiers are a superset of interface modifiers
332 int modifiers = getModifiers() & Modifier.classModifiers();
333 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
334 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
335 if (modifiers != 0) {
336 sb.append(Modifier.toString(modifiers));
337 sb.append(' ');
338 }
339
340 // A class cannot be strictfp and sealed/non-sealed so
341 // it is sufficient to check for sealed-ness after all
342 // modifiers are printed.
343 addSealingInfo(modifiers, sb);
344
345 if (isAnnotation()) {
346 sb.append('@');
347 }
348 if (isInterface()) { // Note: all annotation interfaces are interfaces
349 sb.append("interface");
350 } else {
351 if (isEnum())
352 sb.append("enum");
353 else {
354 if (isValue()) {
355 sb.append("value ");
356 }
357 if (isRecord())
358 sb.append("record");
359 else
360 sb.append("class");
361 }
362 }
363 sb.append(' ');
364 sb.append(getName());
365 }
366
367 TypeVariable<?>[] typeparms = component.getTypeParameters();
368 if (typeparms.length > 0) {
369 sb.append(Arrays.stream(typeparms)
370 .map(Class::typeVarBounds)
371 .collect(Collectors.joining(",", "<", ">")));
372 }
373
374 if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
375
376 return sb.toString();
377 }
378 }
379
380 private void addSealingInfo(int modifiers, StringBuilder sb) {
381 // A class can be final XOR sealed XOR non-sealed.
610 *
611 * @jls 12.2 Loading of Classes and Interfaces
612 * @jls 12.3 Linking of Classes and Interfaces
613 * @since 9
614 */
615 public static Class<?> forName(Module module, String name) {
616 Objects.requireNonNull(module);
617 Objects.requireNonNull(name);
618 if (!ModifiedUtf.isValidLengthInConstantPool(name)) {
619 return null;
620 }
621
622 ClassLoader cl = module.getClassLoader();
623 if (cl != null) {
624 return cl.loadClass(module, name);
625 } else {
626 return BootLoader.loadClass(module, name);
627 }
628 }
629
630 /**
631 * {@return {@code true} if this {@code Class} object represents an identity class,
632 * otherwise {@code false}}
633 *
634 * <ul>
635 * <li>
636 * If this {@code Class} object represents an array type this method returns {@code true}.
637 * <li>
638 * If this {@code Class} object represents an interface, a primitive type,
639 * or {@code void} this method returns {@code false}.
640 * <li>
641 * For all other {@code Class} objects, this method returns {@code true} if either
642 * preview features are disabled or {@linkplain AccessFlag#IDENTITY IDENTITY} is
643 * present in the {@linkplain #accessFlags() class access flags}.
644 * </ul>
645 * @see AccessFlag#IDENTITY
646 * @since Valhalla
647 */
648 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
649 public boolean isIdentity() {
650 return identity;
651 }
652
653 /**
654 * {@return {@code true} if this {@code Class} object represents a value class,
655 * otherwise {@code false}}
656 * <ul>
657 * <li>
658 * If this {@code Class} object represents an array type this method returns {@code false}.
659 * <li>
660 * If this {@code Class} object represents an interface, a primitive type,
661 * or {@code void} this method returns {@code true} only if preview features are enabled.
662 * <li>
663 * For all other {@code Class} objects, this method returns {@code true} only if
664 * preview features are enabled and {@linkplain AccessFlag#IDENTITY IDENTITY} is not
665 * present in the {@linkplain #accessFlags() class access flags}.
666 * </ul>
667 * @see AccessFlag#IDENTITY
668 * @since Valhalla
669 */
670 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
671 public boolean isValue() {
672 if (!PreviewFeatures.isEnabled()) {
673 return false;
674 }
675 return !isIdentity();
676 }
677
678 /**
679 * {@return the {@code Class} object associated with the
680 * {@linkplain #isPrimitive() primitive type} of the given name}
681 * If the argument is not the name of a primitive type, {@code
682 * null} is returned.
683 *
684 * @param primitiveName the name of the primitive type to find
685 *
686 * @jls 4.2 Primitive Types and Values
687 * @jls 15.8.2 Class Literals
688 * @since 22
689 */
690 public static Class<?> forPrimitiveName(String primitiveName) {
691 return switch(primitiveName) {
692 // Integral types
693 case "int" -> int.class;
694 case "long" -> long.class;
695 case "short" -> short.class;
696 case "char" -> char.class;
697 case "byte" -> byte.class;
1061 *
1062 * @since 9
1063 */
1064 public Module getModule() {
1065 return module;
1066 }
1067
1068 // set by VM
1069 @Stable
1070 private transient Module module;
1071
1072 // Initialized in JVM not by private constructor
1073 // This field is filtered from reflection access, i.e. getDeclaredField
1074 // will throw NoSuchFieldException
1075 private final ClassLoader classLoader;
1076
1077 private transient Object classData; // Set by VM
1078 private transient Object[] signers; // Read by VM, mutable
1079 private final transient char modifiers; // Set by the VM
1080 private final transient char classFileAccessFlags; // Set by the VM
1081 private final transient boolean primitive; // Set by the VM if the Class is a primitive type
1082 private final transient boolean identity; // Set by the VM if the Class is an identity class
1083
1084 // package-private
1085 Object getClassData() {
1086 return classData;
1087 }
1088
1089 /**
1090 * Returns an array of {@code TypeVariable} objects that represent the
1091 * type variables declared by the generic declaration represented by this
1092 * {@code GenericDeclaration} object, in declaration order. Returns an
1093 * array of length 0 if the underlying generic declaration declares no type
1094 * variables.
1095 *
1096 * @return an array of {@code TypeVariable} objects that represent
1097 * the type variables declared by this generic declaration
1098 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
1099 * signature of this generic declaration does not conform to
1100 * the format specified in section {@jvms 4.7.9} of
1101 * <cite>The Java Virtual Machine Specification</cite>
1102 * @since 1.5
1382 }
1383 return c;
1384 }
1385
1386 /**
1387 * Returns the Java language modifiers for this class or interface, encoded
1388 * in an integer. The modifiers consist of the Java Virtual Machine's
1389 * constants for {@code public}, {@code protected},
1390 * {@code private}, {@code final}, {@code static},
1391 * {@code abstract} and {@code interface}; they should be decoded
1392 * using the methods of class {@code Modifier}.
1393 *
1394 * <p> If the underlying class is an array class:
1395 * <ul>
1396 * <li> its {@code public}, {@code private} and {@code protected}
1397 * modifiers are the same as those of its component type
1398 * <li> its {@code abstract} and {@code final} modifiers are always
1399 * {@code true}
1400 * <li> its interface modifier is always {@code false}, even when
1401 * the component type is an interface
1402 * <li> when preview features are enabled, its {@linkplain
1403 * AccessFlag#IDENTITY identity} modifier is always true
1404 * </ul>
1405 * If this {@code Class} object represents a primitive type or
1406 * void, its {@code public}, {@code abstract}, and {@code final}
1407 * modifiers are always {@code true}.
1408 * For {@code Class} objects representing void, primitive types, and
1409 * arrays, the values of other modifiers are {@code false} other
1410 * than as specified above.
1411 *
1412 * <p> The modifier encodings are defined in section {@jvms 4.1}
1413 * of <cite>The Java Virtual Machine Specification</cite>.
1414 *
1415 * @return the {@code int} representing the modifiers for this class
1416 * @see java.lang.reflect.Modifier
1417 * @see #accessFlags()
1418 * @see <a
1419 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1420 * programming language and JVM modeling in core reflection</a>
1421 * @since 1.1
1422 * @jls 8.1.1 Class Modifiers
1423 * @jls 9.1.1 Interface Modifiers
1424 * @jvms 4.1 The {@code ClassFile} Structure
1425 */
1426 public int getModifiers() { return modifiers; }
1427
1428 /**
1429 * {@return an unmodifiable set of the {@linkplain AccessFlag access
1430 * flags} for this class, possibly empty}
1431 * The {@code AccessFlags} may depend on the class file format version of the class.
1432 *
1433 * <p> If the underlying class is an array class:
1434 * <ul>
1435 * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1436 * access flags are the same as those of its component type
1437 * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1438 * <li> its {@code INTERFACE} flag is absent, even when the
1439 * component type is an interface
1440 * <li> when preview features are enabled, its {@code IDENTITY} flag
1441 * is present
1442 * </ul>
1443 * If this {@code Class} object represents a primitive type or
1444 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1445 * {@code FINAL}.
1446 * For {@code Class} objects representing void, primitive types, and
1447 * arrays, access flags are absent other than as specified above.
1448 *
1449 * @see #getModifiers()
1450 * @jvms 4.1 The ClassFile Structure
1451 * @jvms 4.7.6 The InnerClasses Attribute
1452 * @since 20
1453 */
1454 public Set<AccessFlag> accessFlags() {
1455 if (!PreviewFeatures.isEnabled()) {
1456 // INNER_CLASS_FLAGS exclusively defines PRIVATE, PROTECTED, and STATIC.
1457 // CLASS_FLAGS exclusively defines SUPER and MODULE.
1458 // Nested classes and interfaces need to report PRIVATE/PROTECTED/STATIC.
1459 // Arrays need to report PRIVATE/PROTECTED.
1460 // Top-level classes need to report SUPER, using getClassFileAccessFlags.
1461 // Module descriptors do not have Class objects so nothing reports MODULE.
1462 return (isArray() || getEnclosingClass() != null)
1463 ? AccessFlagSet.ofValidated(AccessFlagSet.INNER_CLASS_FLAGS, getModifiers())
1464 : AccessFlagSet.ofValidated(AccessFlagSet.CLASS_FLAGS, getClassFileAccessFlags());
1465 }
1466 // CLASS_FLAGS exclusively defines MODULE, but module descriptors are
1467 // never represented with Class objects, so INNER_CLASS_FLAGS works
1468 return AccessFlagSet.ofValidated(PreviewAccessFlags.INNER_CLASS_PREVIEW_FLAGS, getModifiers());
1469 }
1470
1471 /**
1472 * Gets the signers of this class.
1473 *
1474 * @return the signers of this class, or null if there are no signers. In
1475 * particular, this method returns null if this {@code Class} object represents
1476 * a primitive type or void.
1477 * @since 1.1
1478 */
1479 public Object[] getSigners() {
1480 var signers = this.signers;
1481 return signers == null ? null : signers.clone();
1482 }
1483
1484 /**
1485 * Set the signers of this class.
1486 */
1487 void setSigners(Object[] signers) {
1488 if (!isPrimitive() && !isArray()) {
|