jdk/src/java.base/share/classes/java/lang/Class.java
changeset 43184 ac0aed3194d3
parent 43062 b9593792dfd9
child 43712 5dfd0950317c
equal deleted inserted replaced
43183:b50e0f90d284 43184:ac0aed3194d3
   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         }
  1444      */
  1424      */
  1445     @CallerSensitive
  1425     @CallerSensitive
  1446     public Class<?> getDeclaringClass() throws SecurityException {
  1426     public Class<?> getDeclaringClass() throws SecurityException {
  1447         final Class<?> candidate = getDeclaringClass0();
  1427         final Class<?> candidate = getDeclaringClass0();
  1448 
  1428 
  1449         if (candidate != null)
  1429         if (candidate != null) {
  1450             candidate.checkPackageAccess(
  1430             SecurityManager sm = System.getSecurityManager();
       
  1431             if (sm != null) {
       
  1432                 candidate.checkPackageAccess(sm,
  1451                     ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
  1433                     ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
       
  1434             }
       
  1435         }
  1452         return candidate;
  1436         return candidate;
  1453     }
  1437     }
  1454 
  1438 
  1455     private native Class<?> getDeclaringClass0();
  1439     private native Class<?> getDeclaringClass0();
  1456 
  1440 
  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
  1752      * @jls 8.2 Class Members
  1743      * @jls 8.2 Class Members
  1753      * @jls 8.3 Field Declarations
  1744      * @jls 8.3 Field Declarations
  1754      */
  1745      */
  1755     @CallerSensitive
  1746     @CallerSensitive
  1756     public Field[] getFields() throws SecurityException {
  1747     public Field[] getFields() throws SecurityException {
  1757         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
  1748         SecurityManager sm = System.getSecurityManager();
       
  1749         if (sm != null) {
       
  1750             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
       
  1751         }
  1758         return copyFields(privateGetPublicFields(null));
  1752         return copyFields(privateGetPublicFields(null));
  1759     }
  1753     }
  1760 
  1754 
  1761 
  1755 
  1762     /**
  1756     /**
  1839      * @jls 8.4 Method Declarations
  1833      * @jls 8.4 Method Declarations
  1840      * @since 1.1
  1834      * @since 1.1
  1841      */
  1835      */
  1842     @CallerSensitive
  1836     @CallerSensitive
  1843     public Method[] getMethods() throws SecurityException {
  1837     public Method[] getMethods() throws SecurityException {
  1844         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
  1838         SecurityManager sm = System.getSecurityManager();
       
  1839         if (sm != null) {
       
  1840             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
       
  1841         }
  1845         return copyMethods(privateGetPublicMethods());
  1842         return copyMethods(privateGetPublicMethods());
  1846     }
  1843     }
  1847 
  1844 
  1848 
  1845 
  1849     /**
  1846     /**
  1875      *
  1872      *
  1876      * @since 1.1
  1873      * @since 1.1
  1877      */
  1874      */
  1878     @CallerSensitive
  1875     @CallerSensitive
  1879     public Constructor<?>[] getConstructors() throws SecurityException {
  1876     public Constructor<?>[] getConstructors() throws SecurityException {
  1880         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
  1877         SecurityManager sm = System.getSecurityManager();
       
  1878         if (sm != null) {
       
  1879             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
       
  1880         }
  1881         return copyConstructors(privateGetDeclaredConstructors(true));
  1881         return copyConstructors(privateGetDeclaredConstructors(true));
  1882     }
  1882     }
  1883 
  1883 
  1884 
  1884 
  1885     /**
  1885     /**
  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 
  2134      *
  2144      *
  2135      * @since 1.1
  2145      * @since 1.1
  2136      */
  2146      */
  2137     @CallerSensitive
  2147     @CallerSensitive
  2138     public Class<?>[] getDeclaredClasses() throws SecurityException {
  2148     public Class<?>[] getDeclaredClasses() throws SecurityException {
  2139         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false);
  2149         SecurityManager sm = System.getSecurityManager();
       
  2150         if (sm != null) {
       
  2151             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), false);
       
  2152         }
  2140         return getDeclaredClasses0();
  2153         return getDeclaredClasses0();
  2141     }
  2154     }
  2142 
  2155 
  2143 
  2156 
  2144     /**
  2157     /**
  2183      * @jls 8.2 Class Members
  2196      * @jls 8.2 Class Members
  2184      * @jls 8.3 Field Declarations
  2197      * @jls 8.3 Field Declarations
  2185      */
  2198      */
  2186     @CallerSensitive
  2199     @CallerSensitive
  2187     public Field[] getDeclaredFields() throws SecurityException {
  2200     public Field[] getDeclaredFields() throws SecurityException {
  2188         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
  2201         SecurityManager sm = System.getSecurityManager();
       
  2202         if (sm != null) {
       
  2203             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
       
  2204         }
  2189         return copyFields(privateGetDeclaredFields(false));
  2205         return copyFields(privateGetDeclaredFields(false));
  2190     }
  2206     }
  2191 
  2207 
  2192 
  2208 
  2193     /**
  2209     /**
  2242      * @jls 8.4 Method Declarations
  2258      * @jls 8.4 Method Declarations
  2243      * @since 1.1
  2259      * @since 1.1
  2244      */
  2260      */
  2245     @CallerSensitive
  2261     @CallerSensitive
  2246     public Method[] getDeclaredMethods() throws SecurityException {
  2262     public Method[] getDeclaredMethods() throws SecurityException {
  2247         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
  2263         SecurityManager sm = System.getSecurityManager();
       
  2264         if (sm != null) {
       
  2265             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
       
  2266         }
  2248         return copyMethods(privateGetDeclaredMethods(false));
  2267         return copyMethods(privateGetDeclaredMethods(false));
  2249     }
  2268     }
  2250 
  2269 
  2251 
  2270 
  2252     /**
  2271     /**
  2287      *
  2306      *
  2288      * @since 1.1
  2307      * @since 1.1
  2289      */
  2308      */
  2290     @CallerSensitive
  2309     @CallerSensitive
  2291     public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
  2310     public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
  2292         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
  2311         SecurityManager sm = System.getSecurityManager();
       
  2312         if (sm != null) {
       
  2313             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
       
  2314         }
  2293         return copyConstructors(privateGetDeclaredConstructors(false));
  2315         return copyConstructors(privateGetDeclaredConstructors(false));
  2294     }
  2316     }
  2295 
  2317 
  2296 
  2318 
  2297     /**
  2319     /**
  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 "/"
  2753         if (!name.startsWith("/")) {
  2784         if (!name.startsWith("/")) {
  2754             Class<?> c = this;
  2785             Class<?> c = this;
  2755             while (c.isArray()) {
  2786             while (c.isArray()) {
  2756                 c = c.getComponentType();
  2787                 c = c.getComponentType();
  2757             }
  2788             }
  2758             String baseName = c.getName();
  2789             String baseName = c.getPackageName();
  2759             int index = baseName.lastIndexOf('.');
  2790             if (baseName != null && !baseName.isEmpty()) {
  2760             if (index != -1) {
  2791                 name = baseName.replace('.', '/') + "/" + name;
  2761                 name = baseName.substring(0, index).replace('.', '/')
       
  2762                     +"/"+name;
       
  2763             }
  2792             }
  2764         } else {
  2793         } else {
  2765             name = name.substring(1);
  2794             name = name.substring(1);
  2766         }
  2795         }
  2767         return name;
  2796         return name;
  3231             if (arrayContentsEq(parameterTypes,
  3260             if (arrayContentsEq(parameterTypes,
  3232                                 fact.getExecutableSharedParameterTypes(constructor))) {
  3261                                 fact.getExecutableSharedParameterTypes(constructor))) {
  3233                 return constructor;
  3262                 return constructor;
  3234             }
  3263             }
  3235         }
  3264         }
  3236         throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
  3265         throw new NoSuchMethodException(methodToString("<init>", parameterTypes));
  3237     }
  3266     }
  3238 
  3267 
  3239     //
  3268     //
  3240     // Other helpers and base implementation
  3269     // Other helpers and base implementation
  3241     //
  3270     //
  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             }