Skip to content

Commit cf3e460

Browse files
committed
ReflectiveMethodResolver lets local static methods override java.lang.Class methods
Issue: SPR-13918 (cherry picked from commit 43bcab9)
1 parent c960d97 commit cf3e460

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;
@@ -2051,6 +2053,17 @@ public void SPR10417_maps() {
20512053
assertEquals("{X=66}", result.toString());
20522054
}
20532055

2056+
@Test
2057+
public void SPR13918() {
2058+
EvaluationContext context = new StandardEvaluationContext();
2059+
context.setVariable("encoding", "UTF-8");
2060+
2061+
Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
2062+
Object result = ex.getValue(context);
2063+
assertEquals(Charset.forName("UTF-8"), result);
2064+
}
2065+
2066+
20542067
public static class ListOf {
20552068

20562069
private final double value;
@@ -2064,6 +2077,7 @@ public double getValue() {
20642077
}
20652078
}
20662079

2080+
20672081
public static class BeanClass {
20682082

20692083
private final List<ListOf> list;

0 commit comments

Comments
 (0)