Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4464977
  • 博文数量: 192
  • 博客积分: 10014
  • 博客等级: 上将
  • 技术积分: 8232
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-21 00:22
文章分类

全部博文(192)

文章存档

2011年(4)

2009年(14)

2008年(174)

我的朋友

分类: Java

2008-04-05 11:20:05

 

Java Reflection API 运用示例

5示范4提过的每一个Reflection API,及其执行结果。程序中出现的tName()是个辅助函数,可将其第一自变量所代表的Java class完整路径字符串剥除路径部分,留下class名称,储存到第二自变量所代表的一个hashtable去并返回(如果第二自变量为null,就不储存而只是返回)。

 

#001 Class c = null;

#002 c = Class.forName(args[0]);

#003

#004 Package p;

#005 p = c.getPackage();

#006

#007 if (p != null)

#008    System.out.println("package "+p.getName()+";");

 

执行结果(例):

package java.util;

5-1:找出class 隶属的package。其中的c将继续沿用于以下各程序片段。

 

#001 ff = c.getDeclaredFields();

#002 for (int i = 0; i < ff.length; i++)

#003    x = tName(ff[i].getType().getName(), classRef);

#004

#005 cn = c.getDeclaredConstructors();

#006 for (int i = 0; i < cn.length; i++) {

#007    Class cx[] = cn[i].getParameterTypes();

#008    for (int j = 0; j < cx.length; j++)

#009        x = tName(cx[j].getName(), classRef);

#010 }

#011

#012 mm = c.getDeclaredMethods();

#013 for (int i = 0; i < mm.length; i++) {

#014    x = tName(mm[i].getReturnType().getName(), classRef);

#015    Class cx[] = mm[i].getParameterTypes();

#016    for (int j = 0; j < cx.length; j++)

#017        x = tName(cx[j].getName(), classRef);

#018 }

#019 classRef.remove(c.getName()); //不必记录自己(不需import 自己)

 

执行结果(例):

import java.util.ListIterator;

import java.lang.Object;

import java.util.LinkedList$Entry;

import java.util.Collection;

import java.io.ObjectOutputStream;

import java.io.ObjectInputStream;

5-2:找出导入的classes,动作细节详见内文说明。

 

#001 int mod = c.getModifiers();

#002 System.out.print(Modifier.toString(mod)); //整个modifier

#003

#004 if (Modifier.isInterface(mod))

#005    System.out.print(" "); //关键词 "interface" 已含于modifier

#006 else

#007    System.out.print(" class "); //关键词 "class"

#008 System.out.print(tName(c.getName(), null)); //class 名称

 

执行结果(例):

public class LinkedList

5-3:找出classinterface 的名称,及其属性(modifiers)。

 

#001 TypeVariable[] tv;

#002 tv = c.getTypeParameters(); //warning: unchecked conversion

#003 for (int i = 0; i < tv.length; i++) {

#004    x = tName(tv[i].getName(), null); //例如 E,K,V...

#005    if (i == 0) //第一个

#006        System.out.print("<" + x);

#007    else //非第一个

#008        System.out.print("," + x);

#009    if (i == tv.length-1) //最后一个

#010        System.out.println(">");

#011 }

 

执行结果(例):

public abstract interface Map

public class LinkedList

5-4:找出parameterized types 的名称

 

#001 Class supClass;

#002 supClass = c.getSuperclass();

#003 if (supClass != null) //如果有super class

#004    System.out.print(" extends" +

#005 tName(supClass.getName(),classRef));

 

执行结果(例):

public class LinkedList

extends AbstractSequentialList,

5-5:找出base class。执行结果多出一个不该有的逗号于尾端。此非本处重点,为简化计,不多做处理。

 

#001 Class cc[];

#002 Class ctmp;

#003 //找出所有被实现的interfaces

#004 cc = c.getInterfaces();

#005 if (cc.length != 0)

#006    System.out.print(", \r\n" + " implements "); //关键词

