getMethod
public Method getMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException
-
Returns a
Method
object that reflects the specified public member method of the class or interface represented by thisClass
object. Thename
parameter is aString
specifying the simple name the desired method. TheparameterTypes
parameter is an array ofClass
objects that identify the method's formal parameter types, in declared order. IfparameterTypes
isnull
, it is treated as if it were an empty array.If the
name
is "<init>"or "<clinit>" aNoSuchMethodException
is raised. Otherwise, the method to be reflected is determined by the algorithm that follows. Let C be the class represented by this object:返回一个反射的public方法,该方法是由类对象确定的一个类或者是借口。name参数是一个String类型制定一个simple name指向上边的方法。
parameterTypes参数是一个class对象的数组,该数组指定该方法的参数类型。如果parameterTypes参数是null,那么它就被作为一个空数组看待。
- C is searched for any matching methods. If no matching method is found, the algorithm of step 1 is invoked recursively on the superclass of C.
- If no method was found in step 1 above, the superinterfaces of C are searched for a matching method. If any such method is found, it is reflected.
See The Java Language Specification, sections 8.2 and 8.4.
-
-
Parameters:
-
name
- the name of the method -
parameterTypes
- the list of parameters
Returns:
-
the
Method
object that matches the specifiedname
andparameterTypes
Throws:
-
NoSuchMethodException
- if a matching method is not found or if the name is "<init>"or "<clinit>". -
NullPointerException
- ifname
isnull
-
SecurityException
- If a security manager, s, is present and any of the following conditions is met:- invocation of
s.checkMemberAccess(this, Member.PUBLIC)
denies access to the method - the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of
s.checkPackageAccess()
denies access to the package of this class
Since:
- invocation of
- JDK1.1
-
- invoke
-
public Object invoke(Object obj, Object[] args) throws IllegalAccessException, IllegalArgumentException, 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 method invocation conversions as necessary. - 调用由这个Method对象代表的基本方法,用指定的参数在指定的对象上调用。个别的参数是自动打开来符合基本数据类型的参数,并且不管基本数据类型还是引用数据类型的参数都是方法调用这都取决于方法调用中的转换需求。
-
-
If the underlying method is static, then the specified
obj
argument is ignored. It may be null.If the number of formal parameters required by the underlying method is 0, the supplied
args
array may be of length 0 or null.If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, Second Edition, section 15.12.4.4; in particular, overriding based on the runtime type of the target object will occur.
如果基本的方法时静态方法,那么指定的obj参数就被忽略。它可以是null。。。
If the underlying method is static, the class that declared the method is initialized if it has not already been initialized.
If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. If the underlying method return type is void, the invocation returns null.
-
-
Parameters:
-
obj
- the object the underlying method is invoked from -
args
- the arguments used for the method call
Returns:
-
the result of dispatching the method represented by this object on
obj
with parametersargs
- 未完待续。。。
-
-
Invokes the underlying method represented by this
-