`
AquariusM
  • 浏览: 143935 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

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
未完待续。。。

 

分享到:
评论

相关推荐

    java反射机制,调用私有方法

    Method,invoke,getMethod等反射私有方法和私有属性

    Java反射方法调用

    调用空参方法 调用Object类型参数的方法 调用基本类型参数的方法 调用数组参数的方法 调用私有方法

    Java 反射(Reflection) 经典实用例子

    Java提供了一套机制来动态执行方法和构造方法,以及数组操作等,这套机制就叫——反射。反射机制是如今很多流行框架的实现基础,其中包括Spring、Hibernate等。原理性的问题不是本文的重点,接下来让我们在实例中...

    Java 高级特性.doc

    JAVA高级特性 1.静态导入:先举个离例子 。 import java.lang.Integer.parseInt; public class StaticImport { int x = parseInt("123"); System.out.println(x); } 这样的程序如果不在IDE 工具中输入,是很难...

    day021-反射和注解笔记和代码.rar

    1. 反射: Class 类的实例表示正在运行的 Java 应用程序中的类和接口; 枚举是一种类,注解(指的是注解Annotation)是一种接口; 每个数组都是 Class字节码类中的一个具体 对象 基本的 Java 类型...

    Java反射封装库joor.zip

    jOOR 在java原生的反射基础上进行了简单的封装,使得反射使用方便不少! 举个简单的列子,供大家参考。 java 原生反射写法: try { Method m1 = department.getClass().getMethod("getEmployees"); Employee ...

    java通过HTTP调用接口(Get请求和Post请求)

    java调用HTTP接口(Get请求和Post请求)

    个人java总结笔记

    这里需要传入的方法名和方法参数类型的Class对象, 是为了准确的获得方法(因为有方法重载, 方法名一样, 所以需要方法的参数类型) Class对象.getMethods(); 获得所有的公共的方法(包括继承获得的公共构造方法); ...

    水木清华站∶Java版精华区 含jsp及js等集合.chm

    [目录]Java之争 - 谁最吃亏 17. [目录]真成JAVA大牛还是很有前途的 18. [目录]Jbuilder及Swing,多线程问题讨论 7. [目录]快乐Java大家谈 1. [目录]活动简介 2. [目录]庆祝专区 3. ...

    带注释的Bootstrap.java

    import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; ...

    java-servlet-api.doc

    Java Servlet API说明文档 绪言 这是一份关于2.1版JavaServletAPI的说明文档,作为对这本文档的补充,你可以到http://java.sun.com/products/servlet/index.html下面下载Javadoc格式的文档。 谁需要读这份文档 这份...

    java 面试题 总结

    JAVA相关基础知识 1、面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用...

    几种排序算法的比较(java代码)

    SortTest.class.getMethod(s, Number[].class).invoke(this, (Object)testNum); long end=System.currentTimeMillis(); System.out.println(s+":"+(end-begin)+"\t"); System.out.println(Arrays....

    xml与反射.txt

    1 简述反射机制 反射是一种间接操作目标对象的机制,在程序程序运行时获取或者设置对象自身的信息。 只要给定类的名字,就可以通过反 ...后两种属于扩展方法,它们是在基础的方法上扩展出来的,只适用于java平台。

    java重写源码方法-ddi:ddi-动态Dalvik仪器工具包

    java修改源码方法ddi - 动态 Dalvik 仪器工具包 简单易用的 Dalvik 代码动态检测工具包。 检测基于库注入和挂钩方法入口点(内嵌挂钩)。 实际的检测代码是使用 JNI 接口编写的。 DDI 进一步支持将额外的 dex 类加载...

    java万能DAO

    //得到所有列和方法匹配的项 List&lt;PropertyMthod&gt; mList = new ArrayList(); for(String columnName : columnNameArray){ Method m = this.getMethod(cla,columnName); if(m!=null){ PropertyMthod pm = new ...

    \java超强笔记(超级经典)

    全部是txt格式的,容量小,以下内容为其中之一: 5.0新特性: 泛型: 泛型的形式: 类型&gt; &lt;E extends Numner&comparator&gt; 类名&接口,表示E继承Numner类实现comparator接口 &lt;?&gt; 泛型通配符表示任意...

    01:详解C#中的反射.pdf

    可以调用这些成员,其方式是调用Type的InvokeMember()方法,或者调用MethodInfo, PropertyInfo和其他类的Invoke()方法。 查看类中的构造方法: NewClassw nc = new NewClassw(); Type t = nc.GetType(); ...

    JAVA发送HttpClient请求及接收请求完整代码实例

    NULL 博文链接:https://bijian1013.iteye.com/blog/2310211

    c# 反射实例

    //c# 反射实例 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication1 { class Program { static void Main( string...

Global site tag (gtag.js) - Google Analytics