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.
594 *
595 * @jls 12.2 Loading of Classes and Interfaces
596 * @jls 12.3 Linking of Classes and Interfaces
597 * @since 9
598 */
599 public static Class<?> forName(Module module, String name) {
600 Objects.requireNonNull(module);
601 Objects.requireNonNull(name);
602 if (!ModifiedUtf.isValidLengthInConstantPool(name)) {
603 return null;
604 }
605
606 ClassLoader cl = module.getClassLoader();
607 if (cl != null) {
608 return cl.loadClass(module, name);
609 } else {
610 return BootLoader.loadClass(module, name);
611 }
612 }
613
614 /**
615 * {@return the {@code Class} object associated with the
616 * {@linkplain #isPrimitive() primitive type} of the given name}
617 * If the argument is not the name of a primitive type, {@code
618 * null} is returned.
619 *
620 * @param primitiveName the name of the primitive type to find
621 *
622 * @throws NullPointerException if the argument is {@code null}
623 *
624 * @jls 4.2 Primitive Types and Values
625 * @jls 15.8.2 Class Literals
626 * @since 22
627 */
628 public static Class<?> forPrimitiveName(String primitiveName) {
629 return switch(primitiveName) {
630 // Integral types
631 case "int" -> int.class;
632 case "long" -> long.class;
633 case "short" -> short.class;
1321 }
1322 return c;
1323 }
1324
1325 /**
1326 * Returns the Java language modifiers for this class or interface, encoded
1327 * in an integer. The modifiers consist of the Java Virtual Machine's
1328 * constants for {@code public}, {@code protected},
1329 * {@code private}, {@code final}, {@code static},
1330 * {@code abstract} and {@code interface}; they should be decoded
1331 * using the methods of class {@code Modifier}.
1332 *
1333 * <p> If the underlying class is an array class:
1334 * <ul>
1335 * <li> its {@code public}, {@code private} and {@code protected}
1336 * modifiers are the same as those of its component type
1337 * <li> its {@code abstract} and {@code final} modifiers are always
1338 * {@code true}
1339 * <li> its interface modifier is always {@code false}, even when
1340 * the component type is an interface
1341 * </ul>
1342 * If this {@code Class} object represents a primitive type or
1343 * void, its {@code public}, {@code abstract}, and {@code final}
1344 * modifiers are always {@code true}.
1345 * For {@code Class} objects representing void, primitive types, and
1346 * arrays, the values of other modifiers are {@code false} other
1347 * than as specified above.
1348 *
1349 * <p> The modifier encodings are defined in section {@jvms 4.1}
1350 * of <cite>The Java Virtual Machine Specification</cite>.
1351 *
1352 * @return the {@code int} representing the modifiers for this class
1353 * @see java.lang.reflect.Modifier
1354 * @see #accessFlags()
1355 * @see <a
1356 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1357 * programming language and JVM modeling in core reflection</a>
1358 * @since 1.1
1359 * @jls 8.1.1 Class Modifiers
1360 * @jls 9.1.1 Interface Modifiers
1361 * @jvms 4.1 The {@code ClassFile} Structure
1362 */
1363 public int getModifiers() { return modifiers; }
1364
1365 /**
1366 * {@return an unmodifiable set of the {@linkplain AccessFlag access
1367 * flags} for this class, possibly empty}
1368 *
1369 * <p> If the underlying class is an array class:
1370 * <ul>
1371 * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1372 * access flags are the same as those of its component type
1373 * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1374 * <li> its {@code INTERFACE} flag is absent, even when the
1375 * component type is an interface
1376 * </ul>
1377 * If this {@code Class} object represents a primitive type or
1378 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1379 * {@code FINAL}.
1380 * For {@code Class} objects representing void, primitive types, and
1381 * arrays, access flags are absent other than as specified above.
1382 *
1383 * @see #getModifiers()
1384 * @jvms 4.1 The ClassFile Structure
1385 * @jvms 4.7.6 The InnerClasses Attribute
1386 * @since 20
1387 */
1388 public Set<AccessFlag> accessFlags() {
1389 // Location.CLASS allows SUPER and AccessFlag.MODULE which
1390 // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1391 // and STATIC, which are not allowed on Location.CLASS.
1392 // Use getClassFileAccessFlags to expose SUPER status.
1393 var location = (isMemberClass() || isLocalClass() ||
1394 isAnonymousClass() || isArray()) ?
1395 AccessFlag.Location.INNER_CLASS :
1396 AccessFlag.Location.CLASS;
1397 return getReflectionFactory().parseAccessFlags((location == AccessFlag.Location.CLASS) ?
1398 getClassFileAccessFlags() : getModifiers(), location, this);
1399 }
1400
1401 /**
1402 * Gets the signers of this class.
1403 *
1404 * @return the signers of this class, or null if there are no signers. In
1405 * particular, this method returns null if this {@code Class} object represents
1406 * a primitive type or void.
1407 * @since 1.1
1408 */
1409 public Object[] getSigners() {
1410 var signers = this.signers;
1411 return signers == null ? null : signers.clone();
1412 }
1413
1414 /**
1415 * Set the signers of this class.
1416 */
1417 void setSigners(Object[] signers) {
1418 if (!isPrimitive() && !isArray()) {
1419 this.signers = signers;
1420 }
1421 }
1422
1423 /**
1424 * If this {@code Class} object represents a local or anonymous
1425 * class within a method, returns a {@link
1426 * java.lang.reflect.Method Method} object representing the
1427 * immediately enclosing method of the underlying class. Returns
1428 * {@code null} otherwise.
3762 *
3763 * <p> If this {@code Class} object represents an interface, the return
3764 * value is an array containing objects representing the uses of interface
3765 * types to specify interfaces directly extended by the interface. The
3766 * order of the objects in the array corresponds to the order of the
3767 * interface types used in the 'extends' clause of the declaration of this
3768 * {@code Class} object.
3769 *
3770 * <p> If this {@code Class} object represents a class or interface whose
3771 * declaration does not explicitly indicate any annotated superinterfaces,
3772 * the return value is an array of length 0.
3773 *
3774 * <p> If this {@code Class} object represents either the {@code Object}
3775 * class, an array type, a primitive type, or void, the return value is an
3776 * array of length 0.
3777 *
3778 * @return an array representing the superinterfaces
3779 * @since 1.8
3780 */
3781 public AnnotatedType[] getAnnotatedInterfaces() {
3782 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3783 }
3784
3785 private native Class<?> getNestHost0();
3786
3787 /**
3788 * Returns the nest host of the <a href=#nest>nest</a> to which the class
3789 * or interface represented by this {@code Class} object belongs.
3790 * Every class and interface belongs to exactly one nest.
3791 *
3792 * If the nest host of this class or interface has previously
3793 * been determined, then this method returns the nest host.
3794 * If the nest host of this class or interface has
3795 * not previously been determined, then this method determines the nest
3796 * host using the algorithm of JVMS 5.4.4, and returns it.
3797 *
3798 * Often, a class or interface belongs to a nest consisting only of itself,
3799 * in which case this method returns {@code this} to indicate that the class
3800 * or interface is the nest host.
3801 *
3802 * <p>If this {@code Class} object represents a primitive type, an array type,
4102 * @since 17
4103 */
4104 public boolean isSealed() {
4105 if (isArray() || isPrimitive()) {
4106 return false;
4107 }
4108 return getPermittedSubclasses() != null;
4109 }
4110
4111 private native Class<?>[] getPermittedSubclasses0();
4112
4113 /*
4114 * Return the class's major and minor class file version packed into an int.
4115 * The high order 16 bits contain the class's minor version. The low order
4116 * 16 bits contain the class's major version.
4117 *
4118 * If the class is an array type then the class file version of its element
4119 * type is returned. If the class is a primitive type then the latest class
4120 * file major version is returned and zero is returned for the minor version.
4121 */
4122 int getClassFileVersion() {
4123 Class<?> c = isArray() ? elementType() : this;
4124 return c.getClassFileVersion0();
4125 }
4126
4127 private native int getClassFileVersion0();
4128
4129 /**
4130 * Return the access flags as they were in the class's bytecode, including
4131 * the original setting of ACC_SUPER.
4132 *
4133 * If this {@code Class} object represents a primitive type or
4134 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
4135 * {@code FINAL}.
4136 * If this {@code Class} object represents an array type, return 0.
4137 */
4138 int getClassFileAccessFlags() {
4139 return classFileAccessFlags;
4140 }
4141
|
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.
605 *
606 * @jls 12.2 Loading of Classes and Interfaces
607 * @jls 12.3 Linking of Classes and Interfaces
608 * @since 9
609 */
610 public static Class<?> forName(Module module, String name) {
611 Objects.requireNonNull(module);
612 Objects.requireNonNull(name);
613 if (!ModifiedUtf.isValidLengthInConstantPool(name)) {
614 return null;
615 }
616
617 ClassLoader cl = module.getClassLoader();
618 if (cl != null) {
619 return cl.loadClass(module, name);
620 } else {
621 return BootLoader.loadClass(module, name);
622 }
623 }
624
625 /**
626 * {@return {@code true} if this {@code Class} object represents an identity class,
627 * otherwise {@code false}}
628 *
629 * <ul>
630 * <li>
631 * If this {@code Class} object represents an array type this method returns {@code true}.
632 * <li>
633 * If this {@code Class} object represents an interface, a primitive type,
634 * or {@code void} this method returns {@code false}.
635 * <li>
636 * For all other {@code Class} objects, this method returns {@code true} if either
637 * preview features are disabled or {@linkplain Modifier#IDENTITY} is set in the
638 * {@linkplain #getModifiers() class modifiers}.
639 * </ul>
640 * @see AccessFlag#IDENTITY
641 * @since Valhalla
642 */
643 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
644 public boolean isIdentity() {
645 if (isPrimitive()) {
646 return false;
647 } else if (PreviewFeatures.isEnabled()) {
648 return isArray() || Modifier.isIdentity(modifiers);
649 } else {
650 return !isInterface();
651 }
652 }
653
654 /**
655 * {@return {@code true} if this {@code Class} object represents a value class,
656 * otherwise {@code false}}
657 * <ul>
658 * <li>
659 * If this {@code Class} object represents an array type this method returns {@code false}.
660 * <li>
661 * If this {@code Class} object represents an interface, a primitive type,
662 * or {@code void} this method returns {@code true} only if preview features are enabled.
663 * <li>
664 * For all other {@code Class} objects, this method returns {@code true} only if
665 * preview features are enabled and {@linkplain Modifier#IDENTITY} is not set in the
666 * {@linkplain #getModifiers() class modifiers}.
667 * </ul>
668 * @see AccessFlag#IDENTITY
669 * @since Valhalla
670 */
671 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
672 public boolean isValue() {
673 if (!PreviewFeatures.isEnabled()) {
674 return false;
675 }
676 return !isIdentity();
677 }
678
679 /**
680 * {@return the {@code Class} object associated with the
681 * {@linkplain #isPrimitive() primitive type} of the given name}
682 * If the argument is not the name of a primitive type, {@code
683 * null} is returned.
684 *
685 * @param primitiveName the name of the primitive type to find
686 *
687 * @throws NullPointerException if the argument is {@code null}
688 *
689 * @jls 4.2 Primitive Types and Values
690 * @jls 15.8.2 Class Literals
691 * @since 22
692 */
693 public static Class<?> forPrimitiveName(String primitiveName) {
694 return switch(primitiveName) {
695 // Integral types
696 case "int" -> int.class;
697 case "long" -> long.class;
698 case "short" -> short.class;
1386 }
1387 return c;
1388 }
1389
1390 /**
1391 * Returns the Java language modifiers for this class or interface, encoded
1392 * in an integer. The modifiers consist of the Java Virtual Machine's
1393 * constants for {@code public}, {@code protected},
1394 * {@code private}, {@code final}, {@code static},
1395 * {@code abstract} and {@code interface}; they should be decoded
1396 * using the methods of class {@code Modifier}.
1397 *
1398 * <p> If the underlying class is an array class:
1399 * <ul>
1400 * <li> its {@code public}, {@code private} and {@code protected}
1401 * modifiers are the same as those of its component type
1402 * <li> its {@code abstract} and {@code final} modifiers are always
1403 * {@code true}
1404 * <li> its interface modifier is always {@code false}, even when
1405 * the component type is an interface
1406 * <li> its {@code identity} modifier is always true
1407 * </ul>
1408 * If this {@code Class} object represents a primitive type or
1409 * void, its {@code public}, {@code abstract}, and {@code final}
1410 * modifiers are always {@code true}.
1411 * For {@code Class} objects representing void, primitive types, and
1412 * arrays, the values of other modifiers are {@code false} other
1413 * than as specified above.
1414 *
1415 * <p> The modifier encodings are defined in section {@jvms 4.1}
1416 * of <cite>The Java Virtual Machine Specification</cite>.
1417 *
1418 * @return the {@code int} representing the modifiers for this class
1419 * @see java.lang.reflect.Modifier
1420 * @see #accessFlags()
1421 * @see <a
1422 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1423 * programming language and JVM modeling in core reflection</a>
1424 * @since 1.1
1425 * @jls 8.1.1 Class Modifiers
1426 * @jls 9.1.1 Interface Modifiers
1427 * @jvms 4.1 The {@code ClassFile} Structure
1428 */
1429 public int getModifiers() { return modifiers; }
1430
1431 /**
1432 * {@return an unmodifiable set of the {@linkplain AccessFlag access
1433 * flags} for this class, possibly empty}
1434 * The {@code AccessFlags} may depend on the class file format version of the class.
1435 *
1436 * <p> If the underlying class is an array class:
1437 * <ul>
1438 * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1439 * access flags are the same as those of its component type
1440 * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1441 * <li> its {@code INTERFACE} flag is absent, even when the
1442 * component type is an interface
1443 * <li> its {@code identity} modifier is always true
1444 * </ul>
1445 * If this {@code Class} object represents a primitive type or
1446 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1447 * {@code FINAL}.
1448 * For {@code Class} objects representing void, primitive types, and
1449 * arrays, access flags are absent other than as specified above.
1450 *
1451 * @see #getModifiers()
1452 * @jvms 4.1 The ClassFile Structure
1453 * @jvms 4.7.6 The InnerClasses Attribute
1454 * @since 20
1455 */
1456 public Set<AccessFlag> accessFlags() {
1457 // Location.CLASS allows SUPER and AccessFlag.MODULE which
1458 // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1459 // and STATIC, which are not allowed on Location.CLASS.
1460 // Use getClassFileAccessFlags to expose SUPER status.
1461 // Arrays need to use PRIVATE/PROTECTED from its component modifiers.
1462 var location = (isMemberClass() || isLocalClass() ||
1463 isAnonymousClass() || isArray()) ?
1464 AccessFlag.Location.INNER_CLASS :
1465 AccessFlag.Location.CLASS;
1466 int accessFlags = location == AccessFlag.Location.CLASS ? getClassFileAccessFlags() : getModifiers();
1467 var reflectionFactory = getReflectionFactory();
1468 var ans = reflectionFactory.parseAccessFlags(accessFlags, location, this);
1469 if (PreviewFeatures.isEnabled() && reflectionFactory.classFileFormatVersion(this) != ClassFileFormatVersion.CURRENT_PREVIEW_FEATURES
1470 && isIdentity()) {
1471 var set = new HashSet<>(ans);
1472 set.add(AccessFlag.IDENTITY);
1473 return Set.copyOf(set);
1474 }
1475 return ans;
1476 }
1477
1478 /**
1479 * Gets the signers of this class.
1480 *
1481 * @return the signers of this class, or null if there are no signers. In
1482 * particular, this method returns null if this {@code Class} object represents
1483 * a primitive type or void.
1484 * @since 1.1
1485 */
1486
1487 public Object[] getSigners() {
1488 var signers = this.signers;
1489 return signers == null ? null : signers.clone();
1490 }
1491
1492 /**
1493 * Set the signers of this class.
1494 */
1495 void setSigners(Object[] signers) {
1496 if (!isPrimitive() && !isArray()) {
1497 this.signers = signers;
1498 }
1499 }
1500
1501 /**
1502 * If this {@code Class} object represents a local or anonymous
1503 * class within a method, returns a {@link
1504 * java.lang.reflect.Method Method} object representing the
1505 * immediately enclosing method of the underlying class. Returns
1506 * {@code null} otherwise.
3840 *
3841 * <p> If this {@code Class} object represents an interface, the return
3842 * value is an array containing objects representing the uses of interface
3843 * types to specify interfaces directly extended by the interface. The
3844 * order of the objects in the array corresponds to the order of the
3845 * interface types used in the 'extends' clause of the declaration of this
3846 * {@code Class} object.
3847 *
3848 * <p> If this {@code Class} object represents a class or interface whose
3849 * declaration does not explicitly indicate any annotated superinterfaces,
3850 * the return value is an array of length 0.
3851 *
3852 * <p> If this {@code Class} object represents either the {@code Object}
3853 * class, an array type, a primitive type, or void, the return value is an
3854 * array of length 0.
3855 *
3856 * @return an array representing the superinterfaces
3857 * @since 1.8
3858 */
3859 public AnnotatedType[] getAnnotatedInterfaces() {
3860 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3861 }
3862
3863 private native Class<?> getNestHost0();
3864
3865 /**
3866 * Returns the nest host of the <a href=#nest>nest</a> to which the class
3867 * or interface represented by this {@code Class} object belongs.
3868 * Every class and interface belongs to exactly one nest.
3869 *
3870 * If the nest host of this class or interface has previously
3871 * been determined, then this method returns the nest host.
3872 * If the nest host of this class or interface has
3873 * not previously been determined, then this method determines the nest
3874 * host using the algorithm of JVMS 5.4.4, and returns it.
3875 *
3876 * Often, a class or interface belongs to a nest consisting only of itself,
3877 * in which case this method returns {@code this} to indicate that the class
3878 * or interface is the nest host.
3879 *
3880 * <p>If this {@code Class} object represents a primitive type, an array type,
4180 * @since 17
4181 */
4182 public boolean isSealed() {
4183 if (isArray() || isPrimitive()) {
4184 return false;
4185 }
4186 return getPermittedSubclasses() != null;
4187 }
4188
4189 private native Class<?>[] getPermittedSubclasses0();
4190
4191 /*
4192 * Return the class's major and minor class file version packed into an int.
4193 * The high order 16 bits contain the class's minor version. The low order
4194 * 16 bits contain the class's major version.
4195 *
4196 * If the class is an array type then the class file version of its element
4197 * type is returned. If the class is a primitive type then the latest class
4198 * file major version is returned and zero is returned for the minor version.
4199 */
4200 /* package-private */
4201 int getClassFileVersion() {
4202 Class<?> c = isArray() ? elementType() : this;
4203 return c.getClassFileVersion0();
4204 }
4205
4206 private native int getClassFileVersion0();
4207
4208 /**
4209 * Return the access flags as they were in the class's bytecode, including
4210 * the original setting of ACC_SUPER.
4211 *
4212 * If this {@code Class} object represents a primitive type or
4213 * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
4214 * {@code FINAL}.
4215 * If this {@code Class} object represents an array type, return 0.
4216 */
4217 int getClassFileAccessFlags() {
4218 return classFileAccessFlags;
4219 }
4220
|