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
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.lang.annotation.Annotation;
29 import java.lang.constant.ClassDesc;
30 import java.lang.constant.ConstantDescs;
31 import java.lang.invoke.TypeDescriptor;
32 import java.lang.invoke.MethodHandles;
33 import java.lang.ref.SoftReference;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.ObjectStreamField;
37 import java.lang.reflect.AnnotatedElement;
38 import java.lang.reflect.AnnotatedType;
39 import java.lang.reflect.AccessFlag;
40 import java.lang.reflect.Array;
41 import java.lang.reflect.Constructor;
42 import java.lang.reflect.Executable;
43 import java.lang.reflect.Field;
44 import java.lang.reflect.GenericArrayType;
45 import java.lang.reflect.GenericDeclaration;
46 import java.lang.reflect.InvocationTargetException;
47 import java.lang.reflect.Member;
48 import java.lang.reflect.Method;
49 import java.lang.reflect.Modifier;
50 import java.lang.reflect.Proxy;
51 import java.lang.reflect.RecordComponent;
52 import java.lang.reflect.Type;
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.vm.annotation.IntrinsicCandidate;
83 import jdk.internal.vm.annotation.Stable;
84
85 import sun.invoke.util.Wrapper;
86 import sun.reflect.generics.factory.CoreReflectionFactory;
87 import sun.reflect.generics.factory.GenericsFactory;
88 import sun.reflect.generics.repository.ClassRepository;
89 import sun.reflect.generics.repository.MethodRepository;
90 import sun.reflect.generics.repository.ConstructorRepository;
91 import sun.reflect.generics.scope.ClassScope;
92 import sun.reflect.annotation.*;
93
94 /**
200 * HelloWorld}, the methods to get the {@linkplain #getName name} and
201 * {@linkplain #getTypeName type name} return results
202 * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
203 * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
204 * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
205 *
206 * @param <T> the type of the class modeled by this {@code Class}
207 * object. For example, the type of {@code String.class} is {@code
208 * Class<String>}. Use {@code Class<?>} if the class being modeled is
209 * unknown.
210 *
211 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
212 * @since 1.0
213 */
214 public final class Class<T> implements java.io.Serializable,
215 GenericDeclaration,
216 Type,
217 AnnotatedElement,
218 TypeDescriptor.OfField<Class<?>>,
219 Constable {
220 private static final int ANNOTATION= 0x00002000;
221 private static final int ENUM = 0x00004000;
222 private static final int SYNTHETIC = 0x00001000;
223
224 private static native void registerNatives();
225 static {
226 runtimeSetup();
227 }
228
229 // Called from JVM when loading an AOT cache
230 private static void runtimeSetup() {
231 registerNatives();
232 }
233
234 /*
235 * Private constructor. Only the Java Virtual Machine creates Class objects.
236 * This constructor is not used and prevents the default constructor being
237 * generated.
238 */
239 private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim) {
240 // Initialize final field for classLoader. The initialization value of non-null
241 // prevents future JIT optimizations from assuming this final field is null.
242 // The following assignments are done directly by the VM without calling this constructor.
298 *
299 * @since 1.8
300 */
301 public String toGenericString() {
302 if (isPrimitive()) {
303 return toString();
304 } else {
305 StringBuilder sb = new StringBuilder();
306 Class<?> component = this;
307 int arrayDepth = 0;
308
309 if (isArray()) {
310 do {
311 arrayDepth++;
312 component = component.getComponentType();
313 } while (component.isArray());
314 sb.append(component.getName());
315 } else {
316 // Class modifiers are a superset of interface modifiers
317 int modifiers = getModifiers() & Modifier.classModifiers();
318 if (modifiers != 0) {
319 sb.append(Modifier.toString(modifiers));
320 sb.append(' ');
321 }
322
323 // A class cannot be strictfp and sealed/non-sealed so
324 // it is sufficient to check for sealed-ness after all
325 // modifiers are printed.
326 addSealingInfo(modifiers, sb);
327
328 if (isAnnotation()) {
329 sb.append('@');
330 }
331 if (isInterface()) { // Note: all annotation interfaces are interfaces
332 sb.append("interface");
333 } else {
334 if (isEnum())
335 sb.append("enum");
336 else if (isRecord())
337 sb.append("record");
338 else
339 sb.append("class");
340 }
341 sb.append(' ');
342 sb.append(getName());
343 }
344
345 TypeVariable<?>[] typeparms = component.getTypeParameters();
346 if (typeparms.length > 0) {
347 sb.append(Arrays.stream(typeparms)
348 .map(Class::typeVarBounds)
349 .collect(Collectors.joining(",", "<", ">")));
350 }
585 * @throws NullPointerException if the given module or name is {@code null}
586 *
587 * @throws LinkageError if the linkage fails
588 *
589 * @jls 12.2 Loading of Classes and Interfaces
590 * @jls 12.3 Linking of Classes and Interfaces
591 * @since 9
592 */
593 public static Class<?> forName(Module module, String name) {
594 Objects.requireNonNull(module);
595 Objects.requireNonNull(name);
596
597 ClassLoader cl = module.getClassLoader();
598 if (cl != null) {
599 return cl.loadClass(module, name);
600 } else {
601 return BootLoader.loadClass(module, name);
602 }
603 }
604
605 /**
606 * {@return the {@code Class} object associated with the
607 * {@linkplain #isPrimitive() primitive type} of the given name}
608 * If the argument is not the name of a primitive type, {@code
609 * null} is returned.
610 *
611 * @param primitiveName the name of the primitive type to find
612 *
613 * @throws NullPointerException if the argument is {@code null}
614 *
615 * @jls 4.2 Primitive Types and Values
616 * @jls 15.8.2 Class Literals
617 * @since 22
618 */
619 public static Class<?> forPrimitiveName(String primitiveName) {
620 return switch(primitiveName) {
621 // Integral types
622 case "int" -> int.class;
623 case "long" -> long.class;
624 case "short" -> short.class;
1311 }
1312 return c;
1313 }
1314
1315 /**
1316 * Returns the Java language modifiers for this class or interface, encoded
1317 * in an integer. The modifiers consist of the Java Virtual Machine's
1318 * constants for {@code public}, {@code protected},
1319 * {@code private}, {@code final}, {@code static},
1320 * {@code abstract} and {@code interface}; they should be decoded
1321 * using the methods of class {@code Modifier}.
1322 *
1323 * <p> If the underlying class is an array class:
1324 * <ul>
1325 * <li> its {@code public}, {@code private} and {@code protected}
1326 * modifiers are the same as those of its component type
1327 * <li> its {@code abstract} and {@code final} modifiers are always
1328 * {@code true}
1329 * <li> its interface modifier is always {@code false}, even when
1330 * the component type is an interface
1331 * </ul>
1332 * If this {@code Class} object represents a primitive type or
1333 * void, its {@code public}, {@code abstract}, and {@code final}
1334 * modifiers are always {@code true}.
1335 * For {@code Class} objects representing void, primitive types, and
1336 * arrays, the values of other modifiers are {@code false} other
1337 * than as specified above.
1338 *
1339 * <p> The modifier encodings are defined in section {@jvms 4.1}
1340 * of <cite>The Java Virtual Machine Specification</cite>.
1341 *
1342 * @return the {@code int} representing the modifiers for this class
1343 * @see java.lang.reflect.Modifier
1344 * @see #accessFlags()
1345 * @see <a
1346 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1347 * programming language and JVM modeling in core reflection</a>
1348 * @since 1.1
1349 * @jls 8.1.1 Class Modifiers
1350 * @jls 9.1.1 Interface Modifiers
1351 * @jvms 4.1 The {@code ClassFile} Structure
1352 */
1353 public int getModifiers() { return modifiers; }
1354
1355 /**
1356 * {@return an unmodifiable set of the {@linkplain AccessFlag access
1357 * flags} for this class, possibly empty}
1358 *
1359 * <p> If the underlying class is an array class:
1360 * <ul>
1361 * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1362 * access flags are the same as those of its component type
1363 * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1364 * <li> its {@code INTERFACE} flag is absent, even when the
1365 * component type is an interface
1366 * </ul>
1367 * If this {@code Class} object represents a primitive type or
1368 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1369 * {@code FINAL}.
1370 * For {@code Class} objects representing void, primitive types, and
1371 * arrays, access flags are absent other than as specified above.
1372 *
1373 * @see #getModifiers()
1374 * @jvms 4.1 The ClassFile Structure
1375 * @jvms 4.7.6 The InnerClasses Attribute
1376 * @since 20
1377 */
1378 public Set<AccessFlag> accessFlags() {
1379 // Location.CLASS allows SUPER and AccessFlag.MODULE which
1380 // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1381 // and STATIC, which are not allowed on Location.CLASS.
1382 // Use getClassAccessFlagsRaw to expose SUPER status.
1383 var location = (isMemberClass() || isLocalClass() ||
1384 isAnonymousClass() || isArray()) ?
1385 AccessFlag.Location.INNER_CLASS :
1386 AccessFlag.Location.CLASS;
1387 return AccessFlag.maskToAccessFlags((location == AccessFlag.Location.CLASS) ?
1388 getClassAccessFlagsRaw() :
1389 getModifiers(),
1390 location);
1391 }
1392
1393 /**
1394 * Gets the signers of this class.
1395 *
1396 * @return the signers of this class, or null if there are no signers. In
1397 * particular, this method returns null if this {@code Class} object represents
1398 * a primitive type or void.
1399 * @since 1.1
1400 */
1401 public Object[] getSigners() {
1402 var signers = this.signers;
1403 return signers == null ? null : signers.clone();
1404 }
1405
1406 /**
1407 * Set the signers of this class.
1408 */
1409 void setSigners(Object[] signers) {
1410 if (!isPrimitive() && !isArray()) {
1411 this.signers = signers;
1412 }
1413 }
1414
1415 /**
1416 * If this {@code Class} object represents a local or anonymous
1417 * class within a method, returns a {@link
1418 * java.lang.reflect.Method Method} object representing the
1419 * immediately enclosing method of the underlying class. Returns
1420 * {@code null} otherwise.
3768 *
3769 * <p> If this {@code Class} object represents an interface, the return
3770 * value is an array containing objects representing the uses of interface
3771 * types to specify interfaces directly extended by the interface. The
3772 * order of the objects in the array corresponds to the order of the
3773 * interface types used in the 'extends' clause of the declaration of this
3774 * {@code Class} object.
3775 *
3776 * <p> If this {@code Class} object represents a class or interface whose
3777 * declaration does not explicitly indicate any annotated superinterfaces,
3778 * the return value is an array of length 0.
3779 *
3780 * <p> If this {@code Class} object represents either the {@code Object}
3781 * class, an array type, a primitive type, or void, the return value is an
3782 * array of length 0.
3783 *
3784 * @return an array representing the superinterfaces
3785 * @since 1.8
3786 */
3787 public AnnotatedType[] getAnnotatedInterfaces() {
3788 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3789 }
3790
3791 private native Class<?> getNestHost0();
3792
3793 /**
3794 * Returns the nest host of the <a href=#nest>nest</a> to which the class
3795 * or interface represented by this {@code Class} object belongs.
3796 * Every class and interface belongs to exactly one nest.
3797 *
3798 * If the nest host of this class or interface has previously
3799 * been determined, then this method returns the nest host.
3800 * If the nest host of this class or interface has
3801 * not previously been determined, then this method determines the nest
3802 * host using the algorithm of JVMS 5.4.4, and returns it.
3803 *
3804 * Often, a class or interface belongs to a nest consisting only of itself,
3805 * in which case this method returns {@code this} to indicate that the class
3806 * or interface is the nest host.
3807 *
3808 * <p>If this {@code Class} object represents a primitive type, an array type,
4108 * @since 17
4109 */
4110 public boolean isSealed() {
4111 if (isArray() || isPrimitive()) {
4112 return false;
4113 }
4114 return getPermittedSubclasses() != null;
4115 }
4116
4117 private native Class<?>[] getPermittedSubclasses0();
4118
4119 /*
4120 * Return the class's major and minor class file version packed into an int.
4121 * The high order 16 bits contain the class's minor version. The low order
4122 * 16 bits contain the class's major version.
4123 *
4124 * If the class is an array type then the class file version of its element
4125 * type is returned. If the class is a primitive type then the latest class
4126 * file major version is returned and zero is returned for the minor version.
4127 */
4128 private int getClassFileVersion() {
4129 Class<?> c = isArray() ? elementType() : this;
4130 return c.getClassFileVersion0();
4131 }
4132
4133 private native int getClassFileVersion0();
4134
4135 /*
4136 * Return the access flags as they were in the class's bytecode, including
4137 * the original setting of ACC_SUPER.
4138 *
4139 * If the class is an array type then the access flags of the element type is
4140 * returned. If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4141 */
4142 private int getClassAccessFlagsRaw() {
4143 Class<?> c = isArray() ? elementType() : this;
4144 return c.getClassAccessFlagsRaw0();
4145 }
4146
4147 private native int getClassAccessFlagsRaw0();
4148 }
|
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
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.lang.annotation.Annotation;
29 import java.lang.constant.ClassDesc;
30 import java.lang.constant.ConstantDescs;
31 import java.lang.invoke.TypeDescriptor;
32 import java.lang.invoke.MethodHandles;
33 import java.lang.ref.SoftReference;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.ObjectStreamField;
37 import java.lang.reflect.AnnotatedElement;
38 import java.lang.reflect.AnnotatedType;
39 import java.lang.reflect.AccessFlag;
40 import java.lang.reflect.Array;
41 import java.lang.reflect.ClassFileFormatVersion;
42 import java.lang.reflect.Constructor;
43 import java.lang.reflect.Executable;
44 import java.lang.reflect.Field;
45 import java.lang.reflect.GenericArrayType;
46 import java.lang.reflect.GenericDeclaration;
47 import java.lang.reflect.InvocationTargetException;
48 import java.lang.reflect.Member;
49 import java.lang.reflect.Method;
50 import java.lang.reflect.Modifier;
51 import java.lang.reflect.Proxy;
52 import java.lang.reflect.RecordComponent;
53 import java.lang.reflect.Type;
54 import java.lang.reflect.TypeVariable;
55 import java.lang.constant.Constable;
56 import java.net.URL;
57 import java.security.AllPermission;
58 import java.security.Permissions;
59 import java.security.ProtectionDomain;
60 import java.util.ArrayList;
61 import java.util.Arrays;
62 import java.util.Collection;
63 import java.util.HashMap;
64 import java.util.LinkedHashMap;
65 import java.util.LinkedHashSet;
66 import java.util.List;
67 import java.util.Map;
68 import java.util.Objects;
69 import java.util.Optional;
70 import java.util.Set;
71 import java.util.stream.Collectors;
72
73 import jdk.internal.constant.ConstantUtils;
74 import jdk.internal.javac.PreviewFeature;
75 import jdk.internal.loader.BootLoader;
76 import jdk.internal.loader.BuiltinClassLoader;
77 import jdk.internal.misc.PreviewFeatures;
78 import jdk.internal.misc.Unsafe;
79 import jdk.internal.module.Resources;
80 import jdk.internal.reflect.CallerSensitive;
81 import jdk.internal.reflect.CallerSensitiveAdapter;
82 import jdk.internal.reflect.ConstantPool;
83 import jdk.internal.reflect.Reflection;
84 import jdk.internal.reflect.ReflectionFactory;
85 import jdk.internal.vm.annotation.IntrinsicCandidate;
86 import jdk.internal.vm.annotation.Stable;
87
88 import sun.invoke.util.Wrapper;
89 import sun.reflect.generics.factory.CoreReflectionFactory;
90 import sun.reflect.generics.factory.GenericsFactory;
91 import sun.reflect.generics.repository.ClassRepository;
92 import sun.reflect.generics.repository.MethodRepository;
93 import sun.reflect.generics.repository.ConstructorRepository;
94 import sun.reflect.generics.scope.ClassScope;
95 import sun.reflect.annotation.*;
96
97 /**
203 * HelloWorld}, the methods to get the {@linkplain #getName name} and
204 * {@linkplain #getTypeName type name} return results
205 * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
206 * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
207 * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
208 *
209 * @param <T> the type of the class modeled by this {@code Class}
210 * object. For example, the type of {@code String.class} is {@code
211 * Class<String>}. Use {@code Class<?>} if the class being modeled is
212 * unknown.
213 *
214 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
215 * @since 1.0
216 */
217 public final class Class<T> implements java.io.Serializable,
218 GenericDeclaration,
219 Type,
220 AnnotatedElement,
221 TypeDescriptor.OfField<Class<?>>,
222 Constable {
223 private static final int ANNOTATION = 0x00002000;
224 private static final int ENUM = 0x00004000;
225 private static final int SYNTHETIC = 0x00001000;
226
227 private static native void registerNatives();
228 static {
229 runtimeSetup();
230 }
231
232 // Called from JVM when loading an AOT cache
233 private static void runtimeSetup() {
234 registerNatives();
235 }
236
237 /*
238 * Private constructor. Only the Java Virtual Machine creates Class objects.
239 * This constructor is not used and prevents the default constructor being
240 * generated.
241 */
242 private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim) {
243 // Initialize final field for classLoader. The initialization value of non-null
244 // prevents future JIT optimizations from assuming this final field is null.
245 // The following assignments are done directly by the VM without calling this constructor.
301 *
302 * @since 1.8
303 */
304 public String toGenericString() {
305 if (isPrimitive()) {
306 return toString();
307 } else {
308 StringBuilder sb = new StringBuilder();
309 Class<?> component = this;
310 int arrayDepth = 0;
311
312 if (isArray()) {
313 do {
314 arrayDepth++;
315 component = component.getComponentType();
316 } while (component.isArray());
317 sb.append(component.getName());
318 } else {
319 // Class modifiers are a superset of interface modifiers
320 int modifiers = getModifiers() & Modifier.classModifiers();
321 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
322 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
323 if (modifiers != 0) {
324 sb.append(Modifier.toString(modifiers));
325 sb.append(' ');
326 }
327
328 // A class cannot be strictfp and sealed/non-sealed so
329 // it is sufficient to check for sealed-ness after all
330 // modifiers are printed.
331 addSealingInfo(modifiers, sb);
332
333 if (isAnnotation()) {
334 sb.append('@');
335 }
336 if (isValue()) {
337 sb.append("value ");
338 }
339 if (isInterface()) { // Note: all annotation interfaces are interfaces
340 sb.append("interface");
341 } else {
342 if (isEnum())
343 sb.append("enum");
344 else if (isRecord())
345 sb.append("record");
346 else
347 sb.append("class");
348 }
349 sb.append(' ');
350 sb.append(getName());
351 }
352
353 TypeVariable<?>[] typeparms = component.getTypeParameters();
354 if (typeparms.length > 0) {
355 sb.append(Arrays.stream(typeparms)
356 .map(Class::typeVarBounds)
357 .collect(Collectors.joining(",", "<", ">")));
358 }
593 * @throws NullPointerException if the given module or name is {@code null}
594 *
595 * @throws LinkageError if the linkage fails
596 *
597 * @jls 12.2 Loading of Classes and Interfaces
598 * @jls 12.3 Linking of Classes and Interfaces
599 * @since 9
600 */
601 public static Class<?> forName(Module module, String name) {
602 Objects.requireNonNull(module);
603 Objects.requireNonNull(name);
604
605 ClassLoader cl = module.getClassLoader();
606 if (cl != null) {
607 return cl.loadClass(module, name);
608 } else {
609 return BootLoader.loadClass(module, name);
610 }
611 }
612
613 /**
614 * {@return {@code true} if this {@code Class} object represents an identity
615 * class or interface; otherwise {@code false}}
616 *
617 * If this {@code Class} object represents an array type, then this method
618 * returns {@code true}.
619 * If this {@code Class} object represents a primitive type, or {@code void},
620 * then this method returns {@code false}.
621 *
622 * @since Valhalla
623 */
624 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
625 public native boolean isIdentity();
626
627 /**
628 * {@return {@code true} if this {@code Class} object represents a value
629 * class; otherwise {@code false}}
630 *
631 * If this {@code Class} object represents an array type, an interface,
632 * a primitive type, or {@code void}, then this method returns {@code false}.
633 *
634 * @since Valhalla
635 */
636 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
637 public boolean isValue() {
638 if (!PreviewFeatures.isEnabled()) {
639 return false;
640 }
641 if (isPrimitive() || isArray() || isInterface())
642 return false;
643 return ((getModifiers() & Modifier.IDENTITY) == 0);
644 }
645
646 /**
647 * {@return the {@code Class} object associated with the
648 * {@linkplain #isPrimitive() primitive type} of the given name}
649 * If the argument is not the name of a primitive type, {@code
650 * null} is returned.
651 *
652 * @param primitiveName the name of the primitive type to find
653 *
654 * @throws NullPointerException if the argument is {@code null}
655 *
656 * @jls 4.2 Primitive Types and Values
657 * @jls 15.8.2 Class Literals
658 * @since 22
659 */
660 public static Class<?> forPrimitiveName(String primitiveName) {
661 return switch(primitiveName) {
662 // Integral types
663 case "int" -> int.class;
664 case "long" -> long.class;
665 case "short" -> short.class;
1352 }
1353 return c;
1354 }
1355
1356 /**
1357 * Returns the Java language modifiers for this class or interface, encoded
1358 * in an integer. The modifiers consist of the Java Virtual Machine's
1359 * constants for {@code public}, {@code protected},
1360 * {@code private}, {@code final}, {@code static},
1361 * {@code abstract} and {@code interface}; they should be decoded
1362 * using the methods of class {@code Modifier}.
1363 *
1364 * <p> If the underlying class is an array class:
1365 * <ul>
1366 * <li> its {@code public}, {@code private} and {@code protected}
1367 * modifiers are the same as those of its component type
1368 * <li> its {@code abstract} and {@code final} modifiers are always
1369 * {@code true}
1370 * <li> its interface modifier is always {@code false}, even when
1371 * the component type is an interface
1372 * <li> its {@code identity} modifier is always true
1373 * </ul>
1374 * If this {@code Class} object represents a primitive type or
1375 * void, its {@code public}, {@code abstract}, and {@code final}
1376 * modifiers are always {@code true}.
1377 * For {@code Class} objects representing void, primitive types, and
1378 * arrays, the values of other modifiers are {@code false} other
1379 * than as specified above.
1380 *
1381 * <p> The modifier encodings are defined in section {@jvms 4.1}
1382 * of <cite>The Java Virtual Machine Specification</cite>.
1383 *
1384 * @return the {@code int} representing the modifiers for this class
1385 * @see java.lang.reflect.Modifier
1386 * @see #accessFlags()
1387 * @see <a
1388 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1389 * programming language and JVM modeling in core reflection</a>
1390 * @since 1.1
1391 * @jls 8.1.1 Class Modifiers
1392 * @jls 9.1.1 Interface Modifiers
1393 * @jvms 4.1 The {@code ClassFile} Structure
1394 */
1395 public int getModifiers() { return modifiers; }
1396
1397 /**
1398 * {@return an unmodifiable set of the {@linkplain AccessFlag access
1399 * flags} for this class, possibly empty}
1400 * The {@code AccessFlags} may depend on the class file format version of the class.
1401 *
1402 * <p> If the underlying class is an array class:
1403 * <ul>
1404 * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1405 * access flags are the same as those of its component type
1406 * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1407 * <li> its {@code INTERFACE} flag is absent, even when the
1408 * component type is an interface
1409 * <li> its {@code identity} modifier is always true
1410 * </ul>
1411 * If this {@code Class} object represents a primitive type or
1412 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1413 * {@code FINAL}.
1414 * For {@code Class} objects representing void, primitive types, and
1415 * arrays, access flags are absent other than as specified above.
1416 *
1417 * @see #getModifiers()
1418 * @jvms 4.1 The ClassFile Structure
1419 * @jvms 4.7.6 The InnerClasses Attribute
1420 * @since 20
1421 */
1422 public Set<AccessFlag> accessFlags() {
1423 // Location.CLASS allows SUPER and AccessFlag.MODULE which
1424 // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1425 // and STATIC, which are not allowed on Location.CLASS.
1426 // Use getClassAccessFlagsRaw to expose SUPER status.
1427 var location = (isMemberClass() || isLocalClass() ||
1428 isAnonymousClass() || isArray()) ?
1429 AccessFlag.Location.INNER_CLASS :
1430 AccessFlag.Location.CLASS;
1431 int accessFlags = (location == AccessFlag.Location.CLASS) ?
1432 getClassAccessFlagsRaw() : getModifiers();
1433 if (isArray() && PreviewFeatures.isEnabled()) {
1434 accessFlags |= Modifier.IDENTITY;
1435 }
1436 var cffv = ClassFileFormatVersion.fromMajor(getClassFileVersion() & 0xffff);
1437 if (cffv.compareTo(ClassFileFormatVersion.latest()) >= 0) {
1438 // Ignore unspecified (0x0800) access flag for current version
1439 accessFlags &= ~0x0800;
1440 }
1441 return AccessFlag.maskToAccessFlags(accessFlags, location, cffv);
1442 }
1443
1444 /**
1445 * Gets the signers of this class.
1446 *
1447 * @return the signers of this class, or null if there are no signers. In
1448 * particular, this method returns null if this {@code Class} object represents
1449 * a primitive type or void.
1450 * @since 1.1
1451 */
1452
1453 public Object[] getSigners() {
1454 var signers = this.signers;
1455 return signers == null ? null : signers.clone();
1456 }
1457
1458 /**
1459 * Set the signers of this class.
1460 */
1461 void setSigners(Object[] signers) {
1462 if (!isPrimitive() && !isArray()) {
1463 this.signers = signers;
1464 }
1465 }
1466
1467 /**
1468 * If this {@code Class} object represents a local or anonymous
1469 * class within a method, returns a {@link
1470 * java.lang.reflect.Method Method} object representing the
1471 * immediately enclosing method of the underlying class. Returns
1472 * {@code null} otherwise.
3820 *
3821 * <p> If this {@code Class} object represents an interface, the return
3822 * value is an array containing objects representing the uses of interface
3823 * types to specify interfaces directly extended by the interface. The
3824 * order of the objects in the array corresponds to the order of the
3825 * interface types used in the 'extends' clause of the declaration of this
3826 * {@code Class} object.
3827 *
3828 * <p> If this {@code Class} object represents a class or interface whose
3829 * declaration does not explicitly indicate any annotated superinterfaces,
3830 * the return value is an array of length 0.
3831 *
3832 * <p> If this {@code Class} object represents either the {@code Object}
3833 * class, an array type, a primitive type, or void, the return value is an
3834 * array of length 0.
3835 *
3836 * @return an array representing the superinterfaces
3837 * @since 1.8
3838 */
3839 public AnnotatedType[] getAnnotatedInterfaces() {
3840 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3841 }
3842
3843 private native Class<?> getNestHost0();
3844
3845 /**
3846 * Returns the nest host of the <a href=#nest>nest</a> to which the class
3847 * or interface represented by this {@code Class} object belongs.
3848 * Every class and interface belongs to exactly one nest.
3849 *
3850 * If the nest host of this class or interface has previously
3851 * been determined, then this method returns the nest host.
3852 * If the nest host of this class or interface has
3853 * not previously been determined, then this method determines the nest
3854 * host using the algorithm of JVMS 5.4.4, and returns it.
3855 *
3856 * Often, a class or interface belongs to a nest consisting only of itself,
3857 * in which case this method returns {@code this} to indicate that the class
3858 * or interface is the nest host.
3859 *
3860 * <p>If this {@code Class} object represents a primitive type, an array type,
4160 * @since 17
4161 */
4162 public boolean isSealed() {
4163 if (isArray() || isPrimitive()) {
4164 return false;
4165 }
4166 return getPermittedSubclasses() != null;
4167 }
4168
4169 private native Class<?>[] getPermittedSubclasses0();
4170
4171 /*
4172 * Return the class's major and minor class file version packed into an int.
4173 * The high order 16 bits contain the class's minor version. The low order
4174 * 16 bits contain the class's major version.
4175 *
4176 * If the class is an array type then the class file version of its element
4177 * type is returned. If the class is a primitive type then the latest class
4178 * file major version is returned and zero is returned for the minor version.
4179 */
4180 /* package-private */
4181 int getClassFileVersion() {
4182 Class<?> c = isArray() ? elementType() : this;
4183 return c.getClassFileVersion0();
4184 }
4185
4186 private native int getClassFileVersion0();
4187
4188 /*
4189 * Return the access flags as they were in the class's bytecode, including
4190 * the original setting of ACC_SUPER.
4191 *
4192 * If the class is an array type then the access flags of the element type is
4193 * returned. If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4194 */
4195 private int getClassAccessFlagsRaw() {
4196 Class<?> c = isArray() ? elementType() : this;
4197 return c.getClassAccessFlagsRaw0();
4198 }
4199
4200 private native int getClassAccessFlagsRaw0();
4201 }
|