Java Reference
In-Depth Information
zero-length
Annotation
array is provided for that parameter. If the
Method
object represents a method that is itself an element of an annotation,
the
geTDefaultValue
method returns an
Object
representing the default
value of that element. If it is not an annotation element or if there is no
default value, then
null
is returned.
The
Method
class also implements
GenericDeclaration
and so defines the
method
getTypeParameters
which returns an array of
TypeVariable
objects.
If a given
Method
object does not present a generic method, then an
empty array is returned.
You can ask a
Method
object if it is a varargs (variable-argument) method
using the
isVarArgs
method. The method
isBridge
asks if it is a bridge
method (see
Section A.3.1
on page
745
)
.
The most interesting use of a
Method
object is to reflectively invoke it:
public Object
invoke(Object onThis, Object... args)
tHRows Illeg-
alAccessException, IllegalArgumentException, InvocationTargetExcep-
tion
Invokes the method defined by this
Method
object on the ob-
ject
onThis
, setting the parameters of the method from the
values in
args
. For non-static methods the actual type of
onThis
determines the method implementation that is invoked.
For static methods
onThis
is ignored and is traditionally
null
.
The number of
args
values must equal the number of para-
meters for the method, and the types of those values must
all be assignable to those of the method. Otherwise you will
get an
IllegalArgumentException
. Note that for a varargs meth-
od the last parameter is an array that you must fill with the
actual "variable" arguments you want to pass. If you attempt
to invoke a method to which you do not have access, an
Il-
legalAccessException
is thrown. If this method is not a method
of the
onThis
object, an
IllegalArgumentException
is thrown. If
onThis
is
null
and the method is not static, a
NullPointerExcep-
tion
is thrown. If this
Method
object represents a static method