1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3.jexl342;
18
19 import org.apache.commons.jexl3.JexlEngine;
20 import org.apache.commons.jexl3.JexlException;
21 import org.apache.commons.jexl3.introspection.JexlMethod;
22
23
24
25
26 public class ReferenceMethodExecutor implements JexlMethod {
27
28
29 private final ReferenceUberspect.ReferenceHandler handler;
30
31
32 private final JexlMethod method;
33
34
35
36
37
38
39
40 public ReferenceMethodExecutor(final ReferenceUberspect.ReferenceHandler referenceHandler, final JexlMethod jexlMethod) {
41 if (referenceHandler == null || jexlMethod == null) {
42 throw new IllegalArgumentException("handler and method cant be null");
43 }
44 this.method = jexlMethod;
45 this.handler = referenceHandler;
46 }
47
48
49
50
51
52
53
54 protected Object getReference(final Object opt) {
55 return handler.callGet(opt);
56 }
57
58 @Override
59 public Class<?> getReturnType() {
60 return method != null ? method.getReturnType() : null;
61 }
62
63 @Override
64 public Object invoke(final Object ref, final Object... args) throws Exception {
65 final Object obj = getReference(ref);
66 return obj == null ? null : method.invoke(obj, args);
67 }
68
69 @Override
70 public boolean isCacheable() {
71 return method != null && method.isCacheable();
72 }
73
74 @Override
75 public boolean tryFailed(final Object rval) {
76 return method == null || method.tryFailed(rval);
77 }
78
79 @Override
80 public Object tryInvoke(final String name, final Object ref, final Object... args) throws JexlException.TryFailed {
81 final Object obj = getReference(ref);
82 if (method == null) {
83 return obj == null ? null : JexlEngine.TRY_FAILED;
84 }
85 if (obj == ref) {
86 return JexlEngine.TRY_FAILED;
87 }
88 return method.tryInvoke(name, obj, args);
89 }
90 }