Contents | Prev | Next | Java Core Reflection |
package java.lang.reflect;
public final class Method extends Object implements Member
A Method
provides information about, and access to, a single method of a class
or an interface. The reflected method may be an abstract
method, a class
(static
) method, or an instance method.
Only the Java Virtual Machine may create Method
objects; user code obtains
Method
references via the methods getMethod
, getMethods
,
getDeclaredMethod
, and getDeclaredMethods
of class Class
.
A Method
permits widening conversions to occur when matching the actual
parameters to invoke
with the underlying method's formal parameters, but it
throws an IllegalArgumentException
if a narrowing conversion would occur.
public Class getDeclaringClass()
Returns the Class
object for the class or interface that declares the method
represented by this Method
object.
public String getName()
Returns the simple name of the method represented by this Method
object.
public int getModifiers()
Returns the Java language modifiers for the method represented by this Method
object, encoded in an integer. The Modifier
class should be used to decode the
modifiers.
The modifier encodings are defined in The Java Virtual Machine Specification, Table 4.4.
public Class getReturnType()
Returns a Class
object that represents the formal return type of the method
represented by this Method
object.
public Class[] getParameterTypes()
Returns an array of Class
objects that represent the formal parameter types, in
declaration order, of the method represented by this Method
object. Returns an
array of length 0
if the underlying method takes no parameters.
public Class[] getExceptionTypes()
Returns an array of Class
objects that represent the types of the checked
exceptions thrown by the underlying method represented by this Method
object. Returns an array of length 0
if the underlying method throws no
checked exceptions.
public boolean equals(Object obj)
Compares this Method
against the specified object. Returns true
if the objects
are the same; false
otherwise. Two Method
objects are the same if they have
the same declaring class, the same name, and the same formal parameter types.
This method overrides the equals
method of class Object
.
public int hashCode()
Returns a hashcode for this Method
. The hashcode is computed as the
exclusive-or of the hashcodes of the Method
's declaring class name and its
simple name.
This method overrides the hashCode
method of class Object
.
public String toString()
Returns a string describing the underlying method represented by this Method
object. The string is formatted as the underlying method's Java language
modifiers, if any, followed by the fully-qualified name of the method return
type or void
, followed by a space, followed by the fully-qualified name of class
declaring the method, followed by a period, followed by the simple method
name, followed by a parenthesized, comma-separated list of the fully-qualified
names of the underlying method's formal parameter types. If the underlying
method throws checked exceptions, the parameter list is followed by a space,
followed by the word throws
followed by a comma-separated list of the fully-
qualified names of the thrown exception types. For example:
public boolean java.lang.Object.equals(java.lang.Object) public final java.lang.String java.lang.Thread.getName()The method modifiers are placed in canonical order as specified by The Java Language Specification. This is
public
, protected
or private
first, and then
other modifiers in the following order: abstract
, static
, final
,
synchronized
, and native
.
This method overrides the toString
method of class Object
.
Invokes the underlying method represented by thispublic Object invoke(Object obj, Object[] args)
throws NullPointerException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException
Method
object on the
specified object with the specified parameters. Individual parameters are
automatically unwrapped to match primitive formal parameters, and both
primitive and reference parameters are subject to widening conversions as
necessary. The value returned by the underlying method is automatically
wrapped in an object if it has a primitive type.
Method invocation proceeds with the following steps, in order:
static)
method, then the specified
object argument is ignored. It may be null
.
null
, the invocation throws a
NullPointerException
. Otherwise, if the specified object argument is not
an instance of the class or interface declaring the underlying method, the
invocation throws an IllegalArgumentException
.
Method
object enforces Java language access control and the
underlying method is inaccessible, the invocation throws an
IllegalAccessException
.
args
is different from the
number of formal parameters required by the underlying method, the
invocation throws an IllegalArgumentException
. If the underlying
method takes no parameters, args
may be null
.
args
array:
null
, the
conversion fails by throwing a NullPointerException
. If the object
parameter is not an instance of a standard Java wrapper class, the
conversion fails by throwing an IllegalArgumentException
.
IllegalArgumentException
.
static
) method, it is invoked as exactly
the underlying method of the relevant declaring class.
static
) method, it is invoked using
dynamic method lookup as documented in The Java Language Specification,
section 15.11.4.4; in particular, overriding based on the class of the target
object will occur.
InvocationTargetException
; invoke
then completes abruptly by throwing
this new exception.
invoke
; if the value has a primitive type, it is first appropriately
wrapped in an object. If the underlying method is declared void
, invoke
returns null
.