ReflectiveMethodResolver lets local static methods override java.lang.Class methods

Issue: SPR-13918
This commit is contained in:
Juergen Hoeller 2016-02-08 13:23:02 +01:00
parent 903ae48382
commit 43bcab9c1a
2 changed files with 17 additions and 2 deletions

View File

@ -221,14 +221,15 @@ public class ReflectiveMethodResolver implements MethodResolver {
private Collection<Method> getMethods(Class<?> type, Object targetObject) {
if (targetObject instanceof Class) {
Set<Method> result = new LinkedHashSet<Method>();
result.addAll(Arrays.asList(getMethods(targetObject.getClass())));
// Add these also so that static result are invocable on the type: e.g. Float.valueOf(..)
// Add these so that static methods are invocable on the type: e.g. Float.valueOf(..)
Method[] methods = getMethods(type);
for (Method method : methods) {
if (Modifier.isStatic(method.getModifiers())) {
result.add(method);
}
}
// Also expose methods from java.lang.Class itself
result.addAll(Arrays.asList(getMethods(Class.class)));
return result;
}
else {

View File

@ -19,6 +19,7 @@ package org.springframework.expression.spel;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -36,6 +37,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
@ -2080,6 +2082,17 @@ public class SpelReproTests extends AbstractExpressionTests {
assertEquals("{X=66}", result.toString());
}
@Test
public void SPR13918() {
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("encoding", "UTF-8");
Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
Object result = ex.getValue(context);
assertEquals(Charset.forName("UTF-8"), result);
}
public static class ListOf {
private final double value;
@ -2093,6 +2106,7 @@ public class SpelReproTests extends AbstractExpressionTests {
}
}
public static class BeanClass {
private final List<ListOf> list;