Skip to content

Commit 43bcab9

Browse files
committed
ReflectiveMethodResolver lets local static methods override java.lang.Class methods
Issue: SPR-13918
1 parent 903ae48 commit 43bcab9

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,15 @@ else if (matchRequiringConversion != null) {
221221
private Collection<Method> getMethods(Class<?> type, Object targetObject) {
222222
if (targetObject instanceof Class) {
223223
Set<Method> result = new LinkedHashSet<Method>();
224-
result.addAll(Arrays.asList(getMethods(targetObject.getClass())));
225-
// Add these also so that static result are invocable on the type: e.g. Float.valueOf(..)
224+
// Add these so that static methods are invocable on the type: e.g. Float.valueOf(..)
226225
Method[] methods = getMethods(type);
227226
for (Method method : methods) {
228227
if (Modifier.isStatic(method.getModifiers())) {
229228
result.add(method);
230229
}
231230
}
231+
// Also expose methods from java.lang.Class itself
232+
result.addAll(Arrays.asList(getMethods(Class.class)));
232233
return result;
233234
}
234235
else {

spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Array;
2020
import java.lang.reflect.Field;
2121
import java.lang.reflect.Method;
22+
import java.nio.charset.Charset;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.Collection;
@@ -36,6 +37,7 @@
3637
import org.junit.Rule;
3738
import org.junit.Test;
3839
import org.junit.rules.ExpectedException;
40+
3941
import org.springframework.core.MethodParameter;
4042
import org.springframework.core.convert.TypeDescriptor;
4143
import org.springframework.expression.AccessException;
@@ -2080,6 +2082,17 @@ public void SPR10417_maps() {
20802082
assertEquals("{X=66}", result.toString());
20812083
}
20822084

2085+
@Test
2086+
public void SPR13918() {
2087+
EvaluationContext context = new StandardEvaluationContext();
2088+
context.setVariable("encoding", "UTF-8");
2089+
2090+
Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
2091+
Object result = ex.getValue(context);
2092+
assertEquals(Charset.forName("UTF-8"), result);
2093+
}
2094+
2095+
20832096
public static class ListOf {
20842097

20852098
private final double value;
@@ -2093,6 +2106,7 @@ public double getValue() {
20932106
}
20942107
}
20952108

2109+
20962110
public static class BeanClass {
20972111

20982112
private final List<ListOf> list;

0 commit comments

Comments
 (0)