271 *
272 * <blockquote>
273 * {@code Class t = Class.forName("java.lang.Thread")}
274 * </blockquote>
275 * <p>
276 * A call to {@code forName("X")} causes the class named
277 * {@code X} to be initialized.
278 *
279 * @param className the fully qualified name of the desired class.
280 * @return the {@code Class} object for the class with the
281 * specified name.
282 * @exception LinkageError if the linkage fails
283 * @exception ExceptionInInitializerError if the initialization provoked
284 * by this method fails
285 * @exception ClassNotFoundException if the class cannot be located
286 */
287 @CallerSensitive
288 public static Class<?> forName(String className)
289 throws ClassNotFoundException {
290 Class<?> caller = Reflection.getCallerClass();
291 return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
292 }
293
294
295 /**
296 * Returns the {@code Class} object associated with the class or
297 * interface with the given string name, using the given class loader.
298 * Given the fully qualified name for a class or interface (in the same
299 * format returned by {@code getName}) this method attempts to
300 * locate, load, and link the class or interface. The specified class
301 * loader is used to load the class or interface. If the parameter
302 * {@code loader} is null, the class is loaded through the bootstrap
303 * class loader. The class is initialized only if the
304 * {@code initialize} parameter is {@code true} and if it has
305 * not been initialized earlier.
306 *
307 * <p> If {@code name} denotes a primitive type or void, an attempt
308 * will be made to locate a user-defined class in the unnamed package whose
309 * name is {@code name}. Therefore, this method cannot be used to
310 * obtain any of the {@code Class} objects representing primitive
311 * types or void.
354 */
355 @CallerSensitive
356 public static Class<?> forName(String name, boolean initialize,
357 ClassLoader loader)
358 throws ClassNotFoundException
359 {
360 Class<?> caller = null;
361 SecurityManager sm = System.getSecurityManager();
362 if (sm != null) {
363 // Reflective call to get caller class is only needed if a security manager
364 // is present. Avoid the overhead of making this call otherwise.
365 caller = Reflection.getCallerClass();
366 if (VM.isSystemDomainLoader(loader)) {
367 ClassLoader ccl = ClassLoader.getClassLoader(caller);
368 if (!VM.isSystemDomainLoader(ccl)) {
369 sm.checkPermission(
370 SecurityConstants.GET_CLASSLOADER_PERMISSION);
371 }
372 }
373 }
374 return forName0(name, initialize, loader, caller);
375 }
376
377 /** Called after security check for system loader access checks have been made. */
378 private static native Class<?> forName0(String name, boolean initialize,
379 ClassLoader loader,
380 Class<?> caller)
381 throws ClassNotFoundException;
382
383
384 /**
385 * Returns the {@code Class} with the given <a href="ClassLoader.html#name">
386 * binary name</a> in the given module.
387 *
388 * <p> This method attempts to locate, load, and link the class or interface.
389 * It does not run the class initializer. If the class is not found, this
390 * method returns {@code null}. </p>
391 *
392 * <p> If the class loader of the given module defines other modules and
393 * the given name is a class defined in a different module, this method
394 * returns {@code null} after the class is loaded. </p>
395 *
396 * <p> This method does not check whether the requested class is
397 * accessible to its caller. </p>
398 *
426 * @since 9
427 * @spec JPMS
428 */
429 @CallerSensitive
430 public static Class<?> forName(Module module, String name) {
431 Objects.requireNonNull(module);
432 Objects.requireNonNull(name);
433
434 Class<?> caller = Reflection.getCallerClass();
435 if (caller != null && caller.getModule() != module) {
436 // if caller is null, Class.forName is the last java frame on the stack.
437 // java.base has all permissions
438 SecurityManager sm = System.getSecurityManager();
439 if (sm != null) {
440 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
441 }
442 }
443
444 PrivilegedAction<ClassLoader> pa = module::getClassLoader;
445 ClassLoader cl = AccessController.doPrivileged(pa);
446 if (cl != null) {
447 return cl.loadClass(module, name);
448 } else {
449 return BootLoader.loadClass(module, name);
450 }
451 }
452
453 /**
454 * Creates a new instance of the class represented by this {@code Class}
455 * object. The class is instantiated as if by a {@code new}
456 * expression with an empty argument list. The class is initialized if it
457 * has not already been initialized.
458 *
459 * @deprecated This method propagates any exception thrown by the
460 * nullary constructor, including a checked exception. Use of
461 * this method effectively bypasses the compile-time exception
462 * checking that would otherwise be performed by the compiler.
463 * The {@link
464 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
465 * Constructor.newInstance} method avoids this problem by wrapping
466 * any exception thrown by the constructor in a (checked) {@link
467 * java.lang.reflect.InvocationTargetException}.
468 *
469 * <p>The call
491 * @throws InstantiationException
492 * if this {@code Class} represents an abstract class,
493 * an interface, an array class, a primitive type, or void;
494 * or if the class has no nullary constructor;
495 * or if the instantiation fails for some other reason.
496 * @throws ExceptionInInitializerError if the initialization
497 * provoked by this method fails.
498 * @throws SecurityException
499 * If a security manager, <i>s</i>, is present and
500 * the caller's class loader is not the same as or an
501 * ancestor of the class loader for the current class and
502 * invocation of {@link SecurityManager#checkPackageAccess
503 * s.checkPackageAccess()} denies access to the package
504 * of this class.
505 */
506 @CallerSensitive
507 @Deprecated(since="9")
508 public T newInstance()
509 throws InstantiationException, IllegalAccessException
510 {
511 SecurityManager sm = System.getSecurityManager();
512 if (sm != null) {
513 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false);
514 }
515
516 // NOTE: the following code may not be strictly correct under
517 // the current Java memory model.
518
519 // Constructor lookup
520 if (cachedConstructor == null) {
521 if (this == Class.class) {
522 throw new IllegalAccessException(
523 "Can not call newInstance() on the Class for java.lang.Class"
524 );
525 }
526 try {
527 Class<?>[] empty = {};
528 final Constructor<T> c = getReflectionFactory().copyConstructor(
529 getConstructor0(empty, Member.DECLARED));
530 // Disable accessibility checks on the constructor
1220 if (enclosingInfo == null)
1221 return null;
1222 else {
1223 if (!enclosingInfo.isMethod())
1224 return null;
1225
1226 MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(),
1227 getFactory());
1228 Class<?> returnType = toClass(typeInfo.getReturnType());
1229 Type [] parameterTypes = typeInfo.getParameterTypes();
1230 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
1231
1232 // Convert Types to Classes; returned types *should*
1233 // be class objects since the methodDescriptor's used
1234 // don't have generics information
1235 for(int i = 0; i < parameterClasses.length; i++)
1236 parameterClasses[i] = toClass(parameterTypes[i]);
1237
1238 // Perform access check
1239 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
1240 SecurityManager sm = System.getSecurityManager();
1241 if (sm != null) {
1242 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED,
1243 Reflection.getCallerClass(), true);
1244 }
1245 Method[] candidates = enclosingCandidate.privateGetDeclaredMethods(false);
1246
1247 /*
1248 * Loop over all declared methods; match method name,
1249 * number of and type of parameters, *and* return
1250 * type. Matching return type is also necessary
1251 * because of covariant returns, etc.
1252 */
1253 ReflectionFactory fact = getReflectionFactory();
1254 for (Method m : candidates) {
1255 if (m.getName().equals(enclosingInfo.getName()) &&
1256 arrayContentsEq(parameterClasses,
1257 fact.getExecutableSharedParameterTypes(m))) {
1258 // finally, check return type
1259 if (m.getReturnType().equals(returnType)) {
1376
1377 if (enclosingInfo == null)
1378 return null;
1379 else {
1380 if (!enclosingInfo.isConstructor())
1381 return null;
1382
1383 ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(),
1384 getFactory());
1385 Type [] parameterTypes = typeInfo.getParameterTypes();
1386 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
1387
1388 // Convert Types to Classes; returned types *should*
1389 // be class objects since the methodDescriptor's used
1390 // don't have generics information
1391 for(int i = 0; i < parameterClasses.length; i++)
1392 parameterClasses[i] = toClass(parameterTypes[i]);
1393
1394 // Perform access check
1395 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
1396 SecurityManager sm = System.getSecurityManager();
1397 if (sm != null) {
1398 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED,
1399 Reflection.getCallerClass(), true);
1400 }
1401
1402 Constructor<?>[] candidates = enclosingCandidate
1403 .privateGetDeclaredConstructors(false);
1404 /*
1405 * Loop over all declared constructors; match number
1406 * of and type of parameters.
1407 */
1408 ReflectionFactory fact = getReflectionFactory();
1409 for (Constructor<?> c : candidates) {
1410 if (arrayContentsEq(parameterClasses,
1411 fact.getExecutableSharedParameterTypes(c))) {
1412 return fact.copyConstructor(c);
1413 }
1414 }
1415
1672 * and interface members declared by the class. This method returns an
1673 * array of length 0 if this {@code Class} object has no public member
1674 * classes or interfaces. This method also returns an array of length 0 if
1675 * this {@code Class} object represents a primitive type, an array
1676 * class, or void.
1677 *
1678 * @return the array of {@code Class} objects representing the public
1679 * members of this class
1680 * @throws SecurityException
1681 * If a security manager, <i>s</i>, is present and
1682 * the caller's class loader is not the same as or an
1683 * ancestor of the class loader for the current class and
1684 * invocation of {@link SecurityManager#checkPackageAccess
1685 * s.checkPackageAccess()} denies access to the package
1686 * of this class.
1687 *
1688 * @since 1.1
1689 */
1690 @CallerSensitive
1691 public Class<?>[] getClasses() {
1692 SecurityManager sm = System.getSecurityManager();
1693 if (sm != null) {
1694 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false);
1695 }
1696
1697 // Privileged so this implementation can look at DECLARED classes,
1698 // something the caller might not have privilege to do. The code here
1699 // is allowed to look at DECLARED classes because (1) it does not hand
1700 // out anything other than public members and (2) public member access
1701 // has already been ok'd by the SecurityManager.
1702
1703 return java.security.AccessController.doPrivileged(
1704 new java.security.PrivilegedAction<>() {
1705 public Class<?>[] run() {
1706 List<Class<?>> list = new ArrayList<>();
1707 Class<?> currentClass = Class.this;
1708 while (currentClass != null) {
1709 for (Class<?> m : currentClass.getDeclaredClasses()) {
1710 if (Modifier.isPublic(m.getModifiers())) {
1711 list.add(m);
1741 *
1742 * <p> The elements in the returned array are not sorted and are not in any
1743 * particular order.
1744 *
1745 * @return the array of {@code Field} objects representing the
1746 * public fields
1747 * @throws SecurityException
1748 * If a security manager, <i>s</i>, is present and
1749 * the caller's class loader is not the same as or an
1750 * ancestor of the class loader for the current class and
1751 * invocation of {@link SecurityManager#checkPackageAccess
1752 * s.checkPackageAccess()} denies access to the package
1753 * of this class.
1754 *
1755 * @since 1.1
1756 * @jls 8.2 Class Members
1757 * @jls 8.3 Field Declarations
1758 */
1759 @CallerSensitive
1760 public Field[] getFields() throws SecurityException {
1761 SecurityManager sm = System.getSecurityManager();
1762 if (sm != null) {
1763 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1764 }
1765 return copyFields(privateGetPublicFields(null));
1766 }
1767
1768
1769 /**
1770 * Returns an array containing {@code Method} objects reflecting all the
1771 * public methods of the class or interface represented by this {@code
1772 * Class} object, including those declared by the class or interface and
1773 * those inherited from superclasses and superinterfaces.
1774 *
1775 * <p> If this {@code Class} object represents an array type, then the
1776 * returned array has a {@code Method} object for each of the public
1777 * methods inherited by the array type from {@code Object}. It does not
1778 * contain a {@code Method} object for {@code clone()}.
1779 *
1780 * <p> If this {@code Class} object represents an interface then the
1831 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
1832 * method and the overriding method would have the same
1833 * signature but different return types.
1834 *
1835 * @return the array of {@code Method} objects representing the
1836 * public methods of this class
1837 * @throws SecurityException
1838 * If a security manager, <i>s</i>, is present and
1839 * the caller's class loader is not the same as or an
1840 * ancestor of the class loader for the current class and
1841 * invocation of {@link SecurityManager#checkPackageAccess
1842 * s.checkPackageAccess()} denies access to the package
1843 * of this class.
1844 *
1845 * @jls 8.2 Class Members
1846 * @jls 8.4 Method Declarations
1847 * @since 1.1
1848 */
1849 @CallerSensitive
1850 public Method[] getMethods() throws SecurityException {
1851 SecurityManager sm = System.getSecurityManager();
1852 if (sm != null) {
1853 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1854 }
1855 return copyMethods(privateGetPublicMethods());
1856 }
1857
1858
1859 /**
1860 * Returns an array containing {@code Constructor} objects reflecting
1861 * all the public constructors of the class represented by this
1862 * {@code Class} object. An array of length 0 is returned if the
1863 * class has no public constructors, or if the class is an array class, or
1864 * if the class reflects a primitive type or void.
1865 *
1866 * Note that while this method returns an array of {@code
1867 * Constructor<T>} objects (that is an array of constructors from
1868 * this class), the return type of this method is {@code
1869 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1870 * might be expected. This less informative return type is
1871 * necessary since after being returned from this method, the
1872 * array could be modified to hold {@code Constructor} objects for
1873 * different classes, which would violate the type guarantees of
1874 * {@code Constructor<T>[]}.
1875 *
1876 * @return the array of {@code Constructor} objects representing the
1877 * public constructors of this class
1878 * @throws SecurityException
1879 * If a security manager, <i>s</i>, is present and
1880 * the caller's class loader is not the same as or an
1881 * ancestor of the class loader for the current class and
1882 * invocation of {@link SecurityManager#checkPackageAccess
1883 * s.checkPackageAccess()} denies access to the package
1884 * of this class.
1885 *
1886 * @since 1.1
1887 */
1888 @CallerSensitive
1889 public Constructor<?>[] getConstructors() throws SecurityException {
1890 SecurityManager sm = System.getSecurityManager();
1891 if (sm != null) {
1892 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1893 }
1894 return copyConstructors(privateGetDeclaredConstructors(true));
1895 }
1896
1897
1898 /**
1899 * Returns a {@code Field} object that reflects the specified public member
1900 * field of the class or interface represented by this {@code Class}
1901 * object. The {@code name} parameter is a {@code String} specifying the
1902 * simple name of the desired field.
1903 *
1904 * <p> The field to be reflected is determined by the algorithm that
1905 * follows. Let C be the class or interface represented by this object:
1906 *
1907 * <OL>
1908 * <LI> If C declares a public field with the name specified, that is the
1909 * field to be reflected.</LI>
1923 * @return the {@code Field} object of this class specified by
1924 * {@code name}
1925 * @throws NoSuchFieldException if a field with the specified name is
1926 * not found.
1927 * @throws NullPointerException if {@code name} is {@code null}
1928 * @throws SecurityException
1929 * If a security manager, <i>s</i>, is present and
1930 * the caller's class loader is not the same as or an
1931 * ancestor of the class loader for the current class and
1932 * invocation of {@link SecurityManager#checkPackageAccess
1933 * s.checkPackageAccess()} denies access to the package
1934 * of this class.
1935 *
1936 * @since 1.1
1937 * @jls 8.2 Class Members
1938 * @jls 8.3 Field Declarations
1939 */
1940 @CallerSensitive
1941 public Field getField(String name)
1942 throws NoSuchFieldException, SecurityException {
1943 Objects.requireNonNull(name);
1944 SecurityManager sm = System.getSecurityManager();
1945 if (sm != null) {
1946 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1947 }
1948 Field field = getField0(name);
1949 if (field == null) {
1950 throw new NoSuchFieldException(name);
1951 }
1952 return getReflectionFactory().copyField(field);
1953 }
1954
1955
1956 /**
1957 * Returns a {@code Method} object that reflects the specified public
1958 * member method of the class or interface represented by this
1959 * {@code Class} object. The {@code name} parameter is a
1960 * {@code String} specifying the simple name of the desired method. The
1961 * {@code parameterTypes} parameter is an array of {@code Class}
1962 * objects that identify the method's formal parameter types, in declared
2032 * @return the {@code Method} object that matches the specified
2033 * {@code name} and {@code parameterTypes}
2034 * @throws NoSuchMethodException if a matching method is not found
2035 * or if the name is "<init>"or "<clinit>".
2036 * @throws NullPointerException if {@code name} is {@code null}
2037 * @throws SecurityException
2038 * If a security manager, <i>s</i>, is present and
2039 * the caller's class loader is not the same as or an
2040 * ancestor of the class loader for the current class and
2041 * invocation of {@link SecurityManager#checkPackageAccess
2042 * s.checkPackageAccess()} denies access to the package
2043 * of this class.
2044 *
2045 * @jls 8.2 Class Members
2046 * @jls 8.4 Method Declarations
2047 * @since 1.1
2048 */
2049 @CallerSensitive
2050 public Method getMethod(String name, Class<?>... parameterTypes)
2051 throws NoSuchMethodException, SecurityException {
2052 Objects.requireNonNull(name);
2053 SecurityManager sm = System.getSecurityManager();
2054 if (sm != null) {
2055 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
2056 }
2057 Method method = getMethod0(name, parameterTypes);
2058 if (method == null) {
2059 throw new NoSuchMethodException(methodToString(name, parameterTypes));
2060 }
2061 return getReflectionFactory().copyMethod(method);
2062 }
2063
2064 /**
2065 * Returns a {@code Method} object that reflects the specified public
2066 * member method of the class or interface represented by this
2067 * {@code Class} object.
2068 *
2069 * @param name the name of the method
2070 * @param parameterTypes the list of parameters
2071 * @return the {@code Method} object that matches the specified
2096 * types match those specified by {@code parameterTypes}.
2097 *
2098 * @param parameterTypes the parameter array
2099 * @return the {@code Constructor} object of the public constructor that
2100 * matches the specified {@code parameterTypes}
2101 * @throws NoSuchMethodException if a matching method is not found.
2102 * @throws SecurityException
2103 * If a security manager, <i>s</i>, is present and
2104 * the caller's class loader is not the same as or an
2105 * ancestor of the class loader for the current class and
2106 * invocation of {@link SecurityManager#checkPackageAccess
2107 * s.checkPackageAccess()} denies access to the package
2108 * of this class.
2109 *
2110 * @since 1.1
2111 */
2112 @CallerSensitive
2113 public Constructor<T> getConstructor(Class<?>... parameterTypes)
2114 throws NoSuchMethodException, SecurityException
2115 {
2116 SecurityManager sm = System.getSecurityManager();
2117 if (sm != null) {
2118 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
2119 }
2120 return getReflectionFactory().copyConstructor(
2121 getConstructor0(parameterTypes, Member.PUBLIC));
2122 }
2123
2124
2125 /**
2126 * Returns an array of {@code Class} objects reflecting all the
2127 * classes and interfaces declared as members of the class represented by
2128 * this {@code Class} object. This includes public, protected, default
2129 * (package) access, and private classes and interfaces declared by the
2130 * class, but excludes inherited classes and interfaces. This method
2131 * returns an array of length 0 if the class declares no classes or
2132 * interfaces as members, or if this {@code Class} object represents a
2133 * primitive type, an array class, or void.
2134 *
2135 * @return the array of {@code Class} objects representing all the
2142 *
2143 * <li> the caller's class loader is not the same as the
2144 * class loader of this class and invocation of
2145 * {@link SecurityManager#checkPermission
2146 * s.checkPermission} method with
2147 * {@code RuntimePermission("accessDeclaredMembers")}
2148 * denies access to the declared classes within this class
2149 *
2150 * <li> the caller's class loader is not the same as or an
2151 * ancestor of the class loader for the current class and
2152 * invocation of {@link SecurityManager#checkPackageAccess
2153 * s.checkPackageAccess()} denies access to the package
2154 * of this class
2155 *
2156 * </ul>
2157 *
2158 * @since 1.1
2159 */
2160 @CallerSensitive
2161 public Class<?>[] getDeclaredClasses() throws SecurityException {
2162 SecurityManager sm = System.getSecurityManager();
2163 if (sm != null) {
2164 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), false);
2165 }
2166 return getDeclaredClasses0();
2167 }
2168
2169
2170 /**
2171 * Returns an array of {@code Field} objects reflecting all the fields
2172 * declared by the class or interface represented by this
2173 * {@code Class} object. This includes public, protected, default
2174 * (package) access, and private fields, but excludes inherited fields.
2175 *
2176 * <p> If this {@code Class} object represents a class or interface with no
2177 * declared fields, then this method returns an array of length 0.
2178 *
2179 * <p> If this {@code Class} object represents an array type, a primitive
2180 * type, or void, then this method returns an array of length 0.
2181 *
2194 * class loader of this class and invocation of
2195 * {@link SecurityManager#checkPermission
2196 * s.checkPermission} method with
2197 * {@code RuntimePermission("accessDeclaredMembers")}
2198 * denies access to the declared fields within this class
2199 *
2200 * <li> the caller's class loader is not the same as or an
2201 * ancestor of the class loader for the current class and
2202 * invocation of {@link SecurityManager#checkPackageAccess
2203 * s.checkPackageAccess()} denies access to the package
2204 * of this class
2205 *
2206 * </ul>
2207 *
2208 * @since 1.1
2209 * @jls 8.2 Class Members
2210 * @jls 8.3 Field Declarations
2211 */
2212 @CallerSensitive
2213 public Field[] getDeclaredFields() throws SecurityException {
2214 SecurityManager sm = System.getSecurityManager();
2215 if (sm != null) {
2216 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2217 }
2218 return copyFields(privateGetDeclaredFields(false));
2219 }
2220
2221
2222 /**
2223 *
2224 * Returns an array containing {@code Method} objects reflecting all the
2225 * declared methods of the class or interface represented by this {@code
2226 * Class} object, including public, protected, default (package)
2227 * access, and private methods, but excluding inherited methods.
2228 *
2229 * <p> If this {@code Class} object represents a type that has multiple
2230 * declared methods with the same name and parameter types, but different
2231 * return types, then the returned array has a {@code Method} object for
2232 * each such method.
2233 *
2256 * class loader of this class and invocation of
2257 * {@link SecurityManager#checkPermission
2258 * s.checkPermission} method with
2259 * {@code RuntimePermission("accessDeclaredMembers")}
2260 * denies access to the declared methods within this class
2261 *
2262 * <li> the caller's class loader is not the same as or an
2263 * ancestor of the class loader for the current class and
2264 * invocation of {@link SecurityManager#checkPackageAccess
2265 * s.checkPackageAccess()} denies access to the package
2266 * of this class
2267 *
2268 * </ul>
2269 *
2270 * @jls 8.2 Class Members
2271 * @jls 8.4 Method Declarations
2272 * @since 1.1
2273 */
2274 @CallerSensitive
2275 public Method[] getDeclaredMethods() throws SecurityException {
2276 SecurityManager sm = System.getSecurityManager();
2277 if (sm != null) {
2278 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2279 }
2280 return copyMethods(privateGetDeclaredMethods(false));
2281 }
2282
2283
2284 /**
2285 * Returns an array of {@code Constructor} objects reflecting all the
2286 * constructors declared by the class represented by this
2287 * {@code Class} object. These are public, protected, default
2288 * (package) access, and private constructors. The elements in the array
2289 * returned are not sorted and are not in any particular order. If the
2290 * class has a default constructor, it is included in the returned array.
2291 * This method returns an array of length 0 if this {@code Class}
2292 * object represents an interface, a primitive type, an array class, or
2293 * void.
2294 *
2295 * <p> See <em>The Java Language Specification</em>, section 8.2.
2304 *
2305 * <li> the caller's class loader is not the same as the
2306 * class loader of this class and invocation of
2307 * {@link SecurityManager#checkPermission
2308 * s.checkPermission} method with
2309 * {@code RuntimePermission("accessDeclaredMembers")}
2310 * denies access to the declared constructors within this class
2311 *
2312 * <li> the caller's class loader is not the same as or an
2313 * ancestor of the class loader for the current class and
2314 * invocation of {@link SecurityManager#checkPackageAccess
2315 * s.checkPackageAccess()} denies access to the package
2316 * of this class
2317 *
2318 * </ul>
2319 *
2320 * @since 1.1
2321 */
2322 @CallerSensitive
2323 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
2324 SecurityManager sm = System.getSecurityManager();
2325 if (sm != null) {
2326 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2327 }
2328 return copyConstructors(privateGetDeclaredConstructors(false));
2329 }
2330
2331
2332 /**
2333 * Returns a {@code Field} object that reflects the specified declared
2334 * field of the class or interface represented by this {@code Class}
2335 * object. The {@code name} parameter is a {@code String} that specifies
2336 * the simple name of the desired field.
2337 *
2338 * <p> If this {@code Class} object represents an array type, then this
2339 * method does not find the {@code length} field of the array type.
2340 *
2341 * @param name the name of the field
2342 * @return the {@code Field} object for the specified field in this
2343 * class
2355 * {@link SecurityManager#checkPermission
2356 * s.checkPermission} method with
2357 * {@code RuntimePermission("accessDeclaredMembers")}
2358 * denies access to the declared field
2359 *
2360 * <li> the caller's class loader is not the same as or an
2361 * ancestor of the class loader for the current class and
2362 * invocation of {@link SecurityManager#checkPackageAccess
2363 * s.checkPackageAccess()} denies access to the package
2364 * of this class
2365 *
2366 * </ul>
2367 *
2368 * @since 1.1
2369 * @jls 8.2 Class Members
2370 * @jls 8.3 Field Declarations
2371 */
2372 @CallerSensitive
2373 public Field getDeclaredField(String name)
2374 throws NoSuchFieldException, SecurityException {
2375 Objects.requireNonNull(name);
2376 SecurityManager sm = System.getSecurityManager();
2377 if (sm != null) {
2378 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2379 }
2380 Field field = searchFields(privateGetDeclaredFields(false), name);
2381 if (field == null) {
2382 throw new NoSuchFieldException(name);
2383 }
2384 return getReflectionFactory().copyField(field);
2385 }
2386
2387
2388 /**
2389 * Returns a {@code Method} object that reflects the specified
2390 * declared method of the class or interface represented by this
2391 * {@code Class} object. The {@code name} parameter is a
2392 * {@code String} that specifies the simple name of the desired
2393 * method, and the {@code parameterTypes} parameter is an array of
2394 * {@code Class} objects that identify the method's formal parameter
2419 * {@link SecurityManager#checkPermission
2420 * s.checkPermission} method with
2421 * {@code RuntimePermission("accessDeclaredMembers")}
2422 * denies access to the declared method
2423 *
2424 * <li> the caller's class loader is not the same as or an
2425 * ancestor of the class loader for the current class and
2426 * invocation of {@link SecurityManager#checkPackageAccess
2427 * s.checkPackageAccess()} denies access to the package
2428 * of this class
2429 *
2430 * </ul>
2431 *
2432 * @jls 8.2 Class Members
2433 * @jls 8.4 Method Declarations
2434 * @since 1.1
2435 */
2436 @CallerSensitive
2437 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
2438 throws NoSuchMethodException, SecurityException {
2439 Objects.requireNonNull(name);
2440 SecurityManager sm = System.getSecurityManager();
2441 if (sm != null) {
2442 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2443 }
2444 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
2445 if (method == null) {
2446 throw new NoSuchMethodException(methodToString(name, parameterTypes));
2447 }
2448 return getReflectionFactory().copyMethod(method);
2449 }
2450
2451
2452 /**
2453 * Returns a {@code Constructor} object that reflects the specified
2454 * constructor of the class or interface represented by this
2455 * {@code Class} object. The {@code parameterTypes} parameter is
2456 * an array of {@code Class} objects that identify the constructor's
2457 * formal parameter types, in declared order.
2458 *
2474 * class loader of this class and invocation of
2475 * {@link SecurityManager#checkPermission
2476 * s.checkPermission} method with
2477 * {@code RuntimePermission("accessDeclaredMembers")}
2478 * denies access to the declared constructor
2479 *
2480 * <li> the caller's class loader is not the same as or an
2481 * ancestor of the class loader for the current class and
2482 * invocation of {@link SecurityManager#checkPackageAccess
2483 * s.checkPackageAccess()} denies access to the package
2484 * of this class
2485 *
2486 * </ul>
2487 *
2488 * @since 1.1
2489 */
2490 @CallerSensitive
2491 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2492 throws NoSuchMethodException, SecurityException
2493 {
2494 SecurityManager sm = System.getSecurityManager();
2495 if (sm != null) {
2496 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2497 }
2498
2499 return getReflectionFactory().copyConstructor(
2500 getConstructor0(parameterTypes, Member.DECLARED));
2501 }
2502
2503 /**
2504 * Finds a resource with a given name.
2505 *
2506 * <p> If this class is in a named {@link Module Module} then this method
2507 * will attempt to find the resource in the module. This is done by
2508 * delegating to the module's class loader {@link
2509 * ClassLoader#findResource(String,String) findResource(String,String)}
2510 * method, invoking it with the module name and the absolute name of the
2511 * resource. Resources in named modules are subject to the rules for
2512 * encapsulation specified in the {@code Module} {@link
2513 * Module#getResourceAsStream getResourceAsStream} method and so this
|
271 *
272 * <blockquote>
273 * {@code Class t = Class.forName("java.lang.Thread")}
274 * </blockquote>
275 * <p>
276 * A call to {@code forName("X")} causes the class named
277 * {@code X} to be initialized.
278 *
279 * @param className the fully qualified name of the desired class.
280 * @return the {@code Class} object for the class with the
281 * specified name.
282 * @exception LinkageError if the linkage fails
283 * @exception ExceptionInInitializerError if the initialization provoked
284 * by this method fails
285 * @exception ClassNotFoundException if the class cannot be located
286 */
287 @CallerSensitive
288 public static Class<?> forName(String className)
289 throws ClassNotFoundException {
290 Class<?> caller = Reflection.getCallerClass();
291 Class<?> c = forName0(className, true, ClassLoader.getClassLoader(caller), caller);
292 if (c.isValueClass()) {
293 throw new ClassNotFoundException(className + " is a derived value class");
294 }
295 return c;
296 }
297
298
299 /**
300 * Returns the {@code Class} object associated with the class or
301 * interface with the given string name, using the given class loader.
302 * Given the fully qualified name for a class or interface (in the same
303 * format returned by {@code getName}) this method attempts to
304 * locate, load, and link the class or interface. The specified class
305 * loader is used to load the class or interface. If the parameter
306 * {@code loader} is null, the class is loaded through the bootstrap
307 * class loader. The class is initialized only if the
308 * {@code initialize} parameter is {@code true} and if it has
309 * not been initialized earlier.
310 *
311 * <p> If {@code name} denotes a primitive type or void, an attempt
312 * will be made to locate a user-defined class in the unnamed package whose
313 * name is {@code name}. Therefore, this method cannot be used to
314 * obtain any of the {@code Class} objects representing primitive
315 * types or void.
358 */
359 @CallerSensitive
360 public static Class<?> forName(String name, boolean initialize,
361 ClassLoader loader)
362 throws ClassNotFoundException
363 {
364 Class<?> caller = null;
365 SecurityManager sm = System.getSecurityManager();
366 if (sm != null) {
367 // Reflective call to get caller class is only needed if a security manager
368 // is present. Avoid the overhead of making this call otherwise.
369 caller = Reflection.getCallerClass();
370 if (VM.isSystemDomainLoader(loader)) {
371 ClassLoader ccl = ClassLoader.getClassLoader(caller);
372 if (!VM.isSystemDomainLoader(ccl)) {
373 sm.checkPermission(
374 SecurityConstants.GET_CLASSLOADER_PERMISSION);
375 }
376 }
377 }
378 Class<?> c = forName0(name, initialize, loader, caller);
379 if (c.isValueClass()) {
380 throw new ClassNotFoundException(name + " is a derived value class");
381 }
382 return c;
383 }
384
385 /** Called after security check for system loader access checks have been made. */
386 static native Class<?> forName0(String name, boolean initialize,
387 ClassLoader loader,
388 Class<?> caller)
389 throws ClassNotFoundException;
390
391
392 /**
393 * Returns the {@code Class} with the given <a href="ClassLoader.html#name">
394 * binary name</a> in the given module.
395 *
396 * <p> This method attempts to locate, load, and link the class or interface.
397 * It does not run the class initializer. If the class is not found, this
398 * method returns {@code null}. </p>
399 *
400 * <p> If the class loader of the given module defines other modules and
401 * the given name is a class defined in a different module, this method
402 * returns {@code null} after the class is loaded. </p>
403 *
404 * <p> This method does not check whether the requested class is
405 * accessible to its caller. </p>
406 *
434 * @since 9
435 * @spec JPMS
436 */
437 @CallerSensitive
438 public static Class<?> forName(Module module, String name) {
439 Objects.requireNonNull(module);
440 Objects.requireNonNull(name);
441
442 Class<?> caller = Reflection.getCallerClass();
443 if (caller != null && caller.getModule() != module) {
444 // if caller is null, Class.forName is the last java frame on the stack.
445 // java.base has all permissions
446 SecurityManager sm = System.getSecurityManager();
447 if (sm != null) {
448 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
449 }
450 }
451
452 PrivilegedAction<ClassLoader> pa = module::getClassLoader;
453 ClassLoader cl = AccessController.doPrivileged(pa);
454 Class<?> c;
455 if (cl != null) {
456 c = cl.loadClass(module, name);
457 } else {
458 c = BootLoader.loadClass(module, name);
459 }
460
461 return c != null && !c.isValueClass() ? c : null;
462 }
463
464 boolean isValueClass() {
465 Class<?> c = this;
466 while (c.isArray()) {
467 c = c.getComponentType();
468 }
469
470 // This could call MinimalValueTypes_1_0::isValueType or check ACC_VALUE.
471 // For now, check if it is a subtype of __Value to avoid
472 // initializing MinimalValueTypes_1_0 class during early startup.
473 return __Value.class.isAssignableFrom(c) && c != __Value.class;
474 }
475
476 private void ensureNotValueClass() {
477 if (this.isValueClass()) {
478 throw new UnsupportedOperationException("cannot reflect on derived value type "
479 + this.getName());
480 }
481 }
482
483 /**
484 * Creates a new instance of the class represented by this {@code Class}
485 * object. The class is instantiated as if by a {@code new}
486 * expression with an empty argument list. The class is initialized if it
487 * has not already been initialized.
488 *
489 * @deprecated This method propagates any exception thrown by the
490 * nullary constructor, including a checked exception. Use of
491 * this method effectively bypasses the compile-time exception
492 * checking that would otherwise be performed by the compiler.
493 * The {@link
494 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
495 * Constructor.newInstance} method avoids this problem by wrapping
496 * any exception thrown by the constructor in a (checked) {@link
497 * java.lang.reflect.InvocationTargetException}.
498 *
499 * <p>The call
521 * @throws InstantiationException
522 * if this {@code Class} represents an abstract class,
523 * an interface, an array class, a primitive type, or void;
524 * or if the class has no nullary constructor;
525 * or if the instantiation fails for some other reason.
526 * @throws ExceptionInInitializerError if the initialization
527 * provoked by this method fails.
528 * @throws SecurityException
529 * If a security manager, <i>s</i>, is present and
530 * the caller's class loader is not the same as or an
531 * ancestor of the class loader for the current class and
532 * invocation of {@link SecurityManager#checkPackageAccess
533 * s.checkPackageAccess()} denies access to the package
534 * of this class.
535 */
536 @CallerSensitive
537 @Deprecated(since="9")
538 public T newInstance()
539 throws InstantiationException, IllegalAccessException
540 {
541 ensureNotValueClass();
542
543 SecurityManager sm = System.getSecurityManager();
544 if (sm != null) {
545 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false);
546 }
547
548 // NOTE: the following code may not be strictly correct under
549 // the current Java memory model.
550
551 // Constructor lookup
552 if (cachedConstructor == null) {
553 if (this == Class.class) {
554 throw new IllegalAccessException(
555 "Can not call newInstance() on the Class for java.lang.Class"
556 );
557 }
558 try {
559 Class<?>[] empty = {};
560 final Constructor<T> c = getReflectionFactory().copyConstructor(
561 getConstructor0(empty, Member.DECLARED));
562 // Disable accessibility checks on the constructor
1252 if (enclosingInfo == null)
1253 return null;
1254 else {
1255 if (!enclosingInfo.isMethod())
1256 return null;
1257
1258 MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(),
1259 getFactory());
1260 Class<?> returnType = toClass(typeInfo.getReturnType());
1261 Type [] parameterTypes = typeInfo.getParameterTypes();
1262 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
1263
1264 // Convert Types to Classes; returned types *should*
1265 // be class objects since the methodDescriptor's used
1266 // don't have generics information
1267 for(int i = 0; i < parameterClasses.length; i++)
1268 parameterClasses[i] = toClass(parameterTypes[i]);
1269
1270 // Perform access check
1271 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
1272 enclosingCandidate.ensureNotValueClass();
1273
1274 SecurityManager sm = System.getSecurityManager();
1275 if (sm != null) {
1276 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED,
1277 Reflection.getCallerClass(), true);
1278 }
1279 Method[] candidates = enclosingCandidate.privateGetDeclaredMethods(false);
1280
1281 /*
1282 * Loop over all declared methods; match method name,
1283 * number of and type of parameters, *and* return
1284 * type. Matching return type is also necessary
1285 * because of covariant returns, etc.
1286 */
1287 ReflectionFactory fact = getReflectionFactory();
1288 for (Method m : candidates) {
1289 if (m.getName().equals(enclosingInfo.getName()) &&
1290 arrayContentsEq(parameterClasses,
1291 fact.getExecutableSharedParameterTypes(m))) {
1292 // finally, check return type
1293 if (m.getReturnType().equals(returnType)) {
1410
1411 if (enclosingInfo == null)
1412 return null;
1413 else {
1414 if (!enclosingInfo.isConstructor())
1415 return null;
1416
1417 ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(),
1418 getFactory());
1419 Type [] parameterTypes = typeInfo.getParameterTypes();
1420 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
1421
1422 // Convert Types to Classes; returned types *should*
1423 // be class objects since the methodDescriptor's used
1424 // don't have generics information
1425 for(int i = 0; i < parameterClasses.length; i++)
1426 parameterClasses[i] = toClass(parameterTypes[i]);
1427
1428 // Perform access check
1429 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
1430 enclosingCandidate.ensureNotValueClass();
1431
1432 SecurityManager sm = System.getSecurityManager();
1433 if (sm != null) {
1434 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED,
1435 Reflection.getCallerClass(), true);
1436 }
1437
1438 Constructor<?>[] candidates = enclosingCandidate
1439 .privateGetDeclaredConstructors(false);
1440 /*
1441 * Loop over all declared constructors; match number
1442 * of and type of parameters.
1443 */
1444 ReflectionFactory fact = getReflectionFactory();
1445 for (Constructor<?> c : candidates) {
1446 if (arrayContentsEq(parameterClasses,
1447 fact.getExecutableSharedParameterTypes(c))) {
1448 return fact.copyConstructor(c);
1449 }
1450 }
1451
1708 * and interface members declared by the class. This method returns an
1709 * array of length 0 if this {@code Class} object has no public member
1710 * classes or interfaces. This method also returns an array of length 0 if
1711 * this {@code Class} object represents a primitive type, an array
1712 * class, or void.
1713 *
1714 * @return the array of {@code Class} objects representing the public
1715 * members of this class
1716 * @throws SecurityException
1717 * If a security manager, <i>s</i>, is present and
1718 * the caller's class loader is not the same as or an
1719 * ancestor of the class loader for the current class and
1720 * invocation of {@link SecurityManager#checkPackageAccess
1721 * s.checkPackageAccess()} denies access to the package
1722 * of this class.
1723 *
1724 * @since 1.1
1725 */
1726 @CallerSensitive
1727 public Class<?>[] getClasses() {
1728 ensureNotValueClass();
1729
1730 SecurityManager sm = System.getSecurityManager();
1731 if (sm != null) {
1732 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false);
1733 }
1734
1735 // Privileged so this implementation can look at DECLARED classes,
1736 // something the caller might not have privilege to do. The code here
1737 // is allowed to look at DECLARED classes because (1) it does not hand
1738 // out anything other than public members and (2) public member access
1739 // has already been ok'd by the SecurityManager.
1740
1741 return java.security.AccessController.doPrivileged(
1742 new java.security.PrivilegedAction<>() {
1743 public Class<?>[] run() {
1744 List<Class<?>> list = new ArrayList<>();
1745 Class<?> currentClass = Class.this;
1746 while (currentClass != null) {
1747 for (Class<?> m : currentClass.getDeclaredClasses()) {
1748 if (Modifier.isPublic(m.getModifiers())) {
1749 list.add(m);
1779 *
1780 * <p> The elements in the returned array are not sorted and are not in any
1781 * particular order.
1782 *
1783 * @return the array of {@code Field} objects representing the
1784 * public fields
1785 * @throws SecurityException
1786 * If a security manager, <i>s</i>, is present and
1787 * the caller's class loader is not the same as or an
1788 * ancestor of the class loader for the current class and
1789 * invocation of {@link SecurityManager#checkPackageAccess
1790 * s.checkPackageAccess()} denies access to the package
1791 * of this class.
1792 *
1793 * @since 1.1
1794 * @jls 8.2 Class Members
1795 * @jls 8.3 Field Declarations
1796 */
1797 @CallerSensitive
1798 public Field[] getFields() throws SecurityException {
1799 ensureNotValueClass();
1800
1801 SecurityManager sm = System.getSecurityManager();
1802 if (sm != null) {
1803 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1804 }
1805 return copyFields(privateGetPublicFields(null));
1806 }
1807
1808
1809 /**
1810 * Returns an array containing {@code Method} objects reflecting all the
1811 * public methods of the class or interface represented by this {@code
1812 * Class} object, including those declared by the class or interface and
1813 * those inherited from superclasses and superinterfaces.
1814 *
1815 * <p> If this {@code Class} object represents an array type, then the
1816 * returned array has a {@code Method} object for each of the public
1817 * methods inherited by the array type from {@code Object}. It does not
1818 * contain a {@code Method} object for {@code clone()}.
1819 *
1820 * <p> If this {@code Class} object represents an interface then the
1871 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
1872 * method and the overriding method would have the same
1873 * signature but different return types.
1874 *
1875 * @return the array of {@code Method} objects representing the
1876 * public methods of this class
1877 * @throws SecurityException
1878 * If a security manager, <i>s</i>, is present and
1879 * the caller's class loader is not the same as or an
1880 * ancestor of the class loader for the current class and
1881 * invocation of {@link SecurityManager#checkPackageAccess
1882 * s.checkPackageAccess()} denies access to the package
1883 * of this class.
1884 *
1885 * @jls 8.2 Class Members
1886 * @jls 8.4 Method Declarations
1887 * @since 1.1
1888 */
1889 @CallerSensitive
1890 public Method[] getMethods() throws SecurityException {
1891 ensureNotValueClass();
1892
1893 SecurityManager sm = System.getSecurityManager();
1894 if (sm != null) {
1895 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1896 }
1897 return copyMethods(privateGetPublicMethods());
1898 }
1899
1900
1901 /**
1902 * Returns an array containing {@code Constructor} objects reflecting
1903 * all the public constructors of the class represented by this
1904 * {@code Class} object. An array of length 0 is returned if the
1905 * class has no public constructors, or if the class is an array class, or
1906 * if the class reflects a primitive type or void.
1907 *
1908 * Note that while this method returns an array of {@code
1909 * Constructor<T>} objects (that is an array of constructors from
1910 * this class), the return type of this method is {@code
1911 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1912 * might be expected. This less informative return type is
1913 * necessary since after being returned from this method, the
1914 * array could be modified to hold {@code Constructor} objects for
1915 * different classes, which would violate the type guarantees of
1916 * {@code Constructor<T>[]}.
1917 *
1918 * @return the array of {@code Constructor} objects representing the
1919 * public constructors of this class
1920 * @throws SecurityException
1921 * If a security manager, <i>s</i>, is present and
1922 * the caller's class loader is not the same as or an
1923 * ancestor of the class loader for the current class and
1924 * invocation of {@link SecurityManager#checkPackageAccess
1925 * s.checkPackageAccess()} denies access to the package
1926 * of this class.
1927 *
1928 * @since 1.1
1929 */
1930 @CallerSensitive
1931 public Constructor<?>[] getConstructors() throws SecurityException {
1932 ensureNotValueClass();
1933
1934 SecurityManager sm = System.getSecurityManager();
1935 if (sm != null) {
1936 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1937 }
1938 return copyConstructors(privateGetDeclaredConstructors(true));
1939 }
1940
1941
1942 /**
1943 * Returns a {@code Field} object that reflects the specified public member
1944 * field of the class or interface represented by this {@code Class}
1945 * object. The {@code name} parameter is a {@code String} specifying the
1946 * simple name of the desired field.
1947 *
1948 * <p> The field to be reflected is determined by the algorithm that
1949 * follows. Let C be the class or interface represented by this object:
1950 *
1951 * <OL>
1952 * <LI> If C declares a public field with the name specified, that is the
1953 * field to be reflected.</LI>
1967 * @return the {@code Field} object of this class specified by
1968 * {@code name}
1969 * @throws NoSuchFieldException if a field with the specified name is
1970 * not found.
1971 * @throws NullPointerException if {@code name} is {@code null}
1972 * @throws SecurityException
1973 * If a security manager, <i>s</i>, is present and
1974 * the caller's class loader is not the same as or an
1975 * ancestor of the class loader for the current class and
1976 * invocation of {@link SecurityManager#checkPackageAccess
1977 * s.checkPackageAccess()} denies access to the package
1978 * of this class.
1979 *
1980 * @since 1.1
1981 * @jls 8.2 Class Members
1982 * @jls 8.3 Field Declarations
1983 */
1984 @CallerSensitive
1985 public Field getField(String name)
1986 throws NoSuchFieldException, SecurityException {
1987 ensureNotValueClass();
1988
1989 Objects.requireNonNull(name);
1990 SecurityManager sm = System.getSecurityManager();
1991 if (sm != null) {
1992 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1993 }
1994 Field field = getField0(name);
1995 if (field == null) {
1996 throw new NoSuchFieldException(name);
1997 }
1998 return getReflectionFactory().copyField(field);
1999 }
2000
2001
2002 /**
2003 * Returns a {@code Method} object that reflects the specified public
2004 * member method of the class or interface represented by this
2005 * {@code Class} object. The {@code name} parameter is a
2006 * {@code String} specifying the simple name of the desired method. The
2007 * {@code parameterTypes} parameter is an array of {@code Class}
2008 * objects that identify the method's formal parameter types, in declared
2078 * @return the {@code Method} object that matches the specified
2079 * {@code name} and {@code parameterTypes}
2080 * @throws NoSuchMethodException if a matching method is not found
2081 * or if the name is "<init>"or "<clinit>".
2082 * @throws NullPointerException if {@code name} is {@code null}
2083 * @throws SecurityException
2084 * If a security manager, <i>s</i>, is present and
2085 * the caller's class loader is not the same as or an
2086 * ancestor of the class loader for the current class and
2087 * invocation of {@link SecurityManager#checkPackageAccess
2088 * s.checkPackageAccess()} denies access to the package
2089 * of this class.
2090 *
2091 * @jls 8.2 Class Members
2092 * @jls 8.4 Method Declarations
2093 * @since 1.1
2094 */
2095 @CallerSensitive
2096 public Method getMethod(String name, Class<?>... parameterTypes)
2097 throws NoSuchMethodException, SecurityException {
2098 ensureNotValueClass();
2099
2100 Objects.requireNonNull(name);
2101 SecurityManager sm = System.getSecurityManager();
2102 if (sm != null) {
2103 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
2104 }
2105 Method method = getMethod0(name, parameterTypes);
2106 if (method == null) {
2107 throw new NoSuchMethodException(methodToString(name, parameterTypes));
2108 }
2109 return getReflectionFactory().copyMethod(method);
2110 }
2111
2112 /**
2113 * Returns a {@code Method} object that reflects the specified public
2114 * member method of the class or interface represented by this
2115 * {@code Class} object.
2116 *
2117 * @param name the name of the method
2118 * @param parameterTypes the list of parameters
2119 * @return the {@code Method} object that matches the specified
2144 * types match those specified by {@code parameterTypes}.
2145 *
2146 * @param parameterTypes the parameter array
2147 * @return the {@code Constructor} object of the public constructor that
2148 * matches the specified {@code parameterTypes}
2149 * @throws NoSuchMethodException if a matching method is not found.
2150 * @throws SecurityException
2151 * If a security manager, <i>s</i>, is present and
2152 * the caller's class loader is not the same as or an
2153 * ancestor of the class loader for the current class and
2154 * invocation of {@link SecurityManager#checkPackageAccess
2155 * s.checkPackageAccess()} denies access to the package
2156 * of this class.
2157 *
2158 * @since 1.1
2159 */
2160 @CallerSensitive
2161 public Constructor<T> getConstructor(Class<?>... parameterTypes)
2162 throws NoSuchMethodException, SecurityException
2163 {
2164 ensureNotValueClass();
2165
2166 SecurityManager sm = System.getSecurityManager();
2167 if (sm != null) {
2168 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
2169 }
2170 return getReflectionFactory().copyConstructor(
2171 getConstructor0(parameterTypes, Member.PUBLIC));
2172 }
2173
2174
2175 /**
2176 * Returns an array of {@code Class} objects reflecting all the
2177 * classes and interfaces declared as members of the class represented by
2178 * this {@code Class} object. This includes public, protected, default
2179 * (package) access, and private classes and interfaces declared by the
2180 * class, but excludes inherited classes and interfaces. This method
2181 * returns an array of length 0 if the class declares no classes or
2182 * interfaces as members, or if this {@code Class} object represents a
2183 * primitive type, an array class, or void.
2184 *
2185 * @return the array of {@code Class} objects representing all the
2192 *
2193 * <li> the caller's class loader is not the same as the
2194 * class loader of this class and invocation of
2195 * {@link SecurityManager#checkPermission
2196 * s.checkPermission} method with
2197 * {@code RuntimePermission("accessDeclaredMembers")}
2198 * denies access to the declared classes within this class
2199 *
2200 * <li> the caller's class loader is not the same as or an
2201 * ancestor of the class loader for the current class and
2202 * invocation of {@link SecurityManager#checkPackageAccess
2203 * s.checkPackageAccess()} denies access to the package
2204 * of this class
2205 *
2206 * </ul>
2207 *
2208 * @since 1.1
2209 */
2210 @CallerSensitive
2211 public Class<?>[] getDeclaredClasses() throws SecurityException {
2212 ensureNotValueClass();
2213
2214 SecurityManager sm = System.getSecurityManager();
2215 if (sm != null) {
2216 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), false);
2217 }
2218 return getDeclaredClasses0();
2219 }
2220
2221
2222 /**
2223 * Returns an array of {@code Field} objects reflecting all the fields
2224 * declared by the class or interface represented by this
2225 * {@code Class} object. This includes public, protected, default
2226 * (package) access, and private fields, but excludes inherited fields.
2227 *
2228 * <p> If this {@code Class} object represents a class or interface with no
2229 * declared fields, then this method returns an array of length 0.
2230 *
2231 * <p> If this {@code Class} object represents an array type, a primitive
2232 * type, or void, then this method returns an array of length 0.
2233 *
2246 * class loader of this class and invocation of
2247 * {@link SecurityManager#checkPermission
2248 * s.checkPermission} method with
2249 * {@code RuntimePermission("accessDeclaredMembers")}
2250 * denies access to the declared fields within this class
2251 *
2252 * <li> the caller's class loader is not the same as or an
2253 * ancestor of the class loader for the current class and
2254 * invocation of {@link SecurityManager#checkPackageAccess
2255 * s.checkPackageAccess()} denies access to the package
2256 * of this class
2257 *
2258 * </ul>
2259 *
2260 * @since 1.1
2261 * @jls 8.2 Class Members
2262 * @jls 8.3 Field Declarations
2263 */
2264 @CallerSensitive
2265 public Field[] getDeclaredFields() throws SecurityException {
2266 ensureNotValueClass();
2267
2268 SecurityManager sm = System.getSecurityManager();
2269 if (sm != null) {
2270 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2271 }
2272 return copyFields(privateGetDeclaredFields(false));
2273 }
2274
2275
2276 /**
2277 *
2278 * Returns an array containing {@code Method} objects reflecting all the
2279 * declared methods of the class or interface represented by this {@code
2280 * Class} object, including public, protected, default (package)
2281 * access, and private methods, but excluding inherited methods.
2282 *
2283 * <p> If this {@code Class} object represents a type that has multiple
2284 * declared methods with the same name and parameter types, but different
2285 * return types, then the returned array has a {@code Method} object for
2286 * each such method.
2287 *
2310 * class loader of this class and invocation of
2311 * {@link SecurityManager#checkPermission
2312 * s.checkPermission} method with
2313 * {@code RuntimePermission("accessDeclaredMembers")}
2314 * denies access to the declared methods within this class
2315 *
2316 * <li> the caller's class loader is not the same as or an
2317 * ancestor of the class loader for the current class and
2318 * invocation of {@link SecurityManager#checkPackageAccess
2319 * s.checkPackageAccess()} denies access to the package
2320 * of this class
2321 *
2322 * </ul>
2323 *
2324 * @jls 8.2 Class Members
2325 * @jls 8.4 Method Declarations
2326 * @since 1.1
2327 */
2328 @CallerSensitive
2329 public Method[] getDeclaredMethods() throws SecurityException {
2330 ensureNotValueClass();
2331
2332 SecurityManager sm = System.getSecurityManager();
2333 if (sm != null) {
2334 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2335 }
2336 return copyMethods(privateGetDeclaredMethods(false));
2337 }
2338
2339
2340 /**
2341 * Returns an array of {@code Constructor} objects reflecting all the
2342 * constructors declared by the class represented by this
2343 * {@code Class} object. These are public, protected, default
2344 * (package) access, and private constructors. The elements in the array
2345 * returned are not sorted and are not in any particular order. If the
2346 * class has a default constructor, it is included in the returned array.
2347 * This method returns an array of length 0 if this {@code Class}
2348 * object represents an interface, a primitive type, an array class, or
2349 * void.
2350 *
2351 * <p> See <em>The Java Language Specification</em>, section 8.2.
2360 *
2361 * <li> the caller's class loader is not the same as the
2362 * class loader of this class and invocation of
2363 * {@link SecurityManager#checkPermission
2364 * s.checkPermission} method with
2365 * {@code RuntimePermission("accessDeclaredMembers")}
2366 * denies access to the declared constructors within this class
2367 *
2368 * <li> the caller's class loader is not the same as or an
2369 * ancestor of the class loader for the current class and
2370 * invocation of {@link SecurityManager#checkPackageAccess
2371 * s.checkPackageAccess()} denies access to the package
2372 * of this class
2373 *
2374 * </ul>
2375 *
2376 * @since 1.1
2377 */
2378 @CallerSensitive
2379 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
2380 ensureNotValueClass();
2381
2382 SecurityManager sm = System.getSecurityManager();
2383 if (sm != null) {
2384 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2385 }
2386 return copyConstructors(privateGetDeclaredConstructors(false));
2387 }
2388
2389
2390 /**
2391 * Returns a {@code Field} object that reflects the specified declared
2392 * field of the class or interface represented by this {@code Class}
2393 * object. The {@code name} parameter is a {@code String} that specifies
2394 * the simple name of the desired field.
2395 *
2396 * <p> If this {@code Class} object represents an array type, then this
2397 * method does not find the {@code length} field of the array type.
2398 *
2399 * @param name the name of the field
2400 * @return the {@code Field} object for the specified field in this
2401 * class
2413 * {@link SecurityManager#checkPermission
2414 * s.checkPermission} method with
2415 * {@code RuntimePermission("accessDeclaredMembers")}
2416 * denies access to the declared field
2417 *
2418 * <li> the caller's class loader is not the same as or an
2419 * ancestor of the class loader for the current class and
2420 * invocation of {@link SecurityManager#checkPackageAccess
2421 * s.checkPackageAccess()} denies access to the package
2422 * of this class
2423 *
2424 * </ul>
2425 *
2426 * @since 1.1
2427 * @jls 8.2 Class Members
2428 * @jls 8.3 Field Declarations
2429 */
2430 @CallerSensitive
2431 public Field getDeclaredField(String name)
2432 throws NoSuchFieldException, SecurityException {
2433 ensureNotValueClass();
2434
2435 Objects.requireNonNull(name);
2436 SecurityManager sm = System.getSecurityManager();
2437 if (sm != null) {
2438 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2439 }
2440 Field field = searchFields(privateGetDeclaredFields(false), name);
2441 if (field == null) {
2442 throw new NoSuchFieldException(name);
2443 }
2444 return getReflectionFactory().copyField(field);
2445 }
2446
2447
2448 /**
2449 * Returns a {@code Method} object that reflects the specified
2450 * declared method of the class or interface represented by this
2451 * {@code Class} object. The {@code name} parameter is a
2452 * {@code String} that specifies the simple name of the desired
2453 * method, and the {@code parameterTypes} parameter is an array of
2454 * {@code Class} objects that identify the method's formal parameter
2479 * {@link SecurityManager#checkPermission
2480 * s.checkPermission} method with
2481 * {@code RuntimePermission("accessDeclaredMembers")}
2482 * denies access to the declared method
2483 *
2484 * <li> the caller's class loader is not the same as or an
2485 * ancestor of the class loader for the current class and
2486 * invocation of {@link SecurityManager#checkPackageAccess
2487 * s.checkPackageAccess()} denies access to the package
2488 * of this class
2489 *
2490 * </ul>
2491 *
2492 * @jls 8.2 Class Members
2493 * @jls 8.4 Method Declarations
2494 * @since 1.1
2495 */
2496 @CallerSensitive
2497 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
2498 throws NoSuchMethodException, SecurityException {
2499 ensureNotValueClass();
2500
2501 Objects.requireNonNull(name);
2502 SecurityManager sm = System.getSecurityManager();
2503 if (sm != null) {
2504 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2505 }
2506 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
2507 if (method == null) {
2508 throw new NoSuchMethodException(methodToString(name, parameterTypes));
2509 }
2510 return getReflectionFactory().copyMethod(method);
2511 }
2512
2513
2514 /**
2515 * Returns a {@code Constructor} object that reflects the specified
2516 * constructor of the class or interface represented by this
2517 * {@code Class} object. The {@code parameterTypes} parameter is
2518 * an array of {@code Class} objects that identify the constructor's
2519 * formal parameter types, in declared order.
2520 *
2536 * class loader of this class and invocation of
2537 * {@link SecurityManager#checkPermission
2538 * s.checkPermission} method with
2539 * {@code RuntimePermission("accessDeclaredMembers")}
2540 * denies access to the declared constructor
2541 *
2542 * <li> the caller's class loader is not the same as or an
2543 * ancestor of the class loader for the current class and
2544 * invocation of {@link SecurityManager#checkPackageAccess
2545 * s.checkPackageAccess()} denies access to the package
2546 * of this class
2547 *
2548 * </ul>
2549 *
2550 * @since 1.1
2551 */
2552 @CallerSensitive
2553 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2554 throws NoSuchMethodException, SecurityException
2555 {
2556 ensureNotValueClass();
2557
2558 SecurityManager sm = System.getSecurityManager();
2559 if (sm != null) {
2560 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2561 }
2562
2563 return getReflectionFactory().copyConstructor(
2564 getConstructor0(parameterTypes, Member.DECLARED));
2565 }
2566
2567 /**
2568 * Finds a resource with a given name.
2569 *
2570 * <p> If this class is in a named {@link Module Module} then this method
2571 * will attempt to find the resource in the module. This is done by
2572 * delegating to the module's class loader {@link
2573 * ClassLoader#findResource(String,String) findResource(String,String)}
2574 * method, invoking it with the module name and the absolute name of the
2575 * resource. Resources in named modules are subject to the rules for
2576 * encapsulation specified in the {@code Module} {@link
2577 * Module#getResourceAsStream getResourceAsStream} method and so this
|