345 throws ClassNotFoundException; 346 347 /** 348 * Creates a new instance of the class represented by this {@code Class} 349 * object. The class is instantiated as if by a {@code new} 350 * expression with an empty argument list. The class is initialized if it 351 * has not already been initialized. 352 * 353 * <p>Note that this method propagates any exception thrown by the 354 * nullary constructor, including a checked exception. Use of 355 * this method effectively bypasses the compile-time exception 356 * checking that would otherwise be performed by the compiler. 357 * The {@link 358 * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 359 * Constructor.newInstance} method avoids this problem by wrapping 360 * any exception thrown by the constructor in a (checked) {@link 361 * java.lang.reflect.InvocationTargetException}. 362 * 363 * @return a newly allocated instance of the class represented by this 364 * object. 365 * @exception IllegalAccessException if the class or its nullary 366 * constructor is not accessible. 367 * @exception InstantiationException 368 * if this {@code Class} represents an abstract class, 369 * an interface, an array class, a primitive type, or void; 370 * or if the class has no nullary constructor; 371 * or if the instantiation fails for some other reason. 372 * @exception ExceptionInInitializerError if the initialization 373 * provoked by this method fails. 374 * @exception SecurityException 375 * If a security manager, <i>s</i>, is present and any of the 376 * following conditions is met: 377 * 378 * <ul> 379 * 380 * <li> invocation of 381 * {@link SecurityManager#checkMemberAccess 382 * s.checkMemberAccess(this, Member.PUBLIC)} denies 383 * creation of new instances of this class 384 * 385 * <li> the caller's class loader is not the same as or an 386 * ancestor of the class loader for the current class and 387 * invocation of {@link SecurityManager#checkPackageAccess 388 * s.checkPackageAccess()} denies access to the package 389 * of this class 390 * 391 * </ul> 392 * 393 */ 394 @CallerSensitive 395 public T newInstance() 396 throws InstantiationException, IllegalAccessException 397 { 398 if (System.getSecurityManager() != null) { 399 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); 400 } 401 402 // NOTE: the following code may not be strictly correct under 403 // the current Java memory model. 404 405 // Constructor lookup 406 if (cachedConstructor == null) { 407 if (this == Class.class) { 408 throw new IllegalAccessException( 409 "Can not call newInstance() on the Class for java.lang.Class" 410 ); 411 } 412 try { 964 965 /** 966 * Set the signers of this class. 967 */ 968 native void setSigners(Object[] signers); 969 970 971 /** 972 * If this {@code Class} object represents a local or anonymous 973 * class within a method, returns a {@link 974 * java.lang.reflect.Method Method} object representing the 975 * immediately enclosing method of the underlying class. Returns 976 * {@code null} otherwise. 977 * 978 * In particular, this method returns {@code null} if the underlying 979 * class is a local or anonymous class immediately enclosed by a type 980 * declaration, instance initializer or static initializer. 981 * 982 * @return the immediately enclosing method of the underlying class, if 983 * that class is a local or anonymous class; otherwise {@code null}. 984 * @exception SecurityException 985 * If a security manager, <i>s</i>, is present and any of the 986 * following conditions is met: 987 * 988 * <ul> 989 * 990 * <li> invocation of 991 * {@link SecurityManager#checkMemberAccess 992 * s.checkMemberAccess(enclosingClass, Member.DECLARED)} denies 993 * access to the methods within the enclosing class 994 * 995 * <li> the caller's class loader is not the same as or an 996 * ancestor of the class loader for the enclosing class and 997 * invocation of {@link SecurityManager#checkPackageAccess 998 * s.checkPackageAccess()} denies access to the package 999 * of the enclosing class 1000 * 1001 * </ul> 1002 * @since 1.5 1003 */ 1004 @CallerSensitive 1005 public Method getEnclosingMethod() throws SecurityException { 1006 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); 1007 1008 if (enclosingInfo == null) 1009 return null; 1010 else { 1011 if (!enclosingInfo.isMethod()) 1012 return null; 1013 1014 MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(), 1015 getFactory()); 1016 Class<?> returnType = toClass(typeInfo.getReturnType()); 1017 Type [] parameterTypes = typeInfo.getParameterTypes(); 1018 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; 1019 1020 // Convert Types to Classes; returned types *should* 1021 // be class objects since the methodDescriptor's used 1022 // don't have generics information 1023 for(int i = 0; i < parameterClasses.length; i++) 1024 parameterClasses[i] = toClass(parameterTypes[i]); 1025 1026 // Perform access check 1027 Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); 1028 // be very careful not to change the stack depth of this 1029 // checkMemberAccess call for security reasons 1030 // see java.lang.SecurityManager.checkMemberAccess 1031 // 1032 // Note that we need to do this on the enclosing class 1033 enclosingCandidate.checkMemberAccess(Member.DECLARED, 1034 Reflection.getCallerClass(), true); 1035 /* 1036 * Loop over all declared methods; match method name, 1037 * number of and type of parameters, *and* return 1038 * type. Matching return type is also necessary 1039 * because of covariant returns, etc. 1040 */ 1041 for(Method m: enclosingCandidate.getDeclaredMethods()) { 1042 if (m.getName().equals(enclosingInfo.getName()) ) { 1043 Class<?>[] candidateParamClasses = m.getParameterTypes(); 1044 if (candidateParamClasses.length == parameterClasses.length) { 1045 boolean matches = true; 1046 for(int i = 0; i < candidateParamClasses.length; i++) { 1047 if (!candidateParamClasses[i].equals(parameterClasses[i])) { 1048 matches = false; 1049 break; 1050 } 1051 } 1052 1053 if (matches) { // finally, check return type 1054 if (m.getReturnType().equals(returnType) ) 1120 private static Class<?> toClass(Type o) { 1121 if (o instanceof GenericArrayType) 1122 return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()), 1123 0) 1124 .getClass(); 1125 return (Class<?>)o; 1126 } 1127 1128 /** 1129 * If this {@code Class} object represents a local or anonymous 1130 * class within a constructor, returns a {@link 1131 * java.lang.reflect.Constructor Constructor} object representing 1132 * the immediately enclosing constructor of the underlying 1133 * class. Returns {@code null} otherwise. In particular, this 1134 * method returns {@code null} if the underlying class is a local 1135 * or anonymous class immediately enclosed by a type declaration, 1136 * instance initializer or static initializer. 1137 * 1138 * @return the immediately enclosing constructor of the underlying class, if 1139 * that class is a local or anonymous class; otherwise {@code null}. 1140 * @exception SecurityException 1141 * If a security manager, <i>s</i>, is present and any of the 1142 * following conditions is met: 1143 * 1144 * <ul> 1145 * 1146 * <li> invocation of 1147 * {@link SecurityManager#checkMemberAccess 1148 * s.checkMemberAccess(enclosingClass, Member.DECLARED)} denies 1149 * access to the constructors within the enclosing class 1150 * 1151 * <li> the caller's class loader is not the same as or an 1152 * ancestor of the class loader for the enclosing class and 1153 * invocation of {@link SecurityManager#checkPackageAccess 1154 * s.checkPackageAccess()} denies access to the package 1155 * of the enclosing class 1156 * 1157 * </ul> 1158 * @since 1.5 1159 */ 1160 @CallerSensitive 1161 public Constructor<?> getEnclosingConstructor() throws SecurityException { 1162 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); 1163 1164 if (enclosingInfo == null) 1165 return null; 1166 else { 1167 if (!enclosingInfo.isConstructor()) 1168 return null; 1169 1170 ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), 1171 getFactory()); 1172 Type [] parameterTypes = typeInfo.getParameterTypes(); 1173 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; 1174 1175 // Convert Types to Classes; returned types *should* 1176 // be class objects since the methodDescriptor's used 1177 // don't have generics information 1178 for(int i = 0; i < parameterClasses.length; i++) 1179 parameterClasses[i] = toClass(parameterTypes[i]); 1180 1181 // Perform access check 1182 Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); 1183 // be very careful not to change the stack depth of this 1184 // checkMemberAccess call for security reasons 1185 // see java.lang.SecurityManager.checkMemberAccess 1186 // 1187 // Note that we need to do this on the enclosing class 1188 enclosingCandidate.checkMemberAccess(Member.DECLARED, 1189 Reflection.getCallerClass(), true); 1190 /* 1191 * Loop over all declared constructors; match number 1192 * of and type of parameters. 1193 */ 1194 for(Constructor<?> c: enclosingCandidate.getDeclaredConstructors()) { 1195 Class<?>[] candidateParamClasses = c.getParameterTypes(); 1196 if (candidateParamClasses.length == parameterClasses.length) { 1197 boolean matches = true; 1198 for(int i = 0; i < candidateParamClasses.length; i++) { 1199 if (!candidateParamClasses[i].equals(parameterClasses[i])) { 1200 matches = false; 1201 break; 1202 } 1203 } 1204 1205 if (matches) 1206 return c; 1207 } 1208 } 1209 1441 private boolean isLocalOrAnonymousClass() { 1442 // JVM Spec 4.8.6: A class must have an EnclosingMethod 1443 // attribute if and only if it is a local class or an 1444 // anonymous class. 1445 return getEnclosingMethodInfo() != null; 1446 } 1447 1448 /** 1449 * Returns an array containing {@code Class} objects representing all 1450 * the public classes and interfaces that are members of the class 1451 * represented by this {@code Class} object. This includes public 1452 * class and interface members inherited from superclasses and public class 1453 * and interface members declared by the class. This method returns an 1454 * array of length 0 if this {@code Class} object has no public member 1455 * classes or interfaces. This method also returns an array of length 0 if 1456 * this {@code Class} object represents a primitive type, an array 1457 * class, or void. 1458 * 1459 * @return the array of {@code Class} objects representing the public 1460 * members of this class 1461 * @exception SecurityException 1462 * If a security manager, <i>s</i>, is present and any of the 1463 * following conditions is met: 1464 * 1465 * <ul> 1466 * 1467 * <li> invocation of 1468 * {@link SecurityManager#checkMemberAccess 1469 * s.checkMemberAccess(this, Member.PUBLIC)} method 1470 * denies access to the classes within this class 1471 * 1472 * <li> the caller's class loader is not the same as or an 1473 * ancestor of the class loader for the current class and 1474 * invocation of {@link SecurityManager#checkPackageAccess 1475 * s.checkPackageAccess()} denies access to the package 1476 * of this class 1477 * 1478 * </ul> 1479 * 1480 * @since JDK1.1 1481 */ 1482 @CallerSensitive 1483 public Class<?>[] getClasses() { 1484 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); 1485 1486 // Privileged so this implementation can look at DECLARED classes, 1487 // something the caller might not have privilege to do. The code here 1488 // is allowed to look at DECLARED classes because (1) it does not hand 1489 // out anything other than public members and (2) public member access 1490 // has already been ok'd by the SecurityManager. 1491 1492 return java.security.AccessController.doPrivileged( 1493 new java.security.PrivilegedAction<Class<?>[]>() { 1494 public Class<?>[] run() { 1495 List<Class<?>> list = new ArrayList<>(); 1496 Class<?> currentClass = Class.this; 1497 while (currentClass != null) { 1498 Class<?>[] members = currentClass.getDeclaredClasses(); 1499 for (int i = 0; i < members.length; i++) { 1500 if (Modifier.isPublic(members[i].getModifiers())) { 1501 list.add(members[i]); 1502 } 1503 } 1504 currentClass = currentClass.getSuperclass(); 1514 * the accessible public fields of the class or interface represented by 1515 * this {@code Class} object. The elements in the array returned are 1516 * not sorted and are not in any particular order. This method returns an 1517 * array of length 0 if the class or interface has no accessible public 1518 * fields, or if it represents an array class, a primitive type, or void. 1519 * 1520 * <p> Specifically, if this {@code Class} object represents a class, 1521 * this method returns the public fields of this class and of all its 1522 * superclasses. If this {@code Class} object represents an 1523 * interface, this method returns the fields of this interface and of all 1524 * its superinterfaces. 1525 * 1526 * <p> The implicit length field for array class is not reflected by this 1527 * method. User code should use the methods of class {@code Array} to 1528 * manipulate arrays. 1529 * 1530 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1531 * 1532 * @return the array of {@code Field} objects representing the 1533 * public fields 1534 * @exception SecurityException 1535 * If a security manager, <i>s</i>, is present and any of the 1536 * following conditions is met: 1537 * 1538 * <ul> 1539 * 1540 * <li> invocation of 1541 * {@link SecurityManager#checkMemberAccess 1542 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1543 * access to the fields within this class 1544 * 1545 * <li> the caller's class loader is not the same as or an 1546 * ancestor of the class loader for the current class and 1547 * invocation of {@link SecurityManager#checkPackageAccess 1548 * s.checkPackageAccess()} denies access to the package 1549 * of this class 1550 * 1551 * </ul> 1552 * 1553 * @since JDK1.1 1554 */ 1555 @CallerSensitive 1556 public Field[] getFields() throws SecurityException { 1557 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1558 return copyFields(privateGetPublicFields(null)); 1559 } 1560 1561 1562 /** 1563 * Returns an array containing {@code Method} objects reflecting all 1564 * the public <em>member</em> methods of the class or interface represented 1565 * by this {@code Class} object, including those declared by the class 1566 * or interface and those inherited from superclasses and 1567 * superinterfaces. Array classes return all the (public) member methods 1568 * inherited from the {@code Object} class. The elements in the array 1569 * returned are not sorted and are not in any particular order. This 1570 * method returns an array of length 0 if this {@code Class} object 1571 * represents a class or interface that has no public member methods, or if 1572 * this {@code Class} object represents a primitive type or void. 1573 * 1574 * <p> The class initialization method {@code <clinit>} is not 1575 * included in the returned array. If the class declares multiple public 1576 * member methods with the same parameter types, they are all included in 1577 * the returned array. 1578 * 1579 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1580 * 1581 * @return the array of {@code Method} objects representing the 1582 * public methods of this class 1583 * @exception SecurityException 1584 * If a security manager, <i>s</i>, is present and any of the 1585 * following conditions is met: 1586 * 1587 * <ul> 1588 * 1589 * <li> invocation of 1590 * {@link SecurityManager#checkMemberAccess 1591 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1592 * access to the methods within this class 1593 * 1594 * <li> the caller's class loader is not the same as or an 1595 * ancestor of the class loader for the current class and 1596 * invocation of {@link SecurityManager#checkPackageAccess 1597 * s.checkPackageAccess()} denies access to the package 1598 * of this class 1599 * 1600 * </ul> 1601 * 1602 * @since JDK1.1 1603 */ 1604 @CallerSensitive 1605 public Method[] getMethods() throws SecurityException { 1606 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1607 return copyMethods(privateGetPublicMethods()); 1608 } 1609 1610 1611 /** 1612 * Returns an array containing {@code Constructor} objects reflecting 1613 * all the public constructors of the class represented by this 1614 * {@code Class} object. An array of length 0 is returned if the 1615 * class has no public constructors, or if the class is an array class, or 1616 * if the class reflects a primitive type or void. 1617 * 1618 * Note that while this method returns an array of {@code 1619 * Constructor<T>} objects (that is an array of constructors from 1620 * this class), the return type of this method is {@code 1621 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 1622 * might be expected. This less informative return type is 1623 * necessary since after being returned from this method, the 1624 * array could be modified to hold {@code Constructor} objects for 1625 * different classes, which would violate the type guarantees of 1626 * {@code Constructor<T>[]}. 1627 * 1628 * @return the array of {@code Constructor} objects representing the 1629 * public constructors of this class 1630 * @exception SecurityException 1631 * If a security manager, <i>s</i>, is present and any of the 1632 * following conditions is met: 1633 * 1634 * <ul> 1635 * 1636 * <li> invocation of 1637 * {@link SecurityManager#checkMemberAccess 1638 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1639 * access to the constructors within this class 1640 * 1641 * <li> the caller's class loader is not the same as or an 1642 * ancestor of the class loader for the current class and 1643 * invocation of {@link SecurityManager#checkPackageAccess 1644 * s.checkPackageAccess()} denies access to the package 1645 * of this class 1646 * 1647 * </ul> 1648 * 1649 * @since JDK1.1 1650 */ 1651 @CallerSensitive 1652 public Constructor<?>[] getConstructors() throws SecurityException { 1653 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1654 return copyConstructors(privateGetDeclaredConstructors(true)); 1655 } 1656 1657 1658 /** 1659 * Returns a {@code Field} object that reflects the specified public 1660 * member field of the class or interface represented by this 1661 * {@code Class} object. The {@code name} parameter is a 1662 * {@code String} specifying the simple name of the desired field. 1663 * 1664 * <p> The field to be reflected is determined by the algorithm that 1665 * follows. Let C be the class represented by this object: 1666 * <OL> 1667 * <LI> If C declares a public field with the name specified, that is the 1668 * field to be reflected.</LI> 1669 * <LI> If no field was found in step 1 above, this algorithm is applied 1670 * recursively to each direct superinterface of C. The direct 1671 * superinterfaces are searched in the order they were declared.</LI> 1672 * <LI> If no field was found in steps 1 and 2 above, and C has a 1673 * superclass S, then this algorithm is invoked recursively upon S. 1674 * If C has no superclass, then a {@code NoSuchFieldException} 1675 * is thrown.</LI> 1676 * </OL> 1677 * 1678 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1679 * 1680 * @param name the field name 1681 * @return the {@code Field} object of this class specified by 1682 * {@code name} 1683 * @exception NoSuchFieldException if a field with the specified name is 1684 * not found. 1685 * @exception NullPointerException if {@code name} is {@code null} 1686 * @exception SecurityException 1687 * If a security manager, <i>s</i>, is present and any of the 1688 * following conditions is met: 1689 * 1690 * <ul> 1691 * 1692 * <li> invocation of 1693 * {@link SecurityManager#checkMemberAccess 1694 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1695 * access to the field 1696 * 1697 * <li> the caller's class loader is not the same as or an 1698 * ancestor of the class loader for the current class and 1699 * invocation of {@link SecurityManager#checkPackageAccess 1700 * s.checkPackageAccess()} denies access to the package 1701 * of this class 1702 * 1703 * </ul> 1704 * 1705 * @since JDK1.1 1706 */ 1707 @CallerSensitive 1708 public Field getField(String name) 1709 throws NoSuchFieldException, SecurityException { 1710 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1711 Field field = getField0(name); 1712 if (field == null) { 1713 throw new NoSuchFieldException(name); 1714 } 1715 return field; 1716 } 1717 1718 1719 /** 1720 * Returns a {@code Method} object that reflects the specified public 1721 * member method of the class or interface represented by this 1722 * {@code Class} object. The {@code name} parameter is a 1723 * {@code String} specifying the simple name of the desired method. The 1724 * {@code parameterTypes} parameter is an array of {@code Class} 1725 * objects that identify the method's formal parameter types, in declared 1726 * order. If {@code parameterTypes} is {@code null}, it is 1727 * treated as if it were an empty array. 1728 * 1729 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 1730 * {@code NoSuchMethodException} is raised. Otherwise, the method to 1746 * more specific than any of the others, that method is reflected; 1747 * otherwise one of the methods is chosen arbitrarily. 1748 * 1749 * <p>Note that there may be more than one matching method in a 1750 * class because while the Java language forbids a class to 1751 * declare multiple methods with the same signature but different 1752 * return types, the Java virtual machine does not. This 1753 * increased flexibility in the virtual machine can be used to 1754 * implement various language features. For example, covariant 1755 * returns can be implemented with {@linkplain 1756 * java.lang.reflect.Method#isBridge bridge methods}; the bridge 1757 * method and the method being overridden would have the same 1758 * signature but different return types. 1759 * 1760 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1761 * 1762 * @param name the name of the method 1763 * @param parameterTypes the list of parameters 1764 * @return the {@code Method} object that matches the specified 1765 * {@code name} and {@code parameterTypes} 1766 * @exception NoSuchMethodException if a matching method is not found 1767 * or if the name is "<init>"or "<clinit>". 1768 * @exception NullPointerException if {@code name} is {@code null} 1769 * @exception SecurityException 1770 * If a security manager, <i>s</i>, is present and any of the 1771 * following conditions is met: 1772 * 1773 * <ul> 1774 * 1775 * <li> invocation of 1776 * {@link SecurityManager#checkMemberAccess 1777 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1778 * access to the method 1779 * 1780 * <li> the caller's class loader is not the same as or an 1781 * ancestor of the class loader for the current class and 1782 * invocation of {@link SecurityManager#checkPackageAccess 1783 * s.checkPackageAccess()} denies access to the package 1784 * of this class 1785 * 1786 * </ul> 1787 * 1788 * @since JDK1.1 1789 */ 1790 @CallerSensitive 1791 public Method getMethod(String name, Class<?>... parameterTypes) 1792 throws NoSuchMethodException, SecurityException { 1793 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1794 Method method = getMethod0(name, parameterTypes); 1795 if (method == null) { 1796 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 1797 } 1798 return method; 1799 } 1800 1801 1802 /** 1803 * Returns a {@code Constructor} object that reflects the specified 1804 * public constructor of the class represented by this {@code Class} 1805 * object. The {@code parameterTypes} parameter is an array of 1806 * {@code Class} objects that identify the constructor's formal 1807 * parameter types, in declared order. 1808 * 1809 * If this {@code Class} object represents an inner class 1810 * declared in a non-static context, the formal parameter types 1811 * include the explicit enclosing instance as the first parameter. 1812 * 1813 * <p> The constructor to reflect is the public constructor of the class 1814 * represented by this {@code Class} object whose formal parameter 1815 * types match those specified by {@code parameterTypes}. 1816 * 1817 * @param parameterTypes the parameter array 1818 * @return the {@code Constructor} object of the public constructor that 1819 * matches the specified {@code parameterTypes} 1820 * @exception NoSuchMethodException if a matching method is not found. 1821 * @exception SecurityException 1822 * If a security manager, <i>s</i>, is present and any of the 1823 * following conditions is met: 1824 * 1825 * <ul> 1826 * 1827 * <li> invocation of 1828 * {@link SecurityManager#checkMemberAccess 1829 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1830 * access to the constructor 1831 * 1832 * <li> the caller's class loader is not the same as or an 1833 * ancestor of the class loader for the current class and 1834 * invocation of {@link SecurityManager#checkPackageAccess 1835 * s.checkPackageAccess()} denies access to the package 1836 * of this class 1837 * 1838 * </ul> 1839 * 1840 * @since JDK1.1 1841 */ 1842 @CallerSensitive 1843 public Constructor<T> getConstructor(Class<?>... parameterTypes) 1844 throws NoSuchMethodException, SecurityException { 1845 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1846 return getConstructor0(parameterTypes, Member.PUBLIC); 1847 } 1848 1849 1850 /** 1851 * Returns an array of {@code Class} objects reflecting all the 1852 * classes and interfaces declared as members of the class represented by 1853 * this {@code Class} object. This includes public, protected, default 1854 * (package) access, and private classes and interfaces declared by the 1855 * class, but excludes inherited classes and interfaces. This method 1856 * returns an array of length 0 if the class declares no classes or 1857 * interfaces as members, or if this {@code Class} object represents a 1858 * primitive type, an array class, or void. 1859 * 1860 * @return the array of {@code Class} objects representing all the 1861 * declared members of this class 1862 * @exception SecurityException 1863 * If a security manager, <i>s</i>, is present and any of the 1864 * following conditions is met: 1865 * 1866 * <ul> 1867 * 1868 * <li> invocation of 1869 * {@link SecurityManager#checkMemberAccess 1870 * s.checkMemberAccess(this, Member.DECLARED)} denies 1871 * access to the declared classes within this class 1872 * 1873 * <li> the caller's class loader is not the same as or an 1874 * ancestor of the class loader for the current class and 1875 * invocation of {@link SecurityManager#checkPackageAccess 1876 * s.checkPackageAccess()} denies access to the package 1877 * of this class 1878 * 1879 * </ul> 1880 * 1881 * @since JDK1.1 1882 */ 1883 @CallerSensitive 1884 public Class<?>[] getDeclaredClasses() throws SecurityException { 1885 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false); 1886 return getDeclaredClasses0(); 1887 } 1888 1889 1890 /** 1891 * Returns an array of {@code Field} objects reflecting all the fields 1892 * declared by the class or interface represented by this 1893 * {@code Class} object. This includes public, protected, default 1894 * (package) access, and private fields, but excludes inherited fields. 1895 * The elements in the array returned are not sorted and are not in any 1896 * particular order. This method returns an array of length 0 if the class 1897 * or interface declares no fields, or if this {@code Class} object 1898 * represents a primitive type, an array class, or void. 1899 * 1900 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1901 * 1902 * @return the array of {@code Field} objects representing all the 1903 * declared fields of this class 1904 * @exception SecurityException 1905 * If a security manager, <i>s</i>, is present and any of the 1906 * following conditions is met: 1907 * 1908 * <ul> 1909 * 1910 * <li> invocation of 1911 * {@link SecurityManager#checkMemberAccess 1912 * s.checkMemberAccess(this, Member.DECLARED)} denies 1913 * access to the declared fields within this class 1914 * 1915 * <li> the caller's class loader is not the same as or an 1916 * ancestor of the class loader for the current class and 1917 * invocation of {@link SecurityManager#checkPackageAccess 1918 * s.checkPackageAccess()} denies access to the package 1919 * of this class 1920 * 1921 * </ul> 1922 * 1923 * @since JDK1.1 1924 */ 1925 @CallerSensitive 1926 public Field[] getDeclaredFields() throws SecurityException { 1927 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 1928 return copyFields(privateGetDeclaredFields(false)); 1929 } 1930 1931 1932 /** 1933 * Returns an array of {@code Method} objects reflecting all the 1934 * methods declared by the class or interface represented by this 1935 * {@code Class} object. This includes public, protected, default 1936 * (package) access, and private methods, but excludes inherited methods. 1937 * The elements in the array returned are not sorted and are not in any 1938 * particular order. This method returns an array of length 0 if the class 1939 * or interface declares no methods, or if this {@code Class} object 1940 * represents a primitive type, an array class, or void. The class 1941 * initialization method {@code <clinit>} is not included in the 1942 * returned array. If the class declares multiple public member methods 1943 * with the same parameter types, they are all included in the returned 1944 * array. 1945 * 1946 * <p> See <em>The Java Language Specification</em>, section 8.2. 1947 * 1948 * @return the array of {@code Method} objects representing all the 1949 * declared methods of this class 1950 * @exception SecurityException 1951 * If a security manager, <i>s</i>, is present and any of the 1952 * following conditions is met: 1953 * 1954 * <ul> 1955 * 1956 * <li> invocation of 1957 * {@link SecurityManager#checkMemberAccess 1958 * s.checkMemberAccess(this, Member.DECLARED)} denies 1959 * access to the declared methods within this class 1960 * 1961 * <li> the caller's class loader is not the same as or an 1962 * ancestor of the class loader for the current class and 1963 * invocation of {@link SecurityManager#checkPackageAccess 1964 * s.checkPackageAccess()} denies access to the package 1965 * of this class 1966 * 1967 * </ul> 1968 * 1969 * @since JDK1.1 1970 */ 1971 @CallerSensitive 1972 public Method[] getDeclaredMethods() throws SecurityException { 1973 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 1974 return copyMethods(privateGetDeclaredMethods(false)); 1975 } 1976 1977 1978 /** 1979 * Returns an array of {@code Constructor} objects reflecting all the 1980 * constructors declared by the class represented by this 1981 * {@code Class} object. These are public, protected, default 1982 * (package) access, and private constructors. The elements in the array 1983 * returned are not sorted and are not in any particular order. If the 1984 * class has a default constructor, it is included in the returned array. 1985 * This method returns an array of length 0 if this {@code Class} 1986 * object represents an interface, a primitive type, an array class, or 1987 * void. 1988 * 1989 * <p> See <em>The Java Language Specification</em>, section 8.2. 1990 * 1991 * @return the array of {@code Constructor} objects representing all the 1992 * declared constructors of this class 1993 * @exception SecurityException 1994 * If a security manager, <i>s</i>, is present and any of the 1995 * following conditions is met: 1996 * 1997 * <ul> 1998 * 1999 * <li> invocation of 2000 * {@link SecurityManager#checkMemberAccess 2001 * s.checkMemberAccess(this, Member.DECLARED)} denies 2002 * access to the declared constructors within this class 2003 * 2004 * <li> the caller's class loader is not the same as or an 2005 * ancestor of the class loader for the current class and 2006 * invocation of {@link SecurityManager#checkPackageAccess 2007 * s.checkPackageAccess()} denies access to the package 2008 * of this class 2009 * 2010 * </ul> 2011 * 2012 * @since JDK1.1 2013 */ 2014 @CallerSensitive 2015 public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 2016 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 2017 return copyConstructors(privateGetDeclaredConstructors(false)); 2018 } 2019 2020 2021 /** 2022 * Returns a {@code Field} object that reflects the specified declared 2023 * field of the class or interface represented by this {@code Class} 2024 * object. The {@code name} parameter is a {@code String} that 2025 * specifies the simple name of the desired field. Note that this method 2026 * will not reflect the {@code length} field of an array class. 2027 * 2028 * @param name the name of the field 2029 * @return the {@code Field} object for the specified field in this 2030 * class 2031 * @exception NoSuchFieldException if a field with the specified name is 2032 * not found. 2033 * @exception NullPointerException if {@code name} is {@code null} 2034 * @exception SecurityException 2035 * If a security manager, <i>s</i>, is present and any of the 2036 * following conditions is met: 2037 * 2038 * <ul> 2039 * 2040 * <li> invocation of 2041 * {@link SecurityManager#checkMemberAccess 2042 * s.checkMemberAccess(this, Member.DECLARED)} denies 2043 * access to the declared field 2044 * 2045 * <li> the caller's class loader is not the same as or an 2046 * ancestor of the class loader for the current class and 2047 * invocation of {@link SecurityManager#checkPackageAccess 2048 * s.checkPackageAccess()} denies access to the package 2049 * of this class 2050 * 2051 * </ul> 2052 * 2053 * @since JDK1.1 2054 */ 2055 @CallerSensitive 2056 public Field getDeclaredField(String name) 2057 throws NoSuchFieldException, SecurityException { 2058 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 2059 Field field = searchFields(privateGetDeclaredFields(false), name); 2060 if (field == null) { 2061 throw new NoSuchFieldException(name); 2062 } 2063 return field; 2064 } 2065 2066 2067 /** 2068 * Returns a {@code Method} object that reflects the specified 2069 * declared method of the class or interface represented by this 2070 * {@code Class} object. The {@code name} parameter is a 2071 * {@code String} that specifies the simple name of the desired 2072 * method, and the {@code parameterTypes} parameter is an array of 2073 * {@code Class} objects that identify the method's formal parameter 2074 * types, in declared order. If more than one method with the same 2075 * parameter types is declared in a class, and one of these methods has a 2076 * return type that is more specific than any of the others, that method is 2077 * returned; otherwise one of the methods is chosen arbitrarily. If the 2078 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 2079 * is raised. 2080 * 2081 * @param name the name of the method 2082 * @param parameterTypes the parameter array 2083 * @return the {@code Method} object for the method of this class 2084 * matching the specified name and parameters 2085 * @exception NoSuchMethodException if a matching method is not found. 2086 * @exception NullPointerException if {@code name} is {@code null} 2087 * @exception SecurityException 2088 * If a security manager, <i>s</i>, is present and any of the 2089 * following conditions is met: 2090 * 2091 * <ul> 2092 * 2093 * <li> invocation of 2094 * {@link SecurityManager#checkMemberAccess 2095 * s.checkMemberAccess(this, Member.DECLARED)} denies 2096 * access to the declared method 2097 * 2098 * <li> the caller's class loader is not the same as or an 2099 * ancestor of the class loader for the current class and 2100 * invocation of {@link SecurityManager#checkPackageAccess 2101 * s.checkPackageAccess()} denies access to the package 2102 * of this class 2103 * 2104 * </ul> 2105 * 2106 * @since JDK1.1 2107 */ 2108 @CallerSensitive 2109 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 2110 throws NoSuchMethodException, SecurityException { 2111 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 2112 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); 2113 if (method == null) { 2114 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 2115 } 2116 return method; 2117 } 2118 2119 2120 /** 2121 * Returns a {@code Constructor} object that reflects the specified 2122 * constructor of the class or interface represented by this 2123 * {@code Class} object. The {@code parameterTypes} parameter is 2124 * an array of {@code Class} objects that identify the constructor's 2125 * formal parameter types, in declared order. 2126 * 2127 * If this {@code Class} object represents an inner class 2128 * declared in a non-static context, the formal parameter types 2129 * include the explicit enclosing instance as the first parameter. 2130 * 2131 * @param parameterTypes the parameter array 2132 * @return The {@code Constructor} object for the constructor with the 2133 * specified parameter list 2134 * @exception NoSuchMethodException if a matching method is not found. 2135 * @exception SecurityException 2136 * If a security manager, <i>s</i>, is present and any of the 2137 * following conditions is met: 2138 * 2139 * <ul> 2140 * 2141 * <li> invocation of 2142 * {@link SecurityManager#checkMemberAccess 2143 * s.checkMemberAccess(this, Member.DECLARED)} denies 2144 * access to the declared constructor 2145 * 2146 * <li> the caller's class loader is not the same as or an 2147 * ancestor of the class loader for the current class and 2148 * invocation of {@link SecurityManager#checkPackageAccess 2149 * s.checkPackageAccess()} denies access to the package 2150 * of this class 2151 * 2152 * </ul> 2153 * 2154 * @since JDK1.1 2155 */ 2156 @CallerSensitive 2157 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 2158 throws NoSuchMethodException, SecurityException { 2159 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 2160 return getConstructor0(parameterTypes, Member.DECLARED); 2161 } 2162 2163 /** 2164 * Finds a resource with a given name. The rules for searching resources 2165 * associated with a given class are implemented by the defining 2166 * {@linkplain ClassLoader class loader} of the class. This method 2167 * delegates to this object's class loader. If this object was loaded by 2168 * the bootstrap class loader, the method delegates to {@link 2169 * ClassLoader#getSystemResourceAsStream}. 2170 * 2171 * <p> Before delegation, an absolute resource name is constructed from the 2172 * given resource name using this algorithm: 2173 * 2174 * <ul> 2175 * 2176 * <li> If the {@code name} begins with a {@code '/'} 2177 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 2178 * portion of the {@code name} following the {@code '/'}. 2179 * 2289 allPermDomain = 2290 new java.security.ProtectionDomain(null, perms); 2291 } 2292 pd = allPermDomain; 2293 } 2294 return pd; 2295 } 2296 2297 2298 /** 2299 * Returns the ProtectionDomain of this class. 2300 */ 2301 private native java.security.ProtectionDomain getProtectionDomain0(); 2302 2303 /* 2304 * Return the Virtual Machine's Class object for the named 2305 * primitive type. 2306 */ 2307 static native Class<?> getPrimitiveClass(String name); 2308 2309 private static boolean isCheckMemberAccessOverridden(SecurityManager smgr) { 2310 if (smgr.getClass() == SecurityManager.class) return false; 2311 2312 Class<?>[] paramTypes = new Class<?>[] {Class.class, int.class}; 2313 return smgr.getClass().getMethod0("checkMemberAccess", paramTypes). 2314 getDeclaringClass() != SecurityManager.class; 2315 } 2316 2317 /* 2318 * Check if client is allowed to access members. If access is denied, 2319 * throw a SecurityException. 2320 * 2321 * This method also enforces package access. 2322 * 2323 * <p> Default policy: allow all clients access with normal Java access 2324 * control. 2325 */ 2326 private void checkMemberAccess(int which, Class<?> caller, boolean checkProxyInterfaces) { 2327 final SecurityManager s = System.getSecurityManager(); 2328 if (s != null) { 2329 final ClassLoader ccl = ClassLoader.getClassLoader(caller); 2330 final ClassLoader cl = getClassLoader0(); 2331 if (!isCheckMemberAccessOverridden(s)) { 2332 // Inlined SecurityManager.checkMemberAccess 2333 if (which != Member.PUBLIC) { 2334 if (ccl != cl) { 2335 s.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); 2336 } 2337 } 2338 } else { 2339 // Don't refactor; otherwise break the stack depth for 2340 // checkMemberAccess of subclasses of SecurityManager as specified. 2341 s.checkMemberAccess(this, which); 2342 } 2343 this.checkPackageAccess(ccl, checkProxyInterfaces); 2344 } 2345 } 2346 2347 /* 2348 * Checks if a client loaded in ClassLoader ccl is allowed to access this 2349 * class under the current package access policy. If access is denied, 2350 * throw a SecurityException. 2351 */ 2352 private void checkPackageAccess(final ClassLoader ccl, boolean checkProxyInterfaces) { 2353 final SecurityManager s = System.getSecurityManager(); 2354 if (s != null) { 2355 final ClassLoader cl = getClassLoader0(); 2356 2357 if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) { 2358 String name = this.getName(); 2359 int i = name.lastIndexOf('.'); 2360 if (i != -1) { 2361 // skip the package access check on a proxy class in default proxy package 2362 String pkg = name.substring(0, i); | 345 throws ClassNotFoundException; 346 347 /** 348 * Creates a new instance of the class represented by this {@code Class} 349 * object. The class is instantiated as if by a {@code new} 350 * expression with an empty argument list. The class is initialized if it 351 * has not already been initialized. 352 * 353 * <p>Note that this method propagates any exception thrown by the 354 * nullary constructor, including a checked exception. Use of 355 * this method effectively bypasses the compile-time exception 356 * checking that would otherwise be performed by the compiler. 357 * The {@link 358 * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 359 * Constructor.newInstance} method avoids this problem by wrapping 360 * any exception thrown by the constructor in a (checked) {@link 361 * java.lang.reflect.InvocationTargetException}. 362 * 363 * @return a newly allocated instance of the class represented by this 364 * object. 365 * @throws IllegalAccessException if the class or its nullary 366 * constructor is not accessible. 367 * @throws InstantiationException 368 * if this {@code Class} represents an abstract class, 369 * an interface, an array class, a primitive type, or void; 370 * or if the class has no nullary constructor; 371 * or if the instantiation fails for some other reason. 372 * @throws ExceptionInInitializerError if the initialization 373 * provoked by this method fails. 374 * @throws SecurityException 375 * If a security manager, <i>s</i>, is present and 376 * the caller's class loader is not the same as or an 377 * ancestor of the class loader for the current class and 378 * invocation of {@link SecurityManager#checkPackageAccess 379 * s.checkPackageAccess()} denies access to the package 380 * of this class. 381 */ 382 @CallerSensitive 383 public T newInstance() 384 throws InstantiationException, IllegalAccessException 385 { 386 if (System.getSecurityManager() != null) { 387 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); 388 } 389 390 // NOTE: the following code may not be strictly correct under 391 // the current Java memory model. 392 393 // Constructor lookup 394 if (cachedConstructor == null) { 395 if (this == Class.class) { 396 throw new IllegalAccessException( 397 "Can not call newInstance() on the Class for java.lang.Class" 398 ); 399 } 400 try { 952 953 /** 954 * Set the signers of this class. 955 */ 956 native void setSigners(Object[] signers); 957 958 959 /** 960 * If this {@code Class} object represents a local or anonymous 961 * class within a method, returns a {@link 962 * java.lang.reflect.Method Method} object representing the 963 * immediately enclosing method of the underlying class. Returns 964 * {@code null} otherwise. 965 * 966 * In particular, this method returns {@code null} if the underlying 967 * class is a local or anonymous class immediately enclosed by a type 968 * declaration, instance initializer or static initializer. 969 * 970 * @return the immediately enclosing method of the underlying class, if 971 * that class is a local or anonymous class; otherwise {@code null}. 972 * 973 * @throws SecurityException 974 * If a security manager, <i>s</i>, is present and any of the 975 * following conditions is met: 976 * 977 * <ul> 978 * 979 * <li> the caller's class loader is not the same as the 980 * class loader of the enclosing class and invocation of 981 * {@link SecurityManager#checkPermission 982 * s.checkPermission} method with 983 * {@code RuntimePermission("accessDeclaredMembers")} 984 * denies access to the methods within the enclosing class 985 * 986 * <li> the caller's class loader is not the same as or an 987 * ancestor of the class loader for the enclosing class and 988 * invocation of {@link SecurityManager#checkPackageAccess 989 * s.checkPackageAccess()} denies access to the package 990 * of the enclosing class 991 * 992 * </ul> 993 * @since 1.5 994 */ 995 @CallerSensitive 996 public Method getEnclosingMethod() throws SecurityException { 997 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); 998 999 if (enclosingInfo == null) 1000 return null; 1001 else { 1002 if (!enclosingInfo.isMethod()) 1003 return null; 1004 1005 MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(), 1006 getFactory()); 1007 Class<?> returnType = toClass(typeInfo.getReturnType()); 1008 Type [] parameterTypes = typeInfo.getParameterTypes(); 1009 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; 1010 1011 // Convert Types to Classes; returned types *should* 1012 // be class objects since the methodDescriptor's used 1013 // don't have generics information 1014 for(int i = 0; i < parameterClasses.length; i++) 1015 parameterClasses[i] = toClass(parameterTypes[i]); 1016 1017 // Perform access check 1018 Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); 1019 if (System.getSecurityManager() != null) { 1020 enclosingCandidate.checkMemberAccess(Member.DECLARED, 1021 Reflection.getCallerClass(), true); 1022 } 1023 /* 1024 * Loop over all declared methods; match method name, 1025 * number of and type of parameters, *and* return 1026 * type. Matching return type is also necessary 1027 * because of covariant returns, etc. 1028 */ 1029 for(Method m: enclosingCandidate.getDeclaredMethods()) { 1030 if (m.getName().equals(enclosingInfo.getName()) ) { 1031 Class<?>[] candidateParamClasses = m.getParameterTypes(); 1032 if (candidateParamClasses.length == parameterClasses.length) { 1033 boolean matches = true; 1034 for(int i = 0; i < candidateParamClasses.length; i++) { 1035 if (!candidateParamClasses[i].equals(parameterClasses[i])) { 1036 matches = false; 1037 break; 1038 } 1039 } 1040 1041 if (matches) { // finally, check return type 1042 if (m.getReturnType().equals(returnType) ) 1108 private static Class<?> toClass(Type o) { 1109 if (o instanceof GenericArrayType) 1110 return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()), 1111 0) 1112 .getClass(); 1113 return (Class<?>)o; 1114 } 1115 1116 /** 1117 * If this {@code Class} object represents a local or anonymous 1118 * class within a constructor, returns a {@link 1119 * java.lang.reflect.Constructor Constructor} object representing 1120 * the immediately enclosing constructor of the underlying 1121 * class. Returns {@code null} otherwise. In particular, this 1122 * method returns {@code null} if the underlying class is a local 1123 * or anonymous class immediately enclosed by a type declaration, 1124 * instance initializer or static initializer. 1125 * 1126 * @return the immediately enclosing constructor of the underlying class, if 1127 * that class is a local or anonymous class; otherwise {@code null}. 1128 * @throws SecurityException 1129 * If a security manager, <i>s</i>, is present and any of the 1130 * following conditions is met: 1131 * 1132 * <ul> 1133 * 1134 * <li> the caller's class loader is not the same as the 1135 * class loader of the enclosing class and invocation of 1136 * {@link SecurityManager#checkPermission 1137 * s.checkPermission} method with 1138 * {@code RuntimePermission("accessDeclaredMembers")} 1139 * denies access to the constructors within the enclosing class 1140 * 1141 * <li> the caller's class loader is not the same as or an 1142 * ancestor of the class loader for the enclosing class and 1143 * invocation of {@link SecurityManager#checkPackageAccess 1144 * s.checkPackageAccess()} denies access to the package 1145 * of the enclosing class 1146 * 1147 * </ul> 1148 * @since 1.5 1149 */ 1150 @CallerSensitive 1151 public Constructor<?> getEnclosingConstructor() throws SecurityException { 1152 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); 1153 1154 if (enclosingInfo == null) 1155 return null; 1156 else { 1157 if (!enclosingInfo.isConstructor()) 1158 return null; 1159 1160 ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), 1161 getFactory()); 1162 Type [] parameterTypes = typeInfo.getParameterTypes(); 1163 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; 1164 1165 // Convert Types to Classes; returned types *should* 1166 // be class objects since the methodDescriptor's used 1167 // don't have generics information 1168 for(int i = 0; i < parameterClasses.length; i++) 1169 parameterClasses[i] = toClass(parameterTypes[i]); 1170 1171 // Perform access check 1172 Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); 1173 if (System.getSecurityManager() != null) { 1174 enclosingCandidate.checkMemberAccess(Member.DECLARED, 1175 Reflection.getCallerClass(), true); 1176 } 1177 /* 1178 * Loop over all declared constructors; match number 1179 * of and type of parameters. 1180 */ 1181 for(Constructor<?> c: enclosingCandidate.getDeclaredConstructors()) { 1182 Class<?>[] candidateParamClasses = c.getParameterTypes(); 1183 if (candidateParamClasses.length == parameterClasses.length) { 1184 boolean matches = true; 1185 for(int i = 0; i < candidateParamClasses.length; i++) { 1186 if (!candidateParamClasses[i].equals(parameterClasses[i])) { 1187 matches = false; 1188 break; 1189 } 1190 } 1191 1192 if (matches) 1193 return c; 1194 } 1195 } 1196 1428 private boolean isLocalOrAnonymousClass() { 1429 // JVM Spec 4.8.6: A class must have an EnclosingMethod 1430 // attribute if and only if it is a local class or an 1431 // anonymous class. 1432 return getEnclosingMethodInfo() != null; 1433 } 1434 1435 /** 1436 * Returns an array containing {@code Class} objects representing all 1437 * the public classes and interfaces that are members of the class 1438 * represented by this {@code Class} object. This includes public 1439 * class and interface members inherited from superclasses and public class 1440 * and interface members declared by the class. This method returns an 1441 * array of length 0 if this {@code Class} object has no public member 1442 * classes or interfaces. This method also returns an array of length 0 if 1443 * this {@code Class} object represents a primitive type, an array 1444 * class, or void. 1445 * 1446 * @return the array of {@code Class} objects representing the public 1447 * members of this class 1448 * @throws SecurityException 1449 * If a security manager, <i>s</i>, is present and 1450 * the caller's class loader is not the same as or an 1451 * ancestor of the class loader for the current class and 1452 * invocation of {@link SecurityManager#checkPackageAccess 1453 * s.checkPackageAccess()} denies access to the package 1454 * of this class. 1455 * 1456 * @since JDK1.1 1457 */ 1458 @CallerSensitive 1459 public Class<?>[] getClasses() { 1460 if (System.getSecurityManager() != null) { 1461 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); 1462 } 1463 1464 // Privileged so this implementation can look at DECLARED classes, 1465 // something the caller might not have privilege to do. The code here 1466 // is allowed to look at DECLARED classes because (1) it does not hand 1467 // out anything other than public members and (2) public member access 1468 // has already been ok'd by the SecurityManager. 1469 1470 return java.security.AccessController.doPrivileged( 1471 new java.security.PrivilegedAction<Class<?>[]>() { 1472 public Class<?>[] run() { 1473 List<Class<?>> list = new ArrayList<>(); 1474 Class<?> currentClass = Class.this; 1475 while (currentClass != null) { 1476 Class<?>[] members = currentClass.getDeclaredClasses(); 1477 for (int i = 0; i < members.length; i++) { 1478 if (Modifier.isPublic(members[i].getModifiers())) { 1479 list.add(members[i]); 1480 } 1481 } 1482 currentClass = currentClass.getSuperclass(); 1492 * the accessible public fields of the class or interface represented by 1493 * this {@code Class} object. The elements in the array returned are 1494 * not sorted and are not in any particular order. This method returns an 1495 * array of length 0 if the class or interface has no accessible public 1496 * fields, or if it represents an array class, a primitive type, or void. 1497 * 1498 * <p> Specifically, if this {@code Class} object represents a class, 1499 * this method returns the public fields of this class and of all its 1500 * superclasses. If this {@code Class} object represents an 1501 * interface, this method returns the fields of this interface and of all 1502 * its superinterfaces. 1503 * 1504 * <p> The implicit length field for array class is not reflected by this 1505 * method. User code should use the methods of class {@code Array} to 1506 * manipulate arrays. 1507 * 1508 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1509 * 1510 * @return the array of {@code Field} objects representing the 1511 * public fields 1512 * @throws SecurityException 1513 * If a security manager, <i>s</i>, is present and 1514 * the caller's class loader is not the same as or an 1515 * ancestor of the class loader for the current class and 1516 * invocation of {@link SecurityManager#checkPackageAccess 1517 * s.checkPackageAccess()} denies access to the package 1518 * of this class. 1519 * 1520 * @since JDK1.1 1521 */ 1522 @CallerSensitive 1523 public Field[] getFields() throws SecurityException { 1524 if (System.getSecurityManager() != null) { 1525 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1526 } 1527 return copyFields(privateGetPublicFields(null)); 1528 } 1529 1530 1531 /** 1532 * Returns an array containing {@code Method} objects reflecting all 1533 * the public <em>member</em> methods of the class or interface represented 1534 * by this {@code Class} object, including those declared by the class 1535 * or interface and those inherited from superclasses and 1536 * superinterfaces. Array classes return all the (public) member methods 1537 * inherited from the {@code Object} class. The elements in the array 1538 * returned are not sorted and are not in any particular order. This 1539 * method returns an array of length 0 if this {@code Class} object 1540 * represents a class or interface that has no public member methods, or if 1541 * this {@code Class} object represents a primitive type or void. 1542 * 1543 * <p> The class initialization method {@code <clinit>} is not 1544 * included in the returned array. If the class declares multiple public 1545 * member methods with the same parameter types, they are all included in 1546 * the returned array. 1547 * 1548 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1549 * 1550 * @return the array of {@code Method} objects representing the 1551 * public methods of this class 1552 * @throws SecurityException 1553 * If a security manager, <i>s</i>, is present and 1554 * the caller's class loader is not the same as or an 1555 * ancestor of the class loader for the current class and 1556 * invocation of {@link SecurityManager#checkPackageAccess 1557 * s.checkPackageAccess()} denies access to the package 1558 * of this class. 1559 * 1560 * @since JDK1.1 1561 */ 1562 @CallerSensitive 1563 public Method[] getMethods() throws SecurityException { 1564 if (System.getSecurityManager() != null) { 1565 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1566 } 1567 return copyMethods(privateGetPublicMethods()); 1568 } 1569 1570 1571 /** 1572 * Returns an array containing {@code Constructor} objects reflecting 1573 * all the public constructors of the class represented by this 1574 * {@code Class} object. An array of length 0 is returned if the 1575 * class has no public constructors, or if the class is an array class, or 1576 * if the class reflects a primitive type or void. 1577 * 1578 * Note that while this method returns an array of {@code 1579 * Constructor<T>} objects (that is an array of constructors from 1580 * this class), the return type of this method is {@code 1581 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 1582 * might be expected. This less informative return type is 1583 * necessary since after being returned from this method, the 1584 * array could be modified to hold {@code Constructor} objects for 1585 * different classes, which would violate the type guarantees of 1586 * {@code Constructor<T>[]}. 1587 * 1588 * @return the array of {@code Constructor} objects representing the 1589 * public constructors of this class 1590 * @throws SecurityException 1591 * If a security manager, <i>s</i>, is present and 1592 * the caller's class loader is not the same as or an 1593 * ancestor of the class loader for the current class and 1594 * invocation of {@link SecurityManager#checkPackageAccess 1595 * s.checkPackageAccess()} denies access to the package 1596 * of this class. 1597 * 1598 * @since JDK1.1 1599 */ 1600 @CallerSensitive 1601 public Constructor<?>[] getConstructors() throws SecurityException { 1602 if (System.getSecurityManager() != null) { 1603 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1604 } 1605 return copyConstructors(privateGetDeclaredConstructors(true)); 1606 } 1607 1608 1609 /** 1610 * Returns a {@code Field} object that reflects the specified public 1611 * member field of the class or interface represented by this 1612 * {@code Class} object. The {@code name} parameter is a 1613 * {@code String} specifying the simple name of the desired field. 1614 * 1615 * <p> The field to be reflected is determined by the algorithm that 1616 * follows. Let C be the class represented by this object: 1617 * <OL> 1618 * <LI> If C declares a public field with the name specified, that is the 1619 * field to be reflected.</LI> 1620 * <LI> If no field was found in step 1 above, this algorithm is applied 1621 * recursively to each direct superinterface of C. The direct 1622 * superinterfaces are searched in the order they were declared.</LI> 1623 * <LI> If no field was found in steps 1 and 2 above, and C has a 1624 * superclass S, then this algorithm is invoked recursively upon S. 1625 * If C has no superclass, then a {@code NoSuchFieldException} 1626 * is thrown.</LI> 1627 * </OL> 1628 * 1629 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1630 * 1631 * @param name the field name 1632 * @return the {@code Field} object of this class specified by 1633 * {@code name} 1634 * @throws NoSuchFieldException if a field with the specified name is 1635 * not found. 1636 * @throws NullPointerException if {@code name} is {@code null} 1637 * @throws SecurityException 1638 * If a security manager, <i>s</i>, is present and 1639 * the caller's class loader is not the same as or an 1640 * ancestor of the class loader for the current class and 1641 * invocation of {@link SecurityManager#checkPackageAccess 1642 * s.checkPackageAccess()} denies access to the package 1643 * of this class. 1644 * 1645 * @since JDK1.1 1646 */ 1647 @CallerSensitive 1648 public Field getField(String name) 1649 throws NoSuchFieldException, SecurityException { 1650 if (System.getSecurityManager() != null) { 1651 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1652 } 1653 Field field = getField0(name); 1654 if (field == null) { 1655 throw new NoSuchFieldException(name); 1656 } 1657 return field; 1658 } 1659 1660 1661 /** 1662 * Returns a {@code Method} object that reflects the specified public 1663 * member method of the class or interface represented by this 1664 * {@code Class} object. The {@code name} parameter is a 1665 * {@code String} specifying the simple name of the desired method. The 1666 * {@code parameterTypes} parameter is an array of {@code Class} 1667 * objects that identify the method's formal parameter types, in declared 1668 * order. If {@code parameterTypes} is {@code null}, it is 1669 * treated as if it were an empty array. 1670 * 1671 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 1672 * {@code NoSuchMethodException} is raised. Otherwise, the method to 1688 * more specific than any of the others, that method is reflected; 1689 * otherwise one of the methods is chosen arbitrarily. 1690 * 1691 * <p>Note that there may be more than one matching method in a 1692 * class because while the Java language forbids a class to 1693 * declare multiple methods with the same signature but different 1694 * return types, the Java virtual machine does not. This 1695 * increased flexibility in the virtual machine can be used to 1696 * implement various language features. For example, covariant 1697 * returns can be implemented with {@linkplain 1698 * java.lang.reflect.Method#isBridge bridge methods}; the bridge 1699 * method and the method being overridden would have the same 1700 * signature but different return types. 1701 * 1702 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1703 * 1704 * @param name the name of the method 1705 * @param parameterTypes the list of parameters 1706 * @return the {@code Method} object that matches the specified 1707 * {@code name} and {@code parameterTypes} 1708 * @throws NoSuchMethodException if a matching method is not found 1709 * or if the name is "<init>"or "<clinit>". 1710 * @throws NullPointerException if {@code name} is {@code null} 1711 * @throws SecurityException 1712 * If a security manager, <i>s</i>, is present and 1713 * the caller's class loader is not the same as or an 1714 * ancestor of the class loader for the current class and 1715 * invocation of {@link SecurityManager#checkPackageAccess 1716 * s.checkPackageAccess()} denies access to the package 1717 * of this class. 1718 * 1719 * @since JDK1.1 1720 */ 1721 @CallerSensitive 1722 public Method getMethod(String name, Class<?>... parameterTypes) 1723 throws NoSuchMethodException, SecurityException { 1724 if (System.getSecurityManager() != null) { 1725 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1726 } 1727 Method method = getMethod0(name, parameterTypes); 1728 if (method == null) { 1729 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 1730 } 1731 return method; 1732 } 1733 1734 1735 /** 1736 * Returns a {@code Constructor} object that reflects the specified 1737 * public constructor of the class represented by this {@code Class} 1738 * object. The {@code parameterTypes} parameter is an array of 1739 * {@code Class} objects that identify the constructor's formal 1740 * parameter types, in declared order. 1741 * 1742 * If this {@code Class} object represents an inner class 1743 * declared in a non-static context, the formal parameter types 1744 * include the explicit enclosing instance as the first parameter. 1745 * 1746 * <p> The constructor to reflect is the public constructor of the class 1747 * represented by this {@code Class} object whose formal parameter 1748 * types match those specified by {@code parameterTypes}. 1749 * 1750 * @param parameterTypes the parameter array 1751 * @return the {@code Constructor} object of the public constructor that 1752 * matches the specified {@code parameterTypes} 1753 * @throws NoSuchMethodException if a matching method is not found. 1754 * @throws SecurityException 1755 * If a security manager, <i>s</i>, is present and 1756 * the caller's class loader is not the same as or an 1757 * ancestor of the class loader for the current class and 1758 * invocation of {@link SecurityManager#checkPackageAccess 1759 * s.checkPackageAccess()} denies access to the package 1760 * of this class. 1761 * 1762 * @since JDK1.1 1763 */ 1764 @CallerSensitive 1765 public Constructor<T> getConstructor(Class<?>... parameterTypes) 1766 throws NoSuchMethodException, SecurityException { 1767 if (System.getSecurityManager() != null) { 1768 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); 1769 } 1770 return getConstructor0(parameterTypes, Member.PUBLIC); 1771 } 1772 1773 1774 /** 1775 * Returns an array of {@code Class} objects reflecting all the 1776 * classes and interfaces declared as members of the class represented by 1777 * this {@code Class} object. This includes public, protected, default 1778 * (package) access, and private classes and interfaces declared by the 1779 * class, but excludes inherited classes and interfaces. This method 1780 * returns an array of length 0 if the class declares no classes or 1781 * interfaces as members, or if this {@code Class} object represents a 1782 * primitive type, an array class, or void. 1783 * 1784 * @return the array of {@code Class} objects representing all the 1785 * declared members of this class 1786 * @throws SecurityException 1787 * If a security manager, <i>s</i>, is present and any of the 1788 * following conditions is met: 1789 * 1790 * <ul> 1791 * 1792 * <li> the caller's class loader is not the same as the 1793 * class loader of this class and invocation of 1794 * {@link SecurityManager#checkPermission 1795 * s.checkPermission} method with 1796 * {@code RuntimePermission("accessDeclaredMembers")} 1797 * denies access to the declared classes within this class 1798 * 1799 * <li> the caller's class loader is not the same as or an 1800 * ancestor of the class loader for the current class and 1801 * invocation of {@link SecurityManager#checkPackageAccess 1802 * s.checkPackageAccess()} denies access to the package 1803 * of this class 1804 * 1805 * </ul> 1806 * 1807 * @since JDK1.1 1808 */ 1809 @CallerSensitive 1810 public Class<?>[] getDeclaredClasses() throws SecurityException { 1811 if (System.getSecurityManager() != null) { 1812 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false); 1813 } 1814 return getDeclaredClasses0(); 1815 } 1816 1817 1818 /** 1819 * Returns an array of {@code Field} objects reflecting all the fields 1820 * declared by the class or interface represented by this 1821 * {@code Class} object. This includes public, protected, default 1822 * (package) access, and private fields, but excludes inherited fields. 1823 * The elements in the array returned are not sorted and are not in any 1824 * particular order. This method returns an array of length 0 if the class 1825 * or interface declares no fields, or if this {@code Class} object 1826 * represents a primitive type, an array class, or void. 1827 * 1828 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1829 * 1830 * @return the array of {@code Field} objects representing all the 1831 * declared fields of this class 1832 * @throws SecurityException 1833 * If a security manager, <i>s</i>, is present and any of the 1834 * following conditions is met: 1835 * 1836 * <ul> 1837 * 1838 * <li> the caller's class loader is not the same as the 1839 * class loader of this class and invocation of 1840 * {@link SecurityManager#checkPermission 1841 * s.checkPermission} method with 1842 * {@code RuntimePermission("accessDeclaredMembers")} 1843 * denies access to the declared fields within this class 1844 * 1845 * <li> the caller's class loader is not the same as or an 1846 * ancestor of the class loader for the current class and 1847 * invocation of {@link SecurityManager#checkPackageAccess 1848 * s.checkPackageAccess()} denies access to the package 1849 * of this class 1850 * 1851 * </ul> 1852 * 1853 * @since JDK1.1 1854 */ 1855 @CallerSensitive 1856 public Field[] getDeclaredFields() throws SecurityException { 1857 if (System.getSecurityManager() != null) { 1858 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 1859 } 1860 return copyFields(privateGetDeclaredFields(false)); 1861 } 1862 1863 1864 /** 1865 * Returns an array of {@code Method} objects reflecting all the 1866 * methods declared by the class or interface represented by this 1867 * {@code Class} object. This includes public, protected, default 1868 * (package) access, and private methods, but excludes inherited methods. 1869 * The elements in the array returned are not sorted and are not in any 1870 * particular order. This method returns an array of length 0 if the class 1871 * or interface declares no methods, or if this {@code Class} object 1872 * represents a primitive type, an array class, or void. The class 1873 * initialization method {@code <clinit>} is not included in the 1874 * returned array. If the class declares multiple public member methods 1875 * with the same parameter types, they are all included in the returned 1876 * array. 1877 * 1878 * <p> See <em>The Java Language Specification</em>, section 8.2. 1879 * 1880 * @return the array of {@code Method} objects representing all the 1881 * declared methods of this class 1882 * @throws SecurityException 1883 * If a security manager, <i>s</i>, is present and any of the 1884 * following conditions is met: 1885 * 1886 * <ul> 1887 * 1888 * <li> the caller's class loader is not the same as the 1889 * class loader of this class and invocation of 1890 * {@link SecurityManager#checkPermission 1891 * s.checkPermission} method with 1892 * {@code RuntimePermission("accessDeclaredMembers")} 1893 * denies access to the declared methods within this class 1894 * 1895 * <li> the caller's class loader is not the same as or an 1896 * ancestor of the class loader for the current class and 1897 * invocation of {@link SecurityManager#checkPackageAccess 1898 * s.checkPackageAccess()} denies access to the package 1899 * of this class 1900 * 1901 * </ul> 1902 * 1903 * @since JDK1.1 1904 */ 1905 @CallerSensitive 1906 public Method[] getDeclaredMethods() throws SecurityException { 1907 if (System.getSecurityManager() != null) { 1908 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 1909 } 1910 return copyMethods(privateGetDeclaredMethods(false)); 1911 } 1912 1913 1914 /** 1915 * Returns an array of {@code Constructor} objects reflecting all the 1916 * constructors declared by the class represented by this 1917 * {@code Class} object. These are public, protected, default 1918 * (package) access, and private constructors. The elements in the array 1919 * returned are not sorted and are not in any particular order. If the 1920 * class has a default constructor, it is included in the returned array. 1921 * This method returns an array of length 0 if this {@code Class} 1922 * object represents an interface, a primitive type, an array class, or 1923 * void. 1924 * 1925 * <p> See <em>The Java Language Specification</em>, section 8.2. 1926 * 1927 * @return the array of {@code Constructor} objects representing all the 1928 * declared constructors of this class 1929 * @throws SecurityException 1930 * If a security manager, <i>s</i>, is present and any of the 1931 * following conditions is met: 1932 * 1933 * <ul> 1934 * 1935 * <li> the caller's class loader is not the same as the 1936 * class loader of this class and invocation of 1937 * {@link SecurityManager#checkPermission 1938 * s.checkPermission} method with 1939 * {@code RuntimePermission("accessDeclaredMembers")} 1940 * denies access to the declared constructors within this class 1941 * 1942 * <li> the caller's class loader is not the same as or an 1943 * ancestor of the class loader for the current class and 1944 * invocation of {@link SecurityManager#checkPackageAccess 1945 * s.checkPackageAccess()} denies access to the package 1946 * of this class 1947 * 1948 * </ul> 1949 * 1950 * @since JDK1.1 1951 */ 1952 @CallerSensitive 1953 public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 1954 if (System.getSecurityManager() != null) { 1955 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 1956 } 1957 return copyConstructors(privateGetDeclaredConstructors(false)); 1958 } 1959 1960 1961 /** 1962 * Returns a {@code Field} object that reflects the specified declared 1963 * field of the class or interface represented by this {@code Class} 1964 * object. The {@code name} parameter is a {@code String} that 1965 * specifies the simple name of the desired field. Note that this method 1966 * will not reflect the {@code length} field of an array class. 1967 * 1968 * @param name the name of the field 1969 * @return the {@code Field} object for the specified field in this 1970 * class 1971 * @throws NoSuchFieldException if a field with the specified name is 1972 * not found. 1973 * @throws NullPointerException if {@code name} is {@code null} 1974 * @throws SecurityException 1975 * If a security manager, <i>s</i>, is present and any of the 1976 * following conditions is met: 1977 * 1978 * <ul> 1979 * 1980 * <li> the caller's class loader is not the same as the 1981 * class loader of this class and invocation of 1982 * {@link SecurityManager#checkPermission 1983 * s.checkPermission} method with 1984 * {@code RuntimePermission("accessDeclaredMembers")} 1985 * denies access to the declared field 1986 * 1987 * <li> the caller's class loader is not the same as or an 1988 * ancestor of the class loader for the current class and 1989 * invocation of {@link SecurityManager#checkPackageAccess 1990 * s.checkPackageAccess()} denies access to the package 1991 * of this class 1992 * 1993 * </ul> 1994 * 1995 * @since JDK1.1 1996 */ 1997 @CallerSensitive 1998 public Field getDeclaredField(String name) 1999 throws NoSuchFieldException, SecurityException { 2000 if (System.getSecurityManager() != null) { 2001 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 2002 } 2003 Field field = searchFields(privateGetDeclaredFields(false), name); 2004 if (field == null) { 2005 throw new NoSuchFieldException(name); 2006 } 2007 return field; 2008 } 2009 2010 2011 /** 2012 * Returns a {@code Method} object that reflects the specified 2013 * declared method of the class or interface represented by this 2014 * {@code Class} object. The {@code name} parameter is a 2015 * {@code String} that specifies the simple name of the desired 2016 * method, and the {@code parameterTypes} parameter is an array of 2017 * {@code Class} objects that identify the method's formal parameter 2018 * types, in declared order. If more than one method with the same 2019 * parameter types is declared in a class, and one of these methods has a 2020 * return type that is more specific than any of the others, that method is 2021 * returned; otherwise one of the methods is chosen arbitrarily. If the 2022 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 2023 * is raised. 2024 * 2025 * @param name the name of the method 2026 * @param parameterTypes the parameter array 2027 * @return the {@code Method} object for the method of this class 2028 * matching the specified name and parameters 2029 * @throws NoSuchMethodException if a matching method is not found. 2030 * @throws NullPointerException if {@code name} is {@code null} 2031 * @throws SecurityException 2032 * If a security manager, <i>s</i>, is present and any of the 2033 * following conditions is met: 2034 * 2035 * <ul> 2036 * 2037 * <li> the caller's class loader is not the same as the 2038 * class loader of this class and invocation of 2039 * {@link SecurityManager#checkPermission 2040 * s.checkPermission} method with 2041 * {@code RuntimePermission("accessDeclaredMembers")} 2042 * denies access to the declared method 2043 * 2044 * <li> the caller's class loader is not the same as or an 2045 * ancestor of the class loader for the current class and 2046 * invocation of {@link SecurityManager#checkPackageAccess 2047 * s.checkPackageAccess()} denies access to the package 2048 * of this class 2049 * 2050 * </ul> 2051 * 2052 * @since JDK1.1 2053 */ 2054 @CallerSensitive 2055 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 2056 throws NoSuchMethodException, SecurityException { 2057 if (System.getSecurityManager() != null) { 2058 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 2059 } 2060 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); 2061 if (method == null) { 2062 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 2063 } 2064 return method; 2065 } 2066 2067 2068 /** 2069 * Returns a {@code Constructor} object that reflects the specified 2070 * constructor of the class or interface represented by this 2071 * {@code Class} object. The {@code parameterTypes} parameter is 2072 * an array of {@code Class} objects that identify the constructor's 2073 * formal parameter types, in declared order. 2074 * 2075 * If this {@code Class} object represents an inner class 2076 * declared in a non-static context, the formal parameter types 2077 * include the explicit enclosing instance as the first parameter. 2078 * 2079 * @param parameterTypes the parameter array 2080 * @return The {@code Constructor} object for the constructor with the 2081 * specified parameter list 2082 * @throws NoSuchMethodException if a matching method is not found. 2083 * @throws SecurityException 2084 * If a security manager, <i>s</i>, is present and any of the 2085 * following conditions is met: 2086 * 2087 * <ul> 2088 * 2089 * <li> the caller's class loader is not the same as the 2090 * class loader of this class and invocation of 2091 * {@link SecurityManager#checkPermission 2092 * s.checkPermission} method with 2093 * {@code RuntimePermission("accessDeclaredMembers")} 2094 * denies access to the declared constructor 2095 * 2096 * <li> the caller's class loader is not the same as or an 2097 * ancestor of the class loader for the current class and 2098 * invocation of {@link SecurityManager#checkPackageAccess 2099 * s.checkPackageAccess()} denies access to the package 2100 * of this class 2101 * 2102 * </ul> 2103 * 2104 * @since JDK1.1 2105 */ 2106 @CallerSensitive 2107 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 2108 throws NoSuchMethodException, SecurityException { 2109 if (System.getSecurityManager() != null) { 2110 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 2111 } 2112 return getConstructor0(parameterTypes, Member.DECLARED); 2113 } 2114 2115 /** 2116 * Finds a resource with a given name. The rules for searching resources 2117 * associated with a given class are implemented by the defining 2118 * {@linkplain ClassLoader class loader} of the class. This method 2119 * delegates to this object's class loader. If this object was loaded by 2120 * the bootstrap class loader, the method delegates to {@link 2121 * ClassLoader#getSystemResourceAsStream}. 2122 * 2123 * <p> Before delegation, an absolute resource name is constructed from the 2124 * given resource name using this algorithm: 2125 * 2126 * <ul> 2127 * 2128 * <li> If the {@code name} begins with a {@code '/'} 2129 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 2130 * portion of the {@code name} following the {@code '/'}. 2131 * 2241 allPermDomain = 2242 new java.security.ProtectionDomain(null, perms); 2243 } 2244 pd = allPermDomain; 2245 } 2246 return pd; 2247 } 2248 2249 2250 /** 2251 * Returns the ProtectionDomain of this class. 2252 */ 2253 private native java.security.ProtectionDomain getProtectionDomain0(); 2254 2255 /* 2256 * Return the Virtual Machine's Class object for the named 2257 * primitive type. 2258 */ 2259 static native Class<?> getPrimitiveClass(String name); 2260 2261 /* 2262 * Check if client is allowed to access members. If access is denied, 2263 * throw a SecurityException. 2264 * 2265 * This method also enforces package access. 2266 * 2267 * <p> Default policy: allow all clients access with normal Java access 2268 * control. 2269 */ 2270 private void checkMemberAccess(int which, Class<?> caller, boolean checkProxyInterfaces) { 2271 final SecurityManager s = System.getSecurityManager(); 2272 if (s != null) { 2273 /* Default policy allows access to all {@link Member#PUBLIC} members, 2274 * as well as access to classes that have the same class loader as the caller. 2275 * In all other cases, it requires RuntimePermission("accessDeclaredMembers") 2276 * permission. 2277 */ 2278 final ClassLoader ccl = ClassLoader.getClassLoader(caller); 2279 final ClassLoader cl = getClassLoader0(); 2280 if (which != Member.PUBLIC) { 2281 if (ccl != cl) { 2282 s.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); 2283 } 2284 } 2285 this.checkPackageAccess(ccl, checkProxyInterfaces); 2286 } 2287 } 2288 2289 /* 2290 * Checks if a client loaded in ClassLoader ccl is allowed to access this 2291 * class under the current package access policy. If access is denied, 2292 * throw a SecurityException. 2293 */ 2294 private void checkPackageAccess(final ClassLoader ccl, boolean checkProxyInterfaces) { 2295 final SecurityManager s = System.getSecurityManager(); 2296 if (s != null) { 2297 final ClassLoader cl = getClassLoader0(); 2298 2299 if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) { 2300 String name = this.getName(); 2301 int i = name.lastIndexOf('.'); 2302 if (i != -1) { 2303 // skip the package access check on a proxy class in default proxy package 2304 String pkg = name.substring(0, i); |