1 package org.apache.velocity.runtime.parser.node;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.InvocationTargetException;
23
24 import org.apache.commons.lang.ArrayUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.velocity.app.event.EventHandlerUtil;
27 import org.apache.velocity.context.InternalContextAdapter;
28 import org.apache.velocity.exception.MethodInvocationException;
29 import org.apache.velocity.exception.TemplateInitException;
30 import org.apache.velocity.exception.VelocityException;
31 import org.apache.velocity.runtime.RuntimeConstants;
32 import org.apache.velocity.runtime.parser.Parser;
33 import org.apache.velocity.util.introspection.Info;
34 import org.apache.velocity.util.introspection.IntrospectionCacheData;
35 import org.apache.velocity.util.introspection.VelMethod;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class ASTMethod extends SimpleNode
54 {
55 private String methodName = "";
56 private int paramCount = 0;
57
58 protected Info uberInfo;
59
60
61
62
63 protected boolean strictRef = false;
64
65
66
67
68 public ASTMethod(int id)
69 {
70 super(id);
71 }
72
73
74
75
76
77 public ASTMethod(Parser p, int id)
78 {
79 super(p, id);
80 }
81
82
83
84
85 public Object jjtAccept(ParserVisitor visitor, Object data)
86 {
87 return visitor.visit(this, data);
88 }
89
90
91
92
93
94
95
96
97
98 public Object init( InternalContextAdapter context, Object data)
99 throws TemplateInitException
100 {
101 super.init( context, data );
102
103
104
105
106
107 uberInfo = new Info(getTemplateName(),
108 getLine(),getColumn());
109
110
111
112
113 methodName = getFirstToken().image;
114 paramCount = jjtGetNumChildren() - 1;
115
116 strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
117
118 return data;
119 }
120
121
122
123
124
125
126
127
128
129
130 public Object execute(Object o, InternalContextAdapter context)
131 throws MethodInvocationException
132 {
133
134
135
136
137
138
139
140 VelMethod method = null;
141
142 Object [] params = new Object[paramCount];
143
144 try
145 {
146
147
148
149
150
151 final Class[] paramClasses = paramCount > 0 ? new Class[paramCount] : ArrayUtils.EMPTY_CLASS_ARRAY;
152
153 for (int j = 0; j < paramCount; j++)
154 {
155 params[j] = jjtGetChild(j + 1).value(context);
156
157 if (params[j] != null)
158 {
159 paramClasses[j] = params[j].getClass();
160 }
161 }
162
163
164
165
166
167 MethodCacheKey mck = new MethodCacheKey(methodName, paramClasses);
168 IntrospectionCacheData icd = context.icacheGet( mck );
169
170
171
172
173
174
175
176 if ( icd != null && (o != null && icd.contextData == o.getClass()) )
177 {
178
179
180
181
182
183 method = (VelMethod) icd.thingy;
184 }
185 else
186 {
187
188
189
190
191
192 method = rsvc.getUberspect().getMethod(o, methodName, params, new Info(getTemplateName(), getLine(), getColumn()));
193
194 if ((method != null) && (o != null))
195 {
196 icd = new IntrospectionCacheData();
197 icd.contextData = o.getClass();
198 icd.thingy = method;
199
200 context.icachePut( mck, icd );
201 }
202 }
203
204
205
206
207
208
209
210 if (method == null)
211 {
212 if (strictRef)
213 {
214
215 StringBuffer plist = new StringBuffer();
216 for (int i=0; i<params.length; i++)
217 {
218 Class param = paramClasses[i];
219 plist.append(param == null ? "null" : param.getName());
220 if (i < params.length -1) plist.append(", ");
221 }
222 throw new MethodInvocationException("Object '" + o.getClass().getName() +
223 "' does not contain method " + methodName + "(" + plist + ")",
224 null, methodName, uberInfo.getTemplateName(), uberInfo.getLine(), uberInfo.getColumn());
225 }
226 else
227 {
228 return null;
229 }
230 }
231 }
232 catch( MethodInvocationException mie )
233 {
234
235
236
237
238
239
240 throw mie;
241 }
242
243
244
245 catch( RuntimeException e )
246 {
247 throw e;
248 }
249 catch( Exception e )
250 {
251
252
253
254 String msg = "ASTMethod.execute() : exception from introspection";
255 log.error(msg, e);
256 throw new VelocityException(msg, e);
257 }
258
259 try
260 {
261
262
263
264
265
266
267
268
269
270 Object obj = method.invoke(o, params);
271
272 if (obj == null)
273 {
274 if( method.getReturnType() == Void.TYPE)
275 {
276 return "";
277 }
278 }
279
280 return obj;
281 }
282 catch( InvocationTargetException ite )
283 {
284 return handleInvocationException(o, context, ite.getTargetException());
285 }
286
287
288 catch( IllegalArgumentException t )
289 {
290 return handleInvocationException(o, context, t);
291 }
292
293
294
295
296 catch( RuntimeException e )
297 {
298 throw e;
299 }
300 catch( Exception e )
301 {
302 String msg = "ASTMethod.execute() : exception invoking method '"
303 + methodName + "' in " + o.getClass();
304 log.error(msg, e);
305 throw new VelocityException(msg, e);
306 }
307 }
308
309 private Object handleInvocationException(Object o, InternalContextAdapter context, Throwable t)
310 {
311
312
313
314
315
316
317
318
319
320
321
322
323 if (t instanceof Exception)
324 {
325 try
326 {
327 return EventHandlerUtil.methodException( rsvc, context, o.getClass(), methodName, (Exception) t );
328 }
329
330
331
332
333
334
335 catch( Exception e )
336 {
337 throw new MethodInvocationException(
338 "Invocation of method '"
339 + methodName + "' in " + o.getClass()
340 + " threw exception "
341 + e.toString(),
342 e, methodName, getTemplateName(), this.getLine(), this.getColumn());
343 }
344 }
345 else
346 {
347
348
349
350
351 throw new MethodInvocationException(
352 "Invocation of method '"
353 + methodName + "' in " + o.getClass()
354 + " threw exception "
355 + t.toString(),
356 t, methodName, getTemplateName(), this.getLine(), this.getColumn());
357 }
358 }
359
360
361
362
363
364
365
366
367 public static class MethodCacheKey
368 {
369 private final String methodName;
370 private final Class[] params;
371
372 public MethodCacheKey(String methodName, Class[] params)
373 {
374
375
376
377
378 this.methodName = (methodName != null) ? methodName : StringUtils.EMPTY;
379 this.params = (params != null) ? params : ArrayUtils.EMPTY_CLASS_ARRAY;
380 }
381
382
383
384
385 public boolean equals(Object o)
386 {
387
388
389
390
391 if (o instanceof MethodCacheKey)
392 {
393 final MethodCacheKey other = (MethodCacheKey) o;
394 if (params.length == other.params.length &&
395 methodName.equals(other.methodName))
396 {
397 for (int i = 0; i < params.length; ++i)
398 {
399 if (params[i] == null)
400 {
401 if (params[i] != other.params[i])
402 {
403 return false;
404 }
405 }
406 else if (!params[i].equals(other.params[i]))
407 {
408 return false;
409 }
410 }
411 return true;
412 }
413 }
414 return false;
415 }
416
417
418
419
420
421 public int hashCode()
422 {
423 int result = 17;
424
425
426
427
428
429 for (int i = 0; i < params.length; ++i)
430 {
431 final Class param = params[i];
432 if (param != null)
433 {
434 result = result * 37 + param.hashCode();
435 }
436 }
437
438 result = result * 37 + methodName.hashCode();
439
440 return result;
441 }
442 }
443
444
445
446
447
448 public String getMethodName()
449 {
450 return methodName;
451 }
452
453
454 }