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
- invocation of
- Since:
- 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