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:

 



Contents | Prev | Next
Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved.