Fixed EL resolution against static method with changing target class

Issue: SPR-10452
This commit is contained in:
Juergen Hoeller 2013-09-04 15:37:01 +02:00
parent 476af2e962
commit 5e6044c45b
3 changed files with 63 additions and 31 deletions

View File

@ -19,9 +19,9 @@ package org.springframework.expression;
import org.springframework.core.convert.TypeDescriptor;
/**
* Encapsulates an object and a type descriptor that describes it. The type descriptor can
* hold generic information that would not be accessible through a simple
* {@code getClass()} call on the object.
* Encapsulates an object and a type descriptor that describes it.
* The type descriptor can hold generic information that would not be
* accessible through a simple {@code getClass()} call on the object.
*
* @author Andy Clement
* @author Juergen Hoeller
@ -73,7 +73,7 @@ public class TypedValue {
@Override
public String toString() {
StringBuilder str = new StringBuilder();
str.append("TypedValue: '").append(this.value).append("' of [").append(getTypeDescriptor() + "]");
str.append("TypedValue: '").append(this.value).append("' of [").append(getTypeDescriptor()).append("]");
return str.toString();
}

View File

@ -76,11 +76,11 @@ public class MethodReference extends SpelNodeImpl {
Object value = state.getActiveContextObject().getValue();
TypeDescriptor targetType = state.getActiveContextObject().getTypeDescriptor();
Object[] arguments = getArguments(state);
return getValueInternal(evaluationContext, value, arguments, targetType);
return getValueInternal(evaluationContext, value, targetType, arguments);
}
private TypedValue getValueInternal(EvaluationContext evaluationContext,
Object value, Object[] arguments, TypeDescriptor targetType) {
Object value, TypeDescriptor targetType, Object[] arguments) {
List<TypeDescriptor> argumentTypes = getArgumentTypes(arguments);
if (value == null) {
@ -88,7 +88,7 @@ public class MethodReference extends SpelNodeImpl {
return TypedValue.NULL;
}
MethodExecutor executorToUse = getCachedExecutor(targetType, argumentTypes);
MethodExecutor executorToUse = getCachedExecutor(value, targetType, argumentTypes);
if (executorToUse != null) {
try {
return executorToUse.execute(evaluationContext, value, arguments);
@ -108,7 +108,7 @@ public class MethodReference extends SpelNodeImpl {
// Otherwise the method could not be invoked.
throwSimpleExceptionIfPossible(value, ae);
// at this point we know it wasn't a user problem so worth a retry if a
// At this point we know it wasn't a user problem so worth a retry if a
// better candidate can be found
this.cachedExecutor = null;
}
@ -116,7 +116,8 @@ public class MethodReference extends SpelNodeImpl {
// either there was no accessor or it no longer existed
executorToUse = findAccessorForMethod(this.name, argumentTypes, value, evaluationContext);
this.cachedExecutor = new CachedMethodExecutor(executorToUse, targetType, argumentTypes);
this.cachedExecutor = new CachedMethodExecutor(
executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
try {
return executorToUse.execute(evaluationContext, value, arguments);
}
@ -140,8 +141,7 @@ public class MethodReference extends SpelNodeImpl {
private Object[] getArguments(ExpressionState state) {
Object[] arguments = new Object[getChildCount()];
for (int i = 0; i < arguments.length; i++) {
// Make the root object the active context again for evaluating the parameter
// expressions
// Make the root object the active context again for evaluating the parameter expressions
try {
state.pushActiveContextObject(state.getRootContextObject());
arguments[i] = this.children[i].getValueInternal(state).getValue();
@ -161,32 +161,31 @@ public class MethodReference extends SpelNodeImpl {
return Collections.unmodifiableList(descriptors);
}
private MethodExecutor getCachedExecutor(TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
private MethodExecutor getCachedExecutor(Object value, TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck != null && executorToCheck.isSuitable(target, argumentTypes)) {
if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) {
return executorToCheck.get();
}
this.cachedExecutor = null;
return null;
}
private MethodExecutor findAccessorForMethod(String name,
List<TypeDescriptor> argumentTypes, Object contextObject,
EvaluationContext evaluationContext) throws SpelEvaluationException {
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {
List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
if (methodResolvers != null) {
for (MethodResolver methodResolver : methodResolvers) {
try {
MethodExecutor methodExecutor = methodResolver.resolve(
evaluationContext, contextObject, name, argumentTypes);
evaluationContext, targetObject, name, argumentTypes);
if (methodExecutor != null) {
return methodExecutor;
}
}
catch (AccessException ex) {
throw new SpelEvaluationException(getStartPosition(), ex,
SpelMessage.PROBLEM_LOCATING_METHOD, name, contextObject.getClass());
SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
}
}
}
@ -194,7 +193,7 @@ public class MethodReference extends SpelNodeImpl {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
FormatHelper.formatMethodForMessage(name, argumentTypes),
FormatHelper.formatClassNameForMessage(
contextObject instanceof Class ? ((Class<?>) contextObject) : contextObject.getClass()));
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
}
/**
@ -249,7 +248,7 @@ public class MethodReference extends SpelNodeImpl {
@Override
public TypedValue getValue() {
return getValueInternal(this.evaluationContext, this.value, this.arguments, this.targetType);
return getValueInternal(this.evaluationContext, this.value, this.targetType, this.arguments);
}
@Override
@ -268,18 +267,22 @@ public class MethodReference extends SpelNodeImpl {
private final MethodExecutor methodExecutor;
private final Class<?> staticClass;
private final TypeDescriptor target;
private final List<TypeDescriptor> argumentTypes;
public CachedMethodExecutor(MethodExecutor methodExecutor, TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
public CachedMethodExecutor(MethodExecutor methodExecutor, Class<?> staticClass,
TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
this.methodExecutor = methodExecutor;
this.staticClass = staticClass;
this.target = target;
this.argumentTypes = argumentTypes;
}
public boolean isSuitable(TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
return (this.methodExecutor != null && this.target != null &&
public boolean isSuitable(Object value, TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
return ((this.staticClass == null || this.staticClass.equals(value)) &&
this.target.equals(target) && this.argumentTypes.equals(argumentTypes));
}

View File

@ -16,14 +16,7 @@
package org.springframework.expression.spel;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -38,6 +31,7 @@ import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.BeanResolver;
@ -58,6 +52,9 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeLocator;
import org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Reproduction tests cornering various SpEL JIRA issues.
*
@ -1755,6 +1752,38 @@ public class SpelReproTests extends ExpressionTestCase {
exp.getValue(Arrays.asList("foo", "bar", "baz"));
}
@Test
public void SPR_10452() throws Exception {
SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);
StandardEvaluationContext context = new StandardEvaluationContext();
Expression spel = parser.parseExpression("#enumType.values()");
context.setVariable("enumType", ABC.class);
Object result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(ABC.A, Array.get(result, 0));
assertEquals(ABC.B, Array.get(result, 1));
assertEquals(ABC.C, Array.get(result, 2));
context.setVariable("enumType", XYZ.class);
result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(XYZ.X, Array.get(result, 0));
assertEquals(XYZ.Y, Array.get(result, 1));
assertEquals(XYZ.Z, Array.get(result, 2));
}
private static enum ABC {A, B, C}
private static enum XYZ {X, Y, Z}
public static class BooleanHolder {
private Boolean simpleProperty = true;