|
|||||||||
| Home >> All >> java >> lang >> [ reflect overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.reflect
Class Method

java.lang.Objectjava.lang.reflect.AccessibleObject
java.lang.reflect.Method
- All Implemented Interfaces:
- AnnotatedElement, GenericDeclaration, Member
- public final class Method
- extends AccessibleObject
- implements Member, GenericDeclaration
- extends AccessibleObject
The Method class represents a member method of a class. It also allows dynamic invocation, via reflection. This works for both static and instance methods. Invocation on Method objects knows how to do widening conversions, but throws java.lang.IllegalArgumentException if a narrowing conversion would be necessary. You can query for information on this Method regardless of location, but invocation access may be limited by Java language access controls. If you can't do it in the compiler, you can't normally do it here either.
Note: This class returns and accepts types as Classes, even
primitive types; there are Class types defined that represent each
different primitive type. They are java.lang.Boolean.TYPE,
java.lang.Byte.TYPE,, also available as boolean.class,
byte.class, etc. These are not to be confused with the
classes java.lang.Boolean, java.lang.Byte, etc., which are
real classes.
Also note that this is not a serializable class. It is entirely feasible to make it serializable using the Externalizable interface, but this is on Sun, not me.
- Since:
- 1.1
| Field Summary | |
(package private) java.lang.Class |
declaringClass
|
private static int |
METHOD_MODIFIERS
|
(package private) java.lang.String |
name
|
(package private) int |
slot
|
| Fields inherited from class java.lang.reflect.AccessibleObject |
flag |
| Fields inherited from interface java.lang.reflect.Member |
DECLARED, PUBLIC |
| Constructor Summary | |
private |
Method(java.lang.Class declaringClass,
java.lang.String name,
int slot)
This class is uninstantiable. |
| Method Summary | |
boolean |
equals(java.lang.Object o)
Compare two objects to see if they are semantically equivalent. |
java.lang.Class |
getDeclaringClass()
Gets the class that declared this method, or the class where this method is a non-inherited member. |
java.lang.Class[] |
getExceptionTypes()
Get the exception types this method says it throws, in no particular order. |
Type[] |
getGenericExceptionTypes()
Returns an array of Type objects that represents
the exception types declared by this method, in declaration order. |
Type[] |
getGenericParameterTypes()
Returns an array of Type objects that represents
the parameter list for this method, in declaration order. |
Type |
getGenericReturnType()
Returns the return type of this method. |
int |
getModifiers()
Gets the modifiers this method uses. |
private int |
getModifiersInternal()
Return the raw modifiers for this method. |
java.lang.String |
getName()
Gets the name of this method. |
java.lang.Class[] |
getParameterTypes()
Get the parameter list for this method, in declaration order. |
java.lang.Class |
getReturnType()
Gets the return type of this method. |
private java.lang.String |
getSignature()
Return the String in the Signature attribute for this method. |
TypeVariable[] |
getTypeParameters()
Returns an array of TypeVariable objects that represents
the type variables declared by this constructor, in declaration order. |
int |
hashCode()
Get the hash code for the Method. |
java.lang.Object |
invoke(java.lang.Object o,
java.lang.Object[] args)
Invoke the method. |
private java.lang.Object |
invokeNative(java.lang.Object o,
java.lang.Object[] args,
java.lang.Class declaringClass,
int slot)
|
boolean |
isBridge()
Return true if this method is a bridge method. |
boolean |
isSynthetic()
Return true if this method is synthetic, false otherwise. |
boolean |
isVarArgs()
Return true if this is a varargs method, that is if the method takes a variable number of arguments. |
java.lang.String |
toGenericString()
|
java.lang.String |
toString()
Get a String representation of the Method. |
| Methods inherited from class java.lang.reflect.AccessibleObject |
getAnnotation, getAnnotations, getDeclaredAnnotations, isAccessible, isAnnotationPresent, setAccessible, setAccessible |
| Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
declaringClass
java.lang.Class declaringClass
name
java.lang.String name
slot
int slot
METHOD_MODIFIERS
private static final int METHOD_MODIFIERS
- See Also:
- Constant Field Values
| Constructor Detail |
Method
private Method(java.lang.Class declaringClass, java.lang.String name, int slot)
- This class is uninstantiable.
| Method Detail |
getDeclaringClass
public java.lang.Class getDeclaringClass()
- Gets the class that declared this method, or the class where this method
is a non-inherited member.
- Specified by:
getDeclaringClassin interfaceMember
getName
public java.lang.String getName()
getModifiersInternal
private int getModifiersInternal()
- Return the raw modifiers for this method.
getModifiers
public int getModifiers()
- Gets the modifiers this method uses. Use the
Modifierclass to interpret the values. A method can only have a subset of the following modifiers: public, private, protected, abstract, static, final, synchronized, native, and strictfp.- Specified by:
getModifiersin interfaceMember
isBridge
public boolean isBridge()
- Return true if this method is a bridge method. A bridge method
is generated by the compiler in some situations involving
generics and inheritance.
- Since:
- 1.5
isSynthetic
public boolean isSynthetic()
- Return true if this method is synthetic, false otherwise.
- Specified by:
isSyntheticin interfaceMember
- Since:
- 1.5
isVarArgs
public boolean isVarArgs()
- Return true if this is a varargs method, that is if
the method takes a variable number of arguments.
- Since:
- 1.5
getReturnType
public java.lang.Class getReturnType()
- Gets the return type of this method.
getParameterTypes
public java.lang.Class[] getParameterTypes()
- Get the parameter list for this method, in declaration order. If the
method takes no parameters, returns a 0-length array (not null).
getExceptionTypes
public java.lang.Class[] getExceptionTypes()
- Get the exception types this method says it throws, in no particular
order. If the method has no throws clause, returns a 0-length array
(not null).
equals
public boolean equals(java.lang.Object o)
- Compare two objects to see if they are semantically equivalent.
Two Methods are semantically equivalent if they have the same declaring
class, name, parameter list, and return type.
hashCode
public int hashCode()
- Get the hash code for the Method. The Method hash code is the hash code
of its name XOR'd with the hash code of its class name.
toString
public java.lang.String toString()
- Get a String representation of the Method. A Method's String
representation is "<modifiers> <returntype>
<methodname>(<paramtypes>) throws <exceptions>", where
everything after ')' is omitted if there are no exceptions.
Example:public static int run(java.lang.Runnable,int)
toGenericString
public java.lang.String toGenericString()
invoke
public java.lang.Object invoke(java.lang.Object o, java.lang.Object[] args) throws java.lang.IllegalAccessException, InvocationTargetException
- Invoke the method. Arguments are automatically unwrapped and widened,
and the result is automatically wrapped, if needed.
If the method is static,
owill be ignored. Otherwise, the method uses dynamic lookup as described in JLS 15.12.4.4. You cannot mimic the behavior of nonvirtual lookup (as in super.foo()). This means you will get aNullPointerExceptionifois null, and anIllegalArgumentExceptionif it is incompatible with the declaring class of the method. If the method takes 0 arguments, you may use null or a 0-length array forargs.Next, if this Method enforces access control, your runtime context is evaluated, and you may have an
IllegalAccessExceptionif you could not acces this method in similar compiled code. If the method is static, and its class is uninitialized, you trigger class initialization, which may end in aExceptionInInitializerError.Finally, the method is invoked. If it completes normally, the return value will be null for a void method, a wrapped object for a primitive return method, or the actual return of an Object method. If it completes abruptly, the exception is wrapped in an
InvocationTargetException.
invokeNative
private java.lang.Object invokeNative(java.lang.Object o, java.lang.Object[] args, java.lang.Class declaringClass, int slot) throws java.lang.IllegalAccessException, InvocationTargetException
getTypeParameters
public TypeVariable[] getTypeParameters()
- Returns an array of
TypeVariableobjects that represents the type variables declared by this constructor, in declaration order. An array of size zero is returned if this class has no type variables.- Specified by:
getTypeParametersin interfaceGenericDeclaration
- Since:
- 1.5
getSignature
private java.lang.String getSignature()
- Return the String in the Signature attribute for this method. If there
is no Signature attribute, return null.
getGenericExceptionTypes
public Type[] getGenericExceptionTypes()
- Returns an array of
Typeobjects that represents the exception types declared by this method, in declaration order. An array of size zero is returned if this method declares no exceptions.- Since:
- 1.5
getGenericParameterTypes
public Type[] getGenericParameterTypes()
- Returns an array of
Typeobjects that represents the parameter list for this method, in declaration order. An array of size zero is returned if this method takes no parameters.- Since:
- 1.5
getGenericReturnType
public Type getGenericReturnType()
- Returns the return type of this method.
- Since:
- 1.5
|
|||||||||
| Home >> All >> java >> lang >> [ reflect overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
JAVADOC