#007 for (Class cite : cc) //JDK1.5 新式循环写法

#008    System.out.print(tName(cite.getName(), null)+", ");

 

执行结果(例):

public class LinkedList

extends AbstractSequentialList,

implements List, Queue, Cloneable, Serializable,

5-6:找出implemented interfaces。执行结果多出一个不该有的逗号于尾端。此非本处重点,为简化计,不多做处理。

 

#001 cc = c.getDeclaredClasses(); //找出inner classes

#002 for (Class cite : cc)

#003    System.out.println(tName(cite.getName(), null));

#004

#005 ctmp = c.getDeclaringClass(); //找出outer classes

#006 if (ctmp != null)

#007    System.out.println(ctmp.getName());

 

执行结果(例):

LinkedList$Entry

LinkedList$ListItr

5-7:找出inner classes outer class

 

#001 Constructor cn[];

#002 cn = c.getDeclaredConstructors();

#003 for (int i = 0; i < cn.length; i++) {

#004    int md = cn[i].getModifiers();

#005    System.out.print(" " + Modifier.toString(md) + " " +

#006    cn[i].getName());

#007    Class cx[] = cn[i].getParameterTypes();

#008    System.out.print("(");

#009    for (int j = 0; j < cx.length; j++) {

#010        System.out.print(tName(cx[j].getName(), null));

#011        if (j < (cx.length - 1)) System.out.print(", ");

#012    }

#013    System.out.print(")");

#014 }

 

执行结果(例):

public java.util.LinkedList(Collection)

public java.util.LinkedList()

5-8a:找出所有constructors

 

#004 System.out.println(cn[i].toGenericString());

 

执行结果(例):

public java.util.LinkedList(java.util.Collection)

public java.util.LinkedList()

5-8b:找出所有constructors。本例在for 循环内使用toGenericString(),省事。

 

#001 Method mm[];

#002 mm = c.getDeclaredMethods();

#003 for (int i = 0; i < mm.length; i++) {

#004    int md = mm[i].getModifiers();

#005    System.out.print(" "+Modifier.toString(md)+" "+

#006    tName(mm[i].getReturnType().getName(), null)+" "+

#007    mm[i].getName());

#008    Class cx[] = mm[i].getParameterTypes();

#009    System.out.print("(");

#010    for (int j = 0; j < cx.length; j++) {

#011        System.out.print(tName(cx[j].getName(), null));

#012    if (j < (cx.length - 1)) System.out.print(", ");

#013    }

#014    System.out.print(")");

#015 }

 

执行结果(例):

public Object get(int)

public int size()

5-9a:找出所有methods

 

#004 System.out.println(mm[i].toGenericString());

 

public E java.util.LinkedList.get(int)

public int java.util.LinkedList.size()

5-9b:找出所有methods。本例在for 循环内使用toGenericString(),省事。

 

#001 Field ff[];

#002 ff = c.getDeclaredFields();

#003 for (int i = 0; i < ff.length; i++) {

#004    int md = ff[i].getModifiers();

#005    System.out.println(" "+Modifier.toString(md)+" "+

#006    tName(ff[i].getType().getName(), null) +" "+

#007    ff[i].getName()+";");

#008 }

 

执行结果(例):

private transient LinkedList$Entry header;

private transient int size;

5-10a:找出所有fields

 

#004 System.out.println("G: " + ff[i].toGenericString());

 

private transient java.util.LinkedList.java.util.LinkedList$Entry ??

java.util.LinkedList.header

private transient int java.util.LinkedList.size

5-10b:找出所有fields。本例在for 循环内使用toGenericString(),省事。

 

找出class参用(导入)的所有classes

没有直接可用的Reflection API可以为我们找出某个class参用的所有其它classes。要获得这项信息,必须做苦工,一步一脚印逐一记录。我们必须观察所有fields的类型、所有methods(包括constructors)的参数类型和回返类型,剔除重复,留下唯一。这正是为什么5-2程序代码要为tName()指定一个hashtable(而非一个null)做为第二自变量的缘故:hashtable可为我们储存元素(本例为字符串),又保证不重复。

 

