1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3.internal.introspection;
18
19 import org.apache.commons.jexl3.JexlEngine;
20 import org.apache.commons.jexl3.introspection.JexlMethod;
21 import org.apache.commons.jexl3.introspection.JexlPropertyGet;
22 import org.apache.commons.jexl3.introspection.JexlPropertySet;
23
24
25
26
27
28
29
30
31 abstract class AbstractExecutor {
32
33 public static final Object TRY_FAILED = JexlEngine.TRY_FAILED;
34
35
36
37
38
39
40
41
42 static java.lang.reflect.Method initMarker(final Class<?> clazz, final String name, final Class<?>... parms) {
43 try {
44 return clazz.getMethod(name, parms);
45 } catch (final Exception xnever) {
46 throw new Error(xnever);
47 }
48 }
49
50
51
52
53
54
55 static Integer castInteger(final Object arg) {
56 return arg instanceof Number? ((Number) arg).intValue() : null;
57 }
58
59
60
61
62
63
64 static String castString(final Object arg) {
65 return arg instanceof CharSequence || arg instanceof Integer ? arg.toString() : null;
66 }
67
68
69
70
71
72
73 static Object[] makeArgs(final Object... args) {
74 return args;
75 }
76
77
78
79
80
81
82 static Class<?> classOf(final Object instance) {
83 return instance == null? Object.class : instance.getClass();
84 }
85
86
87 protected final Class<?> objectClass;
88
89 protected final java.lang.reflect.Method method;
90
91
92
93
94
95
96 protected AbstractExecutor(final Class<?> theClass, final java.lang.reflect.Method theMethod) {
97 objectClass = theClass;
98 method = theMethod;
99 }
100
101 @Override
102 public boolean equals(final Object obj) {
103 return this == obj || (obj instanceof AbstractExecutor && equals((AbstractExecutor) obj));
104 }
105
106 @Override
107 public int hashCode() {
108 return method.hashCode();
109 }
110
111
112
113
114
115
116 public boolean equals(final AbstractExecutor arg) {
117
118 if (!this.getClass().equals(arg.getClass())) {
119 return false;
120 }
121 if (!this.getMethod().equals(arg.getMethod())) {
122 return false;
123 }
124 if (!this.getTargetClass().equals(arg.getTargetClass())) {
125 return false;
126 }
127
128 final Object lhsp = this.getTargetProperty();
129 final Object rhsp = arg.getTargetProperty();
130 if (lhsp == null && rhsp == null) {
131 return true;
132 }
133 if (lhsp != null && rhsp != null) {
134 return lhsp.equals(rhsp);
135 }
136 return false;
137 }
138
139
140
141
142
143
144
145 public final boolean isAlive() {
146 return (method != null);
147 }
148
149
150
151
152
153
154
155 public boolean isCacheable() {
156 return method != null;
157 }
158
159
160
161
162
163 public final java.lang.reflect.Method getMethod() {
164 return method;
165 }
166
167
168
169
170
171 public final Class<?> getTargetClass() {
172 return objectClass;
173 }
174
175
176
177
178
179 public Object getTargetProperty() {
180 return null;
181 }
182
183
184
185
186
187 public final String getMethodName() {
188 return method.getName();
189 }
190
191
192
193
194
195
196 public final boolean tryFailed(final Object exec) {
197 return exec == JexlEngine.TRY_FAILED;
198 }
199
200
201
202
203 public abstract static class Get extends AbstractExecutor implements JexlPropertyGet {
204
205
206
207
208
209 protected Get(final Class<?> theClass, final java.lang.reflect.Method theMethod) {
210 super(theClass, theMethod);
211 }
212 }
213
214
215
216
217 public abstract static class Set extends AbstractExecutor implements JexlPropertySet {
218
219
220
221
222
223 protected Set(final Class<?> theClass, final java.lang.reflect.Method theMethod) {
224 super(theClass, theMethod);
225 }
226 }
227
228
229
230
231 public abstract static class Method extends AbstractExecutor implements JexlMethod {
232
233 protected final MethodKey key;
234
235
236
237
238
239
240
241 protected Method(final Class<?> c, final java.lang.reflect.Method m, final MethodKey k) {
242 super(c, m);
243 key = k;
244 }
245
246 @Override
247 public Object getTargetProperty() {
248 return key;
249 }
250
251 @Override
252 public final Class<?> getReturnType() {
253 return method.getReturnType();
254 }
255 }
256 }