|
@@ -1,349 +1,349 @@
|
1
|
|
-/* Copyright 2010 Abhinav Sarkar <abhinav@abhinavsarkar.net>
|
2
|
|
- *
|
3
|
|
- * This file is a part of SpelHelper library.
|
4
|
|
- *
|
5
|
|
- * SpelHelper library is free software: you can redistribute it and/or modify
|
6
|
|
- * it under the terms of the GNU Lesser General Public License (GNU LGPL) as
|
7
|
|
- * published by the Free Software Foundation, either version 3 of the License,
|
8
|
|
- * or (at your option) any later version.
|
9
|
|
- *
|
10
|
|
- * SpelHelper library is distributed in the hope that it will be useful,
|
11
|
|
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
|
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
|
- * GNU Lesser General Public License for more details.
|
14
|
|
- *
|
15
|
|
- * You should have received a copy of the GNU Lesser General Public License
|
16
|
|
- * along with SpelHelper library. If not, see <http://www.gnu.org/licenses/>.
|
17
|
|
- */
|
18
|
|
-package net.abhinavsarkar.spelhelper;
|
19
|
|
-
|
20
|
|
-import static java.util.Arrays.asList;
|
21
|
|
-
|
22
|
|
-import java.lang.reflect.Constructor;
|
23
|
|
-import java.lang.reflect.Method;
|
24
|
|
-import java.lang.reflect.Modifier;
|
25
|
|
-import java.util.ArrayList;
|
26
|
|
-import java.util.Arrays;
|
27
|
|
-import java.util.HashSet;
|
28
|
|
-import java.util.List;
|
29
|
|
-import java.util.Map;
|
30
|
|
-import java.util.Set;
|
31
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
32
|
|
-
|
33
|
|
-import org.springframework.expression.ConstructorResolver;
|
34
|
|
-import org.springframework.expression.EvaluationContext;
|
35
|
|
-import org.springframework.expression.Expression;
|
36
|
|
-import org.springframework.expression.ExpressionParser;
|
37
|
|
-import org.springframework.expression.spel.standard.SpelExpressionParser;
|
38
|
|
-import org.springframework.expression.spel.support.StandardEvaluationContext;
|
39
|
|
-import org.springframework.util.Assert;
|
40
|
|
-
|
41
|
|
-/**
|
42
|
|
- * SpelHelper provides additional functionalities to work with
|
43
|
|
- * [Spring Expression Language (SpEL)][1].
|
44
|
|
- *
|
45
|
|
- * The addition functionalities provided are:
|
46
|
|
- *
|
47
|
|
- * 1. Implicit methods
|
48
|
|
- * 2. Implicit properties
|
49
|
|
- * 3. Simplified extension functions
|
50
|
|
- * 4. Simplified constructors
|
51
|
|
- *
|
52
|
|
- * **Implicit Methods**
|
53
|
|
- *
|
54
|
|
- * Implicit methods allow one to registers methods with SpelHelper and attach
|
55
|
|
- * them to particular classes. After that, when that method is called on an
|
56
|
|
- * object of that particular class inside a SpEL expression, SpelHelper
|
57
|
|
- * redirects the method call to the registered method.
|
58
|
|
- *
|
59
|
|
- * Example: {@link ImplicitMethods#sorted(List)} method is automatically
|
60
|
|
- * registered by SpelHelper. The class that the method should be invoked for
|
61
|
|
- * is the type of the first parameter of the method. In this case, the class is
|
62
|
|
- * {@link List}.
|
63
|
|
- *
|
64
|
|
- * So when an expression like `"#list(1,4,2).sorted()"` is evaluated, the
|
65
|
|
- * {@link ImplicitMethods#sorted(List)} method is invoked with the list as its
|
66
|
|
- * first parameter and its return value is used in further evaluation of the
|
67
|
|
- * expression.
|
68
|
|
- *
|
69
|
|
- * See {@link SpelHelper#registerImplicitMethodsFromClass(Class)}.
|
70
|
|
- *
|
71
|
|
- * **Implicit Properties**
|
72
|
|
- *
|
73
|
|
- * Implicit properties allow one to treat no argument methods of an object
|
74
|
|
- * as properties of the object. SpelHelper intercepts the property resolution
|
75
|
|
- * of SpEL and if the property name is same as some no-arg method of the target
|
76
|
|
- * object then it invokes the method on the object and provides its return value
|
77
|
|
- * as the property value for further evaluation of the expression.
|
78
|
|
- *
|
79
|
|
- * Example: Using implicit properties, the example of implicit methods can be
|
80
|
|
- * written as: `"#list(1,4,2).sorted"` - dropping the parens - and it will return
|
81
|
|
- * the same value as the last example.
|
82
|
|
- *
|
83
|
|
- * Implicit property resolution considers both the actual methods of the object
|
84
|
|
- * and the implicit methods registered on the object's class.
|
85
|
|
- *
|
86
|
|
- * **Simplified extension functions**
|
87
|
|
- *
|
88
|
|
- * SpEL [allows][2] to register extension function on the context by providing a
|
89
|
|
- * name and a {@link Method} object. SpelHelper simplifies this by taking a class
|
90
|
|
- * and registering all the `public static` methods of the class which do not
|
91
|
|
- * have a `void` return type. The methods are registered by their simple name.
|
92
|
|
- *
|
93
|
|
- * Example: All the methods of {@link ExtensionFunctions} class are automatically
|
94
|
|
- * registered by SpelHelper. Hence the method {@link ExtensionFunctions#list(Object...)}
|
95
|
|
- * can be called from inside a SpEL expression using the function call syntax:
|
96
|
|
- * `"#list(1,2,3)`".
|
97
|
|
- *
|
98
|
|
- * See {@link SpelHelper#registerFunctionsFromClass(Class)}.
|
99
|
|
- *
|
100
|
|
- * **Simplified constructors**
|
101
|
|
- *
|
102
|
|
- * SpEL [allows][3] calling constructors from inside a SpEL expression using the
|
103
|
|
- * `new` operator. But they have to be called with their full name like:
|
104
|
|
- * `"new org.example.Foo('bar')"`. SpelHelper simplifies this by taking a class
|
105
|
|
- * and registering all its public constructors to the SpEL context by their
|
106
|
|
- * simple name.
|
107
|
|
- *
|
108
|
|
- * Example: After registering the `org.example.Foo` class with SpelHelper, its
|
109
|
|
- * constructor can be called from inside a SpEL expression by: `"new Foo('bar')"`.
|
110
|
|
- *
|
111
|
|
- * See {@link SpelHelper#registerConstructorsFromClass(Class)}.
|
112
|
|
- *
|
113
|
|
- * In addition to all the above functionalities, SpelHelper automatically registers
|
114
|
|
- * some extension functions and implicit methods which are always available in
|
115
|
|
- * the SpEL expressions evaluated through SpelHelper. See {@link ExtensionFunctions}
|
116
|
|
- * and {@link ImplicitMethods} for further details.
|
117
|
|
- *
|
118
|
|
- * [1]: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html
|
119
|
|
- * [2]: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#expressions-ref-functions
|
120
|
|
- * [3]: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#d0e11927
|
121
|
|
- *
|
122
|
|
- * @author Abhinav Sarkar _abhinav@abhinavsarkar.net_
|
123
|
|
- */
|
124
|
|
-public final class SpelHelper {
|
125
|
|
-
|
126
|
|
- static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName();
|
127
|
|
-
|
128
|
|
- private final ExpressionParser PARSER = new SpelExpressionParser();
|
129
|
|
- private static final ThreadLocal<EvaluationContext> CURRENT_CONTEXT =
|
130
|
|
- new ThreadLocal<EvaluationContext>();
|
131
|
|
-
|
132
|
|
- private final Set<Method> registeredFunctions = new HashSet<Method>();
|
133
|
|
- private final Map<String,Method> registeredMethods =
|
134
|
|
- new ConcurrentHashMap<String, Method>();
|
135
|
|
- private final Map<String,Constructor<?>> registeredConstructors =
|
136
|
|
- new ConcurrentHashMap<String, Constructor<?>>();
|
137
|
|
-
|
138
|
|
- /**
|
139
|
|
- * Creates an instance of SpelHelper.
|
140
|
|
- */
|
141
|
|
- public SpelHelper() {
|
142
|
|
- registerFunctionsFromClass(ExtensionFunctions.class);
|
143
|
|
- registerImplicitMethodsFromClass(ImplicitMethods.class);
|
144
|
|
- }
|
145
|
|
-
|
146
|
|
- /**
|
147
|
|
- * Registers the public static methods in the class `clazz` as implicit
|
148
|
|
- * methods for the class of the first parameter of the methods.
|
149
|
|
- *
|
150
|
|
- * Only registers the public static methods with non void return type and at
|
151
|
|
- * least one argument.
|
152
|
|
- * @see ImplicitMethods
|
153
|
|
- * @param clazz The class to register the methods from.
|
154
|
|
- * @return The current instance of SpelHelper. This is for chaining
|
155
|
|
- * the methods calls.
|
156
|
|
- */
|
157
|
|
- public SpelHelper registerImplicitMethodsFromClass(final Class<?> clazz) {
|
158
|
|
- for (Method method : filterMethods(clazz)) {
|
159
|
|
- registeredMethods.put(String.format(
|
160
|
|
- "%s.%s", method.getParameterTypes()[0].getName(), method.getName()),
|
161
|
|
- method);
|
162
|
|
- }
|
163
|
|
- return this;
|
164
|
|
- }
|
165
|
|
-
|
166
|
|
- /**
|
167
|
|
- * Registers the public static methods in the class `clazz` as functions
|
168
|
|
- * which can be called from SpEL expressions.
|
169
|
|
- * The functions are registered with the simple name of the methods.
|
170
|
|
- *
|
171
|
|
- * Only registers the public static methods with non void return type.
|
172
|
|
- * @see ExtensionFunctions
|
173
|
|
- * @param clazz The class to register the functions from.
|
174
|
|
- * @return The current instance of SpelHelper. This is for chaining
|
175
|
|
- * the methods calls.
|
176
|
|
- */
|
177
|
|
- public SpelHelper registerFunctionsFromClass(final Class<?> clazz) {
|
178
|
|
- registeredFunctions.addAll(filterFunctions(clazz));
|
179
|
|
- return this;
|
180
|
|
- }
|
181
|
|
-
|
182
|
|
- /**
|
183
|
|
- * Registers the public constructors of the class `clazz` so that they
|
184
|
|
- * can be called by their simple name from SpEL expressions.
|
185
|
|
- * @param clazz The class to register the constructors from.
|
186
|
|
- * @return The current instance of SpelHelper. This is for chaining
|
187
|
|
- * the methods calls.
|
188
|
|
- */
|
189
|
|
- public SpelHelper registerConstructorsFromClass(final Class<?> clazz) {
|
190
|
|
- for (Constructor<?> constructor : asList(clazz.getConstructors())) {
|
191
|
|
- registeredConstructors.put(
|
192
|
|
- constructor.getDeclaringClass().getSimpleName()
|
193
|
|
- + Arrays.toString(constructor.getParameterTypes()),
|
194
|
|
- constructor);
|
195
|
|
- }
|
196
|
|
- return this;
|
197
|
|
- }
|
198
|
|
-
|
199
|
|
- /**
|
200
|
|
- * Evaluates a SpEL expression `expressionString` in the context
|
201
|
|
- * of root element `rootElement` and gives back a result of type
|
202
|
|
- * `desiredType`.
|
203
|
|
- * @param <T> The type of the result desired.
|
204
|
|
- * @param expressionString The SpEL expression to evaluate.
|
205
|
|
- * @param rootElement The root element in context of which the expression
|
206
|
|
- * is to be evaluated.
|
207
|
|
- * @param desiredType The class of the result desired.
|
208
|
|
- * @return The result of the evaluation of the expression.
|
209
|
|
- * @see ExpressionParser#parseExpression(String)
|
210
|
|
- * @see Expression#getValue(EvaluationContext, Class)
|
211
|
|
- */
|
212
|
|
- public <T> T evalExpression(final String expressionString,
|
213
|
|
- final Object rootElement, final Class<T> desiredType) {
|
214
|
|
- EvaluationContext evaluationContext = getEvaluationContext(rootElement);
|
215
|
|
- CURRENT_CONTEXT.set(evaluationContext);
|
216
|
|
- T value = evalExpression(expressionString, evaluationContext, desiredType);
|
217
|
|
- CURRENT_CONTEXT.set(null);
|
218
|
|
- return value;
|
219
|
|
- }
|
220
|
|
-
|
221
|
|
- /**
|
222
|
|
- * Evaluates a SpEL expression `expressionString` in the provided
|
223
|
|
- * context `evaluationContext` and gives back a result of type
|
224
|
|
- * `desiredType`.
|
225
|
|
- * @param <T> The type of the result desired.
|
226
|
|
- * @param expressionString The SpEL expression to evaluate.
|
227
|
|
- * @param evaluationContext The context in which the expression is to be evaluated.
|
228
|
|
- * @param desiredType The class of the result desired.
|
229
|
|
- * @return The result of the evaluation of the expression.
|
230
|
|
- * @see ExpressionParser#parseExpression(String)
|
231
|
|
- * @see Expression#getValue(EvaluationContext, Class)
|
232
|
|
- */
|
233
|
|
- public <T> T evalExpression(final String expressionString,
|
234
|
|
- final EvaluationContext evaluationContext, final Class<T> desiredType) {
|
235
|
|
- return PARSER.parseExpression(expressionString)
|
236
|
|
- .getValue(evaluationContext, desiredType);
|
237
|
|
- }
|
238
|
|
-
|
239
|
|
- /**
|
240
|
|
- * Evaluates multiple SpEL expressions and returns the result of the last
|
241
|
|
- * expression.
|
242
|
|
- * @param <T> The type of the result desired.
|
243
|
|
- * @param expressionStrings The SpEL expressions to evaluate.
|
244
|
|
- * @param rootElement The root element in context of which the expressions
|
245
|
|
- * are to be evaluated.
|
246
|
|
- * @param desiredType The class of the result desired.
|
247
|
|
- * @return The result of the evaluation of the last expression.
|
248
|
|
- * @see SpelHelper#evalExpression(String, EvaluationContext, Class)
|
249
|
|
- * @see SpelHelper#evalExpression(String, Object, Class)
|
250
|
|
- */
|
251
|
|
- public <T> T evalExpressions(final String[] expressionStrings,
|
252
|
|
- final Object rootElement, final Class<T> desiredType) {
|
253
|
|
- return evalExpressions(
|
254
|
|
- expressionStrings, getEvaluationContext(rootElement), desiredType);
|
255
|
|
- }
|
256
|
|
-
|
257
|
|
- /**
|
258
|
|
- * Evaluates multiple SpEL expressions and returns the result of the last
|
259
|
|
- * expression.
|
260
|
|
- * @param <T> The type of the result desired.
|
261
|
|
- * @param expressionStrings The SpEL expressions to evaluate.
|
262
|
|
- * @param evaluationContext The context in which the expression is to be evaluated.
|
263
|
|
- * @param desiredType The class of the result desired.
|
264
|
|
- * @return The result of the evaluation of the last expression.
|
265
|
|
- * @see SpelHelper#evalExpression(String, EvaluationContext, Class)
|
266
|
|
- * @see SpelHelper#evalExpression(String, Object, Class)
|
267
|
|
- */
|
268
|
|
- public <T> T evalExpressions(final String[] expressionStrings,
|
269
|
|
- final EvaluationContext evaluationContext, final Class<T> desiredType) {
|
270
|
|
- int length = expressionStrings.length;
|
271
|
|
- Assert.isTrue(length > 0,
|
272
|
|
- "expressionStrings should have length more than 0");
|
273
|
|
- for (int i = 0; i < length - 1; i++) {
|
274
|
|
- evalExpression(expressionStrings[i], evaluationContext, Object.class);
|
275
|
|
- }
|
276
|
|
- return evalExpression(expressionStrings[length - 1],
|
277
|
|
- evaluationContext, desiredType);
|
278
|
|
- }
|
279
|
|
-
|
280
|
|
- private EvaluationContext getEvaluationContext(final Object rootObject) {
|
281
|
|
- StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject);
|
282
|
|
- newContext.getMethodResolvers().add(new ImplicitMethodResolver());
|
283
|
|
- newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor());
|
284
|
|
- newContext.setConstructorResolvers(
|
285
|
|
- asList((ConstructorResolver) new ImplicitConstructorResolver()));
|
286
|
|
- for (Method method : registeredFunctions) {
|
287
|
|
- newContext.setVariable(method.getName(), method);
|
288
|
|
- }
|
289
|
|
- newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
|
290
|
|
- return newContext;
|
291
|
|
- }
|
292
|
|
-
|
293
|
|
- /**
|
294
|
|
- * Looks up an implicit method registered with this instance.
|
295
|
|
- * @param lookup key to lookup which should be of form:
|
296
|
|
- * `method.getParameterTypes()[0].getName() + "." + method.getName()`
|
297
|
|
- * @return The registered method if found, else null.
|
298
|
|
- */
|
299
|
|
- public Method lookupImplicitMethod(final String lookup) {
|
300
|
|
- Assert.notNull(lookup);
|
301
|
|
- return registeredMethods.get(lookup);
|
302
|
|
- }
|
303
|
|
-
|
304
|
|
- /**
|
305
|
|
- * Looks up an implicit constructor registered with this instance.
|
306
|
|
- * @param lookup key to lookup which should be of form:
|
307
|
|
- * `constructor.getDeclaringClass().getSimpleName()`
|
308
|
|
- * `+ Arrays.toString(constructor.getParameterTypes())`
|
309
|
|
- * @return The registered constructor if found, else null.
|
310
|
|
- */
|
311
|
|
- public Constructor<?> lookupImplicitConstructor(final String lookup) {
|
312
|
|
- Assert.notNull(lookup);
|
313
|
|
- return registeredConstructors.get(lookup);
|
314
|
|
- }
|
315
|
|
-
|
316
|
|
- /**
|
317
|
|
- * Returns the current evaluation context. Null if there is no context.
|
318
|
|
- * @return The current evaluation context.
|
319
|
|
- */
|
320
|
|
- public static EvaluationContext getCurrentContext() {
|
321
|
|
- return CURRENT_CONTEXT.get();
|
322
|
|
- }
|
323
|
|
-
|
324
|
|
- private static List<Method> filterMethods(final Class<?> clazz) {
|
325
|
|
- List<Method> allowedMethods = new ArrayList<Method>();
|
326
|
|
- for (Method method : clazz.getMethods()) {
|
327
|
|
- int modifiers = method.getModifiers();
|
328
|
|
- if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
|
329
|
|
- && !method.getReturnType().equals(Void.TYPE)
|
330
|
|
- && method.getParameterTypes().length > 0) {
|
331
|
|
- allowedMethods.add(method);
|
332
|
|
- }
|
333
|
|
- }
|
334
|
|
- return allowedMethods;
|
335
|
|
- }
|
336
|
|
-
|
337
|
|
- private static List<Method> filterFunctions(final Class<?> clazz) {
|
338
|
|
- List<Method> allowedMethods = new ArrayList<Method>();
|
339
|
|
- for (Method method : clazz.getMethods()) {
|
340
|
|
- int modifiers = method.getModifiers();
|
341
|
|
- if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
|
342
|
|
- && !method.getReturnType().equals(Void.TYPE)) {
|
343
|
|
- allowedMethods.add(method);
|
344
|
|
- }
|
345
|
|
- }
|
346
|
|
- return allowedMethods;
|
347
|
|
- }
|
348
|
|
-
|
349
|
|
-}
|
|
1
|
+/* Copyright 2010 Abhinav Sarkar <abhinav@abhinavsarkar.net>
|
|
2
|
+ *
|
|
3
|
+ * This file is a part of SpelHelper library.
|
|
4
|
+ *
|
|
5
|
+ * SpelHelper library is free software: you can redistribute it and/or modify
|
|
6
|
+ * it under the terms of the GNU Lesser General Public License (GNU LGPL) as
|
|
7
|
+ * published by the Free Software Foundation, either version 3 of the License,
|
|
8
|
+ * or (at your option) any later version.
|
|
9
|
+ *
|
|
10
|
+ * SpelHelper library is distributed in the hope that it will be useful,
|
|
11
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+ * GNU Lesser General Public License for more details.
|
|
14
|
+ *
|
|
15
|
+ * You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+ * along with SpelHelper library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+ */
|
|
18
|
+package net.abhinavsarkar.spelhelper;
|
|
19
|
+
|
|
20
|
+import static java.util.Arrays.asList;
|
|
21
|
+
|
|
22
|
+import java.lang.reflect.Constructor;
|
|
23
|
+import java.lang.reflect.Method;
|
|
24
|
+import java.lang.reflect.Modifier;
|
|
25
|
+import java.util.ArrayList;
|
|
26
|
+import java.util.Arrays;
|
|
27
|
+import java.util.HashSet;
|
|
28
|
+import java.util.List;
|
|
29
|
+import java.util.Map;
|
|
30
|
+import java.util.Set;
|
|
31
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
32
|
+
|
|
33
|
+import org.springframework.expression.ConstructorResolver;
|
|
34
|
+import org.springframework.expression.EvaluationContext;
|
|
35
|
+import org.springframework.expression.Expression;
|
|
36
|
+import org.springframework.expression.ExpressionParser;
|
|
37
|
+import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
38
|
+import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
39
|
+import org.springframework.util.Assert;
|
|
40
|
+
|
|
41
|
+/**
|
|
42
|
+ * SpelHelper provides additional functionalities to work with
|
|
43
|
+ * [Spring Expression Language (SpEL)][1].
|
|
44
|
+ *
|
|
45
|
+ * The addition functionalities provided are:
|
|
46
|
+ *
|
|
47
|
+ * 1. Implicit methods
|
|
48
|
+ * 2. Implicit properties
|
|
49
|
+ * 3. Simplified extension functions
|
|
50
|
+ * 4. Simplified constructors
|
|
51
|
+ *
|
|
52
|
+ * **Implicit Methods**
|
|
53
|
+ *
|
|
54
|
+ * Implicit methods allow one to registers methods with SpelHelper and attach
|
|
55
|
+ * them to particular classes. After that, when that method is called on an
|
|
56
|
+ * object of that particular class inside a SpEL expression, SpelHelper
|
|
57
|
+ * redirects the method call to the registered method.
|
|
58
|
+ *
|
|
59
|
+ * Example: {@link ImplicitMethods#sorted(List)} method is automatically
|
|
60
|
+ * registered by SpelHelper. The class that the method should be invoked for
|
|
61
|
+ * is the type of the first parameter of the method. In this case, the class is
|
|
62
|
+ * {@link List}.
|
|
63
|
+ *
|
|
64
|
+ * So when an expression like `"#list(1,4,2).sorted()"` is evaluated, the
|
|
65
|
+ * {@link ImplicitMethods#sorted(List)} method is invoked with the list as its
|
|
66
|
+ * first parameter and its return value is used in further evaluation of the
|
|
67
|
+ * expression.
|
|
68
|
+ *
|
|
69
|
+ * See {@link SpelHelper#registerImplicitMethodsFromClass(Class)}.
|
|
70
|
+ *
|
|
71
|
+ * **Implicit Properties**
|
|
72
|
+ *
|
|
73
|
+ * Implicit properties allow one to treat no argument methods of an object
|
|
74
|
+ * as properties of the object. SpelHelper intercepts the property resolution
|
|
75
|
+ * of SpEL and if the property name is same as some no-arg method of the target
|
|
76
|
+ * object then it invokes the method on the object and provides its return value
|
|
77
|
+ * as the property value for further evaluation of the expression.
|
|
78
|
+ *
|
|
79
|
+ * Example: Using implicit properties, the example of implicit methods can be
|
|
80
|
+ * written as: `"#list(1,4,2).sorted"` - dropping the parens - and it will return
|
|
81
|
+ * the same value as the last example.
|
|
82
|
+ *
|
|
83
|
+ * Implicit property resolution considers both the actual methods of the object
|
|
84
|
+ * and the implicit methods registered on the object's class.
|
|
85
|
+ *
|
|
86
|
+ * **Simplified extension functions**
|
|
87
|
+ *
|
|
88
|
+ * SpEL [allows][2] to register extension function on the context by providing a
|
|
89
|
+ * name and a {@link Method} object. SpelHelper simplifies this by taking a class
|
|
90
|
+ * and registering all the `public static` methods of the class which do not
|
|
91
|
+ * have a `void` return type. The methods are registered by their simple name.
|
|
92
|
+ *
|
|
93
|
+ * Example: All the methods of {@link ExtensionFunctions} class are automatically
|
|
94
|
+ * registered by SpelHelper. Hence the method {@link ExtensionFunctions#list(Object...)}
|
|
95
|
+ * can be called from inside a SpEL expression using the function call syntax:
|
|
96
|
+ * `"#list(1,2,3)`".
|
|
97
|
+ *
|
|
98
|
+ * See {@link SpelHelper#registerFunctionsFromClass(Class)}.
|
|
99
|
+ *
|
|
100
|
+ * **Simplified constructors**
|
|
101
|
+ *
|
|
102
|
+ * SpEL [allows][3] calling constructors from inside a SpEL expression using the
|
|
103
|
+ * `new` operator. But they have to be called with their full name like:
|
|
104
|
+ * `"new org.example.Foo('bar')"`. SpelHelper simplifies this by taking a class
|
|
105
|
+ * and registering all its public constructors to the SpEL context by their
|
|
106
|
+ * simple name.
|
|
107
|
+ *
|
|
108
|
+ * Example: After registering the `org.example.Foo` class with SpelHelper, its
|
|
109
|
+ * constructor can be called from inside a SpEL expression by: `"new Foo('bar')"`.
|
|
110
|
+ *
|
|
111
|
+ * See {@link SpelHelper#registerConstructorsFromClass(Class)}.
|
|
112
|
+ *
|
|
113
|
+ * In addition to all the above functionalities, SpelHelper automatically registers
|
|
114
|
+ * some extension functions and implicit methods which are always available in
|
|
115
|
+ * the SpEL expressions evaluated through SpelHelper. See {@link ExtensionFunctions}
|
|
116
|
+ * and {@link ImplicitMethods} for further details.
|
|
117
|
+ *
|
|
118
|
+ * [1]: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html
|
|
119
|
+ * [2]: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#expressions-ref-functions
|
|
120
|
+ * [3]: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#d0e11927
|
|
121
|
+ *
|
|
122
|
+ * @author Abhinav Sarkar _abhinav@abhinavsarkar.net_
|
|
123
|
+ */
|
|
124
|
+public final class SpelHelper {
|
|
125
|
+
|
|
126
|
+ static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName();
|
|
127
|
+
|
|
128
|
+ private final ExpressionParser PARSER = new SpelExpressionParser();
|
|
129
|
+ private static final ThreadLocal<EvaluationContext> CURRENT_CONTEXT =
|
|
130
|
+ new ThreadLocal<EvaluationContext>();
|
|
131
|
+
|
|
132
|
+ private final Set<Method> registeredFunctions = new HashSet<Method>();
|
|
133
|
+ private final Map<String,Method> registeredMethods =
|
|
134
|
+ new ConcurrentHashMap<String, Method>();
|
|
135
|
+ private final Map<String,Constructor<?>> registeredConstructors =
|
|
136
|
+ new ConcurrentHashMap<String, Constructor<?>>();
|
|
137
|
+
|
|
138
|
+ /**
|
|
139
|
+ * Creates an instance of SpelHelper.
|
|
140
|
+ */
|
|
141
|
+ public SpelHelper() {
|
|
142
|
+ registerFunctionsFromClass(ExtensionFunctions.class);
|
|
143
|
+ registerImplicitMethodsFromClass(ImplicitMethods.class);
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ /**
|
|
147
|
+ * Registers the public static methods in the class `clazz` as implicit
|
|
148
|
+ * methods for the class of the first parameter of the methods.
|
|
149
|
+ *
|
|
150
|
+ * Only registers the public static methods with non void return type and at
|
|
151
|
+ * least one argument.
|
|
152
|
+ * @see ImplicitMethods
|
|
153
|
+ * @param clazz The class to register the methods from.
|
|
154
|
+ * @return The current instance of SpelHelper. This is for chaining
|
|
155
|
+ * the methods calls.
|
|
156
|
+ */
|
|
157
|
+ public SpelHelper registerImplicitMethodsFromClass(final Class<?> clazz) {
|
|
158
|
+ for (Method method : filterMethods(clazz)) {
|
|
159
|
+ registeredMethods.put(String.format(
|
|
160
|
+ "%s.%s", method.getParameterTypes()[0].getName(), method.getName()),
|
|
161
|
+ method);
|
|
162
|
+ }
|
|
163
|
+ return this;
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ /**
|
|
167
|
+ * Registers the public static methods in the class `clazz` as functions
|
|
168
|
+ * which can be called from SpEL expressions.
|
|
169
|
+ * The functions are registered with the simple name of the methods.
|
|
170
|
+ *
|
|
171
|
+ * Only registers the public static methods with non void return type.
|
|
172
|
+ * @see ExtensionFunctions
|
|
173
|
+ * @param clazz The class to register the functions from.
|
|
174
|
+ * @return The current instance of SpelHelper. This is for chaining
|
|
175
|
+ * the methods calls.
|
|
176
|
+ */
|
|
177
|
+ public SpelHelper registerFunctionsFromClass(final Class<?> clazz) {
|
|
178
|
+ registeredFunctions.addAll(filterFunctions(clazz));
|
|
179
|
+ return this;
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ /**
|
|
183
|
+ * Registers the public constructors of the class `clazz` so that they
|
|
184
|
+ * can be called by their simple name from SpEL expressions.
|
|
185
|
+ * @param clazz The class to register the constructors from.
|
|
186
|
+ * @return The current instance of SpelHelper. This is for chaining
|
|
187
|
+ * the methods calls.
|
|
188
|
+ */
|
|
189
|
+ public SpelHelper registerConstructorsFromClass(final Class<?> clazz) {
|
|
190
|
+ for (Constructor<?> constructor : asList(clazz.getConstructors())) {
|
|
191
|
+ registeredConstructors.put(
|
|
192
|
+ constructor.getDeclaringClass().getSimpleName()
|
|
193
|
+ + Arrays.toString(constructor.getParameterTypes()),
|
|
194
|
+ constructor);
|
|
195
|
+ }
|
|
196
|
+ return this;
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ /**
|
|
200
|
+ * Evaluates a SpEL expression `expressionString` in the context
|
|
201
|
+ * of root element `rootElement` and gives back a result of type
|
|
202
|
+ * `desiredType`.
|
|
203
|
+ * @param <T> The type of the result desired.
|
|
204
|
+ * @param expressionString The SpEL expression to evaluate.
|
|
205
|
+ * @param rootElement The root element in context of which the expression
|
|
206
|
+ * is to be evaluated.
|
|
207
|
+ * @param desiredType The class of the result desired.
|
|
208
|
+ * @return The result of the evaluation of the expression.
|
|
209
|
+ * @see ExpressionParser#parseExpression(String)
|
|
210
|
+ * @see Expression#getValue(EvaluationContext, Class)
|
|
211
|
+ */
|
|
212
|
+ public <T> T evalExpression(final String expressionString,
|
|
213
|
+ final Object rootElement, final Class<T> desiredType) {
|
|
214
|
+ EvaluationContext evaluationContext = getEvaluationContext(rootElement);
|
|
215
|
+ CURRENT_CONTEXT.set(evaluationContext);
|
|
216
|
+ T value = evalExpression(expressionString, evaluationContext, desiredType);
|
|
217
|
+ CURRENT_CONTEXT.set(null);
|
|
218
|
+ return value;
|
|
219
|
+ }
|
|
220
|
+
|
|
221
|
+ /**
|
|
222
|
+ * Evaluates a SpEL expression `expressionString` in the provided
|
|
223
|
+ * context `evaluationContext` and gives back a result of type
|
|
224
|
+ * `desiredType`.
|
|
225
|
+ * @param <T> The type of the result desired.
|
|
226
|
+ * @param expressionString The SpEL expression to evaluate.
|
|
227
|
+ * @param evaluationContext The context in which the expression is to be evaluated.
|
|
228
|
+ * @param desiredType The class of the result desired.
|
|
229
|
+ * @return The result of the evaluation of the expression.
|
|
230
|
+ * @see ExpressionParser#parseExpression(String)
|
|
231
|
+ * @see Expression#getValue(EvaluationContext, Class)
|
|
232
|
+ */
|
|
233
|
+ public <T> T evalExpression(final String expressionString,
|
|
234
|
+ final EvaluationContext evaluationContext, final Class<T> desiredType) {
|
|
235
|
+ return PARSER.parseExpression(expressionString)
|
|
236
|
+ .getValue(evaluationContext, desiredType);
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ /**
|
|
240
|
+ * Evaluates multiple SpEL expressions and returns the result of the last
|
|
241
|
+ * expression.
|
|
242
|
+ * @param <T> The type of the result desired.
|
|
243
|
+ * @param expressionStrings The SpEL expressions to evaluate.
|
|
244
|
+ * @param rootElement The root element in context of which the expressions
|
|
245
|
+ * are to be evaluated.
|
|
246
|
+ * @param desiredType The class of the result desired.
|
|
247
|
+ * @return The result of the evaluation of the last expression.
|
|
248
|
+ * @see SpelHelper#evalExpression(String, EvaluationContext, Class)
|
|
249
|
+ * @see SpelHelper#evalExpression(String, Object, Class)
|
|
250
|
+ */
|
|
251
|
+ public <T> T evalExpressions(final String[] expressionStrings,
|
|
252
|
+ final Object rootElement, final Class<T> desiredType) {
|
|
253
|
+ return evalExpressions(
|
|
254
|
+ expressionStrings, getEvaluationContext(rootElement), desiredType);
|
|
255
|
+ }
|
|
256
|
+
|
|
257
|
+ /**
|
|
258
|
+ * Evaluates multiple SpEL expressions and returns the result of the last
|
|
259
|
+ * expression.
|
|
260
|
+ * @param <T> The type of the result desired.
|
|
261
|
+ * @param expressionStrings The SpEL expressions to evaluate.
|
|
262
|
+ * @param evaluationContext The context in which the expression is to be evaluated.
|
|
263
|
+ * @param desiredType The class of the result desired.
|
|
264
|
+ * @return The result of the evaluation of the last expression.
|
|
265
|
+ * @see SpelHelper#evalExpression(String, EvaluationContext, Class)
|
|
266
|
+ * @see SpelHelper#evalExpression(String, Object, Class)
|
|
267
|
+ */
|
|
268
|
+ public <T> T evalExpressions(final String[] expressionStrings,
|
|
269
|
+ final EvaluationContext evaluationContext, final Class<T> desiredType) {
|
|
270
|
+ int length = expressionStrings.length;
|
|
271
|
+ Assert.isTrue(length > 0,
|
|
272
|
+ "expressionStrings should have length more than 0");
|
|
273
|
+ for (int i = 0; i < length - 1; i++) {
|
|
274
|
+ evalExpression(expressionStrings[i], evaluationContext, Object.class);
|
|
275
|
+ }
|
|
276
|
+ return evalExpression(expressionStrings[length - 1],
|
|
277
|
+ evaluationContext, desiredType);
|
|
278
|
+ }
|
|
279
|
+
|
|
280
|
+ private EvaluationContext getEvaluationContext(final Object rootObject) {
|
|
281
|
+ StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject);
|
|
282
|
+ newContext.getMethodResolvers().add(new ImplicitMethodResolver());
|
|
283
|
+ newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor());
|
|
284
|
+ newContext.setConstructorResolvers(
|
|
285
|
+ asList((ConstructorResolver) new ImplicitConstructorResolver()));
|
|
286
|
+ for (Method method : registeredFunctions) {
|
|
287
|
+ newContext.setVariable(method.getName(), method);
|
|
288
|
+ }
|
|
289
|
+ newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
|
|
290
|
+ return newContext;
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ /**
|
|
294
|
+ * Looks up an implicit method registered with this instance.
|
|
295
|
+ * @param lookup key to lookup which should be of form:
|
|
296
|
+ * `method.getParameterTypes()[0].getName() + "." + method.getName()`
|
|
297
|
+ * @return The registered method if found, else null.
|
|
298
|
+ */
|
|
299
|
+ public Method lookupImplicitMethod(final String lookup) {
|
|
300
|
+ Assert.notNull(lookup);
|
|
301
|
+ return registeredMethods.get(lookup);
|
|
302
|
+ }
|
|
303
|
+
|
|
304
|
+ /**
|
|
305
|
+ * Looks up an implicit constructor registered with this instance.
|
|
306
|
+ * @param lookup key to lookup which should be of form:
|
|
307
|
+ * `constructor.getDeclaringClass().getSimpleName()`
|
|
308
|
+ * `+ Arrays.toString(constructor.getParameterTypes())`
|
|
309
|
+ * @return The registered constructor if found, else null.
|
|
310
|
+ */
|
|
311
|
+ public Constructor<?> lookupImplicitConstructor(final String lookup) {
|
|
312
|
+ Assert.notNull(lookup);
|
|
313
|
+ return registeredConstructors.get(lookup);
|
|
314
|
+ }
|
|
315
|
+
|
|
316
|
+ /**
|
|
317
|
+ * Returns the current evaluation context. Null if there is no context.
|
|
318
|
+ * @return The current evaluation context.
|
|
319
|
+ */
|
|
320
|
+ public static EvaluationContext getCurrentContext() {
|
|
321
|
+ return CURRENT_CONTEXT.get();
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ private static List<Method> filterMethods(final Class<?> clazz) {
|
|
325
|
+ List<Method> allowedMethods = new ArrayList<Method>();
|
|
326
|
+ for (Method method : clazz.getMethods()) {
|
|
327
|
+ int modifiers = method.getModifiers();
|
|
328
|
+ if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
|
|
329
|
+ && !method.getReturnType().equals(Void.TYPE)
|
|
330
|
+ && method.getParameterTypes().length > 0) {
|
|
331
|
+ allowedMethods.add(method);
|
|
332
|
+ }
|
|
333
|
+ }
|
|
334
|
+ return allowedMethods;
|
|
335
|
+ }
|
|
336
|
+
|
|
337
|
+ private static List<Method> filterFunctions(final Class<?> clazz) {
|
|
338
|
+ List<Method> allowedMethods = new ArrayList<Method>();
|
|
339
|
+ for (Method method : clazz.getMethods()) {
|
|
340
|
+ int modifiers = method.getModifiers();
|
|
341
|
+ if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
|
|
342
|
+ && !method.getReturnType().equals(Void.TYPE)) {
|
|
343
|
+ allowedMethods.add(method);
|
|
344
|
+ }
|
|
345
|
+ }
|
|
346
|
+ return allowedMethods;
|
|
347
|
+ }
|
|
348
|
+
|
|
349
|
+}
|