Java Reflection(反射)是一种在运行期间查看Java对象内部情况的技术,包括Java对象的变量、支持的方法、实现的接口、扩展的类——基本上在编译时你能想知道的关于对象的任何东西。
Reflection API位于java.lang.reflect包中。
载入指定的类,Class c = Class.forName(String className); 例,Class c = Class.forName("java.lang.String");
访问类的内部成员和其他相关信息:
- getMethods():返回一组表示类或接口的所有公共方法的方法对象数组。
- getConstructors():返回一组表示类的所有公共构造器的构造器对象数组。
- getFields():返回一组表示类或接口所有公共字段的字段对象数组。
- getClasses():返回一组表示所有属于类或接口的公共类和接口(例如,内部类)的类对象数组。
- getSuperclass():返回表示类或接口的超类的类对象(接口返回为空值)。
- getInterfaces():返回一组表示类或接口执行的所有接口的类对象数组。
更精确的一些方法:
- getMethod("methodName", Class...):返回表示接受类参数指定参数的类或接口中名为“methodName”的公共方法的方法对象。
- getConstructor(Class...):返回表示接受类参数指定参数的类公共构造器的构造器对象。
- getField("fieldName"):返回表示类或接口中名为“fieldname”的公共字段的字段对象。
动态调用类内部的方法:
- Field.get(Object):返回一个包含输入get()对象实例的字段值对象。(如果字段对象表示静态字段,那么对象参数便被忽略,也可能为空。)
- Method.invoke(Object, Object...):返回一个包含为输入invoke()的第一个对象参数实例调用方法的结果的对象。其余对象参数被传递给输入方法。(如果方法对象表示一个静态方法,那么第一个对象参数便被忽略,也可能为空。)
- Constructor.newInstance(Object...):返回调用构造器的新对象实例。对象参数输入构造器。(注意类的无参数构造器也可以通过newInstance()被调用。)
调用内部方法:
java 代码
- // method
- public int add(int a, int b) {
- return a + b;
- }
- // sample
- Class cls = Class.forName("method2");
- Class partypes[] = new Class[2];
- partypes[0] = Integer.TYPE;
- partypes[1] = Integer.TYPE;
- Method meth = cls.getMethod("add", partypes);
- method2 methobj = new method2();
- Object arglist[] = new Object[2];
- arglist[0] = new Integer(37);
- arglist[1] = new Integer(47);
- Object retobj= meth.invoke(methobj, arglist);
-
初始化对象:
java 代码
-
- public constructor2 ( ) {
-
- }
-
- public constructor2 (int a, int b) {
- System.out.println("a = " + a + " b = " + b);
- }
-
-
- Class cls = Class.forName("constructor2");
- Class partypes[] = new Class[2];
- partypes[0] = Integer.TYPE;
- partypes[1] = Integer.TYPE;
- Constructor ct = cls.getConstructor(partypes);
- Object arglist[] = new Object[2];
- arglist[0] = new Integer(37);
- arglist[1] = new Integer(47);
- Object retobj = ct.newInstance(arglist);
-
改变字段(域)的值:
java 代码
- Class cls = Class.forName("field2");
- Field fld = cls.getField("d");
- field2 f2obj = new field2();
- System.out.println("d = " + f2obj.d);
- fld.setDouble(f2obj, 12.34);
-
使用数组:
java 代码
- Class cls = Class.forName("java.lang.String");
- Object arr = Array.newInstance(cls, 10);
- Array.set(arr, 5, "this is a test");
- String s = (String)Array.get(arr, 5);
- System.out.println(s);
-
参考:
Sun java reflection tutorial http://java.sun.com/docs/books/tutorial/reflect/index.html
Java Reflection-JAVA反射 http://www.mikecat.net/blogview.asp?logID=1421
使用 Java Reflection http://www.apusic.com/article/article5.htm
分享到:
相关推荐
Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time pro- viding the sound conceptual basis that developers need to...
java reflection in action 2005 英文版
JAVA REFLECTION IN ACTION
Java Reflection (JAVA反射)详解
Minning - A book talked about Java Reflection in details.
Java Reflection In Action(英文版)
Java Reflection in Action 英文书 pdf和文中源代码
Java反射经典实例 Java Reflection Cookbook[1].docx.pdf
Reflection 是 Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说“自审”,并能直接操作程序的内部属性。例如,使用它能获得 Java 类中各成员的名称并显示出来。 Java 的这一能力在...
java reflection in action(2005)
Java Reflection in Action Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time providing the sound conceptual basis...
基于Java Reflection自动逆向生成类间方法关系图的解析器.pdf
Java Reflection (JAVA反射.mht
之前上课的时候老师总结的JavaReflection反射学习资料,内容简单易懂,浅显易懂,适合小白入手学习。。