From 512d1263f2d93f17beee6a761fc0bb7f38af4010 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 8 Feb 2016 13:23:02 +0100 Subject: [PATCH] ReflectiveMethodResolver lets local static methods override java.lang.Class methods Issue: SPR-13918 (cherry picked from commit cf3e460) --- .../support/ReflectiveMethodResolver.java | 5 ++-- .../expression/spel/SpelReproTests.java | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java index 4c30ab8e099..21d9c40b024 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java @@ -221,14 +221,15 @@ public class ReflectiveMethodResolver implements MethodResolver { private Collection getMethods(Class type, Object targetObject) { if (targetObject instanceof Class) { Set result = new LinkedHashSet(); - 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 { diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java index 5b9351839b5..2ce8660a75c 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java @@ -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; @@ -1836,7 +1837,7 @@ public class SpelReproTests extends ExpressionTestCase { } @Test - public void SPR12502() throws Exception { + public void SPR12502() { SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("#root.getClass().getName()"); assertEquals(UnnamedUser.class.getName(), expression.getValue(new UnnamedUser())); @@ -1844,7 +1845,7 @@ public class SpelReproTests extends ExpressionTestCase { } @Test - public void SPR12803() throws Exception { + public void SPR12803() { StandardEvaluationContext sec = new StandardEvaluationContext(); sec.setVariable("iterable", Collections.emptyList()); SpelExpressionParser parser = new SpelExpressionParser(); @@ -1852,10 +1853,20 @@ public class SpelReproTests extends ExpressionTestCase { assertTrue(expression.getValue(sec) instanceof ArrayList); } + @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); + } + - private static enum ABC { A, B, C } + private enum ABC { A, B, C } - private static enum XYZ { X, Y, Z } + private enum XYZ { X, Y, Z } public static class BooleanHolder { @@ -1882,7 +1893,7 @@ public class SpelReproTests extends ExpressionTestCase { } - private static interface GenericInterface { + private interface GenericInterface { public T getProperty(); } @@ -1909,9 +1920,9 @@ public class SpelReproTests extends ExpressionTestCase { } - public static interface StaticFinal { + public interface StaticFinal { - public static final String VALUE = "interfaceValue"; + String VALUE = "interfaceValue"; } @@ -1988,6 +1999,7 @@ public class SpelReproTests extends ExpressionTestCase { } + @SuppressWarnings({"rawtypes", "serial"}) public static class MapWithConstant extends HashMap { public static final int X = 1;