KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > introspector > Methods


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

3 package jodd.introspector;
4
5 import jodd.util.ReflectUtil;
6
7 import java.lang.reflect.Method JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Methods
15  */

16 class Methods {
17
18     HashMap JavaDoc mMap = new HashMap JavaDoc();
19     Method JavaDoc[] allMethods;
20     boolean locked = false;
21
22     int count;
23     void addMethod(String JavaDoc name, Method JavaDoc method) {
24         if (locked == true) {
25             throw new IllegalStateException JavaDoc("Methods introspection is already finished.");
26         }
27         count++;
28         List JavaDoc paramList = (List JavaDoc) mMap.get(name);
29         if (paramList == null) {
30             paramList = new ArrayList JavaDoc();
31             mMap.put(name, paramList);
32         }
33         paramList.add(new MethodDescriptor(method));
34     }
35
36     void lock() {
37         HashMap JavaDoc newMap = new HashMap JavaDoc(mMap.size());
38         locked = true;
39         allMethods = new Method JavaDoc[count];
40         Iterator JavaDoc it = mMap.keySet().iterator();
41         int k = 0;
42         while (it.hasNext()) {
43             String JavaDoc name = (String JavaDoc) it.next();
44             List JavaDoc list = (List JavaDoc) mMap.get(name);
45             MethodEntry entry = new MethodEntry();
46             entry.size = list.size();
47             entry.methodsList = new Method JavaDoc[entry.size];
48             entry.paramterTypes = new Class JavaDoc[entry.size][];
49             for (int i = 0; i < entry.size; i++) {
50                 MethodDescriptor md = (MethodDescriptor) list.get(i);
51                 allMethods[k] = md.method;
52                 k++;
53                 entry.methodsList[i] = md.method;
54                 entry.paramterTypes[i] = md.parameterTypes;
55             }
56             newMap.put(name, entry);
57         }
58         mMap = newMap;
59     }
60
61     // ---------------------------------------------------------------- get
62

63     Method JavaDoc getMethod(String JavaDoc name, Class JavaDoc[] paramTypes) {
64         MethodEntry entry = (MethodEntry) mMap.get(name);
65         if (entry == null) {
66             return null;
67         }
68         for (int i = 0; i < entry.size; i++) {
69             if (ReflectUtil.compareParameteres(entry.paramterTypes[i], paramTypes) == true) {
70                 return entry.methodsList[i];
71             }
72         }
73         return null;
74     }
75
76     Method JavaDoc getMethod(String JavaDoc name) {
77         MethodEntry entry = (MethodEntry) mMap.get(name);
78         if (entry == null) {
79             return null;
80         }
81         if (entry.size != 1) {
82             throw new IllegalArgumentException JavaDoc("Method '" + name + "' is not unique!");
83         }
84         return entry.methodsList[0];
85     }
86
87     Method JavaDoc[] getAllMethods(String JavaDoc name) {
88         MethodEntry entry = (MethodEntry) mMap.get(name);
89         if (entry == null) {
90             return null;
91         }
92         return entry.methodsList;
93     }
94
95     Method JavaDoc[] getAllMethods() {
96         return allMethods;
97     }
98 }
99
100
101
102 class MethodDescriptor {
103     final Method JavaDoc method;
104     final Class JavaDoc[] parameterTypes;
105     static final Class JavaDoc[] NOPARAMS = new Class JavaDoc[0];
106     MethodDescriptor(Method JavaDoc method) {
107         Class JavaDoc[] params = method.getParameterTypes();
108         if (params == null) {
109             this.parameterTypes = NOPARAMS;
110         } else {
111             this.parameterTypes = params;
112         }
113         this.method = method;
114     }
115 }
116
117 class MethodEntry {
118     Method JavaDoc[] methodsList;
119     Class JavaDoc[][] paramterTypes;
120     int size;
121 }
Popular Tags