本文讨论至此,几乎可以还原一个class的原貌(唯有methods ctors的定义无法取得)。接下来讨论Reflection 的另三个动态性质:(1) 运行时生成instances(2)

行期唤起methods(3) 运行时改动fields

 

运行时生成instances

欲生成对象实体,在Reflection 动态机制中有两种作法,一个针对“无自变量ctor”,

一个针对“带参数ctor6是面对“无自变量ctor”的例子。如果欲调用的是“带参数ctor“就比较麻烦些,7是个例子,其中不再调用ClassnewInstance(),而是调用Constructor newInstance()7首先准备一个Class[]做为ctor的参数类型(本例指定为一个double和一个int),然后以此为自变量调用getConstructor(),获得一个专属ctor。接下来再准备一个Object[] 做为ctor实参值(本例指定3.14159125),调用上述专属ctornewInstance()

 

#001 Class c = Class.forName("DynTest");

#002 Object obj = null;

#003 obj = c.newInstance(); //不带自变量

#004 System.out.println(obj);

6:动态生成“Class object 所对应之class”的对象实体;无自变量。

 

#001 Class c = Class.forName("DynTest");

#002 Class[] pTypes = new Class[] { double.class, int.class };

#003 Constructor ctor = c.getConstructor(pTypes);

#004 //指定parameter list,便可获得特定之ctor

#005

#006 Object obj = null;

#007 Object[] arg = new Object[] {3.14159, 125}; //自变量

#008 obj = ctor.newInstance(arg);

#009 System.out.println(obj);

7:动态生成“Class object 对应之class”的对象实体;自变量以Object[]表示。

 

运行时调用methods

这个动作和上述调用“带参数之ctor”相当类似。首先准备一个Class[]做为ctor的参数类型(本例指定其中一个是String,另一个是Hashtable),然后以此为自变量调用getMethod(),获得特定的Method object。接下来准备一个Object[]放置自变量,然后调用上述所得之特定Method objectinvoke(),如8。知道为什么索取Method object时不需指定回返类型吗?因为method overloading机制要求signature(署名式)必须唯一,而回返类型并非signature的一个成份。换句话说,只要指定了method名称和参数列,就一定指出了一个独一无二的method

 

#001 public String func(String s, Hashtable ht)

#002 {

#003 System.out.println("func invoked"); return s;

#004 }

#005 public static void main(String args[])

#006 {

#007 Class c = Class.forName("Test");

#008 Class ptypes[] = new Class[2];

#009 ptypes[0] = Class.forName("java.lang.String");

#010 ptypes[1] = Class.forName("java.util.Hashtable");

#011 Method m = c.getMethod("func",ptypes);

#012 Test obj = new Test();

#013 Object args[] = new Object[2];

#014 arg[0] = new String("Hello,world");

#015 arg[1] = null;

#016 Object r = m.invoke(obj, arg);

#017 Integer rval = (String)r;

#018 System.out.println(rval);

#019 }

8:动态唤起method

 

运行时变更fields

与先前两个动作相比,“变更field内容”轻松多了,因为它不需要参数和自变量。首先调用ClassgetField()并指定field名称。获得特定的Field object之后便可直接调用Fieldget()set(),如9

 

#001 public class Test {

#002 public double d;

#003

#004 public static void main(String args[])

#005 {

#006 Class c = Class.forName("Test");

#007 Field f = c.getField("d"); //指定field 名称

#008 Test obj = new Test();

#009 System.out.println("d= " + (Double)f.get(obj));

#010 f.set(obj, 12.34);

#011 System.out.println("d= " + obj.d);

#012 }

#013 }

9:动态变更field 内容

阅读(1382) | 评论(0) | 转发(0) |
0

上一篇:Java反射机制

下一篇:安装Gentoo 2008.0 beta1

给主人留下些什么吧!~~