1 /***
2 * MethodOperator.java
3 *
4 * $Author: mballesteros $
5 * $Date: 2003/11/28 19:18:03 $
6 * $Revision: 1.1 $
7 */
8 package net.sf.jec;
9
10 import java.lang.reflect.InvocationTargetException;
11
12 import org.apache.commons.beanutils.MethodUtils;
13
14 /***
15 *
16 * @author mballesteros
17 */
18 public class MethodOperator extends Operator {
19
20 private String methodName;
21
22 /*** Creates a new instance of FunctionOperator */
23 public MethodOperator(String methodName) {
24 this.methodName = methodName;
25 }
26
27 /*** Returns the associated value to the given context and root context
28 * @param rootCtx The root context, needed for operators that require
29 * expression evaluation from root point. Example: Indexer operators
30 * @param ctx The current context where the operator will work over
31 */
32 protected Object directMap(Object rootCtx, Object ctx)
33 throws EvaluationException {
34 int argOpsNum = argOps == null ? 0 : argOps.length;
35 Object[] arguments = new Object[argOpsNum];
36 for (int i = 0; i < argOpsNum; i++) {
37 arguments[i] = argOps[i].apply(rootCtx, rootCtx);
38 }
39 try {
40 return MethodUtils.invokeMethod(ctx, methodName, arguments);
41 } catch (NoSuchMethodException nsme) {
42 nsme.printStackTrace();
43 throw new EvaluationException(
44 "Method doesn't exist: " + toExpressionString());
45 } catch (InvocationTargetException ite) {
46 throw new EvaluationException(
47 "Exception while invoking method: " + toExpressionString());
48 } catch (IllegalAccessException iae) {
49 throw new EvaluationException(
50 "Method not accessible: " + toExpressionString());
51 }
52 }
53
54 /*** Returns an expression String representation for this operator
55 */
56 public String toExpressionString() {
57 StringBuffer sb = new StringBuffer();
58 sb.append((nestedOp == null ? "" : nestedOp + ".")).append(
59 methodName).append(
60 '(');
61 int argOpsNum = argOps == null ? 0 : argOps.length;
62 for (int i = 0; i < argOpsNum; i++) {
63 if (i > 0)
64 sb.append(", ");
65 sb.append(argOps[i].toExpressionString());
66 }
67 sb.append(')');
68 return sb.toString();
69 }
70
71 /*** Returns a function representation String for this operator
72 */
73 public String toFunctionString() {
74 StringBuffer sb = new StringBuffer();
75 sb.append(methodName).append('(');
76 int argOpsNum = argOps == null ? 0 : argOps.length;
77 for (int i = 0; i < argOpsNum; i++) {
78 if (i > 0)
79 sb.append(", ");
80 sb.append(argOps[i].toExpressionString());
81 }
82 sb.append(')');
83 return "Method('"
84 + sb.toString()
85 + "'"
86 + (nestedOp == null ? "" : ", " + nestedOp)
87 + ")";
88 }
89 }
This page was automatically generated by Maven