176 * This constructor is not used and prevents the default constructor being
177 * generated.
178 */
179 private Class(ClassLoader loader, Class<?> arrayComponentType) {
180 // Initialize final field for classLoader. The initialization value of non-null
181 // prevents future JIT optimizations from assuming this final field is null.
182 classLoader = loader;
183 componentType = arrayComponentType;
184 }
185
186 /**
187 * Converts the object to a string. The string representation is the
188 * string "class" or "interface", followed by a space, and then by the
189 * fully qualified name of the class in the format returned by
190 * {@code getName}. If this {@code Class} object represents a
191 * primitive type, this method returns the name of the primitive type. If
192 * this {@code Class} object represents void this method returns
193 * "void". If this {@code Class} object represents an array type,
194 * this method returns "class " followed by {@code getName}.
195 *
196 * @return a string representation of this class object.
197 */
198 public String toString() {
199 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
200 + getName();
201 }
202
203 /**
204 * Returns a string describing this {@code Class}, including
205 * information about modifiers and type parameters.
206 *
207 * The string is formatted as a list of type modifiers, if any,
208 * followed by the kind of type (empty string for primitive types
209 * and {@code class}, {@code enum}, {@code interface},
210 * <code>@</code>{@code interface}, or {@code record} as appropriate), followed
211 * by the type's name, followed by an angle-bracketed
212 * comma-separated list of the type's type parameters, if any,
213 * including informative bounds on the type parameters, if any.
214 *
215 * A space is used to separate modifiers from one another and to
216 * separate any modifiers from the kind of type. The modifiers
662 *
663 * <p> Specifically, this method tests whether the type represented by the
664 * specified {@code Class} parameter can be converted to the type
665 * represented by this {@code Class} object via an identity conversion
666 * or via a widening reference conversion. See <cite>The Java™ Language
667 * Specification</cite>, sections {@jls 5.1.1} and {@jls 5.1.4},
668 * for details.
669 *
670 * @param cls the {@code Class} object to be checked
671 * @return the {@code boolean} value indicating whether objects of the
672 * type {@code cls} can be assigned to objects of this class
673 * @throws NullPointerException if the specified Class parameter is
674 * null.
675 * @since 1.1
676 */
677 @HotSpotIntrinsicCandidate
678 public native boolean isAssignableFrom(Class<?> cls);
679
680
681 /**
682 * Determines if the specified {@code Class} object represents an
683 * interface type.
684 *
685 * @return {@code true} if this object represents an interface;
686 * {@code false} otherwise.
687 */
688 @HotSpotIntrinsicCandidate
689 public native boolean isInterface();
690
691
692 /**
693 * Determines if this {@code Class} object represents an array class.
694 *
695 * @return {@code true} if this object represents an array class;
696 * {@code false} otherwise.
697 * @since 1.1
698 */
699 @HotSpotIntrinsicCandidate
700 public native boolean isArray();
701
702
703 /**
704 * Determines if the specified {@code Class} object represents a
705 * primitive type.
706 *
707 * <p> There are nine predefined {@code Class} objects to represent
708 * the eight primitive types and void. These are created by the Java
709 * Virtual Machine, and have the same names as the primitive types that
710 * they represent, namely {@code boolean}, {@code byte},
711 * {@code char}, {@code short}, {@code int},
712 * {@code long}, {@code float}, and {@code double}.
713 *
714 * <p> These objects may only be accessed via the following public static
715 * final variables, and are the only {@code Class} objects for which
719 *
720 * @see java.lang.Boolean#TYPE
721 * @see java.lang.Character#TYPE
722 * @see java.lang.Byte#TYPE
723 * @see java.lang.Short#TYPE
724 * @see java.lang.Integer#TYPE
725 * @see java.lang.Long#TYPE
726 * @see java.lang.Float#TYPE
727 * @see java.lang.Double#TYPE
728 * @see java.lang.Void#TYPE
729 * @since 1.1
730 */
731 @HotSpotIntrinsicCandidate
732 public native boolean isPrimitive();
733
734 /**
735 * Returns true if this {@code Class} object represents an annotation
736 * type. Note that if this method returns true, {@link #isInterface()}
737 * would also return true, as all annotation types are also interfaces.
738 *
739 * @return {@code true} if this class object represents an annotation
740 * type; {@code false} otherwise
741 * @since 1.5
742 */
743 public boolean isAnnotation() {
744 return (getModifiers() & ANNOTATION) != 0;
745 }
746
747 /**
748 * Returns {@code true} if this class is a synthetic class;
749 * returns {@code false} otherwise.
750 * @return {@code true} if and only if this class is a synthetic class as
751 * defined by <cite>The Java™ Language Specification</cite>.
752 * @jls 13.1 The Form of a Binary
753 * @since 1.5
754 */
755 public boolean isSynthetic() {
756 return (getModifiers() & SYNTHETIC) != 0;
757 }
758
759 /**
760 * Returns the name of the entity (class, interface, array class,
761 * primitive type, or void) represented by this {@code Class} object,
762 * as a {@code String}.
763 *
764 * <p> If this class object represents a reference type that is
765 * not an array type then the binary name of the class is
766 * returned, as specified by <cite>The Java™ Language
767 * Specification</cite>.
768 *
769 * <p> If this class object represents a primitive type or void, then the
770 * name returned is a {@code String} equal to the Java language
771 * keyword corresponding to the primitive type or void.
772 *
773 * <p> If this class object represents a class of arrays, then the internal
774 * form of the name consists of the name of the element type preceded by
775 * one or more '{@code [}' characters representing the depth of the array
776 * nesting. The encoding of element type names is as follows:
777 *
778 * <blockquote><table class="striped">
779 * <caption style="display:none">Element types and encodings</caption>
780 * <thead>
781 * <tr><th scope="col"> Element Type <th scope="col"> Encoding
782 * </thead>
783 * <tbody style="text-align:left">
784 * <tr><th scope="row"> boolean <td style="text-align:center"> Z
785 * <tr><th scope="row"> byte <td style="text-align:center"> B
786 * <tr><th scope="row"> char <td style="text-align:center"> C
787 * <tr><th scope="row"> class or interface
788 * <td style="text-align:center"> L<i>classname</i>;
789 * <tr><th scope="row"> double <td style="text-align:center"> D
790 * <tr><th scope="row"> float <td style="text-align:center"> F
791 * <tr><th scope="row"> int <td style="text-align:center"> I
792 * <tr><th scope="row"> long <td style="text-align:center"> J
793 * <tr><th scope="row"> short <td style="text-align:center"> S
794 * </tbody>
795 * </table></blockquote>
796 *
797 * <p> The class or interface name <i>classname</i> is the binary name of
798 * the class specified above.
799 *
800 * <p> Examples:
801 * <blockquote><pre>
802 * String.class.getName()
803 * returns "java.lang.String"
804 * byte.class.getName()
805 * returns "byte"
806 * (new Object[3]).getClass().getName()
807 * returns "[Ljava.lang.Object;"
808 * (new int[3][4][5][6][7][8][9]).getClass().getName()
809 * returns "[[[[[[[I"
810 * </pre></blockquote>
811 *
812 * @return the name of the class or interface
813 * represented by this object.
814 */
815 public String getName() {
816 String name = this.name;
817 return name != null ? name : initClassName();
818 }
819
820 // Cache the name to reduce the number of calls into the VM.
821 // This field would be set by VM itself during initClassName call.
822 private transient String name;
823 private native String initClassName();
824
825 /**
826 * Returns the class loader for the class. Some implementations may use
827 * null to represent the bootstrap class loader. This method will return
828 * null in such implementations if this class was loaded by the bootstrap
829 * class loader.
830 *
831 * <p>If this object
832 * represents a primitive type or void, null is returned.
833 *
834 * @return the class loader that loaded the class or interface
835 * represented by this object.
836 * @throws SecurityException
837 * if a security manager is present, and the caller's class loader
838 * is not {@code null} and is not the same as or an ancestor of the
839 * class loader for the class whose class loader is requested,
840 * and the caller does not have the
841 * {@link RuntimePermission}{@code ("getClassLoader")}
842 * @see java.lang.ClassLoader
843 * @see SecurityManager#checkPermission
844 * @see java.lang.RuntimePermission
845 */
846 @CallerSensitive
847 @ForceInline // to ensure Reflection.getCallerClass optimization
848 public ClassLoader getClassLoader() {
849 ClassLoader cl = getClassLoader0();
850 if (cl == null)
851 return null;
852 SecurityManager sm = System.getSecurityManager();
853 if (sm != null) {
854 ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
855 }
901 * signature of this generic declaration does not conform to
902 * the format specified in section {@jvms 4.7.9} of
903 * <cite>The Java™ Virtual Machine Specification</cite>,
904 * @since 1.5
905 */
906 @SuppressWarnings("unchecked")
907 public TypeVariable<Class<T>>[] getTypeParameters() {
908 ClassRepository info = getGenericInfo();
909 if (info != null)
910 return (TypeVariable<Class<T>>[])info.getTypeParameters();
911 else
912 return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
913 }
914
915
916 /**
917 * Returns the {@code Class} representing the direct superclass of the
918 * entity (class, interface, primitive type or void) represented by
919 * this {@code Class}. If this {@code Class} represents either the
920 * {@code Object} class, an interface, a primitive type, or void, then
921 * null is returned. If this object represents an array class then the
922 * {@code Class} object representing the {@code Object} class is
923 * returned.
924 *
925 * @return the direct superclass of the class represented by this object
926 */
927 @HotSpotIntrinsicCandidate
928 public native Class<? super T> getSuperclass();
929
930
931 /**
932 * Returns the {@code Type} representing the direct superclass of
933 * the entity (class, interface, primitive type or void) represented by
934 * this {@code Class}.
935 *
936 * <p>If the superclass is a parameterized type, the {@code Type}
937 * object returned must accurately reflect the actual type
938 * arguments used in the source code. The parameterized type
939 * representing the superclass is created if it had not been
940 * created before. See the declaration of {@link
941 * java.lang.reflect.ParameterizedType ParameterizedType} for the
942 * semantics of the creation process for parameterized types. If
943 * this {@code Class} represents either the {@code Object}
944 * class, an interface, a primitive type, or void, then null is
945 * returned. If this object represents an array class then the
946 * {@code Class} object representing the {@code Object} class is
947 * returned.
948 *
949 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
950 * class signature does not conform to the format specified in
951 * section {@jvms 4.7.9} of <cite>The Java™ Virtual
952 * Machine Specification</cite>
953 * @throws TypeNotPresentException if the generic superclass
954 * refers to a non-existent type declaration
955 * @throws java.lang.reflect.MalformedParameterizedTypeException if the
956 * generic superclass refers to a parameterized type that cannot be
957 * instantiated for any reason
958 * @return the direct superclass of the class represented by this object
959 * @since 1.5
960 */
961 public Type getGenericSuperclass() {
962 ClassRepository info = getGenericInfo();
963 if (info == null) {
964 return getSuperclass();
965 }
966
967 // Historical irregularity:
968 // Generic signature marks interfaces with superclass = Object
969 // but this API returns null for interfaces
970 if (isInterface()) {
971 return null;
972 }
973
974 return info.getSuperclass();
975 }
976
977 /**
978 * Gets the package of this class.
1027 while (c.isArray()) {
1028 c = c.getComponentType();
1029 }
1030 if (c.isPrimitive()) {
1031 pn = "java.lang";
1032 } else {
1033 String cn = c.getName();
1034 int dot = cn.lastIndexOf('.');
1035 pn = (dot != -1) ? cn.substring(0, dot).intern() : "";
1036 }
1037 this.packageName = pn;
1038 }
1039 return pn;
1040 }
1041
1042 // cached package name
1043 private transient String packageName;
1044
1045 /**
1046 * Returns the interfaces directly implemented by the class or interface
1047 * represented by this object.
1048 *
1049 * <p>If this object represents a class, the return value is an array
1050 * containing objects representing all interfaces directly implemented by
1051 * the class. The order of the interface objects in the array corresponds
1052 * to the order of the interface names in the {@code implements} clause of
1053 * the declaration of the class represented by this object. For example,
1054 * given the declaration:
1055 * <blockquote>
1056 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1057 * </blockquote>
1058 * suppose the value of {@code s} is an instance of
1059 * {@code Shimmer}; the value of the expression:
1060 * <blockquote>
1061 * {@code s.getClass().getInterfaces()[0]}
1062 * </blockquote>
1063 * is the {@code Class} object that represents interface
1064 * {@code FloorWax}; and the value of:
1065 * <blockquote>
1066 * {@code s.getClass().getInterfaces()[1]}
1067 * </blockquote>
1068 * is the {@code Class} object that represents interface
1069 * {@code DessertTopping}.
1070 *
1071 * <p>If this object represents an interface, the array contains objects
1072 * representing all interfaces directly extended by the interface. The
1073 * order of the interface objects in the array corresponds to the order of
1074 * the interface names in the {@code extends} clause of the declaration of
1075 * the interface represented by this object.
1076 *
1077 * <p>If this object represents a class or interface that implements no
1078 * interfaces, the method returns an array of length 0.
1079 *
1080 * <p>If this object represents a primitive type or void, the method
1081 * returns an array of length 0.
1082 *
1083 * <p>If this {@code Class} object represents an array type, the
1084 * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1085 * returned in that order.
1086 *
1087 * @return an array of interfaces directly implemented by this class
1088 */
1089 public Class<?>[] getInterfaces() {
1090 // defensively copy before handing over to user code
1091 return getInterfaces(true);
1092 }
1093
1094 private Class<?>[] getInterfaces(boolean cloneArray) {
1095 ReflectionData<T> rd = reflectionData();
1096 if (rd == null) {
1097 // no cloning required
1098 return getInterfaces0();
1099 } else {
1100 Class<?>[] interfaces = rd.interfaces;
1101 if (interfaces == null) {
1102 interfaces = getInterfaces0();
1103 rd.interfaces = interfaces;
1104 }
1105 // defensively copy if requested
1106 return cloneArray ? interfaces.clone() : interfaces;
1107 }
1108 }
1109
1110 private native Class<?>[] getInterfaces0();
1111
1112 /**
1113 * Returns the {@code Type}s representing the interfaces
1114 * directly implemented by the class or interface represented by
1115 * this object.
1116 *
1117 * <p>If a superinterface is a parameterized type, the
1118 * {@code Type} object returned for it must accurately reflect
1119 * the actual type arguments used in the source code. The
1120 * parameterized type representing each superinterface is created
1121 * if it had not been created before. See the declaration of
1122 * {@link java.lang.reflect.ParameterizedType ParameterizedType}
1123 * for the semantics of the creation process for parameterized
1124 * types.
1125 *
1126 * <p>If this object represents a class, the return value is an array
1127 * containing objects representing all interfaces directly implemented by
1128 * the class. The order of the interface objects in the array corresponds
1129 * to the order of the interface names in the {@code implements} clause of
1130 * the declaration of the class represented by this object.
1131 *
1132 * <p>If this object represents an interface, the array contains objects
1133 * representing all interfaces directly extended by the interface. The
1134 * order of the interface objects in the array corresponds to the order of
1135 * the interface names in the {@code extends} clause of the declaration of
1136 * the interface represented by this object.
1137 *
1138 * <p>If this object represents a class or interface that implements no
1139 * interfaces, the method returns an array of length 0.
1140 *
1141 * <p>If this object represents a primitive type or void, the method
1142 * returns an array of length 0.
1143 *
1144 * <p>If this {@code Class} object represents an array type, the
1145 * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1146 * returned in that order.
1147 *
1148 * @throws java.lang.reflect.GenericSignatureFormatError
1149 * if the generic class signature does not conform to the
1150 * format specified in section {@jvms 4.7.9} of <cite>The
1151 * Java™ Virtual Machine Specification</cite>
1152 * @throws TypeNotPresentException if any of the generic
1153 * superinterfaces refers to a non-existent type declaration
1154 * @throws java.lang.reflect.MalformedParameterizedTypeException
1155 * if any of the generic superinterfaces refer to a parameterized
1156 * type that cannot be instantiated for any reason
1157 * @return an array of interfaces directly implemented by this class
1158 * @since 1.5
1159 */
1160 public Type[] getGenericInterfaces() {
1161 ClassRepository info = getGenericInfo();
1179 return componentType;
1180 } else {
1181 return null;
1182 }
1183 }
1184
1185 private final Class<?> componentType;
1186
1187
1188 /**
1189 * Returns the Java language modifiers for this class or interface, encoded
1190 * in an integer. The modifiers consist of the Java Virtual Machine's
1191 * constants for {@code public}, {@code protected},
1192 * {@code private}, {@code final}, {@code static},
1193 * {@code abstract} and {@code interface}; they should be decoded
1194 * using the methods of class {@code Modifier}.
1195 *
1196 * <p> If the underlying class is an array class, then its
1197 * {@code public}, {@code private} and {@code protected}
1198 * modifiers are the same as those of its component type. If this
1199 * {@code Class} represents a primitive type or void, its
1200 * {@code public} modifier is always {@code true}, and its
1201 * {@code protected} and {@code private} modifiers are always
1202 * {@code false}. If this object represents an array class, a
1203 * primitive type or void, then its {@code final} modifier is always
1204 * {@code true} and its interface modifier is always
1205 * {@code false}. The values of its other modifiers are not determined
1206 * by this specification.
1207 *
1208 * <p> The modifier encodings are defined in section {@jvms 4.1}
1209 * of <cite>The Java™ Virtual Machine Specification</cite>.
1210 *
1211 * @return the {@code int} representing the modifiers for this class
1212 * @see java.lang.reflect.Modifier
1213 * @since 1.1
1214 */
1215 @HotSpotIntrinsicCandidate
1216 public native int getModifiers();
1217
1218
1219 /**
1220 * Gets the signers of this class.
1221 *
1222 * @return the signers of this class, or null if there are no signers. In
1223 * particular, this method returns null if this object represents
1224 * a primitive type or void.
1225 * @since 1.1
1226 */
1227 public native Object[] getSigners();
1228
1229
1230 /**
1231 * Set the signers of this class.
1232 */
1233 native void setSigners(Object[] signers);
1234
1235
1236 /**
1237 * If this {@code Class} object represents a local or anonymous
1238 * class within a method, returns a {@link
1239 * java.lang.reflect.Method Method} object representing the
1240 * immediately enclosing method of the underlying class. Returns
1241 * {@code null} otherwise.
1242 *
1243 * In particular, this method returns {@code null} if the underlying
1955 *
1956 * @since 1.1
1957 */
1958 @CallerSensitive
1959 public Constructor<?>[] getConstructors() throws SecurityException {
1960 SecurityManager sm = System.getSecurityManager();
1961 if (sm != null) {
1962 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1963 }
1964 return copyConstructors(privateGetDeclaredConstructors(true));
1965 }
1966
1967
1968 /**
1969 * Returns a {@code Field} object that reflects the specified public member
1970 * field of the class or interface represented by this {@code Class}
1971 * object. The {@code name} parameter is a {@code String} specifying the
1972 * simple name of the desired field.
1973 *
1974 * <p> The field to be reflected is determined by the algorithm that
1975 * follows. Let C be the class or interface represented by this object:
1976 *
1977 * <OL>
1978 * <LI> If C declares a public field with the name specified, that is the
1979 * field to be reflected.</LI>
1980 * <LI> If no field was found in step 1 above, this algorithm is applied
1981 * recursively to each direct superinterface of C. The direct
1982 * superinterfaces are searched in the order they were declared.</LI>
1983 * <LI> If no field was found in steps 1 and 2 above, and C has a
1984 * superclass S, then this algorithm is invoked recursively upon S.
1985 * If C has no superclass, then a {@code NoSuchFieldException}
1986 * is thrown.</LI>
1987 * </OL>
1988 *
1989 * <p> If this {@code Class} object represents an array type, then this
1990 * method does not find the {@code length} field of the array type.
1991 *
1992 * @param name the field name
1993 * @return the {@code Field} object of this class specified by
1994 * {@code name}
1995 * @throws NoSuchFieldException if a field with the specified name is
2640 }
2641
2642 /**
2643 * Finds a resource with a given name.
2644 *
2645 * <p> If this class is in a named {@link Module Module} then this method
2646 * will attempt to find the resource in the module. This is done by
2647 * delegating to the module's class loader {@link
2648 * ClassLoader#findResource(String,String) findResource(String,String)}
2649 * method, invoking it with the module name and the absolute name of the
2650 * resource. Resources in named modules are subject to the rules for
2651 * encapsulation specified in the {@code Module} {@link
2652 * Module#getResourceAsStream getResourceAsStream} method and so this
2653 * method returns {@code null} when the resource is a
2654 * non-"{@code .class}" resource in a package that is not open to the
2655 * caller's module.
2656 *
2657 * <p> Otherwise, if this class is not in a named module then the rules for
2658 * searching resources associated with a given class are implemented by the
2659 * defining {@linkplain ClassLoader class loader} of the class. This method
2660 * delegates to this object's class loader. If this object was loaded by
2661 * the bootstrap class loader, the method delegates to {@link
2662 * ClassLoader#getSystemResourceAsStream}.
2663 *
2664 * <p> Before delegation, an absolute resource name is constructed from the
2665 * given resource name using this algorithm:
2666 *
2667 * <ul>
2668 *
2669 * <li> If the {@code name} begins with a {@code '/'}
2670 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2671 * portion of the {@code name} following the {@code '/'}.
2672 *
2673 * <li> Otherwise, the absolute name is of the following form:
2674 *
2675 * <blockquote>
2676 * {@code modified_package_name/name}
2677 * </blockquote>
2678 *
2679 * <p> Where the {@code modified_package_name} is the package name of this
2680 * object with {@code '/'} substituted for {@code '.'}
2681 * (<code>'\u002e'</code>).
2682 *
2738 }
2739
2740 /**
2741 * Finds a resource with a given name.
2742 *
2743 * <p> If this class is in a named {@link Module Module} then this method
2744 * will attempt to find the resource in the module. This is done by
2745 * delegating to the module's class loader {@link
2746 * ClassLoader#findResource(String,String) findResource(String,String)}
2747 * method, invoking it with the module name and the absolute name of the
2748 * resource. Resources in named modules are subject to the rules for
2749 * encapsulation specified in the {@code Module} {@link
2750 * Module#getResourceAsStream getResourceAsStream} method and so this
2751 * method returns {@code null} when the resource is a
2752 * non-"{@code .class}" resource in a package that is not open to the
2753 * caller's module.
2754 *
2755 * <p> Otherwise, if this class is not in a named module then the rules for
2756 * searching resources associated with a given class are implemented by the
2757 * defining {@linkplain ClassLoader class loader} of the class. This method
2758 * delegates to this object's class loader. If this object was loaded by
2759 * the bootstrap class loader, the method delegates to {@link
2760 * ClassLoader#getSystemResource}.
2761 *
2762 * <p> Before delegation, an absolute resource name is constructed from the
2763 * given resource name using this algorithm:
2764 *
2765 * <ul>
2766 *
2767 * <li> If the {@code name} begins with a {@code '/'}
2768 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2769 * portion of the {@code name} following the {@code '/'}.
2770 *
2771 * <li> Otherwise, the absolute name is of the following form:
2772 *
2773 * <blockquote>
2774 * {@code modified_package_name/name}
2775 * </blockquote>
2776 *
2777 * <p> Where the {@code modified_package_name} is the package name of this
2778 * object with {@code '/'} substituted for {@code '.'}
2779 * (<code>'\u002e'</code>).
2780 *
3641 public boolean isRecord() {
3642 return getSuperclass() == JAVA_LANG_RECORD_CLASS && isRecord0();
3643 }
3644
3645 // Fetches the factory for reflective objects
3646 private static ReflectionFactory getReflectionFactory() {
3647 if (reflectionFactory == null) {
3648 reflectionFactory =
3649 java.security.AccessController.doPrivileged
3650 (new ReflectionFactory.GetReflectionFactoryAction());
3651 }
3652 return reflectionFactory;
3653 }
3654 private static ReflectionFactory reflectionFactory;
3655
3656 /**
3657 * Returns the elements of this enum class or null if this
3658 * Class object does not represent an enum type.
3659 *
3660 * @return an array containing the values comprising the enum class
3661 * represented by this Class object in the order they're
3662 * declared, or null if this Class object does not
3663 * represent an enum type
3664 * @since 1.5
3665 */
3666 public T[] getEnumConstants() {
3667 T[] values = getEnumConstantsShared();
3668 return (values != null) ? values.clone() : null;
3669 }
3670
3671 /**
3672 * Returns the elements of this enum class or null if this
3673 * Class object does not represent an enum type;
3674 * identical to getEnumConstants except that the result is
3675 * uncloned, cached, and shared by all callers.
3676 */
3677 T[] getEnumConstantsShared() {
3678 T[] constants = enumConstants;
3679 if (constants == null) {
3680 if (!isEnum()) return null;
3681 try {
3682 final Method values = getMethod("values");
3735 * null and is not assignable to the type T.
3736 *
3737 * @since 1.5
3738 */
3739 @SuppressWarnings("unchecked")
3740 @HotSpotIntrinsicCandidate
3741 public T cast(Object obj) {
3742 if (obj != null && !isInstance(obj))
3743 throw new ClassCastException(cannotCastMsg(obj));
3744 return (T) obj;
3745 }
3746
3747 private String cannotCastMsg(Object obj) {
3748 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
3749 }
3750
3751 /**
3752 * Casts this {@code Class} object to represent a subclass of the class
3753 * represented by the specified class object. Checks that the cast
3754 * is valid, and throws a {@code ClassCastException} if it is not. If
3755 * this method succeeds, it always returns a reference to this class object.
3756 *
3757 * <p>This method is useful when a client needs to "narrow" the type of
3758 * a {@code Class} object to pass it to an API that restricts the
3759 * {@code Class} objects that it is willing to accept. A cast would
3760 * generate a compile-time warning, as the correctness of the cast
3761 * could not be checked at runtime (because generic types are implemented
3762 * by erasure).
3763 *
3764 * @param <U> the type to cast this class object to
3765 * @param clazz the class of the type to cast this class object to
3766 * @return this {@code Class} object, cast to represent a subclass of
3767 * the specified class object.
3768 * @throws ClassCastException if this {@code Class} object does not
3769 * represent a subclass of the specified class (here "subclass" includes
3770 * the class itself).
3771 * @since 1.5
3772 */
3773 @SuppressWarnings("unchecked")
3774 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
3775 if (clazz.isAssignableFrom(this))
3776 return (Class<? extends U>) this;
3777 else
3778 throw new ClassCastException(this.toString());
3779 }
3780
3781 /**
3782 * @throws NullPointerException {@inheritDoc}
3783 * @since 1.5
3784 */
3785 @SuppressWarnings("unchecked")
|
176 * This constructor is not used and prevents the default constructor being
177 * generated.
178 */
179 private Class(ClassLoader loader, Class<?> arrayComponentType) {
180 // Initialize final field for classLoader. The initialization value of non-null
181 // prevents future JIT optimizations from assuming this final field is null.
182 classLoader = loader;
183 componentType = arrayComponentType;
184 }
185
186 /**
187 * Converts the object to a string. The string representation is the
188 * string "class" or "interface", followed by a space, and then by the
189 * fully qualified name of the class in the format returned by
190 * {@code getName}. If this {@code Class} object represents a
191 * primitive type, this method returns the name of the primitive type. If
192 * this {@code Class} object represents void this method returns
193 * "void". If this {@code Class} object represents an array type,
194 * this method returns "class " followed by {@code getName}.
195 *
196 * @return a string representation of this {@code Class} object.
197 */
198 public String toString() {
199 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
200 + getName();
201 }
202
203 /**
204 * Returns a string describing this {@code Class}, including
205 * information about modifiers and type parameters.
206 *
207 * The string is formatted as a list of type modifiers, if any,
208 * followed by the kind of type (empty string for primitive types
209 * and {@code class}, {@code enum}, {@code interface},
210 * <code>@</code>{@code interface}, or {@code record} as appropriate), followed
211 * by the type's name, followed by an angle-bracketed
212 * comma-separated list of the type's type parameters, if any,
213 * including informative bounds on the type parameters, if any.
214 *
215 * A space is used to separate modifiers from one another and to
216 * separate any modifiers from the kind of type. The modifiers
662 *
663 * <p> Specifically, this method tests whether the type represented by the
664 * specified {@code Class} parameter can be converted to the type
665 * represented by this {@code Class} object via an identity conversion
666 * or via a widening reference conversion. See <cite>The Java™ Language
667 * Specification</cite>, sections {@jls 5.1.1} and {@jls 5.1.4},
668 * for details.
669 *
670 * @param cls the {@code Class} object to be checked
671 * @return the {@code boolean} value indicating whether objects of the
672 * type {@code cls} can be assigned to objects of this class
673 * @throws NullPointerException if the specified Class parameter is
674 * null.
675 * @since 1.1
676 */
677 @HotSpotIntrinsicCandidate
678 public native boolean isAssignableFrom(Class<?> cls);
679
680
681 /**
682 * Determines if this {@code Class} object represents an
683 * interface type.
684 *
685 * @return {@code true} if this {@code Class} object represents an interface;
686 * {@code false} otherwise.
687 */
688 @HotSpotIntrinsicCandidate
689 public native boolean isInterface();
690
691
692 /**
693 * Determines if this {@code Class} object represents an array class.
694 *
695 * @return {@code true} if this {@code Class} object represents an array class;
696 * {@code false} otherwise.
697 * @since 1.1
698 */
699 @HotSpotIntrinsicCandidate
700 public native boolean isArray();
701
702
703 /**
704 * Determines if the specified {@code Class} object represents a
705 * primitive type.
706 *
707 * <p> There are nine predefined {@code Class} objects to represent
708 * the eight primitive types and void. These are created by the Java
709 * Virtual Machine, and have the same names as the primitive types that
710 * they represent, namely {@code boolean}, {@code byte},
711 * {@code char}, {@code short}, {@code int},
712 * {@code long}, {@code float}, and {@code double}.
713 *
714 * <p> These objects may only be accessed via the following public static
715 * final variables, and are the only {@code Class} objects for which
719 *
720 * @see java.lang.Boolean#TYPE
721 * @see java.lang.Character#TYPE
722 * @see java.lang.Byte#TYPE
723 * @see java.lang.Short#TYPE
724 * @see java.lang.Integer#TYPE
725 * @see java.lang.Long#TYPE
726 * @see java.lang.Float#TYPE
727 * @see java.lang.Double#TYPE
728 * @see java.lang.Void#TYPE
729 * @since 1.1
730 */
731 @HotSpotIntrinsicCandidate
732 public native boolean isPrimitive();
733
734 /**
735 * Returns true if this {@code Class} object represents an annotation
736 * type. Note that if this method returns true, {@link #isInterface()}
737 * would also return true, as all annotation types are also interfaces.
738 *
739 * @return {@code true} if this {@code Class} object represents an annotation
740 * type; {@code false} otherwise
741 * @since 1.5
742 */
743 public boolean isAnnotation() {
744 return (getModifiers() & ANNOTATION) != 0;
745 }
746
747 /**
748 * Returns {@code true} if this class is a synthetic class;
749 * returns {@code false} otherwise.
750 * @return {@code true} if and only if this class is a synthetic class as
751 * defined by <cite>The Java™ Language Specification</cite>.
752 * @jls 13.1 The Form of a Binary
753 * @since 1.5
754 */
755 public boolean isSynthetic() {
756 return (getModifiers() & SYNTHETIC) != 0;
757 }
758
759 /**
760 * Returns the name of the entity (class, interface, array class,
761 * primitive type, or void) represented by this {@code Class} object,
762 * as a {@code String}.
763 *
764 * <p> If this {@code Class} object represents a reference type that is
765 * not an array type then the binary name of the class is
766 * returned, as specified by <cite>The Java™ Language
767 * Specification</cite>.
768 *
769 * <p> If this {@code Class} object represents a primitive type or void, then the
770 * name returned is a {@code String} equal to the Java language
771 * keyword corresponding to the primitive type or void.
772 *
773 * <p> If this {@code Class} object represents a class of arrays, then the internal
774 * form of the name consists of the name of the element type preceded by
775 * one or more '{@code [}' characters representing the depth of the array
776 * nesting. The encoding of element type names is as follows:
777 *
778 * <blockquote><table class="striped">
779 * <caption style="display:none">Element types and encodings</caption>
780 * <thead>
781 * <tr><th scope="col"> Element Type <th scope="col"> Encoding
782 * </thead>
783 * <tbody style="text-align:left">
784 * <tr><th scope="row"> boolean <td style="text-align:center"> Z
785 * <tr><th scope="row"> byte <td style="text-align:center"> B
786 * <tr><th scope="row"> char <td style="text-align:center"> C
787 * <tr><th scope="row"> class or interface
788 * <td style="text-align:center"> L<i>classname</i>;
789 * <tr><th scope="row"> double <td style="text-align:center"> D
790 * <tr><th scope="row"> float <td style="text-align:center"> F
791 * <tr><th scope="row"> int <td style="text-align:center"> I
792 * <tr><th scope="row"> long <td style="text-align:center"> J
793 * <tr><th scope="row"> short <td style="text-align:center"> S
794 * </tbody>
795 * </table></blockquote>
796 *
797 * <p> The class or interface name <i>classname</i> is the binary name of
798 * the class specified above.
799 *
800 * <p> Examples:
801 * <blockquote><pre>
802 * String.class.getName()
803 * returns "java.lang.String"
804 * byte.class.getName()
805 * returns "byte"
806 * (new Object[3]).getClass().getName()
807 * returns "[Ljava.lang.Object;"
808 * (new int[3][4][5][6][7][8][9]).getClass().getName()
809 * returns "[[[[[[[I"
810 * </pre></blockquote>
811 *
812 * @return the name of the class or interface
813 * represented by this {@code Class} object.
814 */
815 public String getName() {
816 String name = this.name;
817 return name != null ? name : initClassName();
818 }
819
820 // Cache the name to reduce the number of calls into the VM.
821 // This field would be set by VM itself during initClassName call.
822 private transient String name;
823 private native String initClassName();
824
825 /**
826 * Returns the class loader for the class. Some implementations may use
827 * null to represent the bootstrap class loader. This method will return
828 * null in such implementations if this class was loaded by the bootstrap
829 * class loader.
830 *
831 * <p>If this {@code Class} object
832 * represents a primitive type or void, null is returned.
833 *
834 * @return the class loader that loaded the class or interface
835 * represented by this {@code Class} object.
836 * @throws SecurityException
837 * if a security manager is present, and the caller's class loader
838 * is not {@code null} and is not the same as or an ancestor of the
839 * class loader for the class whose class loader is requested,
840 * and the caller does not have the
841 * {@link RuntimePermission}{@code ("getClassLoader")}
842 * @see java.lang.ClassLoader
843 * @see SecurityManager#checkPermission
844 * @see java.lang.RuntimePermission
845 */
846 @CallerSensitive
847 @ForceInline // to ensure Reflection.getCallerClass optimization
848 public ClassLoader getClassLoader() {
849 ClassLoader cl = getClassLoader0();
850 if (cl == null)
851 return null;
852 SecurityManager sm = System.getSecurityManager();
853 if (sm != null) {
854 ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
855 }
901 * signature of this generic declaration does not conform to
902 * the format specified in section {@jvms 4.7.9} of
903 * <cite>The Java™ Virtual Machine Specification</cite>,
904 * @since 1.5
905 */
906 @SuppressWarnings("unchecked")
907 public TypeVariable<Class<T>>[] getTypeParameters() {
908 ClassRepository info = getGenericInfo();
909 if (info != null)
910 return (TypeVariable<Class<T>>[])info.getTypeParameters();
911 else
912 return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
913 }
914
915
916 /**
917 * Returns the {@code Class} representing the direct superclass of the
918 * entity (class, interface, primitive type or void) represented by
919 * this {@code Class}. If this {@code Class} represents either the
920 * {@code Object} class, an interface, a primitive type, or void, then
921 * null is returned. If this {@code Class} object represents an array class
922 * then the {@code Class} object representing the {@code Object} class is
923 * returned.
924 *
925 * @return the direct superclass of the class represented by this {@code Class} object
926 */
927 @HotSpotIntrinsicCandidate
928 public native Class<? super T> getSuperclass();
929
930
931 /**
932 * Returns the {@code Type} representing the direct superclass of
933 * the entity (class, interface, primitive type or void) represented by
934 * this {@code Class} object.
935 *
936 * <p>If the superclass is a parameterized type, the {@code Type}
937 * object returned must accurately reflect the actual type
938 * arguments used in the source code. The parameterized type
939 * representing the superclass is created if it had not been
940 * created before. See the declaration of {@link
941 * java.lang.reflect.ParameterizedType ParameterizedType} for the
942 * semantics of the creation process for parameterized types. If
943 * this {@code Class} object represents either the {@code Object}
944 * class, an interface, a primitive type, or void, then null is
945 * returned. If this {@code Class} object represents an array class
946 * then the {@code Class} object representing the {@code Object} class is
947 * returned.
948 *
949 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
950 * class signature does not conform to the format specified in
951 * section {@jvms 4.7.9} of <cite>The Java™ Virtual
952 * Machine Specification</cite>
953 * @throws TypeNotPresentException if the generic superclass
954 * refers to a non-existent type declaration
955 * @throws java.lang.reflect.MalformedParameterizedTypeException if the
956 * generic superclass refers to a parameterized type that cannot be
957 * instantiated for any reason
958 * @return the direct superclass of the class represented by this {@code Class} object
959 * @since 1.5
960 */
961 public Type getGenericSuperclass() {
962 ClassRepository info = getGenericInfo();
963 if (info == null) {
964 return getSuperclass();
965 }
966
967 // Historical irregularity:
968 // Generic signature marks interfaces with superclass = Object
969 // but this API returns null for interfaces
970 if (isInterface()) {
971 return null;
972 }
973
974 return info.getSuperclass();
975 }
976
977 /**
978 * Gets the package of this class.
1027 while (c.isArray()) {
1028 c = c.getComponentType();
1029 }
1030 if (c.isPrimitive()) {
1031 pn = "java.lang";
1032 } else {
1033 String cn = c.getName();
1034 int dot = cn.lastIndexOf('.');
1035 pn = (dot != -1) ? cn.substring(0, dot).intern() : "";
1036 }
1037 this.packageName = pn;
1038 }
1039 return pn;
1040 }
1041
1042 // cached package name
1043 private transient String packageName;
1044
1045 /**
1046 * Returns the interfaces directly implemented by the class or interface
1047 * represented by this {@code Class} object.
1048 *
1049 * <p>If this {@code Class} object represents a class, the return value is an array
1050 * containing objects representing all interfaces directly implemented by
1051 * the class. The order of the interface objects in the array corresponds
1052 * to the order of the interface names in the {@code implements} clause of
1053 * the declaration of the class represented by this {@code Class} object. For example,
1054 * given the declaration:
1055 * <blockquote>
1056 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1057 * </blockquote>
1058 * suppose the value of {@code s} is an instance of
1059 * {@code Shimmer}; the value of the expression:
1060 * <blockquote>
1061 * {@code s.getClass().getInterfaces()[0]}
1062 * </blockquote>
1063 * is the {@code Class} object that represents interface
1064 * {@code FloorWax}; and the value of:
1065 * <blockquote>
1066 * {@code s.getClass().getInterfaces()[1]}
1067 * </blockquote>
1068 * is the {@code Class} object that represents interface
1069 * {@code DessertTopping}.
1070 *
1071 * <p>If this {@code Class} object represents an interface, the array contains objects
1072 * representing all interfaces directly extended by the interface. The
1073 * order of the interface objects in the array corresponds to the order of
1074 * the interface names in the {@code extends} clause of the declaration of
1075 * the interface represented by this {@code Class} object.
1076 *
1077 * <p>If this {@code Class} object represents a class or interface that implements no
1078 * interfaces, the method returns an array of length 0.
1079 *
1080 * <p>If this {@code Class} object represents a primitive type or void, the method
1081 * returns an array of length 0.
1082 *
1083 * <p>If this {@code Class} object represents an array type, the
1084 * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1085 * returned in that order.
1086 *
1087 * @return an array of interfaces directly implemented by this class
1088 */
1089 public Class<?>[] getInterfaces() {
1090 // defensively copy before handing over to user code
1091 return getInterfaces(true);
1092 }
1093
1094 private Class<?>[] getInterfaces(boolean cloneArray) {
1095 ReflectionData<T> rd = reflectionData();
1096 if (rd == null) {
1097 // no cloning required
1098 return getInterfaces0();
1099 } else {
1100 Class<?>[] interfaces = rd.interfaces;
1101 if (interfaces == null) {
1102 interfaces = getInterfaces0();
1103 rd.interfaces = interfaces;
1104 }
1105 // defensively copy if requested
1106 return cloneArray ? interfaces.clone() : interfaces;
1107 }
1108 }
1109
1110 private native Class<?>[] getInterfaces0();
1111
1112 /**
1113 * Returns the {@code Type}s representing the interfaces
1114 * directly implemented by the class or interface represented by
1115 * this {@code Class} object.
1116 *
1117 * <p>If a superinterface is a parameterized type, the
1118 * {@code Type} object returned for it must accurately reflect
1119 * the actual type arguments used in the source code. The
1120 * parameterized type representing each superinterface is created
1121 * if it had not been created before. See the declaration of
1122 * {@link java.lang.reflect.ParameterizedType ParameterizedType}
1123 * for the semantics of the creation process for parameterized
1124 * types.
1125 *
1126 * <p>If this {@code Class} object represents a class, the return value is an array
1127 * containing objects representing all interfaces directly implemented by
1128 * the class. The order of the interface objects in the array corresponds
1129 * to the order of the interface names in the {@code implements} clause of
1130 * the declaration of the class represented by this {@code Class} object.
1131 *
1132 * <p>If this {@code Class} object represents an interface, the array contains objects
1133 * representing all interfaces directly extended by the interface. The
1134 * order of the interface objects in the array corresponds to the order of
1135 * the interface names in the {@code extends} clause of the declaration of
1136 * the interface represented by this {@code Class} object.
1137 *
1138 * <p>If this {@code Class} object represents a class or interface that implements no
1139 * interfaces, the method returns an array of length 0.
1140 *
1141 * <p>If this {@code Class} object represents a primitive type or void, the method
1142 * returns an array of length 0.
1143 *
1144 * <p>If this {@code Class} object represents an array type, the
1145 * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1146 * returned in that order.
1147 *
1148 * @throws java.lang.reflect.GenericSignatureFormatError
1149 * if the generic class signature does not conform to the
1150 * format specified in section {@jvms 4.7.9} of <cite>The
1151 * Java™ Virtual Machine Specification</cite>
1152 * @throws TypeNotPresentException if any of the generic
1153 * superinterfaces refers to a non-existent type declaration
1154 * @throws java.lang.reflect.MalformedParameterizedTypeException
1155 * if any of the generic superinterfaces refer to a parameterized
1156 * type that cannot be instantiated for any reason
1157 * @return an array of interfaces directly implemented by this class
1158 * @since 1.5
1159 */
1160 public Type[] getGenericInterfaces() {
1161 ClassRepository info = getGenericInfo();
1179 return componentType;
1180 } else {
1181 return null;
1182 }
1183 }
1184
1185 private final Class<?> componentType;
1186
1187
1188 /**
1189 * Returns the Java language modifiers for this class or interface, encoded
1190 * in an integer. The modifiers consist of the Java Virtual Machine's
1191 * constants for {@code public}, {@code protected},
1192 * {@code private}, {@code final}, {@code static},
1193 * {@code abstract} and {@code interface}; they should be decoded
1194 * using the methods of class {@code Modifier}.
1195 *
1196 * <p> If the underlying class is an array class, then its
1197 * {@code public}, {@code private} and {@code protected}
1198 * modifiers are the same as those of its component type. If this
1199 * {@code Class} object represents a primitive type or void, its
1200 * {@code public} modifier is always {@code true}, and its
1201 * {@code protected} and {@code private} modifiers are always
1202 * {@code false}. If this {@code Class} object represents an array class, a
1203 * primitive type or void, then its {@code final} modifier is always
1204 * {@code true} and its interface modifier is always
1205 * {@code false}. The values of its other modifiers are not determined
1206 * by this specification.
1207 *
1208 * <p> The modifier encodings are defined in section {@jvms 4.1}
1209 * of <cite>The Java™ Virtual Machine Specification</cite>.
1210 *
1211 * @return the {@code int} representing the modifiers for this class
1212 * @see java.lang.reflect.Modifier
1213 * @since 1.1
1214 */
1215 @HotSpotIntrinsicCandidate
1216 public native int getModifiers();
1217
1218
1219 /**
1220 * Gets the signers of this class.
1221 *
1222 * @return the signers of this class, or null if there are no signers. In
1223 * particular, this method returns null if this {@code Class} object represents
1224 * a primitive type or void.
1225 * @since 1.1
1226 */
1227 public native Object[] getSigners();
1228
1229
1230 /**
1231 * Set the signers of this class.
1232 */
1233 native void setSigners(Object[] signers);
1234
1235
1236 /**
1237 * If this {@code Class} object represents a local or anonymous
1238 * class within a method, returns a {@link
1239 * java.lang.reflect.Method Method} object representing the
1240 * immediately enclosing method of the underlying class. Returns
1241 * {@code null} otherwise.
1242 *
1243 * In particular, this method returns {@code null} if the underlying
1955 *
1956 * @since 1.1
1957 */
1958 @CallerSensitive
1959 public Constructor<?>[] getConstructors() throws SecurityException {
1960 SecurityManager sm = System.getSecurityManager();
1961 if (sm != null) {
1962 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1963 }
1964 return copyConstructors(privateGetDeclaredConstructors(true));
1965 }
1966
1967
1968 /**
1969 * Returns a {@code Field} object that reflects the specified public member
1970 * field of the class or interface represented by this {@code Class}
1971 * object. The {@code name} parameter is a {@code String} specifying the
1972 * simple name of the desired field.
1973 *
1974 * <p> The field to be reflected is determined by the algorithm that
1975 * follows. Let C be the class or interface represented by this {@code Class} object:
1976 *
1977 * <OL>
1978 * <LI> If C declares a public field with the name specified, that is the
1979 * field to be reflected.</LI>
1980 * <LI> If no field was found in step 1 above, this algorithm is applied
1981 * recursively to each direct superinterface of C. The direct
1982 * superinterfaces are searched in the order they were declared.</LI>
1983 * <LI> If no field was found in steps 1 and 2 above, and C has a
1984 * superclass S, then this algorithm is invoked recursively upon S.
1985 * If C has no superclass, then a {@code NoSuchFieldException}
1986 * is thrown.</LI>
1987 * </OL>
1988 *
1989 * <p> If this {@code Class} object represents an array type, then this
1990 * method does not find the {@code length} field of the array type.
1991 *
1992 * @param name the field name
1993 * @return the {@code Field} object of this class specified by
1994 * {@code name}
1995 * @throws NoSuchFieldException if a field with the specified name is
2640 }
2641
2642 /**
2643 * Finds a resource with a given name.
2644 *
2645 * <p> If this class is in a named {@link Module Module} then this method
2646 * will attempt to find the resource in the module. This is done by
2647 * delegating to the module's class loader {@link
2648 * ClassLoader#findResource(String,String) findResource(String,String)}
2649 * method, invoking it with the module name and the absolute name of the
2650 * resource. Resources in named modules are subject to the rules for
2651 * encapsulation specified in the {@code Module} {@link
2652 * Module#getResourceAsStream getResourceAsStream} method and so this
2653 * method returns {@code null} when the resource is a
2654 * non-"{@code .class}" resource in a package that is not open to the
2655 * caller's module.
2656 *
2657 * <p> Otherwise, if this class is not in a named module then the rules for
2658 * searching resources associated with a given class are implemented by the
2659 * defining {@linkplain ClassLoader class loader} of the class. This method
2660 * delegates to this {@code Class} object's class loader.
2661 * If this {@code Class} object was loaded by the bootstrap class loader,
2662 * the method delegates to {@link ClassLoader#getSystemResourceAsStream}.
2663 *
2664 * <p> Before delegation, an absolute resource name is constructed from the
2665 * given resource name using this algorithm:
2666 *
2667 * <ul>
2668 *
2669 * <li> If the {@code name} begins with a {@code '/'}
2670 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2671 * portion of the {@code name} following the {@code '/'}.
2672 *
2673 * <li> Otherwise, the absolute name is of the following form:
2674 *
2675 * <blockquote>
2676 * {@code modified_package_name/name}
2677 * </blockquote>
2678 *
2679 * <p> Where the {@code modified_package_name} is the package name of this
2680 * object with {@code '/'} substituted for {@code '.'}
2681 * (<code>'\u002e'</code>).
2682 *
2738 }
2739
2740 /**
2741 * Finds a resource with a given name.
2742 *
2743 * <p> If this class is in a named {@link Module Module} then this method
2744 * will attempt to find the resource in the module. This is done by
2745 * delegating to the module's class loader {@link
2746 * ClassLoader#findResource(String,String) findResource(String,String)}
2747 * method, invoking it with the module name and the absolute name of the
2748 * resource. Resources in named modules are subject to the rules for
2749 * encapsulation specified in the {@code Module} {@link
2750 * Module#getResourceAsStream getResourceAsStream} method and so this
2751 * method returns {@code null} when the resource is a
2752 * non-"{@code .class}" resource in a package that is not open to the
2753 * caller's module.
2754 *
2755 * <p> Otherwise, if this class is not in a named module then the rules for
2756 * searching resources associated with a given class are implemented by the
2757 * defining {@linkplain ClassLoader class loader} of the class. This method
2758 * delegates to this {@code Class} object's class loader.
2759 * If this {@code Class} object was loaded by the bootstrap class loader,
2760 * the method delegates to {@link ClassLoader#getSystemResource}.
2761 *
2762 * <p> Before delegation, an absolute resource name is constructed from the
2763 * given resource name using this algorithm:
2764 *
2765 * <ul>
2766 *
2767 * <li> If the {@code name} begins with a {@code '/'}
2768 * (<code>'\u002f'</code>), then the absolute name of the resource is the
2769 * portion of the {@code name} following the {@code '/'}.
2770 *
2771 * <li> Otherwise, the absolute name is of the following form:
2772 *
2773 * <blockquote>
2774 * {@code modified_package_name/name}
2775 * </blockquote>
2776 *
2777 * <p> Where the {@code modified_package_name} is the package name of this
2778 * object with {@code '/'} substituted for {@code '.'}
2779 * (<code>'\u002e'</code>).
2780 *
3641 public boolean isRecord() {
3642 return getSuperclass() == JAVA_LANG_RECORD_CLASS && isRecord0();
3643 }
3644
3645 // Fetches the factory for reflective objects
3646 private static ReflectionFactory getReflectionFactory() {
3647 if (reflectionFactory == null) {
3648 reflectionFactory =
3649 java.security.AccessController.doPrivileged
3650 (new ReflectionFactory.GetReflectionFactoryAction());
3651 }
3652 return reflectionFactory;
3653 }
3654 private static ReflectionFactory reflectionFactory;
3655
3656 /**
3657 * Returns the elements of this enum class or null if this
3658 * Class object does not represent an enum type.
3659 *
3660 * @return an array containing the values comprising the enum class
3661 * represented by this {@code Class} object in the order they're
3662 * declared, or null if this {@code Class} object does not
3663 * represent an enum type
3664 * @since 1.5
3665 */
3666 public T[] getEnumConstants() {
3667 T[] values = getEnumConstantsShared();
3668 return (values != null) ? values.clone() : null;
3669 }
3670
3671 /**
3672 * Returns the elements of this enum class or null if this
3673 * Class object does not represent an enum type;
3674 * identical to getEnumConstants except that the result is
3675 * uncloned, cached, and shared by all callers.
3676 */
3677 T[] getEnumConstantsShared() {
3678 T[] constants = enumConstants;
3679 if (constants == null) {
3680 if (!isEnum()) return null;
3681 try {
3682 final Method values = getMethod("values");
3735 * null and is not assignable to the type T.
3736 *
3737 * @since 1.5
3738 */
3739 @SuppressWarnings("unchecked")
3740 @HotSpotIntrinsicCandidate
3741 public T cast(Object obj) {
3742 if (obj != null && !isInstance(obj))
3743 throw new ClassCastException(cannotCastMsg(obj));
3744 return (T) obj;
3745 }
3746
3747 private String cannotCastMsg(Object obj) {
3748 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
3749 }
3750
3751 /**
3752 * Casts this {@code Class} object to represent a subclass of the class
3753 * represented by the specified class object. Checks that the cast
3754 * is valid, and throws a {@code ClassCastException} if it is not. If
3755 * this method succeeds, it always returns a reference to this {@code Class} object.
3756 *
3757 * <p>This method is useful when a client needs to "narrow" the type of
3758 * a {@code Class} object to pass it to an API that restricts the
3759 * {@code Class} objects that it is willing to accept. A cast would
3760 * generate a compile-time warning, as the correctness of the cast
3761 * could not be checked at runtime (because generic types are implemented
3762 * by erasure).
3763 *
3764 * @param <U> the type to cast this {@code Class} object to
3765 * @param clazz the class of the type to cast this {@code Class} object to
3766 * @return this {@code Class} object, cast to represent a subclass of
3767 * the specified class object.
3768 * @throws ClassCastException if this {@code Class} object does not
3769 * represent a subclass of the specified class (here "subclass" includes
3770 * the class itself).
3771 * @since 1.5
3772 */
3773 @SuppressWarnings("unchecked")
3774 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
3775 if (clazz.isAssignableFrom(this))
3776 return (Class<? extends U>) this;
3777 else
3778 throw new ClassCastException(this.toString());
3779 }
3780
3781 /**
3782 * @throws NullPointerException {@inheritDoc}
3783 * @since 1.5
3784 */
3785 @SuppressWarnings("unchecked")
|