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

Print this page




1548     /**
1549      * Returns an array containing {@code Method} objects reflecting all the
1550      * public methods of the class or interface represented by this {@code
1551      * Class} object, including those declared by the class or interface and
1552      * those inherited from superclasses and superinterfaces.
1553      *
1554      * <p> If this {@code Class} object represents a type that has multiple
1555      * public methods with the same name and parameter types, but different
1556      * return types, then the returned array has a {@code Method} object for
1557      * each such method.
1558      *
1559      * <p> If this {@code Class} object represents a type with a class
1560      * initialization method {@code <clinit>}, then the returned array does
1561      * <em>not</em> have a corresponding {@code Method} object.
1562      *
1563      * <p> If this {@code Class} object represents an array type, then the
1564      * returned array has a {@code Method} object for each of the public
1565      * methods inherited by the array type from {@code Object}. It does not
1566      * contain a {@code Method} object for {@code clone()}.
1567      *
1568      * <p> If this {@code Class} object represents a class or interface with no
1569      * public methods, then the returned array has length 0.



1570      *
1571      * <p> If this {@code Class} object represents a primitive type or void,
1572      * then the returned array has length 0.
1573      *
1574      * <p> Static methods declared in superinterfaces of the class or interface
1575      * represented by this {@code Class} object are not considered members of
1576      * the class or interface.
1577      *
1578      * <p> The elements in the returned array are not sorted and are not in any
1579      * particular order.
1580      *
1581      * @return the array of {@code Method} objects representing the
1582      *         public methods of this class
1583      * @throws SecurityException
1584      *         If a security manager, <i>s</i>, is present and
1585      *         the caller's class loader is not the same as or an
1586      *         ancestor of the class loader for the current class and
1587      *         invocation of {@link SecurityManager#checkPackageAccess
1588      *         s.checkPackageAccess()} denies access to the package
1589      *         of this class.


1682         throws NoSuchFieldException, SecurityException {
1683         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1684         Field field = getField0(name);
1685         if (field == null) {
1686             throw new NoSuchFieldException(name);
1687         }
1688         return field;
1689     }
1690 
1691 
1692     /**
1693      * Returns a {@code Method} object that reflects the specified public
1694      * member method of the class or interface represented by this
1695      * {@code Class} object. The {@code name} parameter is a
1696      * {@code String} specifying the simple name of the desired method. The
1697      * {@code parameterTypes} parameter is an array of {@code Class}
1698      * objects that identify the method's formal parameter types, in declared
1699      * order. If {@code parameterTypes} is {@code null}, it is
1700      * treated as if it were an empty array.
1701      *
1702      * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
1703      * {@code NoSuchMethodException} is raised. Otherwise, the method to
1704      * be reflected is determined by the algorithm that follows.  Let C be the
1705      * class represented by this object:
1706      * <OL>
1707      * <LI> C is searched for any <I>matching methods</I>. If no matching
1708      *      method is found, the algorithm of step 1 is invoked recursively on
1709      *      the superclass of C.</LI>
1710      * <LI> If no method was found in step 1 above, the superinterfaces of C
1711      *      are searched for a matching method. If any such method is found, it
1712      *      is reflected.</LI>

1713      * </OL>
1714      *
1715      * To find a matching method in a class C:&nbsp; If C declares exactly one
1716      * public method with the specified name and exactly the same formal
1717      * parameter types, that is the method reflected. If more than one such
1718      * method is found in C, and one of these methods has a return type that is
1719      * more specific than any of the others, that method is reflected;
1720      * otherwise one of the methods is chosen arbitrarily.
1721      *
1722      * <p>Note that there may be more than one matching method in a
1723      * class because while the Java language forbids a class to
1724      * declare multiple methods with the same signature but different
1725      * return types, the Java virtual machine does not.  This
1726      * increased flexibility in the virtual machine can be used to
1727      * implement various language features.  For example, covariant
1728      * returns can be implemented with {@linkplain
1729      * java.lang.reflect.Method#isBridge bridge methods}; the bridge
1730      * method and the method being overridden would have the same
1731      * signature but different return types.
1732      *
1733      * <p> If this {@code Class} object represents an array type, then this
1734      * method does not find the {@code clone()} method.
1735      *
1736      * <p> Static methods declared in superinterfaces of the class or interface




1548     /**
1549      * Returns an array containing {@code Method} objects reflecting all the
1550      * public methods of the class or interface represented by this {@code
1551      * Class} object, including those declared by the class or interface and
1552      * those inherited from superclasses and superinterfaces.
1553      *
1554      * <p> If this {@code Class} object represents a type that has multiple
1555      * public methods with the same name and parameter types, but different
1556      * return types, then the returned array has a {@code Method} object for
1557      * each such method.
1558      *
1559      * <p> If this {@code Class} object represents a type with a class
1560      * initialization method {@code <clinit>}, then the returned array does
1561      * <em>not</em> have a corresponding {@code Method} object.
1562      *
1563      * <p> If this {@code Class} object represents an array type, then the
1564      * returned array has a {@code Method} object for each of the public
1565      * methods inherited by the array type from {@code Object}. It does not
1566      * contain a {@code Method} object for {@code clone()}.
1567      *
1568      * <p> If this {@code Class} object represents an interface then the
1569      * returned array does not contain any implicitly declared methods from
1570      * {@code Object}. If no methods are explicitly declared in this interface
1571      * or a superinterface then the returned array has length 0. (A class
1572      * always has public methods inherited from {@code Object}.)
1573      *
1574      * <p> If this {@code Class} object represents a primitive type or void,
1575      * then the returned array has length 0.
1576      *
1577      * <p> Static methods declared in superinterfaces of the class or interface
1578      * represented by this {@code Class} object are not considered members of
1579      * the class or interface.
1580      *
1581      * <p> The elements in the returned array are not sorted and are not in any
1582      * particular order.
1583      *
1584      * @return the array of {@code Method} objects representing the
1585      *         public methods of this class
1586      * @throws SecurityException
1587      *         If a security manager, <i>s</i>, is present and
1588      *         the caller's class loader is not the same as or an
1589      *         ancestor of the class loader for the current class and
1590      *         invocation of {@link SecurityManager#checkPackageAccess
1591      *         s.checkPackageAccess()} denies access to the package
1592      *         of this class.


1685         throws NoSuchFieldException, SecurityException {
1686         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1687         Field field = getField0(name);
1688         if (field == null) {
1689             throw new NoSuchFieldException(name);
1690         }
1691         return field;
1692     }
1693 
1694 
1695     /**
1696      * Returns a {@code Method} object that reflects the specified public
1697      * member method of the class or interface represented by this
1698      * {@code Class} object. The {@code name} parameter is a
1699      * {@code String} specifying the simple name of the desired method. The
1700      * {@code parameterTypes} parameter is an array of {@code Class}
1701      * objects that identify the method's formal parameter types, in declared
1702      * order. If {@code parameterTypes} is {@code null}, it is
1703      * treated as if it were an empty array.
1704      *
1705      * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
1706      * {@code NoSuchMethodException} is raised. Otherwise, the method to
1707      * be reflected is determined by the algorithm that follows.  Let C be the
1708      * class or interface represented by this object:
1709      * <OL>
1710      * <LI> C is searched for a <I>matching methods</I>, as defined below. If a
1711      *      matching method is found, it is reflected.</LI>
1712      * <LI> If no matching method is found in step 1 and C is a class, then
1713      *      step 1 is invoked recursively on the superclass of C.</LI>
1714      * <LI> If C is an interface, or if no method was found in step 1 or 2
1715      *      above, the superinterfaces of C are searched for a matching
1716      *      method. If any such method is found, it is reflected.</LI>
1717      * </OL>
1718      *
1719      * <p>To find a matching method in a class C:&nbsp; If C declares exactly
1720      * one public method with the specified name and exactly the same formal
1721      * parameter types, that is the method reflected. If more than one such
1722      * method is found in C, and one of these methods has a return type that is
1723      * more specific than any of the others, that method is reflected;
1724      * otherwise one of the methods is chosen arbitrarily.
1725      *
1726      * <p>Note that there may be more than one matching method in a
1727      * class because while the Java language forbids a class to
1728      * declare multiple methods with the same signature but different
1729      * return types, the Java virtual machine does not.  This
1730      * increased flexibility in the virtual machine can be used to
1731      * implement various language features.  For example, covariant
1732      * returns can be implemented with {@linkplain
1733      * java.lang.reflect.Method#isBridge bridge methods}; the bridge
1734      * method and the method being overridden would have the same
1735      * signature but different return types.
1736      *
1737      * <p> If this {@code Class} object represents an array type, then this
1738      * method does not find the {@code clone()} method.
1739      *
1740      * <p> Static methods declared in superinterfaces of the class or interface