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 }
351
352 if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
353
354 return sb.toString();
355 }
356 }
357
358 private void addSealingInfo(int modifiers, StringBuilder sb) {
359 // A class can be final XOR sealed XOR non-sealed.
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 getReflectionFactory().parseAccessFlags((location == AccessFlag.Location.CLASS) ?
1388 getClassAccessFlagsRaw() : getModifiers(), location, this);
1389 }
1390
1391 /**
1392 * Gets the signers of this class.
1393 *
1394 * @return the signers of this class, or null if there are no signers. In
1395 * particular, this method returns null if this {@code Class} object represents
1396 * a primitive type or void.
1397 * @since 1.1
1398 */
1399 public Object[] getSigners() {
1400 var signers = this.signers;
1401 return signers == null ? null : signers.clone();
1402 }
1403
1404 /**
1405 * Set the signers of this class.
1406 */
1407 void setSigners(Object[] signers) {
1408 if (!isPrimitive() && !isArray()) {
1409 this.signers = signers;
1410 }
1411 }
1412
1413 /**
1414 * If this {@code Class} object represents a local or anonymous
1415 * class within a method, returns a {@link
1416 * java.lang.reflect.Method Method} object representing the
1417 * immediately enclosing method of the underlying class. Returns
1418 * {@code null} otherwise.
3766 *
3767 * <p> If this {@code Class} object represents an interface, the return
3768 * value is an array containing objects representing the uses of interface
3769 * types to specify interfaces directly extended by the interface. The
3770 * order of the objects in the array corresponds to the order of the
3771 * interface types used in the 'extends' clause of the declaration of this
3772 * {@code Class} object.
3773 *
3774 * <p> If this {@code Class} object represents a class or interface whose
3775 * declaration does not explicitly indicate any annotated superinterfaces,
3776 * the return value is an array of length 0.
3777 *
3778 * <p> If this {@code Class} object represents either the {@code Object}
3779 * class, an array type, a primitive type, or void, the return value is an
3780 * array of length 0.
3781 *
3782 * @return an array representing the superinterfaces
3783 * @since 1.8
3784 */
3785 public AnnotatedType[] getAnnotatedInterfaces() {
3786 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3787 }
3788
3789 private native Class<?> getNestHost0();
3790
3791 /**
3792 * Returns the nest host of the <a href=#nest>nest</a> to which the class
3793 * or interface represented by this {@code Class} object belongs.
3794 * Every class and interface belongs to exactly one nest.
3795 *
3796 * If the nest host of this class or interface has previously
3797 * been determined, then this method returns the nest host.
3798 * If the nest host of this class or interface has
3799 * not previously been determined, then this method determines the nest
3800 * host using the algorithm of JVMS 5.4.4, and returns it.
3801 *
3802 * Often, a class or interface belongs to a nest consisting only of itself,
3803 * in which case this method returns {@code this} to indicate that the class
3804 * or interface is the nest host.
3805 *
3806 * <p>If this {@code Class} object represents a primitive type, an array type,
4106 * @since 17
4107 */
4108 public boolean isSealed() {
4109 if (isArray() || isPrimitive()) {
4110 return false;
4111 }
4112 return getPermittedSubclasses() != null;
4113 }
4114
4115 private native Class<?>[] getPermittedSubclasses0();
4116
4117 /*
4118 * Return the class's major and minor class file version packed into an int.
4119 * The high order 16 bits contain the class's minor version. The low order
4120 * 16 bits contain the class's major version.
4121 *
4122 * If the class is an array type then the class file version of its element
4123 * type is returned. If the class is a primitive type then the latest class
4124 * file major version is returned and zero is returned for the minor version.
4125 */
4126 int getClassFileVersion() {
4127 Class<?> c = isArray() ? elementType() : this;
4128 return c.getClassFileVersion0();
4129 }
4130
4131 private native int getClassFileVersion0();
4132
4133 /*
4134 * Return the access flags as they were in the class's bytecode, including
4135 * the original setting of ACC_SUPER.
4136 *
4137 * If the class is an array type then the access flags of the element type is
4138 * returned. If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4139 */
4140 private int getClassAccessFlagsRaw() {
4141 Class<?> c = isArray() ? elementType() : this;
4142 return c.getClassAccessFlagsRaw0();
4143 }
4144
4145 private native int getClassAccessFlagsRaw0();
|
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.HashSet;
65 import java.util.LinkedHashMap;
66 import java.util.LinkedHashSet;
67 import java.util.List;
68 import java.util.Map;
69 import java.util.Objects;
70 import java.util.Optional;
71 import java.util.Set;
72 import java.util.stream.Collectors;
73
74 import jdk.internal.constant.ConstantUtils;
75 import jdk.internal.javac.PreviewFeature;
76 import jdk.internal.loader.BootLoader;
77 import jdk.internal.loader.BuiltinClassLoader;
78 import jdk.internal.misc.PreviewFeatures;
79 import jdk.internal.misc.Unsafe;
80 import jdk.internal.module.Resources;
81 import jdk.internal.reflect.CallerSensitive;
82 import jdk.internal.reflect.CallerSensitiveAdapter;
83 import jdk.internal.reflect.ConstantPool;
84 import jdk.internal.reflect.Reflection;
85 import jdk.internal.reflect.ReflectionFactory;
86 import jdk.internal.vm.annotation.IntrinsicCandidate;
87 import jdk.internal.vm.annotation.Stable;
88
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.repository.MethodRepository;
94 import sun.reflect.generics.repository.ConstructorRepository;
95 import sun.reflect.generics.scope.ClassScope;
96 import sun.reflect.annotation.*;
97
98 /**
204 * HelloWorld}, the methods to get the {@linkplain #getName name} and
205 * {@linkplain #getTypeName type name} return results
206 * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
207 * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
208 * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
209 *
210 * @param <T> the type of the class modeled by this {@code Class}
211 * object. For example, the type of {@code String.class} is {@code
212 * Class<String>}. Use {@code Class<?>} if the class being modeled is
213 * unknown.
214 *
215 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
216 * @since 1.0
217 */
218 public final class Class<T> implements java.io.Serializable,
219 GenericDeclaration,
220 Type,
221 AnnotatedElement,
222 TypeDescriptor.OfField<Class<?>>,
223 Constable {
224 private static final int ANNOTATION = 0x00002000;
225 private static final int ENUM = 0x00004000;
226 private static final int SYNTHETIC = 0x00001000;
227
228 private static native void registerNatives();
229 static {
230 runtimeSetup();
231 }
232
233 // Called from JVM when loading an AOT cache
234 private static void runtimeSetup() {
235 registerNatives();
236 }
237
238 /*
239 * Private constructor. Only the Java Virtual Machine creates Class objects.
240 * This constructor is not used and prevents the default constructor being
241 * generated.
242 */
243 private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim) {
244 // Initialize final field for classLoader. The initialization value of non-null
245 // prevents future JIT optimizations from assuming this final field is null.
246 // The following assignments are done directly by the VM without calling this constructor.
302 *
303 * @since 1.8
304 */
305 public String toGenericString() {
306 if (isPrimitive()) {
307 return toString();
308 } else {
309 StringBuilder sb = new StringBuilder();
310 Class<?> component = this;
311 int arrayDepth = 0;
312
313 if (isArray()) {
314 do {
315 arrayDepth++;
316 component = component.getComponentType();
317 } while (component.isArray());
318 sb.append(component.getName());
319 } else {
320 // Class modifiers are a superset of interface modifiers
321 int modifiers = getModifiers() & Modifier.classModifiers();
322 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
323 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
324 if (modifiers != 0) {
325 sb.append(Modifier.toString(modifiers));
326 sb.append(' ');
327 }
328
329 // A class cannot be strictfp and sealed/non-sealed so
330 // it is sufficient to check for sealed-ness after all
331 // modifiers are printed.
332 addSealingInfo(modifiers, sb);
333
334 if (isAnnotation()) {
335 sb.append('@');
336 }
337 if (isInterface()) { // Note: all annotation interfaces are interfaces
338 sb.append("interface");
339 } else {
340 if (isEnum())
341 sb.append("enum");
342 else {
343 if (isValue()) {
344 sb.append("value ");
345 }
346 if (isRecord())
347 sb.append("record");
348 else
349 sb.append("class");
350 }
351 }
352 sb.append(' ');
353 sb.append(getName());
354 }
355
356 TypeVariable<?>[] typeparms = component.getTypeParameters();
357 if (typeparms.length > 0) {
358 sb.append(Arrays.stream(typeparms)
359 .map(Class::typeVarBounds)
360 .collect(Collectors.joining(",", "<", ">")));
361 }
362
363 if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
364
365 return sb.toString();
366 }
367 }
368
369 private void addSealingInfo(int modifiers, StringBuilder sb) {
370 // A class can be final XOR sealed XOR non-sealed.
596 * @throws NullPointerException if the given module or name is {@code null}
597 *
598 * @throws LinkageError if the linkage fails
599 *
600 * @jls 12.2 Loading of Classes and Interfaces
601 * @jls 12.3 Linking of Classes and Interfaces
602 * @since 9
603 */
604 public static Class<?> forName(Module module, String name) {
605 Objects.requireNonNull(module);
606 Objects.requireNonNull(name);
607
608 ClassLoader cl = module.getClassLoader();
609 if (cl != null) {
610 return cl.loadClass(module, name);
611 } else {
612 return BootLoader.loadClass(module, name);
613 }
614 }
615
616 /**
617 * {@return {@code true} if this {@code Class} object represents an identity class,
618 * otherwise {@code false}}
619 *
620 * <ul>
621 * <li>
622 * If this {@code Class} object represents an array type this method returns {@code true}.
623 * <li>
624 * If this {@code Class} object represents an interface, a primitive type,
625 * or {@code void} this method returns {@code false}.
626 * <li>
627 * For all other {@code Class} objects, this method returns {@code true} if either
628 * preview features are disabled or {@linkplain Modifier#IDENTITY} is set in the
629 * {@linkplain #getModifiers() class modifiers}.
630 * </ul>
631 * @see AccessFlag#IDENTITY
632 * @since Valhalla
633 */
634 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
635 public boolean isIdentity() {
636 if (isPrimitive()) {
637 return false;
638 } else if (PreviewFeatures.isEnabled()) {
639 return isArray() || Modifier.isIdentity(modifiers);
640 } else {
641 return !isInterface();
642 }
643 }
644
645 /**
646 * {@return {@code true} if this {@code Class} object represents a value class,
647 * otherwise {@code false}}
648 * <ul>
649 * <li>
650 * If this {@code Class} object represents an array type this method returns {@code false}.
651 * <li>
652 * If this {@code Class} object represents an interface, a primitive type,
653 * or {@code void} this method returns {@code true} only if preview features are enabled.
654 * <li>
655 * For all other {@code Class} objects, this method returns {@code true} only if
656 * preview features are enabled and {@linkplain Modifier#IDENTITY} is not set in the
657 * {@linkplain #getModifiers() class modifiers}.
658 * </ul>
659 * @see AccessFlag#IDENTITY
660 * @since Valhalla
661 */
662 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
663 public boolean isValue() {
664 if (!PreviewFeatures.isEnabled()) {
665 return false;
666 }
667 return !isIdentity();
668 }
669
670 /**
671 * {@return the {@code Class} object associated with the
672 * {@linkplain #isPrimitive() primitive type} of the given name}
673 * If the argument is not the name of a primitive type, {@code
674 * null} is returned.
675 *
676 * @param primitiveName the name of the primitive type to find
677 *
678 * @throws NullPointerException if the argument is {@code null}
679 *
680 * @jls 4.2 Primitive Types and Values
681 * @jls 15.8.2 Class Literals
682 * @since 22
683 */
684 public static Class<?> forPrimitiveName(String primitiveName) {
685 return switch(primitiveName) {
686 // Integral types
687 case "int" -> int.class;
688 case "long" -> long.class;
689 case "short" -> short.class;
1376 }
1377 return c;
1378 }
1379
1380 /**
1381 * Returns the Java language modifiers for this class or interface, encoded
1382 * in an integer. The modifiers consist of the Java Virtual Machine's
1383 * constants for {@code public}, {@code protected},
1384 * {@code private}, {@code final}, {@code static},
1385 * {@code abstract} and {@code interface}; they should be decoded
1386 * using the methods of class {@code Modifier}.
1387 *
1388 * <p> If the underlying class is an array class:
1389 * <ul>
1390 * <li> its {@code public}, {@code private} and {@code protected}
1391 * modifiers are the same as those of its component type
1392 * <li> its {@code abstract} and {@code final} modifiers are always
1393 * {@code true}
1394 * <li> its interface modifier is always {@code false}, even when
1395 * the component type is an interface
1396 * <li> its {@code identity} modifier is always true
1397 * </ul>
1398 * If this {@code Class} object represents a primitive type or
1399 * void, its {@code public}, {@code abstract}, and {@code final}
1400 * modifiers are always {@code true}.
1401 * For {@code Class} objects representing void, primitive types, and
1402 * arrays, the values of other modifiers are {@code false} other
1403 * than as specified above.
1404 *
1405 * <p> The modifier encodings are defined in section {@jvms 4.1}
1406 * of <cite>The Java Virtual Machine Specification</cite>.
1407 *
1408 * @return the {@code int} representing the modifiers for this class
1409 * @see java.lang.reflect.Modifier
1410 * @see #accessFlags()
1411 * @see <a
1412 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1413 * programming language and JVM modeling in core reflection</a>
1414 * @since 1.1
1415 * @jls 8.1.1 Class Modifiers
1416 * @jls 9.1.1 Interface Modifiers
1417 * @jvms 4.1 The {@code ClassFile} Structure
1418 */
1419 public int getModifiers() { return modifiers; }
1420
1421 /**
1422 * {@return an unmodifiable set of the {@linkplain AccessFlag access
1423 * flags} for this class, possibly empty}
1424 * The {@code AccessFlags} may depend on the class file format version of the class.
1425 *
1426 * <p> If the underlying class is an array class:
1427 * <ul>
1428 * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1429 * access flags are the same as those of its component type
1430 * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1431 * <li> its {@code INTERFACE} flag is absent, even when the
1432 * component type is an interface
1433 * <li> its {@code identity} modifier is always true
1434 * </ul>
1435 * If this {@code Class} object represents a primitive type or
1436 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1437 * {@code FINAL}.
1438 * For {@code Class} objects representing void, primitive types, and
1439 * arrays, access flags are absent other than as specified above.
1440 *
1441 * @see #getModifiers()
1442 * @jvms 4.1 The ClassFile Structure
1443 * @jvms 4.7.6 The InnerClasses Attribute
1444 * @since 20
1445 */
1446 public Set<AccessFlag> accessFlags() {
1447 // Location.CLASS allows SUPER and AccessFlag.MODULE which
1448 // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1449 // and STATIC, which are not allowed on Location.CLASS.
1450 // Use getClassAccessFlagsRaw to expose SUPER status.
1451 // Arrays need to use PRIVATE/PROTECTED from its component modifiers.
1452 var location = (isMemberClass() || isLocalClass() ||
1453 isAnonymousClass() || isArray()) ?
1454 AccessFlag.Location.INNER_CLASS :
1455 AccessFlag.Location.CLASS;
1456 int accessFlags = location == AccessFlag.Location.CLASS ? getClassAccessFlagsRaw() : getModifiers();
1457 var reflectionFactory = getReflectionFactory();
1458 var ans = reflectionFactory.parseAccessFlags(accessFlags, location, this);
1459 if (PreviewFeatures.isEnabled() && reflectionFactory.classFileFormatVersion(this) != ClassFileFormatVersion.CURRENT_PREVIEW_FEATURES
1460 && isIdentity()) {
1461 var set = new HashSet<>(ans);
1462 set.add(AccessFlag.IDENTITY);
1463 return Set.copyOf(set);
1464 }
1465 return ans;
1466 }
1467
1468 /**
1469 * Gets the signers of this class.
1470 *
1471 * @return the signers of this class, or null if there are no signers. In
1472 * particular, this method returns null if this {@code Class} object represents
1473 * a primitive type or void.
1474 * @since 1.1
1475 */
1476
1477 public Object[] getSigners() {
1478 var signers = this.signers;
1479 return signers == null ? null : signers.clone();
1480 }
1481
1482 /**
1483 * Set the signers of this class.
1484 */
1485 void setSigners(Object[] signers) {
1486 if (!isPrimitive() && !isArray()) {
1487 this.signers = signers;
1488 }
1489 }
1490
1491 /**
1492 * If this {@code Class} object represents a local or anonymous
1493 * class within a method, returns a {@link
1494 * java.lang.reflect.Method Method} object representing the
1495 * immediately enclosing method of the underlying class. Returns
1496 * {@code null} otherwise.
3844 *
3845 * <p> If this {@code Class} object represents an interface, the return
3846 * value is an array containing objects representing the uses of interface
3847 * types to specify interfaces directly extended by the interface. The
3848 * order of the objects in the array corresponds to the order of the
3849 * interface types used in the 'extends' clause of the declaration of this
3850 * {@code Class} object.
3851 *
3852 * <p> If this {@code Class} object represents a class or interface whose
3853 * declaration does not explicitly indicate any annotated superinterfaces,
3854 * the return value is an array of length 0.
3855 *
3856 * <p> If this {@code Class} object represents either the {@code Object}
3857 * class, an array type, a primitive type, or void, the return value is an
3858 * array of length 0.
3859 *
3860 * @return an array representing the superinterfaces
3861 * @since 1.8
3862 */
3863 public AnnotatedType[] getAnnotatedInterfaces() {
3864 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3865 }
3866
3867 private native Class<?> getNestHost0();
3868
3869 /**
3870 * Returns the nest host of the <a href=#nest>nest</a> to which the class
3871 * or interface represented by this {@code Class} object belongs.
3872 * Every class and interface belongs to exactly one nest.
3873 *
3874 * If the nest host of this class or interface has previously
3875 * been determined, then this method returns the nest host.
3876 * If the nest host of this class or interface has
3877 * not previously been determined, then this method determines the nest
3878 * host using the algorithm of JVMS 5.4.4, and returns it.
3879 *
3880 * Often, a class or interface belongs to a nest consisting only of itself,
3881 * in which case this method returns {@code this} to indicate that the class
3882 * or interface is the nest host.
3883 *
3884 * <p>If this {@code Class} object represents a primitive type, an array type,
4184 * @since 17
4185 */
4186 public boolean isSealed() {
4187 if (isArray() || isPrimitive()) {
4188 return false;
4189 }
4190 return getPermittedSubclasses() != null;
4191 }
4192
4193 private native Class<?>[] getPermittedSubclasses0();
4194
4195 /*
4196 * Return the class's major and minor class file version packed into an int.
4197 * The high order 16 bits contain the class's minor version. The low order
4198 * 16 bits contain the class's major version.
4199 *
4200 * If the class is an array type then the class file version of its element
4201 * type is returned. If the class is a primitive type then the latest class
4202 * file major version is returned and zero is returned for the minor version.
4203 */
4204 /* package-private */
4205 int getClassFileVersion() {
4206 Class<?> c = isArray() ? elementType() : this;
4207 return c.getClassFileVersion0();
4208 }
4209
4210 private native int getClassFileVersion0();
4211
4212 /*
4213 * Return the access flags as they were in the class's bytecode, including
4214 * the original setting of ACC_SUPER.
4215 *
4216 * If the class is an array type then the access flags of the element type is
4217 * returned. If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4218 */
4219 private int getClassAccessFlagsRaw() {
4220 Class<?> c = isArray() ? elementType() : this;
4221 return c.getClassAccessFlagsRaw0();
4222 }
4223
4224 private native int getClassAccessFlagsRaw0();
|