Skip to content

Commit 512d126

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

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
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: 19 additions & 7 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;
@@ -1836,26 +1837,36 @@ public void SPR11609() {
18361837
}
18371838

18381839
@Test
1839-
public void SPR12502() throws Exception {
1840+
public void SPR12502() {
18401841
SpelExpressionParser parser = new SpelExpressionParser();
18411842
Expression expression = parser.parseExpression("#root.getClass().getName()");
18421843
assertEquals(UnnamedUser.class.getName(), expression.getValue(new UnnamedUser()));
18431844
assertEquals(NamedUser.class.getName(), expression.getValue(new NamedUser()));
18441845
}
18451846

18461847
@Test
1847-
public void SPR12803() throws Exception {
1848+
public void SPR12803() {
18481849
StandardEvaluationContext sec = new StandardEvaluationContext();
18491850
sec.setVariable("iterable", Collections.emptyList());
18501851
SpelExpressionParser parser = new SpelExpressionParser();
18511852
Expression expression = parser.parseExpression("T(org.springframework.expression.spel.SpelReproTests.GuavaLists).newArrayList(#iterable)");
18521853
assertTrue(expression.getValue(sec) instanceof ArrayList);
18531854
}
18541855

1856+
@Test
1857+
public void SPR13918() {
1858+
EvaluationContext context = new StandardEvaluationContext();
1859+
context.setVariable("encoding", "UTF-8");
1860+
1861+
Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
1862+
Object result = ex.getValue(context);
1863+
assertEquals(Charset.forName("UTF-8"), result);
1864+
}
1865+
18551866

1856-
private static enum ABC { A, B, C }
1867+
private enum ABC { A, B, C }
18571868

1858-
private static enum XYZ { X, Y, Z }
1869+
private enum XYZ { X, Y, Z }
18591870

18601871

18611872
public static class BooleanHolder {
@@ -1882,7 +1893,7 @@ public boolean isPrimitiveProperty() {
18821893
}
18831894

18841895

1885-
private static interface GenericInterface<T extends Number> {
1896+
private interface GenericInterface<T extends Number> {
18861897

18871898
public T getProperty();
18881899
}
@@ -1909,9 +1920,9 @@ public static class OnlyBridgeMethod extends PackagePrivateClassWithGetter {
19091920
}
19101921

19111922

1912-
public static interface StaticFinal {
1923+
public interface StaticFinal {
19131924

1914-
public static final String VALUE = "interfaceValue";
1925+
String VALUE = "interfaceValue";
19151926
}
19161927

19171928

@@ -1988,6 +1999,7 @@ public Object resolve(EvaluationContext context, String beanName) throws AccessE
19881999
}
19892000

19902001

2002+
@SuppressWarnings({"rawtypes", "serial"})
19912003
public static class MapWithConstant extends HashMap {
19922004

19932005
public static final int X = 1;

0 commit comments

Comments
 (0)