#001 cc = c.getDeclaredClasses(); //找出inner classes<o:p></o:p>
#002 for (Class cite : cc)<o:p></o:p>
#003 System.out.println(tName(cite.getName(), null));<o:p></o:p>
#004<o:p></o:p>
#005 ctmp = c.getDeclaringClass(); //找出outer classes<o:p></o:p>
#006 if (ctmp != null)<o:p></o:p>
#007 System.out.println(ctmp.getName());<o:p></o:p>
<o:p> </o:p>
执行结果(例):<o:p></o:p>
LinkedList$Entry<o:p></o:p>
LinkedList$ListItr<o:p></o:p>
图5-7:找出inner classes 和outer class<o:p></o:p>
<o:p> </o:p>
#001 Constructor cn[];<o:p></o:p>
#002 cn = c.getDeclaredConstructors();<o:p></o:p>
#003 for (int i = 0; i < cn.length; i++) {<o:p></o:p>
#004 int md = cn[i].getModifiers();<o:p></o:p>
#005 System.out.print(" " + Modifier.toString(md) + " " +<o:p></o:p>
#006 cn[i].getName());<o:p></o:p>
#007 Class cx[] = cn[i].getParameterTypes();<o:p></o:p>
#008 System.out.print("(");<o:p></o:p>
#009 for (int j = 0; j < cx.length; j++) {<o:p></o:p>
#010 System.out.print(tName(cx[j].getName(), null));<o:p></o:p>
#011 if (j < (cx.length - 1)) System.out.print(", ");<o:p></o:p>
#012 }<o:p></o:p>
#013 System.out.print(")");<o:p></o:p>
#014 }<o:p></o:p>
<o:p> </o:p>
执行结果(例):<o:p></o:p>
public java.util.LinkedList(Collection)<o:p></o:p>
public java.util.LinkedList()<o:p></o:p>
图5<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="True" hasspace="False" sourcevalue="8" unitname="a">-8a</st1:chmetcnv>:找出所有constructors<o:p></o:p>
<o:p> </o:p>
#004 System.out.println(cn[i].toGenericString());<o:p></o:p>
<o:p> </o:p>
执行结果(例):<o:p></o:p>
public java.util.LinkedList(java.util.Collection<? extends E>)<o:p></o:p>
public java.util.LinkedList()<o:p></o:p>
图5-8b:找出所有constructors。本例在for 循环内使用toGenericString(),省事。<o:p></o:p>
<o:p> </o:p>
#001 Method mm[];<o:p></o:p>
#<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="True" sourcevalue="2" unitname="mm">002 mm</st1:chmetcnv> = c.getDeclaredMethods();<o:p></o:p>
#003 for (int i = 0; i < mm.length; i++) {<o:p></o:p>
#004 int md = mm[i].getModifiers();<o:p></o:p>
#005 System.out.print(" "+Modifier.toString(md)+" "+<o:p></o:p>
#006 tName(mm[i].getReturnType().getName(), null)+" "+<o:p></o:p>
#<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="7" unitname="mm">007 </st1:chmetcnv>mm[i].getName());<o:p></o:p>
#008 Class cx[] = mm[i].getParameterTypes();<o:p></o:p>
#009 System.out.print("(");<o:p></o:p>
#010 for (int j = 0; j < cx.length; j++) {<o:p></o:p>
#011 System.out.print(tName(cx[j].getName(), null));<o:p></o:p>
#012 if (j < (cx.length - 1)) System.out.print(", ");<o:p></o:p>
#013 }<o:p></o:p>
#014 System.out.print(")");<o:p></o:p>
#015 }<o:p></o:p>
<o:p> </o:p>
执行结果(例):<o:p></o:p>
public Object get(int)<o:p></o:p>
public int size()<o:p></o:p>
图5<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="True" hasspace="False" sourcevalue="9" unitname="a">-9a</st1:chmetcnv>:找出所有methods<o:p></o:p>
<o:p> </o:p>
#004 System.out.println(mm[i].toGenericString());<o:p></o:p>
<o:p> </o:p>
public E java.util.LinkedList.get(int)<o:p></o:p>
public int java.util.LinkedList.size()<o:p></o:p>
图5-9b:找出所有methods。本例在for 循环内使用toGenericString(),省事。<o:p></o:p>
<o:p> </o:p>
#001 Field ff[];<o:p></o:p>
#002 ff = c.getDeclaredFields();<o:p></o:p>
#003 for (int i = 0; i < ff.length; i++) {<o:p></o:p>
#004 int md = ff[i].getModifiers();<o:p></o:p>
#005 System.out.println(" "+Modifier.toString(md)+" "+<o:p></o:p>
#006 tName(ff[i].getType().getName(), null) +" "+<o:p></o:p>
#007 ff[i].getName()+";");<o:p></o:p>
#008 }<o:p></o:p>
<o:p> </o:p>
执行结果(例):<o:p></o:p>
private transient LinkedList$Entry header;<o:p></o:p>
private transient int size;<o:p></o:p>
图5<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="True" hasspace="False" sourcevalue="10" unitname="a">-10a</st1:chmetcnv>:找出所有fields<o:p></o:p>
<o:p> </o:p>
#004 System.out.println("G: " + ff[i].toGenericString());<o:p></o:p>
<o:p> </o:p>
private transient java.util.LinkedList.java.util.LinkedList$Entry<E> ??<o:p></o:p>
java.util.LinkedList.header<o:p></o:p>
private transient int java.util.LinkedList.size<o:p></o:p>
图5-10b:找出所有fields。本例在for 循环内使用toGenericString(),省事。<o:p></o:p>
<o:p> </o:p>
找出class参用(导入)的所有classes<o:p></o:p>
没有直接可用的Reflection API可以为我们找出某个class参用的所有其它classes。要获得这项信息,必须做苦工,一步一脚印逐一记录。我们必须观察所有fields的类型、所有methods(包括constructors)的参数类型和回返类型,剔除重复,留下唯一。这正是为什么图5-2程序代码要为tName()指定一个hashtable(而非一个null)做为第二自变量的缘故:hashtable可为我们储存元素(本例为字符串),又保证不重复。<o:p></o:p>
<o:p> </o:p>
本文讨论至此,几乎可以还原一个class的原貌(唯有methods 和ctors的定义无法取得)。接下来讨论Reflection 的另三个动态性质:(1) 运行时生成instances,(2) 执<o:p></o:p>
行期唤起methods,(3) 运行时改动fields