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,
final List<? extends V> values) {
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>();
int length = keys.size();
for (int i = 0; i < length; i++) {

View File

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

View File

@ -125,11 +125,10 @@ public final class SpelHelper {
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 =
new ThreadLocal<EvaluationContext>();
private EvaluationContext context;
private final Set<Method> registeredFunctions = new HashSet<Method>();
private final Map<String,Method> registeredMethods =
new ConcurrentHashMap<String, Method>();
@ -177,9 +176,6 @@ public final class SpelHelper {
*/
public SpelHelper registerFunctionsFromClass(final Class<?> clazz) {
registeredFunctions.addAll(filterFunctions(clazz));
synchronized (PARSER) {
context = null;
}
return this;
}
@ -194,7 +190,7 @@ public final class SpelHelper {
for (Constructor<?> constructor : asList(clazz.getConstructors())) {
registeredConstructors.put(
constructor.getDeclaringClass().getSimpleName()
+ Arrays.toString(constructor.getParameterTypes()),
+ Arrays.toString(constructor.getParameterTypes()),
constructor);
}
return this;
@ -254,14 +250,8 @@ public final class SpelHelper {
*/
public <T> T evalExpressions(final String[] expressionStrings,
final Object rootElement, final Class<T> desiredType) {
int length = expressionStrings.length;
Assert.isTrue(length > 0,
"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);
return evalExpressions(
expressionStrings, getEvaluationContext(rootElement), desiredType);
}
/**
@ -288,23 +278,16 @@ public final class SpelHelper {
}
private EvaluationContext getEvaluationContext(final Object rootObject) {
if (context == null) {
synchronized (PARSER) {
if (context == null) {
StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject);
newContext.getMethodResolvers().add(new ImplicitMethodResolver());
newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor());
newContext.setConstructorResolvers(
asList((ConstructorResolver) new ImplicitConstructorResolver()));
for (Method method : registeredFunctions) {
newContext.setVariable(method.getName(), method);
}
newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
context = newContext;
}
}
StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject);
newContext.getMethodResolvers().add(new ImplicitMethodResolver());
newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor());
newContext.setConstructorResolvers(
asList((ConstructorResolver) new ImplicitConstructorResolver()));
for (Method method : registeredFunctions) {
newContext.setVariable(method.getName(), method);
}
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 static String test(String str) {
public static String test(final String str) {
return str;
}
static String testNonPublic(String str) {
static String testNonPublic(final String str) {
return str;
}
public String testNonStatic(String str) {
public String testNonStatic(final String str) {
return str;
}
public static void testVoid(String str) {
public static void testVoid(final String str) {
return;
}
@ -22,14 +22,15 @@ public final class Functions {
return "a";
}
public static String testContext(String str) {
if (SpelHelper.getCurrentContext() == null)
public static String testContext(final String str) {
if (SpelHelper.getCurrentContext() == null) {
throw new AssertionError();
}
return str;
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
return o instanceof Functions;
}
}