1 package org.apache.commons.jexl.parser;
2
3 import java.lang.reflect.InvocationTargetException;
4
5 import org.apache.commons.jexl.JexlContext;
6 import org.apache.commons.jexl.util.Introspector;
7 import org.apache.commons.jexl.util.introspection.VelMethod;
8 import org.apache.commons.jexl.util.introspection.Info;
9
10 public class ASTMethod extends SimpleNode
11 {
12 /*** dummy velocity info */
13 private static Info DUMMY = new Info("", 1, 1);
14
15 public ASTMethod(int id)
16 {
17 super(id);
18 }
19
20 public ASTMethod(Parser p, int id)
21 {
22 super(p, id);
23 }
24
25
26 /*** Accept the visitor. **/
27 public Object jjtAccept(ParserVisitor visitor, Object data)
28 {
29 return visitor.visit(this, data);
30 }
31
32 /***
33 * returns the value of itself applied to the object.
34 * We assume that an identifier can be gotten via a get(String)
35 */
36 public Object execute(Object obj, JexlContext jc)
37 throws Exception
38 {
39 String methodName = ((ASTIdentifier)jjtGetChild(0)).val;
40
41 int paramCount = jjtGetNumChildren()-1;
42
43
44
45
46
47 Object params[] = new Object[paramCount];
48
49 try
50 {
51 for (int i=0; i<paramCount; i++)
52 {
53 params[i] = ( (SimpleNode) jjtGetChild(i+1)).value(jc);
54 }
55
56 VelMethod vm = Introspector.getUberspect().getMethod(obj, methodName, params, DUMMY);
57
58 if (vm == null)
59 return null;
60
61 return vm.invoke(obj, params);
62 }
63 catch(InvocationTargetException e)
64 {
65 Throwable t = e.getTargetException();
66
67 if (t instanceof Exception)
68 {
69 throw (Exception) t;
70 }
71
72 throw e;
73 }
74 }
75 }