Contents | Prev | Next | Java Core Reflection

# The class java.lang.reflect.Method

        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.

## Methods

### getDeclaringClass

        public Class getDeclaringClass()

Returns the `Class` object for the class or interface that declares the method represented by this `Method` object.

### getName

        public String getName()

Returns the simple name of the method represented by this `Method` object.

### getModifiers

        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.

### getReturnType

        public Class getReturnType()

Returns a `Class` object that represents the formal return type of the method represented by this `Method` object.

### getParameterTypes

        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.

### getExceptionTypes

        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.

### equals

        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`.

### hashCode

        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`.

### toString

        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`.

### invoke

        public Object invoke(Object obj, Object[] args)

            throws NullPointerException, IllegalArgumentException,

                IllegalAccessException, InvocationTargetException

Invokes the underlying method represented by this `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:

- If the underlying method is a class (`static)` method, then the specified object argument is ignored. It may be `null`.
- Otherwise, the underlying method is an abstract method or an instance method. If the specified object argument is `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`.
- If this `Method` object enforces Java language access control and the underlying method is inaccessible, the invocation throws an `IllegalAccessException`.
- If the number of actual parameters supplied via `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`.
- For each actual parameter in the supplied `args` array:
  - If the corresponding formal parameter has a primitive type, an unwrapping conversion is attempted to convert the object parameter value to a value of a primitive type. If the object parameter is `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`.
  - If, after possible unwrapping, the parameter value cannot be converted to the corresponding formal parameter type by an identity conversion or a widening conversion, the invocation throws an `IllegalArgumentException`.
- If the underlying method is a class (`static`) method, it is invoked as exactly the underlying method of the relevant declaring class.
- If the underlying method is not a 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.
- Control transfers to the underlying method.
- If the underlying method completes abruptly by throwing an exception, the exception is caught and placed in a new exception object of class `InvocationTargetException`; `invoke` then completes abruptly by throwing this new exception.
- If the underlying method completes normally, the value it returns is returned by `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`.

Contents | Prev | Next

Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved.