KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > util > Introspector


1 package jfun.yan.util;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.WeakHashMap JavaDoc;
9
10 /**
11  * This class provides introspection analysis against any java class.
12  * The entire class, super class and super interfaces are traversed
13  * to build a ClassDescriptor object.
14  * <p>
15  * Caching of ClassDescriptor objects is implemented to improve performance.
16  * </p>
17  * <p>
18  * @author Ben Yu
19  * Nov 14, 2005 9:39:56 PM
20  */

21 public class Introspector {
22   private final WeakHashMap JavaDoc<Class JavaDoc,ClassDescriptor> cache
23   = new WeakHashMap JavaDoc<Class JavaDoc,ClassDescriptor>();
24   private interface Sink{
25     void addMethod(Method JavaDoc mtd);
26     void addField(Field JavaDoc fld);
27     void addConstructor(Constructor JavaDoc ctor);
28   }
29   /**
30    * Get the ClassDescriptor object for a class.
31    * @param type the class.
32    * @return the ClassDescriptor object.
33    */

34   public synchronized <T> ClassDescriptor<T> getClassDescriptor(Class JavaDoc<T> type){
35     ClassDescriptor<T> ret = (ClassDescriptor<T>)cache.get(type);
36     if(ret!=null) return ret;
37     ret = describeClass(type);
38     cache.put(type, ret);
39     return ret;
40   }
41   private void populateParent(ClassDescriptor desc, Class JavaDoc type, Set JavaDoc visited){
42     if(visited.contains(type))
43       return;
44     final ClassDescriptor parent = getClassDescriptor(type);
45     desc.addAll(parent);
46     visited.add(type);
47   }
48   private Class JavaDoc getSuperclass(Class JavaDoc type){
49     if(type.isInterface()){
50       return Object JavaDoc.class;
51     }
52     else return type.getSuperclass();
53   }
54   private <T> ClassDescriptor<T> describeClass(Class JavaDoc<T> type){
55     final ClassDescriptor<T> desc = new ClassDescriptor<T>(type);
56     final Class JavaDoc[] itfs = type.getInterfaces();
57     final HashSet JavaDoc visited = new HashSet JavaDoc();
58     if(itfs!=null){
59       for(int i=0; i<itfs.length; i++){
60         populateParent(desc, itfs[i], visited);
61       }
62     }
63     final Class JavaDoc parent = getSuperclass(type);
64     if(parent!=null && !parent.equals(type)){
65       populateParent(desc, parent, visited);
66     }
67     final Field JavaDoc[] flds = type.getDeclaredFields();
68     for(int i=0; i<flds.length; i++){
69       desc.addField(flds[i]);
70     }
71     final Method JavaDoc[] mtds = type.getDeclaredMethods();
72     for(int i=0; i<mtds.length; i++){
73       desc.addMethod(mtds[i]);
74     }
75     final Constructor JavaDoc<T>[] ctors = type.getDeclaredConstructors();
76     for(int i=0; i<ctors.length; i++){
77       desc.addConstructor(ctors[i]);
78     }
79     return desc;
80   }
81 }
82
Popular Tags