jdk/src/share/classes/java/lang/Class.java
changeset 16126 aad71cf676d7
parent 16051 649a03329639
parent 16115 dd60b90b6d20
child 16743 b0b34102bb4c
equal deleted inserted replaced
16069:469ad49d6185 16126:aad71cf676d7
    62 import sun.reflect.generics.repository.MethodRepository;
    62 import sun.reflect.generics.repository.MethodRepository;
    63 import sun.reflect.generics.repository.ConstructorRepository;
    63 import sun.reflect.generics.repository.ConstructorRepository;
    64 import sun.reflect.generics.scope.ClassScope;
    64 import sun.reflect.generics.scope.ClassScope;
    65 import sun.security.util.SecurityConstants;
    65 import sun.security.util.SecurityConstants;
    66 import java.lang.annotation.Annotation;
    66 import java.lang.annotation.Annotation;
       
    67 import java.lang.reflect.Proxy;
    67 import sun.reflect.annotation.*;
    68 import sun.reflect.annotation.*;
       
    69 import sun.reflect.misc.ReflectUtil;
    68 
    70 
    69 /**
    71 /**
    70  * Instances of the class {@code Class} represent classes and
    72  * Instances of the class {@code Class} represent classes and
    71  * interfaces in a running Java application.  An enum is a kind of
    73  * interfaces in a running Java application.  An enum is a kind of
    72  * class and an annotation is a kind of interface.  Every array also
    74  * class and an annotation is a kind of interface.  Every array also
   249      */
   251      */
   250     public static Class<?> forName(String name, boolean initialize,
   252     public static Class<?> forName(String name, boolean initialize,
   251                                    ClassLoader loader)
   253                                    ClassLoader loader)
   252         throws ClassNotFoundException
   254         throws ClassNotFoundException
   253     {
   255     {
   254         if (loader == null) {
   256         if (sun.misc.VM.isSystemDomainLoader(loader)) {
   255             SecurityManager sm = System.getSecurityManager();
   257             SecurityManager sm = System.getSecurityManager();
   256             if (sm != null) {
   258             if (sm != null) {
   257                 ClassLoader ccl = ClassLoader.getCallerClassLoader();
   259                 ClassLoader ccl = ClassLoader.getCallerClassLoader();
   258                 if (ccl != null) {
   260                 if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
   259                     sm.checkPermission(
   261                     sm.checkPermission(
   260                         SecurityConstants.GET_CLASSLOADER_PERMISSION);
   262                         SecurityConstants.GET_CLASSLOADER_PERMISSION);
   261                 }
   263                 }
   262             }
   264             }
   263         }
   265         }
   318      */
   320      */
   319     public T newInstance()
   321     public T newInstance()
   320         throws InstantiationException, IllegalAccessException
   322         throws InstantiationException, IllegalAccessException
   321     {
   323     {
   322         if (System.getSecurityManager() != null) {
   324         if (System.getSecurityManager() != null) {
   323             checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
   325             checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false);
   324         }
   326         }
   325         return newInstance0();
   327         return newInstance0();
   326     }
   328     }
   327 
   329 
   328     private T newInstance0()
   330     private T newInstance0()
  1298      */
  1300      */
  1299     public Class<?>[] getClasses() {
  1301     public Class<?>[] getClasses() {
  1300         // be very careful not to change the stack depth of this
  1302         // be very careful not to change the stack depth of this
  1301         // checkMemberAccess call for security reasons
  1303         // checkMemberAccess call for security reasons
  1302         // see java.lang.SecurityManager.checkMemberAccess
  1304         // see java.lang.SecurityManager.checkMemberAccess
  1303         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  1305         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false);
  1304 
  1306 
  1305         // Privileged so this implementation can look at DECLARED classes,
  1307         // Privileged so this implementation can look at DECLARED classes,
  1306         // something the caller might not have privilege to do.  The code here
  1308         // something the caller might not have privilege to do.  The code here
  1307         // is allowed to look at DECLARED classes because (1) it does not hand
  1309         // is allowed to look at DECLARED classes because (1) it does not hand
  1308         // out anything other than public members and (2) public member access
  1310         // out anything other than public members and (2) public member access
  1373      */
  1375      */
  1374     public Field[] getFields() throws SecurityException {
  1376     public Field[] getFields() throws SecurityException {
  1375         // be very careful not to change the stack depth of this
  1377         // be very careful not to change the stack depth of this
  1376         // checkMemberAccess call for security reasons
  1378         // checkMemberAccess call for security reasons
  1377         // see java.lang.SecurityManager.checkMemberAccess
  1379         // see java.lang.SecurityManager.checkMemberAccess
  1378         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  1380         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
  1379         return copyFields(privateGetPublicFields(null));
  1381         return copyFields(privateGetPublicFields(null));
  1380     }
  1382     }
  1381 
  1383 
  1382 
  1384 
  1383     /**
  1385     /**
  1424      */
  1426      */
  1425     public Method[] getMethods() throws SecurityException {
  1427     public Method[] getMethods() throws SecurityException {
  1426         // be very careful not to change the stack depth of this
  1428         // be very careful not to change the stack depth of this
  1427         // checkMemberAccess call for security reasons
  1429         // checkMemberAccess call for security reasons
  1428         // see java.lang.SecurityManager.checkMemberAccess
  1430         // see java.lang.SecurityManager.checkMemberAccess
  1429         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  1431         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
  1430         return copyMethods(privateGetPublicMethods());
  1432         return copyMethods(privateGetPublicMethods());
  1431     }
  1433     }
  1432 
  1434 
  1433 
  1435 
  1434     /**
  1436     /**
  1473      */
  1475      */
  1474     public Constructor<?>[] getConstructors() throws SecurityException {
  1476     public Constructor<?>[] getConstructors() throws SecurityException {
  1475         // be very careful not to change the stack depth of this
  1477         // be very careful not to change the stack depth of this
  1476         // checkMemberAccess call for security reasons
  1478         // checkMemberAccess call for security reasons
  1477         // see java.lang.SecurityManager.checkMemberAccess
  1479         // see java.lang.SecurityManager.checkMemberAccess
  1478         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  1480         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
  1479         return copyConstructors(privateGetDeclaredConstructors(true));
  1481         return copyConstructors(privateGetDeclaredConstructors(true));
  1480     }
  1482     }
  1481 
  1483 
  1482 
  1484 
  1483     /**
  1485     /**
  1532     public Field getField(String name)
  1534     public Field getField(String name)
  1533         throws NoSuchFieldException, SecurityException {
  1535         throws NoSuchFieldException, SecurityException {
  1534         // be very careful not to change the stack depth of this
  1536         // be very careful not to change the stack depth of this
  1535         // checkMemberAccess call for security reasons
  1537         // checkMemberAccess call for security reasons
  1536         // see java.lang.SecurityManager.checkMemberAccess
  1538         // see java.lang.SecurityManager.checkMemberAccess
  1537         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  1539         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
  1538         Field field = getField0(name);
  1540         Field field = getField0(name);
  1539         if (field == null) {
  1541         if (field == null) {
  1540             throw new NoSuchFieldException(name);
  1542             throw new NoSuchFieldException(name);
  1541         }
  1543         }
  1542         return field;
  1544         return field;
  1617     public Method getMethod(String name, Class<?>... parameterTypes)
  1619     public Method getMethod(String name, Class<?>... parameterTypes)
  1618         throws NoSuchMethodException, SecurityException {
  1620         throws NoSuchMethodException, SecurityException {
  1619         // be very careful not to change the stack depth of this
  1621         // be very careful not to change the stack depth of this
  1620         // checkMemberAccess call for security reasons
  1622         // checkMemberAccess call for security reasons
  1621         // see java.lang.SecurityManager.checkMemberAccess
  1623         // see java.lang.SecurityManager.checkMemberAccess
  1622         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  1624         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
  1623         Method method = getMethod0(name, parameterTypes);
  1625         Method method = getMethod0(name, parameterTypes);
  1624         if (method == null) {
  1626         if (method == null) {
  1625             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
  1627             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
  1626         }
  1628         }
  1627         return method;
  1629         return method;
  1671     public Constructor<T> getConstructor(Class<?>... parameterTypes)
  1673     public Constructor<T> getConstructor(Class<?>... parameterTypes)
  1672         throws NoSuchMethodException, SecurityException {
  1674         throws NoSuchMethodException, SecurityException {
  1673         // be very careful not to change the stack depth of this
  1675         // be very careful not to change the stack depth of this
  1674         // checkMemberAccess call for security reasons
  1676         // checkMemberAccess call for security reasons
  1675         // see java.lang.SecurityManager.checkMemberAccess
  1677         // see java.lang.SecurityManager.checkMemberAccess
  1676         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  1678         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
  1677         return getConstructor0(parameterTypes, Member.PUBLIC);
  1679         return getConstructor0(parameterTypes, Member.PUBLIC);
  1678     }
  1680     }
  1679 
  1681 
  1680 
  1682 
  1681     /**
  1683     /**
  1713      */
  1715      */
  1714     public Class<?>[] getDeclaredClasses() throws SecurityException {
  1716     public Class<?>[] getDeclaredClasses() throws SecurityException {
  1715         // be very careful not to change the stack depth of this
  1717         // be very careful not to change the stack depth of this
  1716         // checkMemberAccess call for security reasons
  1718         // checkMemberAccess call for security reasons
  1717         // see java.lang.SecurityManager.checkMemberAccess
  1719         // see java.lang.SecurityManager.checkMemberAccess
  1718         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1720         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), false);
  1719         return getDeclaredClasses0();
  1721         return getDeclaredClasses0();
  1720     }
  1722     }
  1721 
  1723 
  1722 
  1724 
  1723     /**
  1725     /**
  1757      */
  1759      */
  1758     public Field[] getDeclaredFields() throws SecurityException {
  1760     public Field[] getDeclaredFields() throws SecurityException {
  1759         // be very careful not to change the stack depth of this
  1761         // be very careful not to change the stack depth of this
  1760         // checkMemberAccess call for security reasons
  1762         // checkMemberAccess call for security reasons
  1761         // see java.lang.SecurityManager.checkMemberAccess
  1763         // see java.lang.SecurityManager.checkMemberAccess
  1762         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1764         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
  1763         return copyFields(privateGetDeclaredFields(false));
  1765         return copyFields(privateGetDeclaredFields(false));
  1764     }
  1766     }
  1765 
  1767 
  1766 
  1768 
  1767     /**
  1769     /**
  1805      */
  1807      */
  1806     public Method[] getDeclaredMethods() throws SecurityException {
  1808     public Method[] getDeclaredMethods() throws SecurityException {
  1807         // be very careful not to change the stack depth of this
  1809         // be very careful not to change the stack depth of this
  1808         // checkMemberAccess call for security reasons
  1810         // checkMemberAccess call for security reasons
  1809         // see java.lang.SecurityManager.checkMemberAccess
  1811         // see java.lang.SecurityManager.checkMemberAccess
  1810         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1812         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
  1811         return copyMethods(privateGetDeclaredMethods(false));
  1813         return copyMethods(privateGetDeclaredMethods(false));
  1812     }
  1814     }
  1813 
  1815 
  1814 
  1816 
  1815     /**
  1817     /**
  1850      */
  1852      */
  1851     public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
  1853     public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
  1852         // be very careful not to change the stack depth of this
  1854         // be very careful not to change the stack depth of this
  1853         // checkMemberAccess call for security reasons
  1855         // checkMemberAccess call for security reasons
  1854         // see java.lang.SecurityManager.checkMemberAccess
  1856         // see java.lang.SecurityManager.checkMemberAccess
  1855         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1857         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
  1856         return copyConstructors(privateGetDeclaredConstructors(false));
  1858         return copyConstructors(privateGetDeclaredConstructors(false));
  1857     }
  1859     }
  1858 
  1860 
  1859 
  1861 
  1860     /**
  1862     /**
  1894     public Field getDeclaredField(String name)
  1896     public Field getDeclaredField(String name)
  1895         throws NoSuchFieldException, SecurityException {
  1897         throws NoSuchFieldException, SecurityException {
  1896         // be very careful not to change the stack depth of this
  1898         // be very careful not to change the stack depth of this
  1897         // checkMemberAccess call for security reasons
  1899         // checkMemberAccess call for security reasons
  1898         // see java.lang.SecurityManager.checkMemberAccess
  1900         // see java.lang.SecurityManager.checkMemberAccess
  1899         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1901         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
  1900         Field field = searchFields(privateGetDeclaredFields(false), name);
  1902         Field field = searchFields(privateGetDeclaredFields(false), name);
  1901         if (field == null) {
  1903         if (field == null) {
  1902             throw new NoSuchFieldException(name);
  1904             throw new NoSuchFieldException(name);
  1903         }
  1905         }
  1904         return field;
  1906         return field;
  1949     public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
  1951     public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
  1950         throws NoSuchMethodException, SecurityException {
  1952         throws NoSuchMethodException, SecurityException {
  1951         // be very careful not to change the stack depth of this
  1953         // be very careful not to change the stack depth of this
  1952         // checkMemberAccess call for security reasons
  1954         // checkMemberAccess call for security reasons
  1953         // see java.lang.SecurityManager.checkMemberAccess
  1955         // see java.lang.SecurityManager.checkMemberAccess
  1954         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1956         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
  1955         Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
  1957         Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
  1956         if (method == null) {
  1958         if (method == null) {
  1957             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
  1959             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
  1958         }
  1960         }
  1959         return method;
  1961         return method;
  1999     public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
  2001     public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
  2000         throws NoSuchMethodException, SecurityException {
  2002         throws NoSuchMethodException, SecurityException {
  2001         // be very careful not to change the stack depth of this
  2003         // be very careful not to change the stack depth of this
  2002         // checkMemberAccess call for security reasons
  2004         // checkMemberAccess call for security reasons
  2003         // see java.lang.SecurityManager.checkMemberAccess
  2005         // see java.lang.SecurityManager.checkMemberAccess
  2004         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  2006         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
  2005         return getConstructor0(parameterTypes, Member.DECLARED);
  2007         return getConstructor0(parameterTypes, Member.DECLARED);
  2006     }
  2008     }
  2007 
  2009 
  2008     /**
  2010     /**
  2009      * Finds a resource with a given name.  The rules for searching resources
  2011      * Finds a resource with a given name.  The rules for searching resources
  2169      * See java.lang.SecurityManager.checkMemberAccess.
  2171      * See java.lang.SecurityManager.checkMemberAccess.
  2170      *
  2172      *
  2171      * <p> Default policy: allow all clients access with normal Java access
  2173      * <p> Default policy: allow all clients access with normal Java access
  2172      * control.
  2174      * control.
  2173      */
  2175      */
  2174     private void checkMemberAccess(int which, ClassLoader ccl) {
  2176     private void checkMemberAccess(int which, ClassLoader ccl, boolean checkProxyInterfaces) {
  2175         SecurityManager s = System.getSecurityManager();
  2177         SecurityManager s = System.getSecurityManager();
  2176         if (s != null) {
  2178         if (s != null) {
  2177             s.checkMemberAccess(this, which);
  2179             s.checkMemberAccess(this, which);
  2178             ClassLoader cl = getClassLoader0();
  2180             ClassLoader cl = getClassLoader0();
  2179             if (sun.reflect.misc.ReflectUtil.needsPackageAccessCheck(ccl, cl)) {
  2181             if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) {
  2180                 String name = this.getName();
  2182                 String name = this.getName();
  2181                 int i = name.lastIndexOf('.');
  2183                 int i = name.lastIndexOf('.');
  2182                 if (i != -1) {
  2184                 if (i != -1) {
  2183                     s.checkPackageAccess(name.substring(0, i));
  2185                     // skip the package access check on a proxy class in default proxy package
       
  2186                     String pkg = name.substring(0, i);
       
  2187                     if (!Proxy.isProxyClass(this) || !pkg.equals(ReflectUtil.PROXY_PACKAGE)) {
       
  2188                         s.checkPackageAccess(pkg);
       
  2189                     }
  2184                 }
  2190                 }
       
  2191             }
       
  2192             // check package access on the proxy interfaces
       
  2193             if (checkProxyInterfaces && Proxy.isProxyClass(this)) {
       
  2194                 ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces());
  2185             }
  2195             }
  2186         }
  2196         }
  2187     }
  2197     }
  2188 
  2198 
  2189     /**
  2199     /**