272 throws ClassNotFoundException; 273 274 /** 275 * Creates a new instance of the class represented by this {@code Class} 276 * object. The class is instantiated as if by a {@code new} 277 * expression with an empty argument list. The class is initialized if it 278 * has not already been initialized. 279 * 280 * <p>Note that this method propagates any exception thrown by the 281 * nullary constructor, including a checked exception. Use of 282 * this method effectively bypasses the compile-time exception 283 * checking that would otherwise be performed by the compiler. 284 * The {@link 285 * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 286 * Constructor.newInstance} method avoids this problem by wrapping 287 * any exception thrown by the constructor in a (checked) {@link 288 * java.lang.reflect.InvocationTargetException}. 289 * 290 * @return a newly allocated instance of the class represented by this 291 * object. 292 * @exception IllegalAccessException if the class or its nullary 293 * constructor is not accessible. 294 * @exception InstantiationException 295 * if this {@code Class} represents an abstract class, 296 * an interface, an array class, a primitive type, or void; 297 * or if the class has no nullary constructor; 298 * or if the instantiation fails for some other reason. 299 * @exception ExceptionInInitializerError if the initialization 300 * provoked by this method fails. 301 * @exception SecurityException 302 * If a security manager, <i>s</i>, is present and any of the 303 * following conditions is met: 304 * 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( 1260 private boolean isLocalOrAnonymousClass() { 1261 // JVM Spec 4.8.6: A class must have an EnclosingMethod 1262 // attribute if and only if it is a local class or an 1263 // anonymous class. 1264 return getEnclosingMethodInfo() != null; 1265 } 1266 1267 /** 1268 * Returns an array containing {@code Class} objects representing all 1269 * the public classes and interfaces that are members of the class 1270 * represented by this {@code Class} object. This includes public 1271 * class and interface members inherited from superclasses and public class 1272 * and interface members declared by the class. This method returns an 1273 * array of length 0 if this {@code Class} object has no public member 1274 * classes or interfaces. This method also returns an array of length 0 if 1275 * this {@code Class} object represents a primitive type, an array 1276 * class, or void. 1277 * 1278 * @return the array of {@code Class} objects representing the public 1279 * members of this class 1280 * @exception SecurityException 1281 * If a security manager, <i>s</i>, is present and any of the 1282 * following conditions is met: 1283 * 1284 * <ul> 1285 * 1286 * <li> invocation of 1287 * {@link SecurityManager#checkMemberAccess 1288 * s.checkMemberAccess(this, Member.PUBLIC)} method 1289 * denies access to the classes within this class 1290 * 1291 * <li> the caller's class loader is not the same as or an 1292 * ancestor of the class loader for the current class and 1293 * invocation of {@link SecurityManager#checkPackageAccess 1294 * s.checkPackageAccess()} denies access to the package 1295 * of this class 1296 * 1297 * </ul> 1298 * 1299 * @since JDK1.1 1300 */ 1301 public Class<?>[] getClasses() { 1302 // be very careful not to change the stack depth of this 1303 // checkMemberAccess call for security reasons 1304 // see java.lang.SecurityManager.checkMemberAccess 1305 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false); 1306 1307 // Privileged so this implementation can look at DECLARED classes, 1308 // something the caller might not have privilege to do. The code here 1309 // is allowed to look at DECLARED classes because (1) it does not hand 1310 // out anything other than public members and (2) public member access 1311 // has already been ok'd by the SecurityManager. 1312 1313 return java.security.AccessController.doPrivileged( 1314 new java.security.PrivilegedAction<Class<?>[]>() { 1315 public Class<?>[] run() { 1316 List<Class<?>> list = new ArrayList<>(); 1317 Class<?> currentClass = Class.this; 1318 while (currentClass != null) { 1319 Class<?>[] members = currentClass.getDeclaredClasses(); 1320 for (int i = 0; i < members.length; i++) { 1321 if (Modifier.isPublic(members[i].getModifiers())) { 1322 list.add(members[i]); 1323 } 1324 } 1335 * the accessible public fields of the class or interface represented by 1336 * this {@code Class} object. The elements in the array returned are 1337 * not sorted and are not in any particular order. This method returns an 1338 * array of length 0 if the class or interface has no accessible public 1339 * fields, or if it represents an array class, a primitive type, or void. 1340 * 1341 * <p> Specifically, if this {@code Class} object represents a class, 1342 * this method returns the public fields of this class and of all its 1343 * superclasses. If this {@code Class} object represents an 1344 * interface, this method returns the fields of this interface and of all 1345 * its superinterfaces. 1346 * 1347 * <p> The implicit length field for array class is not reflected by this 1348 * method. User code should use the methods of class {@code Array} to 1349 * manipulate arrays. 1350 * 1351 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1352 * 1353 * @return the array of {@code Field} objects representing the 1354 * public fields 1355 * @exception SecurityException 1356 * If a security manager, <i>s</i>, is present and any of the 1357 * following conditions is met: 1358 * 1359 * <ul> 1360 * 1361 * <li> invocation of 1362 * {@link SecurityManager#checkMemberAccess 1363 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1364 * access to the fields within this class 1365 * 1366 * <li> the caller's class loader is not the same as or an 1367 * ancestor of the class loader for the current class and 1368 * invocation of {@link SecurityManager#checkPackageAccess 1369 * s.checkPackageAccess()} denies access to the package 1370 * of this class 1371 * 1372 * </ul> 1373 * 1374 * @since JDK1.1 1375 */ 1376 public Field[] getFields() throws SecurityException { 1377 // be very careful not to change the stack depth of this 1378 // checkMemberAccess call for security reasons 1379 // see java.lang.SecurityManager.checkMemberAccess 1380 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1381 return copyFields(privateGetPublicFields(null)); 1382 } 1383 1384 1385 /** 1386 * Returns an array containing {@code Method} objects reflecting all 1387 * the public <em>member</em> methods of the class or interface represented 1388 * by this {@code Class} object, including those declared by the class 1389 * or interface and those inherited from superclasses and 1390 * superinterfaces. Array classes return all the (public) member methods 1391 * inherited from the {@code Object} class. The elements in the array 1392 * returned are not sorted and are not in any particular order. This 1393 * method returns an array of length 0 if this {@code Class} object 1394 * represents a class or interface that has no public member methods, or if 1395 * this {@code Class} object represents a primitive type or void. 1396 * 1397 * <p> The class initialization method {@code <clinit>} is not 1398 * included in the returned array. If the class declares multiple public 1399 * member methods with the same parameter types, they are all included in 1400 * the returned array. 1401 * 1402 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1403 * 1404 * @return the array of {@code Method} objects representing the 1405 * public methods of this class 1406 * @exception SecurityException 1407 * If a security manager, <i>s</i>, is present and any of the 1408 * following conditions is met: 1409 * 1410 * <ul> 1411 * 1412 * <li> invocation of 1413 * {@link SecurityManager#checkMemberAccess 1414 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1415 * access to the methods within this class 1416 * 1417 * <li> the caller's class loader is not the same as or an 1418 * ancestor of the class loader for the current class and 1419 * invocation of {@link SecurityManager#checkPackageAccess 1420 * s.checkPackageAccess()} denies access to the package 1421 * of this class 1422 * 1423 * </ul> 1424 * 1425 * @since JDK1.1 1426 */ 1427 public Method[] getMethods() throws SecurityException { 1428 // be very careful not to change the stack depth of this 1429 // checkMemberAccess call for security reasons 1430 // see java.lang.SecurityManager.checkMemberAccess 1431 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1432 return copyMethods(privateGetPublicMethods()); 1433 } 1434 1435 1436 /** 1437 * Returns an array containing {@code Constructor} objects reflecting 1438 * all the public constructors of the class represented by this 1439 * {@code Class} object. An array of length 0 is returned if the 1440 * class has no public constructors, or if the class is an array class, or 1441 * if the class reflects a primitive type or void. 1442 * 1443 * Note that while this method returns an array of {@code 1444 * Constructor<T>} objects (that is an array of constructors from 1445 * this class), the return type of this method is {@code 1446 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 1447 * might be expected. This less informative return type is 1448 * necessary since after being returned from this method, the 1449 * array could be modified to hold {@code Constructor} objects for 1450 * different classes, which would violate the type guarantees of 1451 * {@code Constructor<T>[]}. 1452 * 1453 * @return the array of {@code Constructor} objects representing the 1454 * public constructors of this class 1455 * @exception SecurityException 1456 * If a security manager, <i>s</i>, is present and any of the 1457 * following conditions is met: 1458 * 1459 * <ul> 1460 * 1461 * <li> invocation of 1462 * {@link SecurityManager#checkMemberAccess 1463 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1464 * access to the constructors within this class 1465 * 1466 * <li> the caller's class loader is not the same as or an 1467 * ancestor of the class loader for the current class and 1468 * invocation of {@link SecurityManager#checkPackageAccess 1469 * s.checkPackageAccess()} denies access to the package 1470 * of this class 1471 * 1472 * </ul> 1473 * 1474 * @since JDK1.1 1475 */ 1476 public Constructor<?>[] getConstructors() throws SecurityException { 1477 // be very careful not to change the stack depth of this 1478 // checkMemberAccess call for security reasons 1479 // see java.lang.SecurityManager.checkMemberAccess 1480 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1481 return copyConstructors(privateGetDeclaredConstructors(true)); 1482 } 1483 1484 1485 /** 1486 * Returns a {@code Field} object that reflects the specified public 1487 * member field of the class or interface represented by this 1488 * {@code Class} object. The {@code name} parameter is a 1489 * {@code String} specifying the simple name of the desired field. 1490 * 1491 * <p> The field to be reflected is determined by the algorithm that 1492 * follows. Let C be the class represented by this object: 1493 * <OL> 1494 * <LI> If C declares a public field with the name specified, that is the 1495 * field to be reflected.</LI> 1496 * <LI> If no field was found in step 1 above, this algorithm is applied 1497 * recursively to each direct superinterface of C. The direct 1498 * superinterfaces are searched in the order they were declared.</LI> 1499 * <LI> If no field was found in steps 1 and 2 above, and C has a 1500 * superclass S, then this algorithm is invoked recursively upon S. 1501 * If C has no superclass, then a {@code NoSuchFieldException} 1502 * is thrown.</LI> 1503 * </OL> 1504 * 1505 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1506 * 1507 * @param name the field name 1508 * @return the {@code Field} object of this class specified by 1509 * {@code name} 1510 * @exception NoSuchFieldException if a field with the specified name is 1511 * not found. 1512 * @exception NullPointerException if {@code name} is {@code null} 1513 * @exception SecurityException 1514 * If a security manager, <i>s</i>, is present and any of the 1515 * following conditions is met: 1516 * 1517 * <ul> 1518 * 1519 * <li> invocation of 1520 * {@link SecurityManager#checkMemberAccess 1521 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1522 * access to the field 1523 * 1524 * <li> the caller's class loader is not the same as or an 1525 * ancestor of the class loader for the current class and 1526 * invocation of {@link SecurityManager#checkPackageAccess 1527 * s.checkPackageAccess()} denies access to the package 1528 * of this class 1529 * 1530 * </ul> 1531 * 1532 * @since JDK1.1 1533 */ 1534 public Field getField(String name) 1535 throws NoSuchFieldException, SecurityException { 1536 // be very careful not to change the stack depth of this 1537 // checkMemberAccess call for security reasons 1538 // see java.lang.SecurityManager.checkMemberAccess 1539 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1540 Field field = getField0(name); 1541 if (field == null) { 1542 throw new NoSuchFieldException(name); 1543 } 1544 return field; 1545 } 1546 1547 1548 /** 1549 * Returns a {@code Method} object that reflects the specified public 1550 * member method of the class or interface represented by this 1551 * {@code Class} object. The {@code name} parameter is a 1552 * {@code String} specifying the simple name of the desired method. The 1553 * {@code parameterTypes} parameter is an array of {@code Class} 1554 * objects that identify the method's formal parameter types, in declared 1555 * order. If {@code parameterTypes} is {@code null}, it is 1556 * treated as if it were an empty array. 1557 * 1558 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 1575 * more specific than any of the others, that method is reflected; 1576 * otherwise one of the methods is chosen arbitrarily. 1577 * 1578 * <p>Note that there may be more than one matching method in a 1579 * class because while the Java language forbids a class to 1580 * declare multiple methods with the same signature but different 1581 * return types, the Java virtual machine does not. This 1582 * increased flexibility in the virtual machine can be used to 1583 * implement various language features. For example, covariant 1584 * returns can be implemented with {@linkplain 1585 * java.lang.reflect.Method#isBridge bridge methods}; the bridge 1586 * method and the method being overridden would have the same 1587 * signature but different return types. 1588 * 1589 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1590 * 1591 * @param name the name of the method 1592 * @param parameterTypes the list of parameters 1593 * @return the {@code Method} object that matches the specified 1594 * {@code name} and {@code parameterTypes} 1595 * @exception NoSuchMethodException if a matching method is not found 1596 * or if the name is "<init>"or "<clinit>". 1597 * @exception NullPointerException if {@code name} is {@code null} 1598 * @exception SecurityException 1599 * If a security manager, <i>s</i>, is present and any of the 1600 * following conditions is met: 1601 * 1602 * <ul> 1603 * 1604 * <li> invocation of 1605 * {@link SecurityManager#checkMemberAccess 1606 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1607 * access to the method 1608 * 1609 * <li> the caller's class loader is not the same as or an 1610 * ancestor of the class loader for the current class and 1611 * invocation of {@link SecurityManager#checkPackageAccess 1612 * s.checkPackageAccess()} denies access to the package 1613 * of this class 1614 * 1615 * </ul> 1616 * 1617 * @since JDK1.1 1618 */ 1619 public Method getMethod(String name, Class<?>... parameterTypes) 1620 throws NoSuchMethodException, SecurityException { 1621 // be very careful not to change the stack depth of this 1622 // checkMemberAccess call for security reasons 1623 // see java.lang.SecurityManager.checkMemberAccess 1624 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1625 Method method = getMethod0(name, parameterTypes); 1626 if (method == null) { 1627 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 1628 } 1629 return method; 1630 } 1631 1632 1633 /** 1634 * Returns a {@code Constructor} object that reflects the specified 1635 * public constructor of the class represented by this {@code Class} 1636 * object. The {@code parameterTypes} parameter is an array of 1637 * {@code Class} objects that identify the constructor's formal 1638 * parameter types, in declared order. 1639 * 1640 * If this {@code Class} object represents an inner class 1641 * declared in a non-static context, the formal parameter types 1642 * include the explicit enclosing instance as the first parameter. 1643 * 1644 * <p> The constructor to reflect is the public constructor of the class 1645 * represented by this {@code Class} object whose formal parameter 1646 * types match those specified by {@code parameterTypes}. 1647 * 1648 * @param parameterTypes the parameter array 1649 * @return the {@code Constructor} object of the public constructor that 1650 * matches the specified {@code parameterTypes} 1651 * @exception NoSuchMethodException if a matching method is not found. 1652 * @exception SecurityException 1653 * If a security manager, <i>s</i>, is present and any of the 1654 * following conditions is met: 1655 * 1656 * <ul> 1657 * 1658 * <li> invocation of 1659 * {@link SecurityManager#checkMemberAccess 1660 * s.checkMemberAccess(this, Member.PUBLIC)} denies 1661 * access to the constructor 1662 * 1663 * <li> the caller's class loader is not the same as or an 1664 * ancestor of the class loader for the current class and 1665 * invocation of {@link SecurityManager#checkPackageAccess 1666 * s.checkPackageAccess()} denies access to the package 1667 * of this class 1668 * 1669 * </ul> 1670 * 1671 * @since JDK1.1 1672 */ 1673 public Constructor<T> getConstructor(Class<?>... parameterTypes) 1674 throws NoSuchMethodException, SecurityException { 1675 // be very careful not to change the stack depth of this 1676 // checkMemberAccess call for security reasons 1677 // see java.lang.SecurityManager.checkMemberAccess 1678 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1679 return getConstructor0(parameterTypes, Member.PUBLIC); 1680 } 1681 1682 1683 /** 1684 * Returns an array of {@code Class} objects reflecting all the 1685 * classes and interfaces declared as members of the class represented by 1686 * this {@code Class} object. This includes public, protected, default 1687 * (package) access, and private classes and interfaces declared by the 1688 * class, but excludes inherited classes and interfaces. This method 1689 * returns an array of length 0 if the class declares no classes or 1690 * interfaces as members, or if this {@code Class} object represents a 1691 * primitive type, an array class, or void. 1692 * 1693 * @return the array of {@code Class} objects representing all the 1694 * declared members of this class 1695 * @exception SecurityException 1696 * If a security manager, <i>s</i>, is present and any of the 1697 * following conditions is met: 1698 * 1699 * <ul> 1700 * 1701 * <li> invocation of 1702 * {@link SecurityManager#checkMemberAccess 1703 * s.checkMemberAccess(this, Member.DECLARED)} denies 1704 * access to the declared classes within this class 1705 * 1706 * <li> the caller's class loader is not the same as or an 1707 * ancestor of the class loader for the current class and 1708 * invocation of {@link SecurityManager#checkPackageAccess 1709 * s.checkPackageAccess()} denies access to the package 1710 * of this class 1711 * 1712 * </ul> 1713 * 1714 * @since JDK1.1 1715 */ 1716 public Class<?>[] getDeclaredClasses() throws SecurityException { 1717 // be very careful not to change the stack depth of this 1718 // checkMemberAccess call for security reasons 1719 // see java.lang.SecurityManager.checkMemberAccess 1720 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), false); 1721 return getDeclaredClasses0(); 1722 } 1723 1724 1725 /** 1726 * Returns an array of {@code Field} objects reflecting all the fields 1727 * declared by the class or interface represented by this 1728 * {@code Class} object. This includes public, protected, default 1729 * (package) access, and private fields, but excludes inherited fields. 1730 * The elements in the array returned are not sorted and are not in any 1731 * particular order. This method returns an array of length 0 if the class 1732 * or interface declares no fields, or if this {@code Class} object 1733 * represents a primitive type, an array class, or void. 1734 * 1735 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1736 * 1737 * @return the array of {@code Field} objects representing all the 1738 * declared fields of this class 1739 * @exception SecurityException 1740 * If a security manager, <i>s</i>, is present and any of the 1741 * following conditions is met: 1742 * 1743 * <ul> 1744 * 1745 * <li> invocation of 1746 * {@link SecurityManager#checkMemberAccess 1747 * s.checkMemberAccess(this, Member.DECLARED)} denies 1748 * access to the declared fields within this class 1749 * 1750 * <li> the caller's class loader is not the same as or an 1751 * ancestor of the class loader for the current class and 1752 * invocation of {@link SecurityManager#checkPackageAccess 1753 * s.checkPackageAccess()} denies access to the package 1754 * of this class 1755 * 1756 * </ul> 1757 * 1758 * @since JDK1.1 1759 */ 1760 public Field[] getDeclaredFields() throws SecurityException { 1761 // be very careful not to change the stack depth of this 1762 // checkMemberAccess call for security reasons 1763 // see java.lang.SecurityManager.checkMemberAccess 1764 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1765 return copyFields(privateGetDeclaredFields(false)); 1766 } 1767 1768 1769 /** 1770 * Returns an array of {@code Method} objects reflecting all the 1771 * methods declared by the class or interface represented by this 1772 * {@code Class} object. This includes public, protected, default 1773 * (package) access, and private methods, but excludes inherited methods. 1774 * The elements in the array returned are not sorted and are not in any 1775 * particular order. This method returns an array of length 0 if the class 1776 * or interface declares no methods, or if this {@code Class} object 1777 * represents a primitive type, an array class, or void. The class 1778 * initialization method {@code <clinit>} is not included in the 1779 * returned array. If the class declares multiple public member methods 1780 * with the same parameter types, they are all included in the returned 1781 * array. 1782 * 1783 * <p> See <em>The Java Language Specification</em>, section 8.2. 1784 * 1785 * @return the array of {@code Method} objects representing all the 1786 * declared methods of this class 1787 * @exception SecurityException 1788 * If a security manager, <i>s</i>, is present and any of the 1789 * following conditions is met: 1790 * 1791 * <ul> 1792 * 1793 * <li> invocation of 1794 * {@link SecurityManager#checkMemberAccess 1795 * s.checkMemberAccess(this, Member.DECLARED)} denies 1796 * access to the declared methods within this class 1797 * 1798 * <li> the caller's class loader is not the same as or an 1799 * ancestor of the class loader for the current class and 1800 * invocation of {@link SecurityManager#checkPackageAccess 1801 * s.checkPackageAccess()} denies access to the package 1802 * of this class 1803 * 1804 * </ul> 1805 * 1806 * @since JDK1.1 1807 */ 1808 public Method[] getDeclaredMethods() throws SecurityException { 1809 // be very careful not to change the stack depth of this 1810 // checkMemberAccess call for security reasons 1811 // see java.lang.SecurityManager.checkMemberAccess 1812 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1813 return copyMethods(privateGetDeclaredMethods(false)); 1814 } 1815 1816 1817 /** 1818 * Returns an array of {@code Constructor} objects reflecting all the 1819 * constructors declared by the class represented by this 1820 * {@code Class} object. These are public, protected, default 1821 * (package) access, and private constructors. The elements in the array 1822 * returned are not sorted and are not in any particular order. If the 1823 * class has a default constructor, it is included in the returned array. 1824 * This method returns an array of length 0 if this {@code Class} 1825 * object represents an interface, a primitive type, an array class, or 1826 * void. 1827 * 1828 * <p> See <em>The Java Language Specification</em>, section 8.2. 1829 * 1830 * @return the array of {@code Constructor} objects representing all the 1831 * declared constructors of this class 1832 * @exception 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> invocation of 1839 * {@link SecurityManager#checkMemberAccess 1840 * s.checkMemberAccess(this, Member.DECLARED)} denies 1841 * access to the declared constructors within this class 1842 * 1843 * <li> the caller's class loader is not the same as or an 1844 * ancestor of the class loader for the current class and 1845 * invocation of {@link SecurityManager#checkPackageAccess 1846 * s.checkPackageAccess()} denies access to the package 1847 * of this class 1848 * 1849 * </ul> 1850 * 1851 * @since JDK1.1 1852 */ 1853 public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 1854 // be very careful not to change the stack depth of this 1855 // checkMemberAccess call for security reasons 1856 // see java.lang.SecurityManager.checkMemberAccess 1857 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1858 return copyConstructors(privateGetDeclaredConstructors(false)); 1859 } 1860 1861 1862 /** 1863 * Returns a {@code Field} object that reflects the specified declared 1864 * field of the class or interface represented by this {@code Class} 1865 * object. The {@code name} parameter is a {@code String} that 1866 * specifies the simple name of the desired field. Note that this method 1867 * will not reflect the {@code length} field of an array class. 1868 * 1869 * @param name the name of the field 1870 * @return the {@code Field} object for the specified field in this 1871 * class 1872 * @exception NoSuchFieldException if a field with the specified name is 1873 * not found. 1874 * @exception NullPointerException if {@code name} is {@code null} 1875 * @exception SecurityException 1876 * If a security manager, <i>s</i>, is present and any of the 1877 * following conditions is met: 1878 * 1879 * <ul> 1880 * 1881 * <li> invocation of 1882 * {@link SecurityManager#checkMemberAccess 1883 * s.checkMemberAccess(this, Member.DECLARED)} denies 1884 * access to the declared field 1885 * 1886 * <li> the caller's class loader is not the same as or an 1887 * ancestor of the class loader for the current class and 1888 * invocation of {@link SecurityManager#checkPackageAccess 1889 * s.checkPackageAccess()} denies access to the package 1890 * of this class 1891 * 1892 * </ul> 1893 * 1894 * @since JDK1.1 1895 */ 1896 public Field getDeclaredField(String name) 1897 throws NoSuchFieldException, SecurityException { 1898 // be very careful not to change the stack depth of this 1899 // checkMemberAccess call for security reasons 1900 // see java.lang.SecurityManager.checkMemberAccess 1901 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1902 Field field = searchFields(privateGetDeclaredFields(false), name); 1903 if (field == null) { 1904 throw new NoSuchFieldException(name); 1905 } 1906 return field; 1907 } 1908 1909 1910 /** 1911 * Returns a {@code Method} object that reflects the specified 1912 * declared method of the class or interface represented by this 1913 * {@code Class} object. The {@code name} parameter is a 1914 * {@code String} that specifies the simple name of the desired 1915 * method, and the {@code parameterTypes} parameter is an array of 1916 * {@code Class} objects that identify the method's formal parameter 1917 * types, in declared order. If more than one method with the same 1918 * parameter types is declared in a class, and one of these methods has a 1919 * return type that is more specific than any of the others, that method is 1920 * returned; otherwise one of the methods is chosen arbitrarily. If the 1921 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 1922 * is raised. 1923 * 1924 * @param name the name of the method 1925 * @param parameterTypes the parameter array 1926 * @return the {@code Method} object for the method of this class 1927 * matching the specified name and parameters 1928 * @exception NoSuchMethodException if a matching method is not found. 1929 * @exception NullPointerException if {@code name} is {@code null} 1930 * @exception SecurityException 1931 * If a security manager, <i>s</i>, is present and any of the 1932 * following conditions is met: 1933 * 1934 * <ul> 1935 * 1936 * <li> invocation of 1937 * {@link SecurityManager#checkMemberAccess 1938 * s.checkMemberAccess(this, Member.DECLARED)} denies 1939 * access to the declared method 1940 * 1941 * <li> the caller's class loader is not the same as or an 1942 * ancestor of the class loader for the current class and 1943 * invocation of {@link SecurityManager#checkPackageAccess 1944 * s.checkPackageAccess()} denies access to the package 1945 * of this class 1946 * 1947 * </ul> 1948 * 1949 * @since JDK1.1 1950 */ 1951 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 1952 throws NoSuchMethodException, SecurityException { 1953 // be very careful not to change the stack depth of this 1954 // checkMemberAccess call for security reasons 1955 // see java.lang.SecurityManager.checkMemberAccess 1956 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1957 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); 1958 if (method == null) { 1959 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 1960 } 1961 return method; 1962 } 1963 1964 1965 /** 1966 * Returns a {@code Constructor} object that reflects the specified 1967 * constructor of the class or interface represented by this 1968 * {@code Class} object. The {@code parameterTypes} parameter is 1969 * an array of {@code Class} objects that identify the constructor's 1970 * formal parameter types, in declared order. 1971 * 1972 * If this {@code Class} object represents an inner class 1973 * declared in a non-static context, the formal parameter types 1974 * include the explicit enclosing instance as the first parameter. 1975 * 1976 * @param parameterTypes the parameter array 1977 * @return The {@code Constructor} object for the constructor with the 1978 * specified parameter list 1979 * @exception NoSuchMethodException if a matching method is not found. 1980 * @exception SecurityException 1981 * If a security manager, <i>s</i>, is present and any of the 1982 * following conditions is met: 1983 * 1984 * <ul> 1985 * 1986 * <li> invocation of 1987 * {@link SecurityManager#checkMemberAccess 1988 * s.checkMemberAccess(this, Member.DECLARED)} denies 1989 * access to the declared constructor 1990 * 1991 * <li> the caller's class loader is not the same as or an 1992 * ancestor of the class loader for the current class and 1993 * invocation of {@link SecurityManager#checkPackageAccess 1994 * s.checkPackageAccess()} denies access to the package 1995 * of this class 1996 * 1997 * </ul> 1998 * 1999 * @since JDK1.1 2000 */ 2001 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 2002 throws NoSuchMethodException, SecurityException { 2003 // be very careful not to change the stack depth of this 2004 // checkMemberAccess call for security reasons 2005 // see java.lang.SecurityManager.checkMemberAccess 2006 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 2007 return getConstructor0(parameterTypes, Member.DECLARED); 2008 } 2009 2010 /** 2011 * Finds a resource with a given name. The rules for searching resources 2012 * associated with a given class are implemented by the defining 2013 * {@linkplain ClassLoader class loader} of the class. This method 2014 * delegates to this object's class loader. If this object was loaded by 2015 * the bootstrap class loader, the method delegates to {@link 2016 * ClassLoader#getSystemResourceAsStream}. 2017 * 2018 * <p> Before delegation, an absolute resource name is constructed from the 2019 * given resource name using this algorithm: 2020 * 2021 * <ul> 2022 * 2023 * <li> If the {@code name} begins with a {@code '/'} 2024 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 2025 * portion of the {@code name} following the {@code '/'}. 2149 2150 2151 /** 2152 * Set the ProtectionDomain for this class. Called by 2153 * ClassLoader.defineClass. 2154 */ 2155 native void setProtectionDomain0(java.security.ProtectionDomain pd); 2156 2157 2158 /* 2159 * Return the Virtual Machine's Class object for the named 2160 * primitive type. 2161 */ 2162 static native Class<?> getPrimitiveClass(String name); 2163 2164 2165 /* 2166 * Check if client is allowed to access members. If access is denied, 2167 * throw a SecurityException. 2168 * 2169 * Be very careful not to change the stack depth of this checkMemberAccess 2170 * call for security reasons. 2171 * See java.lang.SecurityManager.checkMemberAccess. 2172 * 2173 * <p> Default policy: allow all clients access with normal Java access 2174 * control. 2175 */ 2176 private void checkMemberAccess(int which, ClassLoader ccl, boolean checkProxyInterfaces) { 2177 SecurityManager s = System.getSecurityManager(); 2178 if (s != null) { 2179 s.checkMemberAccess(this, which); 2180 ClassLoader cl = getClassLoader0(); 2181 if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) { 2182 String name = this.getName(); 2183 int i = name.lastIndexOf('.'); 2184 if (i != -1) { 2185 // skip the package access check on a proxy class in default proxy package 2186 String pkg = name.substring(0, i); 2187 if (!Proxy.isProxyClass(this) || !pkg.equals(ReflectUtil.PROXY_PACKAGE)) { 2188 s.checkPackageAccess(pkg); 2189 } 2190 } 2191 } 2192 // check package access on the proxy interfaces 2193 if (checkProxyInterfaces && Proxy.isProxyClass(this)) { 2194 ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces()); 2195 } 2196 } 2197 } 2198 2199 /** | 272 throws ClassNotFoundException; 273 274 /** 275 * Creates a new instance of the class represented by this {@code Class} 276 * object. The class is instantiated as if by a {@code new} 277 * expression with an empty argument list. The class is initialized if it 278 * has not already been initialized. 279 * 280 * <p>Note that this method propagates any exception thrown by the 281 * nullary constructor, including a checked exception. Use of 282 * this method effectively bypasses the compile-time exception 283 * checking that would otherwise be performed by the compiler. 284 * The {@link 285 * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 286 * Constructor.newInstance} method avoids this problem by wrapping 287 * any exception thrown by the constructor in a (checked) {@link 288 * java.lang.reflect.InvocationTargetException}. 289 * 290 * @return a newly allocated instance of the class represented by this 291 * object. 292 * @throws IllegalAccessException if the class or its nullary 293 * constructor is not accessible. 294 * @throws InstantiationException 295 * if this {@code Class} represents an abstract class, 296 * an interface, an array class, a primitive type, or void; 297 * or if the class has no nullary constructor; 298 * or if the instantiation fails for some other reason. 299 * @throws ExceptionInInitializerError if the initialization 300 * provoked by this method fails. 301 * @throws SecurityException 302 * If a security manager, <i>s</i>, is present and 303 * the caller's class loader is not the same as or an 304 * ancestor of the class loader for the current class and 305 * invocation of {@link SecurityManager#checkPackageAccess 306 * s.checkPackageAccess()} denies access to the package 307 * of this class. 308 */ 309 public T newInstance() 310 throws InstantiationException, IllegalAccessException 311 { 312 if (System.getSecurityManager() != null) { 313 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false); 314 } 315 return newInstance0(); 316 } 317 318 private T newInstance0() 319 throws InstantiationException, IllegalAccessException 320 { 321 // NOTE: the following code may not be strictly correct under 322 // the current Java memory model. 323 324 // Constructor lookup 325 if (cachedConstructor == null) { 326 if (this == Class.class) { 327 throw new IllegalAccessException( 1248 private boolean isLocalOrAnonymousClass() { 1249 // JVM Spec 4.8.6: A class must have an EnclosingMethod 1250 // attribute if and only if it is a local class or an 1251 // anonymous class. 1252 return getEnclosingMethodInfo() != null; 1253 } 1254 1255 /** 1256 * Returns an array containing {@code Class} objects representing all 1257 * the public classes and interfaces that are members of the class 1258 * represented by this {@code Class} object. This includes public 1259 * class and interface members inherited from superclasses and public class 1260 * and interface members declared by the class. This method returns an 1261 * array of length 0 if this {@code Class} object has no public member 1262 * classes or interfaces. This method also returns an array of length 0 if 1263 * this {@code Class} object represents a primitive type, an array 1264 * class, or void. 1265 * 1266 * @return the array of {@code Class} objects representing the public 1267 * members of this class 1268 * @throws SecurityException 1269 * If a security manager, <i>s</i>, is present and 1270 * the caller's class loader is not the same as or an 1271 * ancestor of the class loader for the current class and 1272 * invocation of {@link SecurityManager#checkPackageAccess 1273 * s.checkPackageAccess()} denies access to the package 1274 * of this class. 1275 * 1276 * @since JDK1.1 1277 */ 1278 public Class<?>[] getClasses() { 1279 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false); 1280 1281 // Privileged so this implementation can look at DECLARED classes, 1282 // something the caller might not have privilege to do. The code here 1283 // is allowed to look at DECLARED classes because (1) it does not hand 1284 // out anything other than public members and (2) public member access 1285 // has already been ok'd by the SecurityManager. 1286 1287 return java.security.AccessController.doPrivileged( 1288 new java.security.PrivilegedAction<Class<?>[]>() { 1289 public Class<?>[] run() { 1290 List<Class<?>> list = new ArrayList<>(); 1291 Class<?> currentClass = Class.this; 1292 while (currentClass != null) { 1293 Class<?>[] members = currentClass.getDeclaredClasses(); 1294 for (int i = 0; i < members.length; i++) { 1295 if (Modifier.isPublic(members[i].getModifiers())) { 1296 list.add(members[i]); 1297 } 1298 } 1309 * the accessible public fields of the class or interface represented by 1310 * this {@code Class} object. The elements in the array returned are 1311 * not sorted and are not in any particular order. This method returns an 1312 * array of length 0 if the class or interface has no accessible public 1313 * fields, or if it represents an array class, a primitive type, or void. 1314 * 1315 * <p> Specifically, if this {@code Class} object represents a class, 1316 * this method returns the public fields of this class and of all its 1317 * superclasses. If this {@code Class} object represents an 1318 * interface, this method returns the fields of this interface and of all 1319 * its superinterfaces. 1320 * 1321 * <p> The implicit length field for array class is not reflected by this 1322 * method. User code should use the methods of class {@code Array} to 1323 * manipulate arrays. 1324 * 1325 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1326 * 1327 * @return the array of {@code Field} objects representing the 1328 * public fields 1329 * 1330 * @throws SecurityException 1331 * If a security manager, <i>s</i>, is present and 1332 * the caller's class loader is not the same as or an 1333 * ancestor of the class loader for the current class and 1334 * invocation of {@link SecurityManager#checkPackageAccess 1335 * s.checkPackageAccess()} denies access to the package 1336 * of this class. 1337 * 1338 * @since JDK1.1 1339 */ 1340 public Field[] getFields() throws SecurityException { 1341 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1342 return copyFields(privateGetPublicFields(null)); 1343 } 1344 1345 1346 /** 1347 * Returns an array containing {@code Method} objects reflecting all 1348 * the public <em>member</em> methods of the class or interface represented 1349 * by this {@code Class} object, including those declared by the class 1350 * or interface and those inherited from superclasses and 1351 * superinterfaces. Array classes return all the (public) member methods 1352 * inherited from the {@code Object} class. The elements in the array 1353 * returned are not sorted and are not in any particular order. This 1354 * method returns an array of length 0 if this {@code Class} object 1355 * represents a class or interface that has no public member methods, or if 1356 * this {@code Class} object represents a primitive type or void. 1357 * 1358 * <p> The class initialization method {@code <clinit>} is not 1359 * included in the returned array. If the class declares multiple public 1360 * member methods with the same parameter types, they are all included in 1361 * the returned array. 1362 * 1363 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1364 * 1365 * @return the array of {@code Method} objects representing the 1366 * public methods of this class 1367 * @throws SecurityException 1368 * If a security manager, <i>s</i>, is present and 1369 * the caller's class loader is not the same as or an 1370 * ancestor of the class loader for the current class and 1371 * invocation of {@link SecurityManager#checkPackageAccess 1372 * s.checkPackageAccess()} denies access to the package 1373 * of this class. 1374 * 1375 * @since JDK1.1 1376 */ 1377 public Method[] getMethods() throws SecurityException { 1378 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1379 return copyMethods(privateGetPublicMethods()); 1380 } 1381 1382 1383 /** 1384 * Returns an array containing {@code Constructor} objects reflecting 1385 * all the public constructors of the class represented by this 1386 * {@code Class} object. An array of length 0 is returned if the 1387 * class has no public constructors, or if the class is an array class, or 1388 * if the class reflects a primitive type or void. 1389 * 1390 * Note that while this method returns an array of {@code 1391 * Constructor<T>} objects (that is an array of constructors from 1392 * this class), the return type of this method is {@code 1393 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 1394 * might be expected. This less informative return type is 1395 * necessary since after being returned from this method, the 1396 * array could be modified to hold {@code Constructor} objects for 1397 * different classes, which would violate the type guarantees of 1398 * {@code Constructor<T>[]}. 1399 * 1400 * @return the array of {@code Constructor} objects representing the 1401 * public constructors of this class 1402 * @throws SecurityException 1403 * If a security manager, <i>s</i>, is present and 1404 * the caller's class loader is not the same as or an 1405 * ancestor of the class loader for the current class and 1406 * invocation of {@link SecurityManager#checkPackageAccess 1407 * s.checkPackageAccess()} denies access to the package 1408 * of this class. 1409 * 1410 * @since JDK1.1 1411 */ 1412 public Constructor<?>[] getConstructors() throws SecurityException { 1413 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1414 return copyConstructors(privateGetDeclaredConstructors(true)); 1415 } 1416 1417 1418 /** 1419 * Returns a {@code Field} object that reflects the specified public 1420 * member field of the class or interface represented by this 1421 * {@code Class} object. The {@code name} parameter is a 1422 * {@code String} specifying the simple name of the desired field. 1423 * 1424 * <p> The field to be reflected is determined by the algorithm that 1425 * follows. Let C be the class represented by this object: 1426 * <OL> 1427 * <LI> If C declares a public field with the name specified, that is the 1428 * field to be reflected.</LI> 1429 * <LI> If no field was found in step 1 above, this algorithm is applied 1430 * recursively to each direct superinterface of C. The direct 1431 * superinterfaces are searched in the order they were declared.</LI> 1432 * <LI> If no field was found in steps 1 and 2 above, and C has a 1433 * superclass S, then this algorithm is invoked recursively upon S. 1434 * If C has no superclass, then a {@code NoSuchFieldException} 1435 * is thrown.</LI> 1436 * </OL> 1437 * 1438 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1439 * 1440 * @param name the field name 1441 * @return the {@code Field} object of this class specified by 1442 * {@code name} 1443 * @throws NoSuchFieldException if a field with the specified name is 1444 * not found. 1445 * @throws NullPointerException if {@code name} is {@code null} 1446 * @throws SecurityException 1447 * If a security manager, <i>s</i>, is present and 1448 * the caller's class loader is not the same as or an 1449 * ancestor of the class loader for the current class and 1450 * invocation of {@link SecurityManager#checkPackageAccess 1451 * s.checkPackageAccess()} denies access to the package 1452 * of this class. 1453 * 1454 * @since JDK1.1 1455 */ 1456 public Field getField(String name) 1457 throws NoSuchFieldException, SecurityException { 1458 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1459 Field field = getField0(name); 1460 if (field == null) { 1461 throw new NoSuchFieldException(name); 1462 } 1463 return field; 1464 } 1465 1466 1467 /** 1468 * Returns a {@code Method} object that reflects the specified public 1469 * member method of the class or interface represented by this 1470 * {@code Class} object. The {@code name} parameter is a 1471 * {@code String} specifying the simple name of the desired method. The 1472 * {@code parameterTypes} parameter is an array of {@code Class} 1473 * objects that identify the method's formal parameter types, in declared 1474 * order. If {@code parameterTypes} is {@code null}, it is 1475 * treated as if it were an empty array. 1476 * 1477 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 1494 * more specific than any of the others, that method is reflected; 1495 * otherwise one of the methods is chosen arbitrarily. 1496 * 1497 * <p>Note that there may be more than one matching method in a 1498 * class because while the Java language forbids a class to 1499 * declare multiple methods with the same signature but different 1500 * return types, the Java virtual machine does not. This 1501 * increased flexibility in the virtual machine can be used to 1502 * implement various language features. For example, covariant 1503 * returns can be implemented with {@linkplain 1504 * java.lang.reflect.Method#isBridge bridge methods}; the bridge 1505 * method and the method being overridden would have the same 1506 * signature but different return types. 1507 * 1508 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 1509 * 1510 * @param name the name of the method 1511 * @param parameterTypes the list of parameters 1512 * @return the {@code Method} object that matches the specified 1513 * {@code name} and {@code parameterTypes} 1514 * @throws NoSuchMethodException if a matching method is not found 1515 * or if the name is "<init>"or "<clinit>". 1516 * @throws NullPointerException if {@code name} is {@code null} 1517 * @throws SecurityException 1518 * If a security manager, <i>s</i>, is present and 1519 * 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 * @since JDK1.1 1526 */ 1527 public Method getMethod(String name, Class<?>... parameterTypes) 1528 throws NoSuchMethodException, SecurityException { 1529 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1530 Method method = getMethod0(name, parameterTypes); 1531 if (method == null) { 1532 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 1533 } 1534 return method; 1535 } 1536 1537 1538 /** 1539 * Returns a {@code Constructor} object that reflects the specified 1540 * public constructor of the class represented by this {@code Class} 1541 * object. The {@code parameterTypes} parameter is an array of 1542 * {@code Class} objects that identify the constructor's formal 1543 * parameter types, in declared order. 1544 * 1545 * If this {@code Class} object represents an inner class 1546 * declared in a non-static context, the formal parameter types 1547 * include the explicit enclosing instance as the first parameter. 1548 * 1549 * <p> The constructor to reflect is the public constructor of the class 1550 * represented by this {@code Class} object whose formal parameter 1551 * types match those specified by {@code parameterTypes}. 1552 * 1553 * @param parameterTypes the parameter array 1554 * @return the {@code Constructor} object of the public constructor that 1555 * matches the specified {@code parameterTypes} 1556 * @throws NoSuchMethodException if a matching method is not found. 1557 * @throws SecurityException 1558 * If a security manager, <i>s</i>, is present and 1559 * the caller's class loader is not the same as or an 1560 * ancestor of the class loader for the current class and 1561 * invocation of {@link SecurityManager#checkPackageAccess 1562 * s.checkPackageAccess()} denies access to the package 1563 * of this class. 1564 * 1565 * @since JDK1.1 1566 */ 1567 public Constructor<T> getConstructor(Class<?>... parameterTypes) 1568 throws NoSuchMethodException, SecurityException { 1569 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), true); 1570 return getConstructor0(parameterTypes, Member.PUBLIC); 1571 } 1572 1573 1574 /** 1575 * Returns an array of {@code Class} objects reflecting all the 1576 * classes and interfaces declared as members of the class represented by 1577 * this {@code Class} object. This includes public, protected, default 1578 * (package) access, and private classes and interfaces declared by the 1579 * class, but excludes inherited classes and interfaces. This method 1580 * returns an array of length 0 if the class declares no classes or 1581 * interfaces as members, or if this {@code Class} object represents a 1582 * primitive type, an array class, or void. 1583 * 1584 * @return the array of {@code Class} objects representing all the 1585 * declared members of this class 1586 * @throws SecurityException 1587 * If a security manager, <i>s</i>, is present and any of the 1588 * following conditions is met: 1589 * 1590 * <ul> 1591 * 1592 * <li> the caller's class loader is not the same as the 1593 * class loader of this class and invocation of 1594 * {@link SecurityManager#checkPermission 1595 * s.checkPermission} method with 1596 * {@code RuntimePermission("accessDeclaredMembers")} 1597 * denies access to the declared classes within this class 1598 * 1599 * <li> the caller's class loader is not the same as or an 1600 * ancestor of the class loader for the current class and 1601 * invocation of {@link SecurityManager#checkPackageAccess 1602 * s.checkPackageAccess()} denies access to the package 1603 * of this class 1604 * 1605 * </ul> 1606 * 1607 * @since JDK1.1 1608 */ 1609 public Class<?>[] getDeclaredClasses() throws SecurityException { 1610 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), false); 1611 return getDeclaredClasses0(); 1612 } 1613 1614 1615 /** 1616 * Returns an array of {@code Field} objects reflecting all the fields 1617 * declared by the class or interface represented by this 1618 * {@code Class} object. This includes public, protected, default 1619 * (package) access, and private fields, but excludes inherited fields. 1620 * The elements in the array returned are not sorted and are not in any 1621 * particular order. This method returns an array of length 0 if the class 1622 * or interface declares no fields, or if this {@code Class} object 1623 * represents a primitive type, an array class, or void. 1624 * 1625 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 1626 * 1627 * @return the array of {@code Field} objects representing all the 1628 * declared fields of this class 1629 * @throws SecurityException 1630 * If a security manager, <i>s</i>, is present and any of the 1631 * following conditions is met: 1632 * 1633 * <ul> 1634 * 1635 * <li> the caller's class loader is not the same as the 1636 * class loader of this class and invocation of 1637 * {@link SecurityManager#checkPermission 1638 * s.checkPermission} method with 1639 * {@code RuntimePermission("accessDeclaredMembers")} 1640 * denies access to the declared fields within this class 1641 * 1642 * <li> the caller's class loader is not the same as or an 1643 * ancestor of the class loader for the current class and 1644 * invocation of {@link SecurityManager#checkPackageAccess 1645 * s.checkPackageAccess()} denies access to the package 1646 * of this class 1647 * 1648 * </ul> 1649 * 1650 * @since JDK1.1 1651 */ 1652 public Field[] getDeclaredFields() throws SecurityException { 1653 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1654 return copyFields(privateGetDeclaredFields(false)); 1655 } 1656 1657 1658 /** 1659 * Returns an array of {@code Method} objects reflecting all the 1660 * methods declared by the class or interface represented by this 1661 * {@code Class} object. This includes public, protected, default 1662 * (package) access, and private methods, but excludes inherited methods. 1663 * The elements in the array returned are not sorted and are not in any 1664 * particular order. This method returns an array of length 0 if the class 1665 * or interface declares no methods, or if this {@code Class} object 1666 * represents a primitive type, an array class, or void. The class 1667 * initialization method {@code <clinit>} is not included in the 1668 * returned array. If the class declares multiple public member methods 1669 * with the same parameter types, they are all included in the returned 1670 * array. 1671 * 1672 * <p> See <em>The Java Language Specification</em>, section 8.2. 1673 * 1674 * @return the array of {@code Method} objects representing all the 1675 * declared methods of this class 1676 * @throws SecurityException 1677 * If a security manager, <i>s</i>, is present and any of the 1678 * following conditions is met: 1679 * 1680 * <ul> 1681 * 1682 * <li> the caller's class loader is not the same as the 1683 * class loader of this class and invocation of 1684 * {@link SecurityManager#checkPermission 1685 * s.checkPermission} method with 1686 * {@code RuntimePermission("accessDeclaredMembers")} 1687 * denies access to the declared methods within this class 1688 * 1689 * <li> the caller's class loader is not the same as or an 1690 * ancestor of the class loader for the current class and 1691 * invocation of {@link SecurityManager#checkPackageAccess 1692 * s.checkPackageAccess()} denies access to the package 1693 * of this class 1694 * 1695 * </ul> 1696 * 1697 * @since JDK1.1 1698 */ 1699 public Method[] getDeclaredMethods() throws SecurityException { 1700 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1701 return copyMethods(privateGetDeclaredMethods(false)); 1702 } 1703 1704 1705 /** 1706 * Returns an array of {@code Constructor} objects reflecting all the 1707 * constructors declared by the class represented by this 1708 * {@code Class} object. These are public, protected, default 1709 * (package) access, and private constructors. The elements in the array 1710 * returned are not sorted and are not in any particular order. If the 1711 * class has a default constructor, it is included in the returned array. 1712 * This method returns an array of length 0 if this {@code Class} 1713 * object represents an interface, a primitive type, an array class, or 1714 * void. 1715 * 1716 * <p> See <em>The Java Language Specification</em>, section 8.2. 1717 * 1718 * @return the array of {@code Constructor} objects representing all the 1719 * declared constructors of this class 1720 * @throws SecurityException 1721 * If a security manager, <i>s</i>, is present and any of the 1722 * following conditions is met: 1723 * 1724 * <ul> 1725 * 1726 * <li> the caller's class loader is not the same as the 1727 * class loader of this class and invocation of 1728 * {@link SecurityManager#checkPermission 1729 * s.checkPermission} method with 1730 * {@code RuntimePermission("accessDeclaredMembers")} 1731 * denies access to the declared constructors within this class 1732 * 1733 * <li> the caller's class loader is not the same as or an 1734 * ancestor of the class loader for the current class and 1735 * invocation of {@link SecurityManager#checkPackageAccess 1736 * s.checkPackageAccess()} denies access to the package 1737 * of this class 1738 * 1739 * </ul> 1740 * 1741 * @since JDK1.1 1742 */ 1743 public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 1744 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1745 return copyConstructors(privateGetDeclaredConstructors(false)); 1746 } 1747 1748 1749 /** 1750 * Returns a {@code Field} object that reflects the specified declared 1751 * field of the class or interface represented by this {@code Class} 1752 * object. The {@code name} parameter is a {@code String} that 1753 * specifies the simple name of the desired field. Note that this method 1754 * will not reflect the {@code length} field of an array class. 1755 * 1756 * @param name the name of the field 1757 * @return the {@code Field} object for the specified field in this 1758 * class 1759 * @throws NoSuchFieldException if a field with the specified name is 1760 * not found. 1761 * @throws NullPointerException if {@code name} is {@code null} 1762 * @throws SecurityException 1763 * If a security manager, <i>s</i>, is present and any of the 1764 * following conditions is met: 1765 * 1766 * <ul> 1767 * 1768 * <li> the caller's class loader is not the same as the 1769 * class loader of this class and invocation of 1770 * {@link SecurityManager#checkPermission 1771 * s.checkPermission} method with 1772 * {@code RuntimePermission("accessDeclaredMembers")} 1773 * denies access to the declared field 1774 * 1775 * <li> the caller's class loader is not the same as or an 1776 * ancestor of the class loader for the current class and 1777 * invocation of {@link SecurityManager#checkPackageAccess 1778 * s.checkPackageAccess()} denies access to the package 1779 * of this class 1780 * 1781 * </ul> 1782 * 1783 * @since JDK1.1 1784 */ 1785 public Field getDeclaredField(String name) 1786 throws NoSuchFieldException, SecurityException { 1787 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1788 Field field = searchFields(privateGetDeclaredFields(false), name); 1789 if (field == null) { 1790 throw new NoSuchFieldException(name); 1791 } 1792 return field; 1793 } 1794 1795 1796 /** 1797 * Returns a {@code Method} object that reflects the specified 1798 * declared method of the class or interface represented by this 1799 * {@code Class} object. The {@code name} parameter is a 1800 * {@code String} that specifies the simple name of the desired 1801 * method, and the {@code parameterTypes} parameter is an array of 1802 * {@code Class} objects that identify the method's formal parameter 1803 * types, in declared order. If more than one method with the same 1804 * parameter types is declared in a class, and one of these methods has a 1805 * return type that is more specific than any of the others, that method is 1806 * returned; otherwise one of the methods is chosen arbitrarily. If the 1807 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 1808 * is raised. 1809 * 1810 * @param name the name of the method 1811 * @param parameterTypes the parameter array 1812 * @return the {@code Method} object for the method of this class 1813 * matching the specified name and parameters 1814 * @throws NoSuchMethodException if a matching method is not found. 1815 * @throws NullPointerException if {@code name} is {@code null} 1816 * @throws SecurityException 1817 * If a security manager, <i>s</i>, is present and any of the 1818 * following conditions is met: 1819 * 1820 * <ul> 1821 * 1822 * <li> the caller's class loader is not the same as the 1823 * class loader of this class and invocation of 1824 * {@link SecurityManager#checkPermission 1825 * s.checkPermission} method with 1826 * {@code RuntimePermission("accessDeclaredMembers")} 1827 * denies access to the declared method 1828 * 1829 * <li> the caller's class loader is not the same as or an 1830 * ancestor of the class loader for the current class and 1831 * invocation of {@link SecurityManager#checkPackageAccess 1832 * s.checkPackageAccess()} denies access to the package 1833 * of this class 1834 * 1835 * </ul> 1836 * 1837 * @since JDK1.1 1838 */ 1839 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 1840 throws NoSuchMethodException, SecurityException { 1841 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1842 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); 1843 if (method == null) { 1844 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); 1845 } 1846 return method; 1847 } 1848 1849 1850 /** 1851 * Returns a {@code Constructor} object that reflects the specified 1852 * constructor of the class or interface represented by this 1853 * {@code Class} object. The {@code parameterTypes} parameter is 1854 * an array of {@code Class} objects that identify the constructor's 1855 * formal parameter types, in declared order. 1856 * 1857 * If this {@code Class} object represents an inner class 1858 * declared in a non-static context, the formal parameter types 1859 * include the explicit enclosing instance as the first parameter. 1860 * 1861 * @param parameterTypes the parameter array 1862 * @return The {@code Constructor} object for the constructor with the 1863 * specified parameter list 1864 * @throws NoSuchMethodException if a matching method is not found. 1865 * @throws SecurityException 1866 * If a security manager, <i>s</i>, is present and any of the 1867 * following conditions is met: 1868 * 1869 * <ul> 1870 * 1871 * <li> the caller's class loader is not the same as the 1872 * class loader of this class and invocation of 1873 * {@link SecurityManager#checkPermission 1874 * s.checkPermission} method with 1875 * {@code RuntimePermission("accessDeclaredMembers")} 1876 * denies access to the declared constructor 1877 * 1878 * <li> the caller's class loader is not the same as or an 1879 * ancestor of the class loader for the current class and 1880 * invocation of {@link SecurityManager#checkPackageAccess 1881 * s.checkPackageAccess()} denies access to the package 1882 * of this class 1883 * 1884 * </ul> 1885 * 1886 * @since JDK1.1 1887 */ 1888 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 1889 throws NoSuchMethodException, SecurityException { 1890 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader(), true); 1891 return getConstructor0(parameterTypes, Member.DECLARED); 1892 } 1893 1894 /** 1895 * Finds a resource with a given name. The rules for searching resources 1896 * associated with a given class are implemented by the defining 1897 * {@linkplain ClassLoader class loader} of the class. This method 1898 * delegates to this object's class loader. If this object was loaded by 1899 * the bootstrap class loader, the method delegates to {@link 1900 * ClassLoader#getSystemResourceAsStream}. 1901 * 1902 * <p> Before delegation, an absolute resource name is constructed from the 1903 * given resource name using this algorithm: 1904 * 1905 * <ul> 1906 * 1907 * <li> If the {@code name} begins with a {@code '/'} 1908 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 1909 * portion of the {@code name} following the {@code '/'}. 2033 2034 2035 /** 2036 * Set the ProtectionDomain for this class. Called by 2037 * ClassLoader.defineClass. 2038 */ 2039 native void setProtectionDomain0(java.security.ProtectionDomain pd); 2040 2041 2042 /* 2043 * Return the Virtual Machine's Class object for the named 2044 * primitive type. 2045 */ 2046 static native Class<?> getPrimitiveClass(String name); 2047 2048 2049 /* 2050 * Check if client is allowed to access members. If access is denied, 2051 * throw a SecurityException. 2052 * 2053 * <p> Default policy: allow all clients access with normal Java access 2054 * control. 2055 */ 2056 private void checkMemberAccess(int which, ClassLoader ccl, boolean checkProxyInterfaces) { 2057 SecurityManager s = System.getSecurityManager(); 2058 if (s != null) { 2059 /* Default policy allows access to all {@link Member#PUBLIC} members, 2060 * as well as access to classes that have the same class loader as the caller. 2061 * In all other cases, it requires RuntimePermission("accessDeclaredMembers") 2062 * permission. 2063 */ 2064 if (which != Member.PUBLIC) { 2065 if (ccl != this.getClassLoader()) { 2066 s.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); 2067 } 2068 } 2069 ClassLoader cl = getClassLoader0(); 2070 if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) { 2071 String name = this.getName(); 2072 int i = name.lastIndexOf('.'); 2073 if (i != -1) { 2074 // skip the package access check on a proxy class in default proxy package 2075 String pkg = name.substring(0, i); 2076 if (!Proxy.isProxyClass(this) || !pkg.equals(ReflectUtil.PROXY_PACKAGE)) { 2077 s.checkPackageAccess(pkg); 2078 } 2079 } 2080 } 2081 // check package access on the proxy interfaces 2082 if (checkProxyInterfaces && Proxy.isProxyClass(this)) { 2083 ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces()); 2084 } 2085 } 2086 } 2087 2088 /** |