506 @CallerSensitive |
506 @CallerSensitive |
507 @Deprecated(since="9") |
507 @Deprecated(since="9") |
508 public T newInstance() |
508 public T newInstance() |
509 throws InstantiationException, IllegalAccessException |
509 throws InstantiationException, IllegalAccessException |
510 { |
510 { |
511 if (System.getSecurityManager() != null) { |
511 SecurityManager sm = System.getSecurityManager(); |
512 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); |
512 if (sm != null) { |
|
513 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false); |
513 } |
514 } |
514 |
515 |
515 // NOTE: the following code may not be strictly correct under |
516 // NOTE: the following code may not be strictly correct under |
516 // the current Java memory model. |
517 // the current Java memory model. |
517 |
518 |
1221 for(int i = 0; i < parameterClasses.length; i++) |
1222 for(int i = 0; i < parameterClasses.length; i++) |
1222 parameterClasses[i] = toClass(parameterTypes[i]); |
1223 parameterClasses[i] = toClass(parameterTypes[i]); |
1223 |
1224 |
1224 // Perform access check |
1225 // Perform access check |
1225 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); |
1226 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); |
1226 enclosingCandidate.checkMemberAccess(Member.DECLARED, |
1227 SecurityManager sm = System.getSecurityManager(); |
1227 Reflection.getCallerClass(), true); |
1228 if (sm != null) { |
1228 // Client is ok to access declared methods but j.l.Class might not be. |
1229 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED, |
1229 Method[] candidates = AccessController.doPrivileged( |
1230 Reflection.getCallerClass(), true); |
1230 new PrivilegedAction<>() { |
1231 } |
1231 @Override |
1232 Method[] candidates = enclosingCandidate.privateGetDeclaredMethods(false); |
1232 public Method[] run() { |
1233 |
1233 return enclosingCandidate.getDeclaredMethods(); |
|
1234 } |
|
1235 }); |
|
1236 /* |
1234 /* |
1237 * Loop over all declared methods; match method name, |
1235 * Loop over all declared methods; match method name, |
1238 * number of and type of parameters, *and* return |
1236 * number of and type of parameters, *and* return |
1239 * type. Matching return type is also necessary |
1237 * type. Matching return type is also necessary |
1240 * because of covariant returns, etc. |
1238 * because of covariant returns, etc. |
1241 */ |
1239 */ |
1242 for(Method m: candidates) { |
1240 ReflectionFactory fact = getReflectionFactory(); |
1243 if (m.getName().equals(enclosingInfo.getName()) ) { |
1241 for (Method m : candidates) { |
1244 Class<?>[] candidateParamClasses = m.getParameterTypes(); |
1242 if (m.getName().equals(enclosingInfo.getName()) && |
1245 if (candidateParamClasses.length == parameterClasses.length) { |
1243 arrayContentsEq(parameterClasses, |
1246 boolean matches = true; |
1244 fact.getExecutableSharedParameterTypes(m))) { |
1247 for(int i = 0; i < candidateParamClasses.length; i++) { |
1245 // finally, check return type |
1248 if (!candidateParamClasses[i].equals(parameterClasses[i])) { |
1246 if (m.getReturnType().equals(returnType)) { |
1249 matches = false; |
1247 return fact.copyMethod(m); |
1250 break; |
|
1251 } |
|
1252 } |
|
1253 |
|
1254 if (matches) { // finally, check return type |
|
1255 if (m.getReturnType().equals(returnType) ) |
|
1256 return m; |
|
1257 } |
|
1258 } |
1248 } |
1259 } |
1249 } |
1260 } |
1250 } |
1261 |
1251 |
1262 throw new InternalError("Enclosing method not found"); |
1252 throw new InternalError("Enclosing method not found"); |
1388 for(int i = 0; i < parameterClasses.length; i++) |
1378 for(int i = 0; i < parameterClasses.length; i++) |
1389 parameterClasses[i] = toClass(parameterTypes[i]); |
1379 parameterClasses[i] = toClass(parameterTypes[i]); |
1390 |
1380 |
1391 // Perform access check |
1381 // Perform access check |
1392 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); |
1382 final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); |
1393 enclosingCandidate.checkMemberAccess(Member.DECLARED, |
1383 SecurityManager sm = System.getSecurityManager(); |
1394 Reflection.getCallerClass(), true); |
1384 if (sm != null) { |
1395 // Client is ok to access declared methods but j.l.Class might not be. |
1385 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED, |
1396 Constructor<?>[] candidates = AccessController.doPrivileged( |
1386 Reflection.getCallerClass(), true); |
1397 new PrivilegedAction<>() { |
1387 } |
1398 @Override |
1388 |
1399 public Constructor<?>[] run() { |
1389 Constructor<?>[] candidates = enclosingCandidate |
1400 return enclosingCandidate.getDeclaredConstructors(); |
1390 .privateGetDeclaredConstructors(false); |
1401 } |
|
1402 }); |
|
1403 /* |
1391 /* |
1404 * Loop over all declared constructors; match number |
1392 * Loop over all declared constructors; match number |
1405 * of and type of parameters. |
1393 * of and type of parameters. |
1406 */ |
1394 */ |
1407 for(Constructor<?> c: candidates) { |
1395 ReflectionFactory fact = getReflectionFactory(); |
1408 Class<?>[] candidateParamClasses = c.getParameterTypes(); |
1396 for (Constructor<?> c : candidates) { |
1409 if (candidateParamClasses.length == parameterClasses.length) { |
1397 if (arrayContentsEq(parameterClasses, |
1410 boolean matches = true; |
1398 fact.getExecutableSharedParameterTypes(c))) { |
1411 for(int i = 0; i < candidateParamClasses.length; i++) { |
1399 return fact.copyConstructor(c); |
1412 if (!candidateParamClasses[i].equals(parameterClasses[i])) { |
|
1413 matches = false; |
|
1414 break; |
|
1415 } |
|
1416 } |
|
1417 |
|
1418 if (matches) |
|
1419 return c; |
|
1420 } |
1400 } |
1421 } |
1401 } |
1422 |
1402 |
1423 throw new InternalError("Enclosing constructor not found"); |
1403 throw new InternalError("Enclosing constructor not found"); |
1424 } |
1404 } |
1494 throw new InternalError("Malformed enclosing method information"); |
1478 throw new InternalError("Malformed enclosing method information"); |
1495 else |
1479 else |
1496 enclosingCandidate = enclosingClass; |
1480 enclosingCandidate = enclosingClass; |
1497 } |
1481 } |
1498 |
1482 |
1499 if (enclosingCandidate != null) |
1483 if (enclosingCandidate != null) { |
1500 enclosingCandidate.checkPackageAccess( |
1484 SecurityManager sm = System.getSecurityManager(); |
|
1485 if (sm != null) { |
|
1486 enclosingCandidate.checkPackageAccess(sm, |
1501 ClassLoader.getClassLoader(Reflection.getCallerClass()), true); |
1487 ClassLoader.getClassLoader(Reflection.getCallerClass()), true); |
|
1488 } |
|
1489 } |
1502 return enclosingCandidate; |
1490 return enclosingCandidate; |
1503 } |
1491 } |
1504 |
1492 |
1505 /** |
1493 /** |
1506 * Returns the simple name of the underlying class as given in the |
1494 * Returns the simple name of the underlying class as given in the |
1686 * |
1674 * |
1687 * @since 1.1 |
1675 * @since 1.1 |
1688 */ |
1676 */ |
1689 @CallerSensitive |
1677 @CallerSensitive |
1690 public Class<?>[] getClasses() { |
1678 public Class<?>[] getClasses() { |
1691 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); |
1679 SecurityManager sm = System.getSecurityManager(); |
|
1680 if (sm != null) { |
|
1681 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false); |
|
1682 } |
1692 |
1683 |
1693 // Privileged so this implementation can look at DECLARED classes, |
1684 // Privileged so this implementation can look at DECLARED classes, |
1694 // something the caller might not have privilege to do. The code here |
1685 // something the caller might not have privilege to do. The code here |
1695 // is allowed to look at DECLARED classes because (1) it does not hand |
1686 // is allowed to look at DECLARED classes because (1) it does not hand |
1696 // out anything other than public members and (2) public member access |
1687 // out anything other than public members and (2) public member access |
1926 */ |
1926 */ |
1927 @CallerSensitive |
1927 @CallerSensitive |
1928 public Field getField(String name) |
1928 public Field getField(String name) |
1929 throws NoSuchFieldException, SecurityException { |
1929 throws NoSuchFieldException, SecurityException { |
1930 Objects.requireNonNull(name); |
1930 Objects.requireNonNull(name); |
1931 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); |
1931 SecurityManager sm = System.getSecurityManager(); |
|
1932 if (sm != null) { |
|
1933 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true); |
|
1934 } |
1932 Field field = getField0(name); |
1935 Field field = getField0(name); |
1933 if (field == null) { |
1936 if (field == null) { |
1934 throw new NoSuchFieldException(name); |
1937 throw new NoSuchFieldException(name); |
1935 } |
1938 } |
1936 return getReflectionFactory().copyField(field); |
1939 return getReflectionFactory().copyField(field); |
2032 */ |
2035 */ |
2033 @CallerSensitive |
2036 @CallerSensitive |
2034 public Method getMethod(String name, Class<?>... parameterTypes) |
2037 public Method getMethod(String name, Class<?>... parameterTypes) |
2035 throws NoSuchMethodException, SecurityException { |
2038 throws NoSuchMethodException, SecurityException { |
2036 Objects.requireNonNull(name); |
2039 Objects.requireNonNull(name); |
2037 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); |
2040 SecurityManager sm = System.getSecurityManager(); |
|
2041 if (sm != null) { |
|
2042 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true); |
|
2043 } |
2038 Method method = getMethod0(name, parameterTypes); |
2044 Method method = getMethod0(name, parameterTypes); |
2039 if (method == null) { |
2045 if (method == null) { |
2040 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); |
2046 throw new NoSuchMethodException(methodToString(name, parameterTypes)); |
2041 } |
2047 } |
2042 return getReflectionFactory().copyMethod(method); |
2048 return getReflectionFactory().copyMethod(method); |
2043 } |
2049 } |
2044 |
2050 |
2045 /** |
2051 /** |
2090 * |
2096 * |
2091 * @since 1.1 |
2097 * @since 1.1 |
2092 */ |
2098 */ |
2093 @CallerSensitive |
2099 @CallerSensitive |
2094 public Constructor<T> getConstructor(Class<?>... parameterTypes) |
2100 public Constructor<T> getConstructor(Class<?>... parameterTypes) |
2095 throws NoSuchMethodException, SecurityException { |
2101 throws NoSuchMethodException, SecurityException |
2096 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); |
2102 { |
|
2103 SecurityManager sm = System.getSecurityManager(); |
|
2104 if (sm != null) { |
|
2105 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true); |
|
2106 } |
2097 return getReflectionFactory().copyConstructor( |
2107 return getReflectionFactory().copyConstructor( |
2098 getConstructor0(parameterTypes, Member.PUBLIC)); |
2108 getConstructor0(parameterTypes, Member.PUBLIC)); |
2099 } |
2109 } |
2100 |
2110 |
2101 |
2111 |
2336 */ |
2358 */ |
2337 @CallerSensitive |
2359 @CallerSensitive |
2338 public Field getDeclaredField(String name) |
2360 public Field getDeclaredField(String name) |
2339 throws NoSuchFieldException, SecurityException { |
2361 throws NoSuchFieldException, SecurityException { |
2340 Objects.requireNonNull(name); |
2362 Objects.requireNonNull(name); |
2341 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); |
2363 SecurityManager sm = System.getSecurityManager(); |
|
2364 if (sm != null) { |
|
2365 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true); |
|
2366 } |
2342 Field field = searchFields(privateGetDeclaredFields(false), name); |
2367 Field field = searchFields(privateGetDeclaredFields(false), name); |
2343 if (field == null) { |
2368 if (field == null) { |
2344 throw new NoSuchFieldException(name); |
2369 throw new NoSuchFieldException(name); |
2345 } |
2370 } |
2346 return getReflectionFactory().copyField(field); |
2371 return getReflectionFactory().copyField(field); |
2397 */ |
2422 */ |
2398 @CallerSensitive |
2423 @CallerSensitive |
2399 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) |
2424 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) |
2400 throws NoSuchMethodException, SecurityException { |
2425 throws NoSuchMethodException, SecurityException { |
2401 Objects.requireNonNull(name); |
2426 Objects.requireNonNull(name); |
2402 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); |
2427 SecurityManager sm = System.getSecurityManager(); |
|
2428 if (sm != null) { |
|
2429 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true); |
|
2430 } |
2403 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); |
2431 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); |
2404 if (method == null) { |
2432 if (method == null) { |
2405 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); |
2433 throw new NoSuchMethodException(methodToString(name, parameterTypes)); |
2406 } |
2434 } |
2407 return getReflectionFactory().copyMethod(method); |
2435 return getReflectionFactory().copyMethod(method); |
2408 } |
2436 } |
2409 |
2437 |
2410 |
2438 |
2446 * |
2474 * |
2447 * @since 1.1 |
2475 * @since 1.1 |
2448 */ |
2476 */ |
2449 @CallerSensitive |
2477 @CallerSensitive |
2450 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) |
2478 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) |
2451 throws NoSuchMethodException, SecurityException { |
2479 throws NoSuchMethodException, SecurityException |
2452 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); |
2480 { |
|
2481 SecurityManager sm = System.getSecurityManager(); |
|
2482 if (sm != null) { |
|
2483 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true); |
|
2484 } |
|
2485 |
2453 return getReflectionFactory().copyConstructor( |
2486 return getReflectionFactory().copyConstructor( |
2454 getConstructor0(parameterTypes, Member.DECLARED)); |
2487 getConstructor0(parameterTypes, Member.DECLARED)); |
2455 } |
2488 } |
2456 |
2489 |
2457 /** |
2490 /** |
2695 * |
2728 * |
2696 * This method also enforces package access. |
2729 * This method also enforces package access. |
2697 * |
2730 * |
2698 * <p> Default policy: allow all clients access with normal Java access |
2731 * <p> Default policy: allow all clients access with normal Java access |
2699 * control. |
2732 * control. |
2700 */ |
2733 * |
2701 private void checkMemberAccess(int which, Class<?> caller, boolean checkProxyInterfaces) { |
2734 * <p> NOTE: should only be called if a SecurityManager is installed |
2702 final SecurityManager s = System.getSecurityManager(); |
2735 */ |
2703 if (s != null) { |
2736 private void checkMemberAccess(SecurityManager sm, int which, |
2704 /* Default policy allows access to all {@link Member#PUBLIC} members, |
2737 Class<?> caller, boolean checkProxyInterfaces) { |
2705 * as well as access to classes that have the same class loader as the caller. |
2738 /* Default policy allows access to all {@link Member#PUBLIC} members, |
2706 * In all other cases, it requires RuntimePermission("accessDeclaredMembers") |
2739 * as well as access to classes that have the same class loader as the caller. |
2707 * permission. |
2740 * In all other cases, it requires RuntimePermission("accessDeclaredMembers") |
2708 */ |
2741 * permission. |
2709 final ClassLoader ccl = ClassLoader.getClassLoader(caller); |
2742 */ |
|
2743 final ClassLoader ccl = caller.getClassLoader0(); |
|
2744 if (which != Member.PUBLIC) { |
2710 final ClassLoader cl = getClassLoader0(); |
2745 final ClassLoader cl = getClassLoader0(); |
2711 if (which != Member.PUBLIC) { |
2746 if (ccl != cl) { |
2712 if (ccl != cl) { |
2747 sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); |
2713 s.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); |
2748 } |
2714 } |
2749 } |
2715 } |
2750 this.checkPackageAccess(sm, ccl, checkProxyInterfaces); |
2716 this.checkPackageAccess(ccl, checkProxyInterfaces); |
|
2717 } |
|
2718 } |
2751 } |
2719 |
2752 |
2720 /* |
2753 /* |
2721 * Checks if a client loaded in ClassLoader ccl is allowed to access this |
2754 * Checks if a client loaded in ClassLoader ccl is allowed to access this |
2722 * class under the current package access policy. If access is denied, |
2755 * class under the current package access policy. If access is denied, |
2723 * throw a SecurityException. |
2756 * throw a SecurityException. |
2724 */ |
2757 * |
2725 private void checkPackageAccess(final ClassLoader ccl, boolean checkProxyInterfaces) { |
2758 * NOTE: this method should only be called if a SecurityManager is active |
2726 final SecurityManager s = System.getSecurityManager(); |
2759 */ |
2727 if (s != null) { |
2760 private void checkPackageAccess(SecurityManager sm, final ClassLoader ccl, |
2728 final ClassLoader cl = getClassLoader0(); |
2761 boolean checkProxyInterfaces) { |
2729 |
2762 final ClassLoader cl = getClassLoader0(); |
2730 if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) { |
2763 |
2731 String name = this.getName(); |
2764 if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) { |
2732 int i = name.lastIndexOf('.'); |
2765 String pkg = this.getPackageName(); |
2733 if (i != -1) { |
2766 if (pkg != null && !pkg.isEmpty()) { |
2734 // skip the package access check on a proxy class in default proxy package |
2767 // skip the package access check on a proxy class in default proxy package |
2735 String pkg = name.substring(0, i); |
2768 if (!Proxy.isProxyClass(this) || ReflectUtil.isNonPublicProxyClass(this)) { |
2736 if (!Proxy.isProxyClass(this) || ReflectUtil.isNonPublicProxyClass(this)) { |
2769 sm.checkPackageAccess(pkg); |
2737 s.checkPackageAccess(pkg); |
|
2738 } |
|
2739 } |
2770 } |
2740 } |
2771 } |
2741 // check package access on the proxy interfaces |
2772 } |
2742 if (checkProxyInterfaces && Proxy.isProxyClass(this)) { |
2773 // check package access on the proxy interfaces |
2743 ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces()); |
2774 if (checkProxyInterfaces && Proxy.isProxyClass(this)) { |
2744 } |
2775 ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces()); |
2745 } |
2776 } |
2746 } |
2777 } |
2747 |
2778 |
2748 /** |
2779 /** |
2749 * Add a package name prefix if the name is not absolute Remove leading "/" |
2780 * Add a package name prefix if the name is not absolute Remove leading "/" |
3292 private native Field[] getDeclaredFields0(boolean publicOnly); |
3321 private native Field[] getDeclaredFields0(boolean publicOnly); |
3293 private native Method[] getDeclaredMethods0(boolean publicOnly); |
3322 private native Method[] getDeclaredMethods0(boolean publicOnly); |
3294 private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly); |
3323 private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly); |
3295 private native Class<?>[] getDeclaredClasses0(); |
3324 private native Class<?>[] getDeclaredClasses0(); |
3296 |
3325 |
3297 private static String argumentTypesToString(Class<?>[] argTypes) { |
3326 /** |
3298 StringJoiner sj = new StringJoiner(", ", "(", ")"); |
3327 * Helper method to get the method name from arguments. |
|
3328 */ |
|
3329 private String methodToString(String name, Class<?>[] argTypes) { |
|
3330 StringJoiner sj = new StringJoiner(", ", getName() + "." + name + "(", ")"); |
3299 if (argTypes != null) { |
3331 if (argTypes != null) { |
3300 for (int i = 0; i < argTypes.length; i++) { |
3332 for (int i = 0; i < argTypes.length; i++) { |
3301 Class<?> c = argTypes[i]; |
3333 Class<?> c = argTypes[i]; |
3302 sj.add((c == null) ? "null" : c.getName()); |
3334 sj.add((c == null) ? "null" : c.getName()); |
3303 } |
3335 } |