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