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 this Class 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 is null, 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
未完待续。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值