1 |
| package org.jboss.cache.pojo.util; |
2 |
| |
3 |
| import org.apache.commons.logging.Log; |
4 |
| import org.apache.commons.logging.LogFactory; |
5 |
| |
6 |
| import java.lang.reflect.InvocationTargetException; |
7 |
| import java.lang.reflect.Method; |
8 |
| import java.util.ArrayList; |
9 |
| import java.util.Iterator; |
10 |
| import java.util.List; |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| public class MethodCall |
19 |
| { |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| protected String method_name = null; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| protected Object[] args = null; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| protected Class[] types = null; |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| protected String[] signature = null; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| protected Method method = null; |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| protected Object target = null; |
50 |
| |
51 |
| protected static final Log log = LogFactory.getLog(MethodCall.class); |
52 |
| |
53 |
15676
| public MethodCall(Method method, Object[] arguments, Object target)
|
54 |
| { |
55 |
15676
| init(method);
|
56 |
15676
| if (arguments != null) args = arguments;
|
57 |
15676
| this.target = target;
|
58 |
| } |
59 |
| |
60 |
15676
| private void init(Method method)
|
61 |
| { |
62 |
15676
| this.method = method;
|
63 |
15676
| method_name = method.getName();
|
64 |
| } |
65 |
| |
66 |
0
| public String getName()
|
67 |
| { |
68 |
0
| return method_name;
|
69 |
| } |
70 |
| |
71 |
0
| public void setName(String n)
|
72 |
| { |
73 |
0
| method_name = n;
|
74 |
| } |
75 |
| |
76 |
0
| public Object[] getArgs()
|
77 |
| { |
78 |
0
| return args;
|
79 |
| } |
80 |
| |
81 |
0
| public void setArgs(Object[] args)
|
82 |
| { |
83 |
0
| if (args != null)
|
84 |
0
| this.args = args;
|
85 |
| } |
86 |
| |
87 |
0
| public Method getMethod()
|
88 |
| { |
89 |
0
| return method;
|
90 |
| } |
91 |
| |
92 |
0
| public void setMethod(Method m)
|
93 |
| { |
94 |
0
| init(m);
|
95 |
| } |
96 |
| |
97 |
0
| Method findMethod(Class target_class) throws Exception
|
98 |
| { |
99 |
0
| int len = args != null ? args.length : 0;
|
100 |
0
| Method m;
|
101 |
| |
102 |
0
| Method[] methods = getAllMethods(target_class);
|
103 |
0
| for (int i = 0; i < methods.length; i++)
|
104 |
| { |
105 |
0
| m = methods[i];
|
106 |
0
| if (m.getName().equals(method_name))
|
107 |
| { |
108 |
0
| if (m.getParameterTypes().length == len)
|
109 |
0
| return m;
|
110 |
| } |
111 |
| } |
112 |
| |
113 |
0
| return null;
|
114 |
| } |
115 |
| |
116 |
0
| Method[] getAllMethods(Class target)
|
117 |
| { |
118 |
| |
119 |
0
| Class superclass = target;
|
120 |
0
| List methods = new ArrayList();
|
121 |
0
| int size = 0;
|
122 |
| |
123 |
0
| while (superclass != null)
|
124 |
| { |
125 |
0
| Method[] m = superclass.getDeclaredMethods();
|
126 |
0
| methods.add(m);
|
127 |
0
| size += m.length;
|
128 |
0
| superclass = superclass.getSuperclass();
|
129 |
| } |
130 |
| |
131 |
0
| Method[] result = new Method[size];
|
132 |
0
| int index = 0;
|
133 |
0
| for (Iterator i = methods.iterator(); i.hasNext();)
|
134 |
| { |
135 |
0
| Method[] m = (Method[]) i.next();
|
136 |
0
| System.arraycopy(m, 0, result, index, m.length);
|
137 |
0
| index += m.length;
|
138 |
| } |
139 |
0
| return result;
|
140 |
| } |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
0
| Method getMethod(Class target, String methodName, Class[] types)
|
150 |
| { |
151 |
| |
152 |
0
| if (types == null)
|
153 |
| { |
154 |
0
| types = new Class[0];
|
155 |
| } |
156 |
| |
157 |
0
| Method[] methods = getAllMethods(target);
|
158 |
0
| methods:
|
159 |
0
| for (int i = 0; i < methods.length; i++)
|
160 |
| { |
161 |
0
| Method m = methods[i];
|
162 |
0
| if (!methodName.equals(m.getName()))
|
163 |
| { |
164 |
0
| continue;
|
165 |
| } |
166 |
0
| Class[] parameters = m.getParameterTypes();
|
167 |
0
| if (types.length != parameters.length)
|
168 |
| { |
169 |
0
| continue;
|
170 |
| } |
171 |
0
| for (int j = 0; j < types.length; j++)
|
172 |
| { |
173 |
0
| if (!types[j].equals(parameters[j]))
|
174 |
| { |
175 |
0
| continue methods;
|
176 |
| } |
177 |
| } |
178 |
0
| return m;
|
179 |
| } |
180 |
0
| return null;
|
181 |
| } |
182 |
| |
183 |
159
| public Object invoke() throws Throwable
|
184 |
| { |
185 |
159
| return this.invoke(this.target);
|
186 |
| } |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
159
| protected Object invoke(Object target) throws Throwable
|
197 |
| { |
198 |
159
| Class cl;
|
199 |
159
| Method meth = null;
|
200 |
159
| Object retval = null;
|
201 |
| |
202 |
| |
203 |
159
| if (method_name == null || target == null)
|
204 |
| { |
205 |
0
| if (log.isErrorEnabled()) log.error("method name or target is null");
|
206 |
0
| return null;
|
207 |
| } |
208 |
159
| cl = target.getClass();
|
209 |
159
| try
|
210 |
| { |
211 |
159
| if (this.method != null)
|
212 |
159
| meth = this.method;
|
213 |
| |
214 |
159
| if (meth != null)
|
215 |
| { |
216 |
159
| retval = meth.invoke(target, args);
|
217 |
| } |
218 |
| else |
219 |
| { |
220 |
0
| throw new NoSuchMethodException(method_name);
|
221 |
| } |
222 |
159
| return retval;
|
223 |
| } |
224 |
| catch (InvocationTargetException inv_ex) |
225 |
| { |
226 |
0
| throw inv_ex.getTargetException();
|
227 |
| } |
228 |
| catch (NoSuchMethodException no) |
229 |
| { |
230 |
0
| StringBuffer sb = new StringBuffer();
|
231 |
0
| sb.append("found no method called ").append(method_name).append(" in class ");
|
232 |
0
| sb.append(cl.getName()).append(" with (");
|
233 |
0
| if (args != null)
|
234 |
| { |
235 |
0
| for (int i = 0; i < args.length; i++)
|
236 |
| { |
237 |
0
| if (i > 0)
|
238 |
0
| sb.append(", ");
|
239 |
0
| sb.append((args[i] != null) ? args[i].getClass().getName() : "null");
|
240 |
| } |
241 |
| } |
242 |
0
| sb.append(") formal parameters");
|
243 |
0
| log.error(sb.toString());
|
244 |
0
| throw no;
|
245 |
| } |
246 |
| catch (Throwable e) |
247 |
| { |
248 |
| |
249 |
0
| if (log.isErrorEnabled()) log.error("exception in invoke()", e);
|
250 |
0
| throw e;
|
251 |
| } |
252 |
| } |
253 |
| |
254 |
0
| public Object invoke(Object target, Object[] args) throws Throwable
|
255 |
| { |
256 |
0
| if (args != null)
|
257 |
0
| this.args = args;
|
258 |
0
| return invoke(target);
|
259 |
| } |
260 |
| |
261 |
| |
262 |
0
| Class[] getTypesFromString(Class cl, String[] signature) throws Exception
|
263 |
| { |
264 |
0
| String name;
|
265 |
0
| Class parameter;
|
266 |
0
| Class[] mytypes = new Class[signature.length];
|
267 |
| |
268 |
0
| for (int i = 0; i < signature.length; i++)
|
269 |
| { |
270 |
0
| name = signature[i];
|
271 |
0
| if ("long".equals(name))
|
272 |
0
| parameter = long.class;
|
273 |
0
| else if ("int".equals(name))
|
274 |
0
| parameter = int.class;
|
275 |
0
| else if ("short".equals(name))
|
276 |
0
| parameter = short.class;
|
277 |
0
| else if ("char".equals(name))
|
278 |
0
| parameter = char.class;
|
279 |
0
| else if ("byte".equals(name))
|
280 |
0
| parameter = byte.class;
|
281 |
0
| else if ("float".equals(name))
|
282 |
0
| parameter = float.class;
|
283 |
0
| else if ("double".equals(name))
|
284 |
0
| parameter = double.class;
|
285 |
0
| else if ("boolean".equals(name))
|
286 |
0
| parameter = boolean.class;
|
287 |
| else |
288 |
0
| parameter = Class.forName(name, false, cl.getClassLoader());
|
289 |
0
| mytypes[i] = parameter;
|
290 |
| } |
291 |
0
| return mytypes;
|
292 |
| } |
293 |
| |
294 |
| |
295 |
0
| public String toString()
|
296 |
| { |
297 |
0
| StringBuffer ret = new StringBuffer();
|
298 |
0
| boolean first = true;
|
299 |
0
| if (method_name != null)
|
300 |
0
| ret.append(method_name);
|
301 |
0
| ret.append('(');
|
302 |
0
| if (args != null)
|
303 |
| { |
304 |
0
| for (int i = 0; i < args.length; i++)
|
305 |
| { |
306 |
0
| if (first)
|
307 |
0
| first = false;
|
308 |
| else |
309 |
0
| ret.append(", ");
|
310 |
0
| ret.append(args[i]);
|
311 |
| } |
312 |
| } |
313 |
0
| ret.append(')');
|
314 |
0
| return ret.toString();
|
315 |
| } |
316 |
| |
317 |
0
| public String toStringDetails()
|
318 |
| { |
319 |
0
| StringBuffer ret = new StringBuffer();
|
320 |
0
| ret.append("MethodCall ");
|
321 |
0
| if (method_name != null)
|
322 |
0
| ret.append("name=").append(method_name);
|
323 |
0
| ret.append(", number of args=").append((args != null ? args.length : 0)).append(')');
|
324 |
0
| if (args != null)
|
325 |
| { |
326 |
0
| ret.append("\nArgs:");
|
327 |
0
| for (int i = 0; i < args.length; i++)
|
328 |
| { |
329 |
0
| ret.append("\n[").append(args[i]).append(" (").
|
330 |
0
| append((args[i] != null ? args[i].getClass().getName() : "null")).append(")]");
|
331 |
| } |
332 |
| } |
333 |
0
| return ret.toString();
|
334 |
| } |
335 |
| |
336 |
| } |
337 |
| |