源码天下

源码天下 -> 技术文档 -> WEB开发 -> JSP技术 -> 文章内容

java新手入门:Java反射机制



作者:佚名  来源:pccode.net整理  发布时间:2008-5-8 21:40:53  发布人:wongrs

执行结果(例):
public class LinkedList
图5-3:找出class或interface 的名称,及其属性(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 }
9 7 3 12 4 8 :


[] [返回上一页] [打 印] [收 藏]

上一篇文章:JSP/Servlet的URL重定向的技巧
下一篇文章:Java程序最容易犯的21种错误实例分析
中国福网提供带宽支持