KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > introspector > Introspector


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.introspector;
4
5 import java.util.WeakHashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8
9 /**
10  * Provides introspection analysis against any java class.
11  * Caching of {@link ClassDescriptor} objects is implemented to improve performance.
12  * <p>
13  * Users may have one or more introspectors instances, depending on their purposes.
14  * <p>
15  * Moreover, one default global introspector is also defined. It can be used
16  * directly, or through simple {@link DefaultIntrospector} wrapper.
17  */

18 public class Introspector {
19
20     public static Introspector DEFAULT = new Introspector();
21
22     protected Map JavaDoc cache = new WeakHashMap JavaDoc();
23
24     /**
25      * Returns the {@link ClassDescriptor} object for specified class.
26      */

27     public ClassDescriptor lookup(Class JavaDoc type) {
28         ClassDescriptor cd = (ClassDescriptor) cache.get(type);
29         if (cd != null) {
30             cd.increaseUsage();
31             return cd;
32         }
33         cd = describeClass(type);
34         cache.put(type, cd);
35         return cd;
36     }
37
38     /**
39      * Searches for a class in local Introspector and
40      * if not founded, performs {@link #lookup(Class)}
41      * in {@link #DEFAULT} one.
42      */

43     public ClassDescriptor search(Class JavaDoc type) {
44         if (this.getClass() == DEFAULT.getClass()) {
45             return lookup(type);
46         }
47         ClassDescriptor cd = (ClassDescriptor) cache.get(type);
48         if (cd == null) {
49             cd = DEFAULT.lookup(type);
50         }
51         return cd;
52     }
53
54     /**
55      * Registers new class type. If type already registered, it will be
56      * reseted and registered again with new class descriptor.
57      */

58     public ClassDescriptor register(Class JavaDoc type) {
59         ClassDescriptor cd = describeClass(type);
60         cache.put(type, cd);
61         return cd;
62     }
63
64     /**
65      * Describes a class by creating a new instance of {@link ClassDescriptor}.
66      */

67     protected ClassDescriptor describeClass(Class JavaDoc type) {
68         return new ClassDescriptor(type);
69     }
70
71     /**
72      * Resets current cache.
73      */

74     public void reset() {
75         cache = new WeakHashMap JavaDoc();
76     }
77
78     /**
79      * Returns simple statistics information about all cached descriptors and their usage.
80      */

81     public String JavaDoc getStatistics() {
82         StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
83         sbuffer.append("Total classes: ").append(cache.size()).append('\n');
84         Iterator JavaDoc i = cache.keySet().iterator();
85         while (i.hasNext()) {
86             Class JavaDoc clazz = (Class JavaDoc) i.next();
87             ClassDescriptor bcd = (ClassDescriptor) cache.get(clazz);
88             sbuffer.append('\t').append(clazz.getName()).append(" (");
89             sbuffer.append(bcd.getUsage()).append(" uses)").append('\n');
90         }
91         return sbuffer.toString();
92     }
93
94 }
95
Popular Tags