Browse Source

Fixed the issue with cached evaluation context. Other minor fixes

Abhinav Sarkar 9 years ago
parent
commit
4316301c51

+ 1
- 1
src/main/java/net/abhinavsarkar/spelhelper/ExtensionFunctions.java View File

@@ -85,7 +85,7 @@ public final class ExtensionFunctions {
85 85
     public static <K,V> Map<K,V> map(final List<? extends K> keys,
86 86
             final List<? extends V> values) {
87 87
         Assert.isTrue(keys.size() == values.size(),
88
-                "There should equal number of keys and values");
88
+                "There should be equal number of keys and values");
89 89
         Map<K,V> map = new HashMap<K,V>();
90 90
         int length = keys.size();
91 91
         for (int i = 0; i < length; i++) {

+ 1
- 1
src/main/java/net/abhinavsarkar/spelhelper/ReadOnlyGenericPropertyAccessor.java View File

@@ -31,7 +31,7 @@ abstract class ReadOnlyGenericPropertyAccessor implements
31 31
         return false;
32 32
     }
33 33
 
34
-    @SuppressWarnings("unchecked")
34
+    @SuppressWarnings("rawtypes")
35 35
     public final Class[] getSpecificTargetClasses() {
36 36
         return null;
37 37
     }

+ 13
- 30
src/main/java/net/abhinavsarkar/spelhelper/SpelHelper.java View File

@@ -125,11 +125,10 @@ public final class SpelHelper {
125 125
 
126 126
     static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName();
127 127
 
128
-    private static final ExpressionParser PARSER = new SpelExpressionParser();
128
+    private final ExpressionParser PARSER = new SpelExpressionParser();
129 129
     private static final ThreadLocal<EvaluationContext> CURRENT_CONTEXT =
130 130
         new ThreadLocal<EvaluationContext>();
131 131
 
132
-    private EvaluationContext context;
133 132
     private final Set<Method> registeredFunctions = new HashSet<Method>();
134 133
     private final Map<String,Method> registeredMethods =
135 134
         new ConcurrentHashMap<String, Method>();
@@ -177,9 +176,6 @@ public final class SpelHelper {
177 176
      */
178 177
     public SpelHelper registerFunctionsFromClass(final Class<?> clazz) {
179 178
         registeredFunctions.addAll(filterFunctions(clazz));
180
-        synchronized (PARSER) {
181
-            context = null;
182
-        }
183 179
         return this;
184 180
     }
185 181
 
@@ -194,7 +190,7 @@ public final class SpelHelper {
194 190
         for (Constructor<?> constructor : asList(clazz.getConstructors())) {
195 191
             registeredConstructors.put(
196 192
                     constructor.getDeclaringClass().getSimpleName()
197
-                    + Arrays.toString(constructor.getParameterTypes()),
193
+                        + Arrays.toString(constructor.getParameterTypes()),
198 194
                     constructor);
199 195
         }
200 196
         return this;
@@ -254,14 +250,8 @@ public final class SpelHelper {
254 250
      */
255 251
     public <T> T evalExpressions(final String[] expressionStrings,
256 252
             final Object rootElement, final Class<T> desiredType) {
257
-        int length = expressionStrings.length;
258
-        Assert.isTrue(length > 0,
259
-                "expressionStrings should have length more than 0");
260
-        for (int i = 0; i < length - 1; i++) {
261
-            evalExpression(expressionStrings[i], rootElement, Object.class);
262
-        }
263
-        return evalExpression(expressionStrings[length - 1],
264
-                rootElement, desiredType);
253
+        return evalExpressions(
254
+                expressionStrings, getEvaluationContext(rootElement), desiredType);
265 255
     }
266 256
 
267 257
     /**
@@ -288,23 +278,16 @@ public final class SpelHelper {
288 278
     }
289 279
 
290 280
     private EvaluationContext getEvaluationContext(final Object rootObject) {
291
-        if (context == null) {
292
-            synchronized (PARSER) {
293
-                if (context == null) {
294
-                    StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject);
295
-                    newContext.getMethodResolvers().add(new ImplicitMethodResolver());
296
-                    newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor());
297
-                    newContext.setConstructorResolvers(
298
-                            asList((ConstructorResolver) new ImplicitConstructorResolver()));
299
-                    for (Method method : registeredFunctions) {
300
-                        newContext.setVariable(method.getName(), method);
301
-                    }
302
-                    newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
303
-                    context = newContext;
304
-                }
305
-            }
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);
306 288
         }
307
-        return context;
289
+        newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
290
+        return newContext;
308 291
     }
309 292
 
310 293
     /**

+ 8
- 7
src/test/java/net/abhinavsarkar/spelhelper/Functions.java View File

@@ -2,19 +2,19 @@ package net.abhinavsarkar.spelhelper;
2 2
 
3 3
 public final class Functions {
4 4
 
5
-    public static String test(String str) {
5
+    public static String test(final String str) {
6 6
         return str;
7 7
     }
8 8
 
9
-    static String testNonPublic(String str) {
9
+    static String testNonPublic(final String str) {
10 10
         return str;
11 11
     }
12 12
 
13
-    public String testNonStatic(String str) {
13
+    public String testNonStatic(final String str) {
14 14
         return str;
15 15
     }
16 16
 
17
-    public static void testVoid(String str) {
17
+    public static void testVoid(final String str) {
18 18
         return;
19 19
     }
20 20
 
@@ -22,14 +22,15 @@ public final class Functions {
22 22
         return "a";
23 23
     }
24 24
 
25
-    public static String testContext(String str) {
26
-        if (SpelHelper.getCurrentContext() == null)
25
+    public static String testContext(final String str) {
26
+        if (SpelHelper.getCurrentContext() == null) {
27 27
             throw new AssertionError();
28
+        }
28 29
         return str;
29 30
     }
30 31
 
31 32
     @Override
32
-    public boolean equals(Object o) {
33
+    public boolean equals(final Object o) {
33 34
         return o instanceof Functions;
34 35
     }
35 36
 }

Loading…
Cancel
Save