jdk/src/share/classes/java/lang/Class.java

Print this page
rev 5671 : 7197546: (proxy) Reflect about creating reflective proxies
Reviewed-by: alanb, jdn, jrose


  48 import java.util.Iterator;
  49 import java.util.List;
  50 import java.util.LinkedList;
  51 import java.util.LinkedHashSet;
  52 import java.util.Set;
  53 import java.util.Map;
  54 import java.util.HashMap;
  55 import sun.misc.Unsafe;
  56 import sun.reflect.ConstantPool;
  57 import sun.reflect.Reflection;
  58 import sun.reflect.ReflectionFactory;
  59 import sun.reflect.SignatureIterator;
  60 import sun.reflect.generics.factory.CoreReflectionFactory;
  61 import sun.reflect.generics.factory.GenericsFactory;
  62 import sun.reflect.generics.repository.ClassRepository;
  63 import sun.reflect.generics.repository.MethodRepository;
  64 import sun.reflect.generics.repository.ConstructorRepository;
  65 import sun.reflect.generics.scope.ClassScope;
  66 import sun.security.util.SecurityConstants;
  67 import java.lang.annotation.Annotation;

  68 import sun.reflect.annotation.*;

  69 
  70 /**
  71  * Instances of the class {@code Class} represent classes and
  72  * interfaces in a running Java application.  An enum is a kind of
  73  * class and an annotation is a kind of interface.  Every array also
  74  * belongs to a class that is reflected as a {@code Class} object
  75  * that is shared by all arrays with the same element type and number
  76  * of dimensions.  The primitive Java types ({@code boolean},
  77  * {@code byte}, {@code char}, {@code short},
  78  * {@code int}, {@code long}, {@code float}, and
  79  * {@code double}), and the keyword {@code void} are also
  80  * represented as {@code Class} objects.
  81  *
  82  * <p> {@code Class} has no public constructor. Instead {@code Class}
  83  * objects are constructed automatically by the Java Virtual Machine as classes
  84  * are loaded and by calls to the {@code defineClass} method in the class
  85  * loader.
  86  *
  87  * <p> The following example uses a {@code Class} object to print the
  88  * class name of an object:


 303      *             <ul>
 304      *
 305      *             <li> invocation of
 306      *             {@link SecurityManager#checkMemberAccess
 307      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
 308      *             creation of new instances of this class
 309      *
 310      *             <li> the caller's class loader is not the same as or an
 311      *             ancestor of the class loader for the current class and
 312      *             invocation of {@link SecurityManager#checkPackageAccess
 313      *             s.checkPackageAccess()} denies access to the package
 314      *             of this class
 315      *
 316      *             </ul>
 317      *
 318      */
 319     public T newInstance()
 320         throws InstantiationException, IllegalAccessException
 321     {
 322         if (System.getSecurityManager() != null) {
 323             checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
 324         }
 325         return newInstance0();
 326     }
 327 
 328     private T newInstance0()
 329         throws InstantiationException, IllegalAccessException
 330     {
 331         // NOTE: the following code may not be strictly correct under
 332         // the current Java memory model.
 333 
 334         // Constructor lookup
 335         if (cachedConstructor == null) {
 336             if (this == Class.class) {
 337                 throw new IllegalAccessException(
 338                     "Can not call newInstance() on the Class for java.lang.Class"
 339                 );
 340             }
 341             try {
 342                 Class<?>[] empty = {};
 343                 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);


1280      *
1281      *             <li> invocation of
1282      *             {@link SecurityManager#checkMemberAccess
1283      *             s.checkMemberAccess(this, Member.PUBLIC)} method
1284      *             denies access to the classes within this class
1285      *
1286      *             <li> the caller's class loader is not the same as or an
1287      *             ancestor of the class loader for the current class and
1288      *             invocation of {@link SecurityManager#checkPackageAccess
1289      *             s.checkPackageAccess()} denies access to the package
1290      *             of this class
1291      *
1292      *             </ul>
1293      *
1294      * @since JDK1.1
1295      */
1296     public Class<?>[] getClasses() {
1297         // be very careful not to change the stack depth of this
1298         // checkMemberAccess call for security reasons
1299         // see java.lang.SecurityManager.checkMemberAccess
1300         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1301 
1302         // Privileged so this implementation can look at DECLARED classes,
1303         // something the caller might not have privilege to do.  The code here
1304         // is allowed to look at DECLARED classes because (1) it does not hand
1305         // out anything other than public members and (2) public member access
1306         // has already been ok'd by the SecurityManager.
1307 
1308         return java.security.AccessController.doPrivileged(
1309             new java.security.PrivilegedAction<Class<?>[]>() {
1310                 public Class[] run() {
1311                     List<Class<?>> list = new ArrayList<>();
1312                     Class<?> currentClass = Class.this;
1313                     while (currentClass != null) {
1314                         Class<?>[] members = currentClass.getDeclaredClasses();
1315                         for (int i = 0; i < members.length; i++) {
1316                             if (Modifier.isPublic(members[i].getModifiers())) {
1317                                 list.add(members[i]);
1318                             }
1319                         }
1320                         currentClass = currentClass.getSuperclass();


1355      *
1356      *             <li> invocation of
1357      *             {@link SecurityManager#checkMemberAccess
1358      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1359      *             access to the fields within this class
1360      *
1361      *             <li> the caller's class loader is not the same as or an
1362      *             ancestor of the class loader for the current class and
1363      *             invocation of {@link SecurityManager#checkPackageAccess
1364      *             s.checkPackageAccess()} denies access to the package
1365      *             of this class
1366      *
1367      *             </ul>
1368      *
1369      * @since JDK1.1
1370      */
1371     public Field[] getFields() throws SecurityException {
1372         // be very careful not to change the stack depth of this
1373         // checkMemberAccess call for security reasons
1374         // see java.lang.SecurityManager.checkMemberAccess
1375         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1376         return copyFields(privateGetPublicFields(null));
1377     }
1378 
1379 
1380     /**
1381      * Returns an array containing {@code Method} objects reflecting all
1382      * the public <em>member</em> methods of the class or interface represented
1383      * by this {@code Class} object, including those declared by the class
1384      * or interface and those inherited from superclasses and
1385      * superinterfaces.  Array classes return all the (public) member methods
1386      * inherited from the {@code Object} class.  The elements in the array
1387      * returned are not sorted and are not in any particular order.  This
1388      * method returns an array of length 0 if this {@code Class} object
1389      * represents a class or interface that has no public member methods, or if
1390      * this {@code Class} object represents a primitive type or void.
1391      *
1392      * <p> The class initialization method {@code <clinit>} is not
1393      * included in the returned array. If the class declares multiple public
1394      * member methods with the same parameter types, they are all included in
1395      * the returned array.


1406      *
1407      *             <li> invocation of
1408      *             {@link SecurityManager#checkMemberAccess
1409      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1410      *             access to the methods within this class
1411      *
1412      *             <li> the caller's class loader is not the same as or an
1413      *             ancestor of the class loader for the current class and
1414      *             invocation of {@link SecurityManager#checkPackageAccess
1415      *             s.checkPackageAccess()} denies access to the package
1416      *             of this class
1417      *
1418      *             </ul>
1419      *
1420      * @since JDK1.1
1421      */
1422     public Method[] getMethods() throws SecurityException {
1423         // be very careful not to change the stack depth of this
1424         // checkMemberAccess call for security reasons
1425         // see java.lang.SecurityManager.checkMemberAccess
1426         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1427         return copyMethods(privateGetPublicMethods());
1428     }
1429 
1430 
1431     /**
1432      * Returns an array containing {@code Constructor} objects reflecting
1433      * all the public constructors of the class represented by this
1434      * {@code Class} object.  An array of length 0 is returned if the
1435      * class has no public constructors, or if the class is an array class, or
1436      * if the class reflects a primitive type or void.
1437      *
1438      * Note that while this method returns an array of {@code
1439      * Constructor<T>} objects (that is an array of constructors from
1440      * this class), the return type of this method is {@code
1441      * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1442      * might be expected.  This less informative return type is
1443      * necessary since after being returned from this method, the
1444      * array could be modified to hold {@code Constructor} objects for
1445      * different classes, which would violate the type guarantees of
1446      * {@code Constructor<T>[]}.


1455      *
1456      *             <li> invocation of
1457      *             {@link SecurityManager#checkMemberAccess
1458      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1459      *             access to the constructors within this class
1460      *
1461      *             <li> the caller's class loader is not the same as or an
1462      *             ancestor of the class loader for the current class and
1463      *             invocation of {@link SecurityManager#checkPackageAccess
1464      *             s.checkPackageAccess()} denies access to the package
1465      *             of this class
1466      *
1467      *             </ul>
1468      *
1469      * @since JDK1.1
1470      */
1471     public Constructor<?>[] getConstructors() throws SecurityException {
1472         // be very careful not to change the stack depth of this
1473         // checkMemberAccess call for security reasons
1474         // see java.lang.SecurityManager.checkMemberAccess
1475         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1476         return copyConstructors(privateGetDeclaredConstructors(true));
1477     }
1478 
1479 
1480     /**
1481      * Returns a {@code Field} object that reflects the specified public
1482      * member field of the class or interface represented by this
1483      * {@code Class} object. The {@code name} parameter is a
1484      * {@code String} specifying the simple name of the desired field.
1485      *
1486      * <p> The field to be reflected is determined by the algorithm that
1487      * follows.  Let C be the class represented by this object:
1488      * <OL>
1489      * <LI> If C declares a public field with the name specified, that is the
1490      *      field to be reflected.</LI>
1491      * <LI> If no field was found in step 1 above, this algorithm is applied
1492      *      recursively to each direct superinterface of C. The direct
1493      *      superinterfaces are searched in the order they were declared.</LI>
1494      * <LI> If no field was found in steps 1 and 2 above, and C has a
1495      *      superclass S, then this algorithm is invoked recursively upon S.


1514      *             <li> invocation of
1515      *             {@link SecurityManager#checkMemberAccess
1516      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1517      *             access to the field
1518      *
1519      *             <li> the caller's class loader is not the same as or an
1520      *             ancestor of the class loader for the current class and
1521      *             invocation of {@link SecurityManager#checkPackageAccess
1522      *             s.checkPackageAccess()} denies access to the package
1523      *             of this class
1524      *
1525      *             </ul>
1526      *
1527      * @since JDK1.1
1528      */
1529     public Field getField(String name)
1530         throws NoSuchFieldException, SecurityException {
1531         // be very careful not to change the stack depth of this
1532         // checkMemberAccess call for security reasons
1533         // see java.lang.SecurityManager.checkMemberAccess
1534         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1535         Field field = getField0(name);
1536         if (field == null) {
1537             throw new NoSuchFieldException(name);
1538         }
1539         return field;
1540     }
1541 
1542 
1543     /**
1544      * Returns a {@code Method} object that reflects the specified public
1545      * member method of the class or interface represented by this
1546      * {@code Class} object. The {@code name} parameter is a
1547      * {@code String} specifying the simple name of the desired method. The
1548      * {@code parameterTypes} parameter is an array of {@code Class}
1549      * objects that identify the method's formal parameter types, in declared
1550      * order. If {@code parameterTypes} is {@code null}, it is
1551      * treated as if it were an empty array.
1552      *
1553      * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
1554      * {@code NoSuchMethodException} is raised. Otherwise, the method to


1599      *             <li> invocation of
1600      *             {@link SecurityManager#checkMemberAccess
1601      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1602      *             access to the method
1603      *
1604      *             <li> the caller's class loader is not the same as or an
1605      *             ancestor of the class loader for the current class and
1606      *             invocation of {@link SecurityManager#checkPackageAccess
1607      *             s.checkPackageAccess()} denies access to the package
1608      *             of this class
1609      *
1610      *             </ul>
1611      *
1612      * @since JDK1.1
1613      */
1614     public Method getMethod(String name, Class<?>... parameterTypes)
1615         throws NoSuchMethodException, SecurityException {
1616         // be very careful not to change the stack depth of this
1617         // checkMemberAccess call for security reasons
1618         // see java.lang.SecurityManager.checkMemberAccess
1619         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1620         Method method = getMethod0(name, parameterTypes);
1621         if (method == null) {
1622             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1623         }
1624         return method;
1625     }
1626 
1627 
1628     /**
1629      * Returns a {@code Constructor} object that reflects the specified
1630      * public constructor of the class represented by this {@code Class}
1631      * object. The {@code parameterTypes} parameter is an array of
1632      * {@code Class} objects that identify the constructor's formal
1633      * parameter types, in declared order.
1634      *
1635      * If this {@code Class} object represents an inner class
1636      * declared in a non-static context, the formal parameter types
1637      * include the explicit enclosing instance as the first parameter.
1638      *
1639      * <p> The constructor to reflect is the public constructor of the class


1653      *             <li> invocation of
1654      *             {@link SecurityManager#checkMemberAccess
1655      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1656      *             access to the constructor
1657      *
1658      *             <li> the caller's class loader is not the same as or an
1659      *             ancestor of the class loader for the current class and
1660      *             invocation of {@link SecurityManager#checkPackageAccess
1661      *             s.checkPackageAccess()} denies access to the package
1662      *             of this class
1663      *
1664      *             </ul>
1665      *
1666      * @since JDK1.1
1667      */
1668     public Constructor<T> getConstructor(Class<?>... parameterTypes)
1669         throws NoSuchMethodException, SecurityException {
1670         // be very careful not to change the stack depth of this
1671         // checkMemberAccess call for security reasons
1672         // see java.lang.SecurityManager.checkMemberAccess
1673         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1674         return getConstructor0(parameterTypes, Member.PUBLIC);
1675     }
1676 
1677 
1678     /**
1679      * Returns an array of {@code Class} objects reflecting all the
1680      * classes and interfaces declared as members of the class represented by
1681      * this {@code Class} object. This includes public, protected, default
1682      * (package) access, and private classes and interfaces declared by the
1683      * class, but excludes inherited classes and interfaces.  This method
1684      * returns an array of length 0 if the class declares no classes or
1685      * interfaces as members, or if this {@code Class} object represents a
1686      * primitive type, an array class, or void.
1687      *
1688      * @return the array of {@code Class} objects representing all the
1689      * declared members of this class
1690      * @exception  SecurityException
1691      *             If a security manager, <i>s</i>, is present and any of the
1692      *             following conditions is met:
1693      *


1695      *
1696      *             <li> invocation of
1697      *             {@link SecurityManager#checkMemberAccess
1698      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1699      *             access to the declared classes within this class
1700      *
1701      *             <li> the caller's class loader is not the same as or an
1702      *             ancestor of the class loader for the current class and
1703      *             invocation of {@link SecurityManager#checkPackageAccess
1704      *             s.checkPackageAccess()} denies access to the package
1705      *             of this class
1706      *
1707      *             </ul>
1708      *
1709      * @since JDK1.1
1710      */
1711     public Class<?>[] getDeclaredClasses() throws SecurityException {
1712         // be very careful not to change the stack depth of this
1713         // checkMemberAccess call for security reasons
1714         // see java.lang.SecurityManager.checkMemberAccess
1715         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
1716         return getDeclaredClasses0();
1717     }
1718 
1719 
1720     /**
1721      * Returns an array of {@code Field} objects reflecting all the fields
1722      * declared by the class or interface represented by this
1723      * {@code Class} object. This includes public, protected, default
1724      * (package) access, and private fields, but excludes inherited fields.
1725      * The elements in the array returned are not sorted and are not in any
1726      * particular order.  This method returns an array of length 0 if the class
1727      * or interface declares no fields, or if this {@code Class} object
1728      * represents a primitive type, an array class, or void.
1729      *
1730      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1731      *
1732      * @return    the array of {@code Field} objects representing all the
1733      * declared fields of this class
1734      * @exception  SecurityException
1735      *             If a security manager, <i>s</i>, is present and any of the


1739      *
1740      *             <li> invocation of
1741      *             {@link SecurityManager#checkMemberAccess
1742      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1743      *             access to the declared fields within this class
1744      *
1745      *             <li> the caller's class loader is not the same as or an
1746      *             ancestor of the class loader for the current class and
1747      *             invocation of {@link SecurityManager#checkPackageAccess
1748      *             s.checkPackageAccess()} denies access to the package
1749      *             of this class
1750      *
1751      *             </ul>
1752      *
1753      * @since JDK1.1
1754      */
1755     public Field[] getDeclaredFields() throws SecurityException {
1756         // be very careful not to change the stack depth of this
1757         // checkMemberAccess call for security reasons
1758         // see java.lang.SecurityManager.checkMemberAccess
1759         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
1760         return copyFields(privateGetDeclaredFields(false));
1761     }
1762 
1763 
1764     /**
1765      * Returns an array of {@code Method} objects reflecting all the
1766      * methods declared by the class or interface represented by this
1767      * {@code Class} object. This includes public, protected, default
1768      * (package) access, and private methods, but excludes inherited methods.
1769      * The elements in the array returned are not sorted and are not in any
1770      * particular order.  This method returns an array of length 0 if the class
1771      * or interface declares no methods, or if this {@code Class} object
1772      * represents a primitive type, an array class, or void.  The class
1773      * initialization method {@code <clinit>} is not included in the
1774      * returned array. If the class declares multiple public member methods
1775      * with the same parameter types, they are all included in the returned
1776      * array.
1777      *
1778      * <p> See <em>The Java Language Specification</em>, section 8.2.
1779      *


1787      *
1788      *             <li> invocation of
1789      *             {@link SecurityManager#checkMemberAccess
1790      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1791      *             access to the declared methods within this class
1792      *
1793      *             <li> the caller's class loader is not the same as or an
1794      *             ancestor of the class loader for the current class and
1795      *             invocation of {@link SecurityManager#checkPackageAccess
1796      *             s.checkPackageAccess()} denies access to the package
1797      *             of this class
1798      *
1799      *             </ul>
1800      *
1801      * @since JDK1.1
1802      */
1803     public Method[] getDeclaredMethods() throws SecurityException {
1804         // be very careful not to change the stack depth of this
1805         // checkMemberAccess call for security reasons
1806         // see java.lang.SecurityManager.checkMemberAccess
1807         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
1808         return copyMethods(privateGetDeclaredMethods(false));
1809     }
1810 
1811 
1812     /**
1813      * Returns an array of {@code Constructor} objects reflecting all the
1814      * constructors declared by the class represented by this
1815      * {@code Class} object. These are public, protected, default
1816      * (package) access, and private constructors.  The elements in the array
1817      * returned are not sorted and are not in any particular order.  If the
1818      * class has a default constructor, it is included in the returned array.
1819      * This method returns an array of length 0 if this {@code Class}
1820      * object represents an interface, a primitive type, an array class, or
1821      * void.
1822      *
1823      * <p> See <em>The Java Language Specification</em>, section 8.2.
1824      *
1825      * @return    the array of {@code Constructor} objects representing all the
1826      * declared constructors of this class
1827      * @exception  SecurityException


1832      *
1833      *             <li> invocation of
1834      *             {@link SecurityManager#checkMemberAccess
1835      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1836      *             access to the declared constructors within this class
1837      *
1838      *             <li> the caller's class loader is not the same as or an
1839      *             ancestor of the class loader for the current class and
1840      *             invocation of {@link SecurityManager#checkPackageAccess
1841      *             s.checkPackageAccess()} denies access to the package
1842      *             of this class
1843      *
1844      *             </ul>
1845      *
1846      * @since JDK1.1
1847      */
1848     public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1849         // be very careful not to change the stack depth of this
1850         // checkMemberAccess call for security reasons
1851         // see java.lang.SecurityManager.checkMemberAccess
1852         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
1853         return copyConstructors(privateGetDeclaredConstructors(false));
1854     }
1855 
1856 
1857     /**
1858      * Returns a {@code Field} object that reflects the specified declared
1859      * field of the class or interface represented by this {@code Class}
1860      * object. The {@code name} parameter is a {@code String} that
1861      * specifies the simple name of the desired field.  Note that this method
1862      * will not reflect the {@code length} field of an array class.
1863      *
1864      * @param name the name of the field
1865      * @return the {@code Field} object for the specified field in this
1866      * class
1867      * @exception NoSuchFieldException if a field with the specified name is
1868      *              not found.
1869      * @exception NullPointerException if {@code name} is {@code null}
1870      * @exception  SecurityException
1871      *             If a security manager, <i>s</i>, is present and any of the
1872      *             following conditions is met:


1876      *             <li> invocation of
1877      *             {@link SecurityManager#checkMemberAccess
1878      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1879      *             access to the declared field
1880      *
1881      *             <li> the caller's class loader is not the same as or an
1882      *             ancestor of the class loader for the current class and
1883      *             invocation of {@link SecurityManager#checkPackageAccess
1884      *             s.checkPackageAccess()} denies access to the package
1885      *             of this class
1886      *
1887      *             </ul>
1888      *
1889      * @since JDK1.1
1890      */
1891     public Field getDeclaredField(String name)
1892         throws NoSuchFieldException, SecurityException {
1893         // be very careful not to change the stack depth of this
1894         // checkMemberAccess call for security reasons
1895         // see java.lang.SecurityManager.checkMemberAccess
1896         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
1897         Field field = searchFields(privateGetDeclaredFields(false), name);
1898         if (field == null) {
1899             throw new NoSuchFieldException(name);
1900         }
1901         return field;
1902     }
1903 
1904 
1905     /**
1906      * Returns a {@code Method} object that reflects the specified
1907      * declared method of the class or interface represented by this
1908      * {@code Class} object. The {@code name} parameter is a
1909      * {@code String} that specifies the simple name of the desired
1910      * method, and the {@code parameterTypes} parameter is an array of
1911      * {@code Class} objects that identify the method's formal parameter
1912      * types, in declared order.  If more than one method with the same
1913      * parameter types is declared in a class, and one of these methods has a
1914      * return type that is more specific than any of the others, that method is
1915      * returned; otherwise one of the methods is chosen arbitrarily.  If the
1916      * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}


1931      *             <li> invocation of
1932      *             {@link SecurityManager#checkMemberAccess
1933      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1934      *             access to the declared method
1935      *
1936      *             <li> the caller's class loader is not the same as or an
1937      *             ancestor of the class loader for the current class and
1938      *             invocation of {@link SecurityManager#checkPackageAccess
1939      *             s.checkPackageAccess()} denies access to the package
1940      *             of this class
1941      *
1942      *             </ul>
1943      *
1944      * @since JDK1.1
1945      */
1946     public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1947         throws NoSuchMethodException, SecurityException {
1948         // be very careful not to change the stack depth of this
1949         // checkMemberAccess call for security reasons
1950         // see java.lang.SecurityManager.checkMemberAccess
1951         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
1952         Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
1953         if (method == null) {
1954             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1955         }
1956         return method;
1957     }
1958 
1959 
1960     /**
1961      * Returns a {@code Constructor} object that reflects the specified
1962      * constructor of the class or interface represented by this
1963      * {@code Class} object.  The {@code parameterTypes} parameter is
1964      * an array of {@code Class} objects that identify the constructor's
1965      * formal parameter types, in declared order.
1966      *
1967      * If this {@code Class} object represents an inner class
1968      * declared in a non-static context, the formal parameter types
1969      * include the explicit enclosing instance as the first parameter.
1970      *
1971      * @param parameterTypes the parameter array


1981      *             <li> invocation of
1982      *             {@link SecurityManager#checkMemberAccess
1983      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1984      *             access to the declared constructor
1985      *
1986      *             <li> the caller's class loader is not the same as or an
1987      *             ancestor of the class loader for the current class and
1988      *             invocation of {@link SecurityManager#checkPackageAccess
1989      *             s.checkPackageAccess()} denies access to the package
1990      *             of this class
1991      *
1992      *             </ul>
1993      *
1994      * @since JDK1.1
1995      */
1996     public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1997         throws NoSuchMethodException, SecurityException {
1998         // be very careful not to change the stack depth of this
1999         // checkMemberAccess call for security reasons
2000         // see java.lang.SecurityManager.checkMemberAccess
2001         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
2002         return getConstructor0(parameterTypes, Member.DECLARED);
2003     }
2004 
2005     /**
2006      * Finds a resource with a given name.  The rules for searching resources
2007      * associated with a given class are implemented by the defining
2008      * {@linkplain ClassLoader class loader} of the class.  This method
2009      * delegates to this object's class loader.  If this object was loaded by
2010      * the bootstrap class loader, the method delegates to {@link
2011      * ClassLoader#getSystemResourceAsStream}.
2012      *
2013      * <p> Before delegation, an absolute resource name is constructed from the
2014      * given resource name using this algorithm:
2015      *
2016      * <ul>
2017      *
2018      * <li> If the {@code name} begins with a {@code '/'}
2019      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
2020      * portion of the {@code name} following the {@code '/'}.
2021      *


2151 
2152 
2153     /*
2154      * Return the Virtual Machine's Class object for the named
2155      * primitive type.
2156      */
2157     static native Class getPrimitiveClass(String name);
2158 
2159 
2160     /*
2161      * Check if client is allowed to access members.  If access is denied,
2162      * throw a SecurityException.
2163      *
2164      * Be very careful not to change the stack depth of this checkMemberAccess
2165      * call for security reasons.
2166      * See java.lang.SecurityManager.checkMemberAccess.
2167      *
2168      * <p> Default policy: allow all clients access with normal Java access
2169      * control.
2170      */
2171     private void checkMemberAccess(int which, ClassLoader ccl) {
2172         SecurityManager s = System.getSecurityManager();
2173         if (s != null) {
2174             s.checkMemberAccess(this, which);
2175             ClassLoader cl = getClassLoader0();
2176             if ((ccl != null) && (ccl != cl) &&
2177                   ((cl == null) || !cl.isAncestor(ccl))) {
2178                 String name = this.getName();
2179                 int i = name.lastIndexOf('.');
2180                 if (i != -1) {
2181                     s.checkPackageAccess(name.substring(0, i));



2182                 }





2183             }
2184         }
2185     }
2186 
2187     /**
2188      * Add a package name prefix if the name is not absolute Remove leading "/"
2189      * if name is absolute
2190      */
2191     private String resolveName(String name) {
2192         if (name == null) {
2193             return name;
2194         }
2195         if (!name.startsWith("/")) {
2196             Class<?> c = this;
2197             while (c.isArray()) {
2198                 c = c.getComponentType();
2199             }
2200             String baseName = c.getName();
2201             int index = baseName.lastIndexOf('.');
2202             if (index != -1) {




  48 import java.util.Iterator;
  49 import java.util.List;
  50 import java.util.LinkedList;
  51 import java.util.LinkedHashSet;
  52 import java.util.Set;
  53 import java.util.Map;
  54 import java.util.HashMap;
  55 import sun.misc.Unsafe;
  56 import sun.reflect.ConstantPool;
  57 import sun.reflect.Reflection;
  58 import sun.reflect.ReflectionFactory;
  59 import sun.reflect.SignatureIterator;
  60 import sun.reflect.generics.factory.CoreReflectionFactory;
  61 import sun.reflect.generics.factory.GenericsFactory;
  62 import sun.reflect.generics.repository.ClassRepository;
  63 import sun.reflect.generics.repository.MethodRepository;
  64 import sun.reflect.generics.repository.ConstructorRepository;
  65 import sun.reflect.generics.scope.ClassScope;
  66 import sun.security.util.SecurityConstants;
  67 import java.lang.annotation.Annotation;
  68 import java.lang.reflect.Proxy;
  69 import sun.reflect.annotation.*;
  70 import sun.reflect.misc.ReflectUtil;
  71 
  72 /**
  73  * Instances of the class {@code Class} represent classes and
  74  * interfaces in a running Java application.  An enum is a kind of
  75  * class and an annotation is a kind of interface.  Every array also
  76  * belongs to a class that is reflected as a {@code Class} object
  77  * that is shared by all arrays with the same element type and number
  78  * of dimensions.  The primitive Java types ({@code boolean},
  79  * {@code byte}, {@code char}, {@code short},
  80  * {@code int}, {@code long}, {@code float}, and
  81  * {@code double}), and the keyword {@code void} are also
  82  * represented as {@code Class} objects.
  83  *
  84  * <p> {@code Class} has no public constructor. Instead {@code Class}
  85  * objects are constructed automatically by the Java Virtual Machine as classes
  86  * are loaded and by calls to the {@code defineClass} method in the class
  87  * loader.
  88  *
  89  * <p> The following example uses a {@code Class} object to print the
  90  * class name of an object:


 305      *             <ul>
 306      *
 307      *             <li> invocation of
 308      *             {@link SecurityManager#checkMemberAccess
 309      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
 310      *             creation of new instances of this class
 311      *
 312      *             <li> the caller's class loader is not the same as or an
 313      *             ancestor of the class loader for the current class and
 314      *             invocation of {@link SecurityManager#checkPackageAccess
 315      *             s.checkPackageAccess()} denies access to the package
 316      *             of this class
 317      *
 318      *             </ul>
 319      *
 320      */
 321     public T newInstance()
 322         throws InstantiationException, IllegalAccessException
 323     {
 324         if (System.getSecurityManager() != null) {
 325             checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false);
 326         }
 327         return newInstance0();
 328     }
 329 
 330     private T newInstance0()
 331         throws InstantiationException, IllegalAccessException
 332     {
 333         // NOTE: the following code may not be strictly correct under
 334         // the current Java memory model.
 335 
 336         // Constructor lookup
 337         if (cachedConstructor == null) {
 338             if (this == Class.class) {
 339                 throw new IllegalAccessException(
 340                     "Can not call newInstance() on the Class for java.lang.Class"
 341                 );
 342             }
 343             try {
 344                 Class<?>[] empty = {};
 345                 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);


1282      *
1283      *             <li> invocation of
1284      *             {@link SecurityManager#checkMemberAccess
1285      *             s.checkMemberAccess(this, Member.PUBLIC)} method
1286      *             denies access to the classes within this class
1287      *
1288      *             <li> the caller's class loader is not the same as or an
1289      *             ancestor of the class loader for the current class and
1290      *             invocation of {@link SecurityManager#checkPackageAccess
1291      *             s.checkPackageAccess()} denies access to the package
1292      *             of this class
1293      *
1294      *             </ul>
1295      *
1296      * @since JDK1.1
1297      */
1298     public Class<?>[] getClasses() {
1299         // be very careful not to change the stack depth of this
1300         // checkMemberAccess call for security reasons
1301         // see java.lang.SecurityManager.checkMemberAccess
1302         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false);
1303 
1304         // Privileged so this implementation can look at DECLARED classes,
1305         // something the caller might not have privilege to do.  The code here
1306         // is allowed to look at DECLARED classes because (1) it does not hand
1307         // out anything other than public members and (2) public member access
1308         // has already been ok'd by the SecurityManager.
1309 
1310         return java.security.AccessController.doPrivileged(
1311             new java.security.PrivilegedAction<Class<?>[]>() {
1312                 public Class[] run() {
1313                     List<Class<?>> list = new ArrayList<>();
1314                     Class<?> currentClass = Class.this;
1315                     while (currentClass != null) {
1316                         Class<?>[] members = currentClass.getDeclaredClasses();
1317                         for (int i = 0; i < members.length; i++) {
1318                             if (Modifier.isPublic(members[i].getModifiers())) {
1319                                 list.add(members[i]);
1320                             }
1321                         }
1322                         currentClass = currentClass.getSuperclass();


1357      *
1358      *             <li> invocation of
1359      *             {@link SecurityManager#checkMemberAccess
1360      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1361      *             access to the fields within this class
1362      *
1363      *             <li> the caller's class loader is not the same as or an
1364      *             ancestor of the class loader for the current class and
1365      *             invocation of {@link SecurityManager#checkPackageAccess
1366      *             s.checkPackageAccess()} denies access to the package
1367      *             of this class
1368      *
1369      *             </ul>
1370      *
1371      * @since JDK1.1
1372      */
1373     public Field[] getFields() throws SecurityException {
1374         // be very careful not to change the stack depth of this
1375         // checkMemberAccess call for security reasons
1376         // see java.lang.SecurityManager.checkMemberAccess
1377         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
1378         return copyFields(privateGetPublicFields(null));
1379     }
1380 
1381 
1382     /**
1383      * Returns an array containing {@code Method} objects reflecting all
1384      * the public <em>member</em> methods of the class or interface represented
1385      * by this {@code Class} object, including those declared by the class
1386      * or interface and those inherited from superclasses and
1387      * superinterfaces.  Array classes return all the (public) member methods
1388      * inherited from the {@code Object} class.  The elements in the array
1389      * returned are not sorted and are not in any particular order.  This
1390      * method returns an array of length 0 if this {@code Class} object
1391      * represents a class or interface that has no public member methods, or if
1392      * this {@code Class} object represents a primitive type or void.
1393      *
1394      * <p> The class initialization method {@code <clinit>} is not
1395      * included in the returned array. If the class declares multiple public
1396      * member methods with the same parameter types, they are all included in
1397      * the returned array.


1408      *
1409      *             <li> invocation of
1410      *             {@link SecurityManager#checkMemberAccess
1411      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1412      *             access to the methods within this class
1413      *
1414      *             <li> the caller's class loader is not the same as or an
1415      *             ancestor of the class loader for the current class and
1416      *             invocation of {@link SecurityManager#checkPackageAccess
1417      *             s.checkPackageAccess()} denies access to the package
1418      *             of this class
1419      *
1420      *             </ul>
1421      *
1422      * @since JDK1.1
1423      */
1424     public Method[] getMethods() throws SecurityException {
1425         // be very careful not to change the stack depth of this
1426         // checkMemberAccess call for security reasons
1427         // see java.lang.SecurityManager.checkMemberAccess
1428         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
1429         return copyMethods(privateGetPublicMethods());
1430     }
1431 
1432 
1433     /**
1434      * Returns an array containing {@code Constructor} objects reflecting
1435      * all the public constructors of the class represented by this
1436      * {@code Class} object.  An array of length 0 is returned if the
1437      * class has no public constructors, or if the class is an array class, or
1438      * if the class reflects a primitive type or void.
1439      *
1440      * Note that while this method returns an array of {@code
1441      * Constructor<T>} objects (that is an array of constructors from
1442      * this class), the return type of this method is {@code
1443      * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1444      * might be expected.  This less informative return type is
1445      * necessary since after being returned from this method, the
1446      * array could be modified to hold {@code Constructor} objects for
1447      * different classes, which would violate the type guarantees of
1448      * {@code Constructor<T>[]}.


1457      *
1458      *             <li> invocation of
1459      *             {@link SecurityManager#checkMemberAccess
1460      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1461      *             access to the constructors within this class
1462      *
1463      *             <li> the caller's class loader is not the same as or an
1464      *             ancestor of the class loader for the current class and
1465      *             invocation of {@link SecurityManager#checkPackageAccess
1466      *             s.checkPackageAccess()} denies access to the package
1467      *             of this class
1468      *
1469      *             </ul>
1470      *
1471      * @since JDK1.1
1472      */
1473     public Constructor<?>[] getConstructors() throws SecurityException {
1474         // be very careful not to change the stack depth of this
1475         // checkMemberAccess call for security reasons
1476         // see java.lang.SecurityManager.checkMemberAccess
1477         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
1478         return copyConstructors(privateGetDeclaredConstructors(true));
1479     }
1480 
1481 
1482     /**
1483      * Returns a {@code Field} object that reflects the specified public
1484      * member field of the class or interface represented by this
1485      * {@code Class} object. The {@code name} parameter is a
1486      * {@code String} specifying the simple name of the desired field.
1487      *
1488      * <p> The field to be reflected is determined by the algorithm that
1489      * follows.  Let C be the class represented by this object:
1490      * <OL>
1491      * <LI> If C declares a public field with the name specified, that is the
1492      *      field to be reflected.</LI>
1493      * <LI> If no field was found in step 1 above, this algorithm is applied
1494      *      recursively to each direct superinterface of C. The direct
1495      *      superinterfaces are searched in the order they were declared.</LI>
1496      * <LI> If no field was found in steps 1 and 2 above, and C has a
1497      *      superclass S, then this algorithm is invoked recursively upon S.


1516      *             <li> invocation of
1517      *             {@link SecurityManager#checkMemberAccess
1518      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1519      *             access to the field
1520      *
1521      *             <li> the caller's class loader is not the same as or an
1522      *             ancestor of the class loader for the current class and
1523      *             invocation of {@link SecurityManager#checkPackageAccess
1524      *             s.checkPackageAccess()} denies access to the package
1525      *             of this class
1526      *
1527      *             </ul>
1528      *
1529      * @since JDK1.1
1530      */
1531     public Field getField(String name)
1532         throws NoSuchFieldException, SecurityException {
1533         // be very careful not to change the stack depth of this
1534         // checkMemberAccess call for security reasons
1535         // see java.lang.SecurityManager.checkMemberAccess
1536         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
1537         Field field = getField0(name);
1538         if (field == null) {
1539             throw new NoSuchFieldException(name);
1540         }
1541         return field;
1542     }
1543 
1544 
1545     /**
1546      * Returns a {@code Method} object that reflects the specified public
1547      * member method of the class or interface represented by this
1548      * {@code Class} object. The {@code name} parameter is a
1549      * {@code String} specifying the simple name of the desired method. The
1550      * {@code parameterTypes} parameter is an array of {@code Class}
1551      * objects that identify the method's formal parameter types, in declared
1552      * order. If {@code parameterTypes} is {@code null}, it is
1553      * treated as if it were an empty array.
1554      *
1555      * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
1556      * {@code NoSuchMethodException} is raised. Otherwise, the method to


1601      *             <li> invocation of
1602      *             {@link SecurityManager#checkMemberAccess
1603      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1604      *             access to the method
1605      *
1606      *             <li> the caller's class loader is not the same as or an
1607      *             ancestor of the class loader for the current class and
1608      *             invocation of {@link SecurityManager#checkPackageAccess
1609      *             s.checkPackageAccess()} denies access to the package
1610      *             of this class
1611      *
1612      *             </ul>
1613      *
1614      * @since JDK1.1
1615      */
1616     public Method getMethod(String name, Class<?>... parameterTypes)
1617         throws NoSuchMethodException, SecurityException {
1618         // be very careful not to change the stack depth of this
1619         // checkMemberAccess call for security reasons
1620         // see java.lang.SecurityManager.checkMemberAccess
1621         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
1622         Method method = getMethod0(name, parameterTypes);
1623         if (method == null) {
1624             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1625         }
1626         return method;
1627     }
1628 
1629 
1630     /**
1631      * Returns a {@code Constructor} object that reflects the specified
1632      * public constructor of the class represented by this {@code Class}
1633      * object. The {@code parameterTypes} parameter is an array of
1634      * {@code Class} objects that identify the constructor's formal
1635      * parameter types, in declared order.
1636      *
1637      * If this {@code Class} object represents an inner class
1638      * declared in a non-static context, the formal parameter types
1639      * include the explicit enclosing instance as the first parameter.
1640      *
1641      * <p> The constructor to reflect is the public constructor of the class


1655      *             <li> invocation of
1656      *             {@link SecurityManager#checkMemberAccess
1657      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
1658      *             access to the constructor
1659      *
1660      *             <li> the caller's class loader is not the same as or an
1661      *             ancestor of the class loader for the current class and
1662      *             invocation of {@link SecurityManager#checkPackageAccess
1663      *             s.checkPackageAccess()} denies access to the package
1664      *             of this class
1665      *
1666      *             </ul>
1667      *
1668      * @since JDK1.1
1669      */
1670     public Constructor<T> getConstructor(Class<?>... parameterTypes)
1671         throws NoSuchMethodException, SecurityException {
1672         // be very careful not to change the stack depth of this
1673         // checkMemberAccess call for security reasons
1674         // see java.lang.SecurityManager.checkMemberAccess
1675         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true);
1676         return getConstructor0(parameterTypes, Member.PUBLIC);
1677     }
1678 
1679 
1680     /**
1681      * Returns an array of {@code Class} objects reflecting all the
1682      * classes and interfaces declared as members of the class represented by
1683      * this {@code Class} object. This includes public, protected, default
1684      * (package) access, and private classes and interfaces declared by the
1685      * class, but excludes inherited classes and interfaces.  This method
1686      * returns an array of length 0 if the class declares no classes or
1687      * interfaces as members, or if this {@code Class} object represents a
1688      * primitive type, an array class, or void.
1689      *
1690      * @return the array of {@code Class} objects representing all the
1691      * declared members of this class
1692      * @exception  SecurityException
1693      *             If a security manager, <i>s</i>, is present and any of the
1694      *             following conditions is met:
1695      *


1697      *
1698      *             <li> invocation of
1699      *             {@link SecurityManager#checkMemberAccess
1700      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1701      *             access to the declared classes within this class
1702      *
1703      *             <li> the caller's class loader is not the same as or an
1704      *             ancestor of the class loader for the current class and
1705      *             invocation of {@link SecurityManager#checkPackageAccess
1706      *             s.checkPackageAccess()} denies access to the package
1707      *             of this class
1708      *
1709      *             </ul>
1710      *
1711      * @since JDK1.1
1712      */
1713     public Class<?>[] getDeclaredClasses() throws SecurityException {
1714         // be very careful not to change the stack depth of this
1715         // checkMemberAccess call for security reasons
1716         // see java.lang.SecurityManager.checkMemberAccess
1717         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), false);
1718         return getDeclaredClasses0();
1719     }
1720 
1721 
1722     /**
1723      * Returns an array of {@code Field} objects reflecting all the fields
1724      * declared by the class or interface represented by this
1725      * {@code Class} object. This includes public, protected, default
1726      * (package) access, and private fields, but excludes inherited fields.
1727      * The elements in the array returned are not sorted and are not in any
1728      * particular order.  This method returns an array of length 0 if the class
1729      * or interface declares no fields, or if this {@code Class} object
1730      * represents a primitive type, an array class, or void.
1731      *
1732      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1733      *
1734      * @return    the array of {@code Field} objects representing all the
1735      * declared fields of this class
1736      * @exception  SecurityException
1737      *             If a security manager, <i>s</i>, is present and any of the


1741      *
1742      *             <li> invocation of
1743      *             {@link SecurityManager#checkMemberAccess
1744      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1745      *             access to the declared fields within this class
1746      *
1747      *             <li> the caller's class loader is not the same as or an
1748      *             ancestor of the class loader for the current class and
1749      *             invocation of {@link SecurityManager#checkPackageAccess
1750      *             s.checkPackageAccess()} denies access to the package
1751      *             of this class
1752      *
1753      *             </ul>
1754      *
1755      * @since JDK1.1
1756      */
1757     public Field[] getDeclaredFields() throws SecurityException {
1758         // be very careful not to change the stack depth of this
1759         // checkMemberAccess call for security reasons
1760         // see java.lang.SecurityManager.checkMemberAccess
1761         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
1762         return copyFields(privateGetDeclaredFields(false));
1763     }
1764 
1765 
1766     /**
1767      * Returns an array of {@code Method} objects reflecting all the
1768      * methods declared by the class or interface represented by this
1769      * {@code Class} object. This includes public, protected, default
1770      * (package) access, and private methods, but excludes inherited methods.
1771      * The elements in the array returned are not sorted and are not in any
1772      * particular order.  This method returns an array of length 0 if the class
1773      * or interface declares no methods, or if this {@code Class} object
1774      * represents a primitive type, an array class, or void.  The class
1775      * initialization method {@code <clinit>} is not included in the
1776      * returned array. If the class declares multiple public member methods
1777      * with the same parameter types, they are all included in the returned
1778      * array.
1779      *
1780      * <p> See <em>The Java Language Specification</em>, section 8.2.
1781      *


1789      *
1790      *             <li> invocation of
1791      *             {@link SecurityManager#checkMemberAccess
1792      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1793      *             access to the declared methods within this class
1794      *
1795      *             <li> the caller's class loader is not the same as or an
1796      *             ancestor of the class loader for the current class and
1797      *             invocation of {@link SecurityManager#checkPackageAccess
1798      *             s.checkPackageAccess()} denies access to the package
1799      *             of this class
1800      *
1801      *             </ul>
1802      *
1803      * @since JDK1.1
1804      */
1805     public Method[] getDeclaredMethods() throws SecurityException {
1806         // be very careful not to change the stack depth of this
1807         // checkMemberAccess call for security reasons
1808         // see java.lang.SecurityManager.checkMemberAccess
1809         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
1810         return copyMethods(privateGetDeclaredMethods(false));
1811     }
1812 
1813 
1814     /**
1815      * Returns an array of {@code Constructor} objects reflecting all the
1816      * constructors declared by the class represented by this
1817      * {@code Class} object. These are public, protected, default
1818      * (package) access, and private constructors.  The elements in the array
1819      * returned are not sorted and are not in any particular order.  If the
1820      * class has a default constructor, it is included in the returned array.
1821      * This method returns an array of length 0 if this {@code Class}
1822      * object represents an interface, a primitive type, an array class, or
1823      * void.
1824      *
1825      * <p> See <em>The Java Language Specification</em>, section 8.2.
1826      *
1827      * @return    the array of {@code Constructor} objects representing all the
1828      * declared constructors of this class
1829      * @exception  SecurityException


1834      *
1835      *             <li> invocation of
1836      *             {@link SecurityManager#checkMemberAccess
1837      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1838      *             access to the declared constructors within this class
1839      *
1840      *             <li> the caller's class loader is not the same as or an
1841      *             ancestor of the class loader for the current class and
1842      *             invocation of {@link SecurityManager#checkPackageAccess
1843      *             s.checkPackageAccess()} denies access to the package
1844      *             of this class
1845      *
1846      *             </ul>
1847      *
1848      * @since JDK1.1
1849      */
1850     public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1851         // be very careful not to change the stack depth of this
1852         // checkMemberAccess call for security reasons
1853         // see java.lang.SecurityManager.checkMemberAccess
1854         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
1855         return copyConstructors(privateGetDeclaredConstructors(false));
1856     }
1857 
1858 
1859     /**
1860      * Returns a {@code Field} object that reflects the specified declared
1861      * field of the class or interface represented by this {@code Class}
1862      * object. The {@code name} parameter is a {@code String} that
1863      * specifies the simple name of the desired field.  Note that this method
1864      * will not reflect the {@code length} field of an array class.
1865      *
1866      * @param name the name of the field
1867      * @return the {@code Field} object for the specified field in this
1868      * class
1869      * @exception NoSuchFieldException if a field with the specified name is
1870      *              not found.
1871      * @exception NullPointerException if {@code name} is {@code null}
1872      * @exception  SecurityException
1873      *             If a security manager, <i>s</i>, is present and any of the
1874      *             following conditions is met:


1878      *             <li> invocation of
1879      *             {@link SecurityManager#checkMemberAccess
1880      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1881      *             access to the declared field
1882      *
1883      *             <li> the caller's class loader is not the same as or an
1884      *             ancestor of the class loader for the current class and
1885      *             invocation of {@link SecurityManager#checkPackageAccess
1886      *             s.checkPackageAccess()} denies access to the package
1887      *             of this class
1888      *
1889      *             </ul>
1890      *
1891      * @since JDK1.1
1892      */
1893     public Field getDeclaredField(String name)
1894         throws NoSuchFieldException, SecurityException {
1895         // be very careful not to change the stack depth of this
1896         // checkMemberAccess call for security reasons
1897         // see java.lang.SecurityManager.checkMemberAccess
1898         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
1899         Field field = searchFields(privateGetDeclaredFields(false), name);
1900         if (field == null) {
1901             throw new NoSuchFieldException(name);
1902         }
1903         return field;
1904     }
1905 
1906 
1907     /**
1908      * Returns a {@code Method} object that reflects the specified
1909      * declared method of the class or interface represented by this
1910      * {@code Class} object. The {@code name} parameter is a
1911      * {@code String} that specifies the simple name of the desired
1912      * method, and the {@code parameterTypes} parameter is an array of
1913      * {@code Class} objects that identify the method's formal parameter
1914      * types, in declared order.  If more than one method with the same
1915      * parameter types is declared in a class, and one of these methods has a
1916      * return type that is more specific than any of the others, that method is
1917      * returned; otherwise one of the methods is chosen arbitrarily.  If the
1918      * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}


1933      *             <li> invocation of
1934      *             {@link SecurityManager#checkMemberAccess
1935      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1936      *             access to the declared method
1937      *
1938      *             <li> the caller's class loader is not the same as or an
1939      *             ancestor of the class loader for the current class and
1940      *             invocation of {@link SecurityManager#checkPackageAccess
1941      *             s.checkPackageAccess()} denies access to the package
1942      *             of this class
1943      *
1944      *             </ul>
1945      *
1946      * @since JDK1.1
1947      */
1948     public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1949         throws NoSuchMethodException, SecurityException {
1950         // be very careful not to change the stack depth of this
1951         // checkMemberAccess call for security reasons
1952         // see java.lang.SecurityManager.checkMemberAccess
1953         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
1954         Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
1955         if (method == null) {
1956             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1957         }
1958         return method;
1959     }
1960 
1961 
1962     /**
1963      * Returns a {@code Constructor} object that reflects the specified
1964      * constructor of the class or interface represented by this
1965      * {@code Class} object.  The {@code parameterTypes} parameter is
1966      * an array of {@code Class} objects that identify the constructor's
1967      * formal parameter types, in declared order.
1968      *
1969      * If this {@code Class} object represents an inner class
1970      * declared in a non-static context, the formal parameter types
1971      * include the explicit enclosing instance as the first parameter.
1972      *
1973      * @param parameterTypes the parameter array


1983      *             <li> invocation of
1984      *             {@link SecurityManager#checkMemberAccess
1985      *             s.checkMemberAccess(this, Member.DECLARED)} denies
1986      *             access to the declared constructor
1987      *
1988      *             <li> the caller's class loader is not the same as or an
1989      *             ancestor of the class loader for the current class and
1990      *             invocation of {@link SecurityManager#checkPackageAccess
1991      *             s.checkPackageAccess()} denies access to the package
1992      *             of this class
1993      *
1994      *             </ul>
1995      *
1996      * @since JDK1.1
1997      */
1998     public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1999         throws NoSuchMethodException, SecurityException {
2000         // be very careful not to change the stack depth of this
2001         // checkMemberAccess call for security reasons
2002         // see java.lang.SecurityManager.checkMemberAccess
2003         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true);
2004         return getConstructor0(parameterTypes, Member.DECLARED);
2005     }
2006 
2007     /**
2008      * Finds a resource with a given name.  The rules for searching resources
2009      * associated with a given class are implemented by the defining
2010      * {@linkplain ClassLoader class loader} of the class.  This method
2011      * delegates to this object's class loader.  If this object was loaded by
2012      * the bootstrap class loader, the method delegates to {@link
2013      * ClassLoader#getSystemResourceAsStream}.
2014      *
2015      * <p> Before delegation, an absolute resource name is constructed from the
2016      * given resource name using this algorithm:
2017      *
2018      * <ul>
2019      *
2020      * <li> If the {@code name} begins with a {@code '/'}
2021      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
2022      * portion of the {@code name} following the {@code '/'}.
2023      *


2153 
2154 
2155     /*
2156      * Return the Virtual Machine's Class object for the named
2157      * primitive type.
2158      */
2159     static native Class getPrimitiveClass(String name);
2160 
2161 
2162     /*
2163      * Check if client is allowed to access members.  If access is denied,
2164      * throw a SecurityException.
2165      *
2166      * Be very careful not to change the stack depth of this checkMemberAccess
2167      * call for security reasons.
2168      * See java.lang.SecurityManager.checkMemberAccess.
2169      *
2170      * <p> Default policy: allow all clients access with normal Java access
2171      * control.
2172      */
2173     private void checkMemberAccess(int which, ClassLoader ccl, boolean checkProxyInterfaces) {
2174         SecurityManager s = System.getSecurityManager();
2175         if (s != null) {
2176             s.checkMemberAccess(this, which);
2177             ClassLoader cl = getClassLoader0();
2178             if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) {

2179                 String name = this.getName();
2180                 int i = name.lastIndexOf('.');
2181                 if (i != -1) {
2182                     // skip the package access check on a proxy class in default proxy package
2183                     String pkg = name.substring(0, i);
2184                     if (!Proxy.isProxyClass(this) || !pkg.equals(ReflectUtil.PROXY_PACKAGE)) {
2185                         s.checkPackageAccess(pkg);
2186                     }
2187                 }
2188             }
2189             // check package access on the proxy interfaces
2190             if (checkProxyInterfaces && Proxy.isProxyClass(this)) {
2191                 ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces());
2192             }
2193         }
2194     }
2195 
2196     /**
2197      * Add a package name prefix if the name is not absolute Remove leading "/"
2198      * if name is absolute
2199      */
2200     private String resolveName(String name) {
2201         if (name == null) {
2202             return name;
2203         }
2204         if (!name.startsWith("/")) {
2205             Class<?> c = this;
2206             while (c.isArray()) {
2207                 c = c.getComponentType();
2208             }
2209             String baseName = c.getName();
2210             int index = baseName.lastIndexOf('.');
2211             if (index != -1) {