Fixed the issue with cached evaluation context. Other minor fixes

master
Abhinav Sarkar 2011-06-28 16:53:31 +05:30
parent 932e614a50
commit 4316301c51
4 changed files with 23 additions and 39 deletions

View File

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

View File

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

View File

@ -125,11 +125,10 @@ public final class SpelHelper {
static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName(); static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName();
private static final ExpressionParser PARSER = new SpelExpressionParser(); private final ExpressionParser PARSER = new SpelExpressionParser();
private static final ThreadLocal<EvaluationContext> CURRENT_CONTEXT = private static final ThreadLocal<EvaluationContext> CURRENT_CONTEXT =
new ThreadLocal<EvaluationContext>(); new ThreadLocal<EvaluationContext>();
private EvaluationContext context;
private final Set<Method> registeredFunctions = new HashSet<Method>(); private final Set<Method> registeredFunctions = new HashSet<Method>();
private final Map<String,Method> registeredMethods = private final Map<String,Method> registeredMethods =
new ConcurrentHashMap<String, Method>(); new ConcurrentHashMap<String, Method>();
@ -177,9 +176,6 @@ public final class SpelHelper {
*/ */
public SpelHelper registerFunctionsFromClass(final Class<?> clazz) { public SpelHelper registerFunctionsFromClass(final Class<?> clazz) {
registeredFunctions.addAll(filterFunctions(clazz)); registeredFunctions.addAll(filterFunctions(clazz));
synchronized (PARSER) {
context = null;
}
return this; return this;
} }
@ -194,7 +190,7 @@ public final class SpelHelper {
for (Constructor<?> constructor : asList(clazz.getConstructors())) { for (Constructor<?> constructor : asList(clazz.getConstructors())) {
registeredConstructors.put( registeredConstructors.put(
constructor.getDeclaringClass().getSimpleName() constructor.getDeclaringClass().getSimpleName()
+ Arrays.toString(constructor.getParameterTypes()), + Arrays.toString(constructor.getParameterTypes()),
constructor); constructor);
} }
return this; return this;
@ -254,14 +250,8 @@ public final class SpelHelper {
*/ */
public <T> T evalExpressions(final String[] expressionStrings, public <T> T evalExpressions(final String[] expressionStrings,
final Object rootElement, final Class<T> desiredType) { final Object rootElement, final Class<T> desiredType) {
int length = expressionStrings.length; return evalExpressions(
Assert.isTrue(length > 0, expressionStrings, getEvaluationContext(rootElement), desiredType);
"expressionStrings should have length more than 0");
for (int i = 0; i < length - 1; i++) {
evalExpression(expressionStrings[i], rootElement, Object.class);
}
return evalExpression(expressionStrings[length - 1],
rootElement, desiredType);
} }
/** /**
@ -288,23 +278,16 @@ public final class SpelHelper {
} }
private EvaluationContext getEvaluationContext(final Object rootObject) { private EvaluationContext getEvaluationContext(final Object rootObject) {
if (context == null) { StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject);
synchronized (PARSER) { newContext.getMethodResolvers().add(new ImplicitMethodResolver());
if (context == null) { newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor());
StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject); newContext.setConstructorResolvers(
newContext.getMethodResolvers().add(new ImplicitMethodResolver()); asList((ConstructorResolver) new ImplicitConstructorResolver()));
newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor()); for (Method method : registeredFunctions) {
newContext.setConstructorResolvers( newContext.setVariable(method.getName(), method);
asList((ConstructorResolver) new ImplicitConstructorResolver()));
for (Method method : registeredFunctions) {
newContext.setVariable(method.getName(), method);
}
newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
context = newContext;
}
}
} }
return context; newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
return newContext;
} }
/** /**

View File

@ -2,19 +2,19 @@ package net.abhinavsarkar.spelhelper;
public final class Functions { public final class Functions {
public static String test(String str) { public static String test(final String str) {
return str; return str;
} }
static String testNonPublic(String str) { static String testNonPublic(final String str) {
return str; return str;
} }
public String testNonStatic(String str) { public String testNonStatic(final String str) {
return str; return str;
} }
public static void testVoid(String str) { public static void testVoid(final String str) {
return; return;
} }
@ -22,14 +22,15 @@ public final class Functions {
return "a"; return "a";
} }
public static String testContext(String str) { public static String testContext(final String str) {
if (SpelHelper.getCurrentContext() == null) if (SpelHelper.getCurrentContext() == null) {
throw new AssertionError(); throw new AssertionError();
}
return str; return str;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
return o instanceof Functions; return o instanceof Functions;
} }
} }