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