2010年9月25號---Java基礎學習之---Java反射方法學習getMethod()和invoke()方法學習

 

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. The name parameter is a String specifying the simple name the desired method. The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes isnull, it is treated as if it were an empty array.

If the name is "<init>"or "<clinit>" a NoSuchMethodException 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,那麼它就被作爲一個空數組看待。

  1. 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.
  2. 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.
To find a matching method in a class C:  If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily.

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 specified name and parameterTypes
Throws:
NoSuchMethodException - if a matching method is not found or if the name is "<init>"or "<clinit>".
NullPointerException - if name is null
SecurityException - If a security manager, s, is present and any of the following conditions is met:
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 parameters args
未完待續。。。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章