|
32 | 32 | import org.junit.Test;
|
33 | 33 | import org.junit.rules.ExpectedException;
|
34 | 34 |
|
| 35 | +import org.springframework.core.MethodParameter; |
35 | 36 | import org.springframework.core.convert.TypeDescriptor;
|
36 | 37 | import org.springframework.expression.AccessException;
|
37 | 38 | import org.springframework.expression.BeanResolver;
|
@@ -1777,6 +1778,47 @@ public void SPR_10452() throws Exception {
|
1777 | 1778 | assertEquals(XYZ.Z, Array.get(result, 2));
|
1778 | 1779 | }
|
1779 | 1780 |
|
| 1781 | + @Test |
| 1782 | + public void SPR_9495() throws Exception { |
| 1783 | + SpelParserConfiguration configuration = new SpelParserConfiguration(false, false); |
| 1784 | + ExpressionParser parser = new SpelExpressionParser(configuration); |
| 1785 | + |
| 1786 | + StandardEvaluationContext context = new StandardEvaluationContext(); |
| 1787 | + Expression spel = parser.parseExpression("#enumType.values()"); |
| 1788 | + |
| 1789 | + context.setVariable("enumType", ABC.class); |
| 1790 | + Object result = spel.getValue(context); |
| 1791 | + assertNotNull(result); |
| 1792 | + assertTrue(result.getClass().isArray()); |
| 1793 | + assertEquals(ABC.A, Array.get(result, 0)); |
| 1794 | + assertEquals(ABC.B, Array.get(result, 1)); |
| 1795 | + assertEquals(ABC.C, Array.get(result, 2)); |
| 1796 | + |
| 1797 | + context.addMethodResolver(new MethodResolver() { |
| 1798 | + @Override |
| 1799 | + public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException { |
| 1800 | + return new MethodExecutor() { |
| 1801 | + @Override |
| 1802 | + public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException { |
| 1803 | + try { |
| 1804 | + Method method = XYZ.class.getMethod("values"); |
| 1805 | + Object value = method.invoke(target, arguments); |
| 1806 | + return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value)); |
| 1807 | + } |
| 1808 | + catch (Exception ex) { |
| 1809 | + throw new AccessException(ex.getMessage(), ex); |
| 1810 | + } |
| 1811 | + } |
| 1812 | + }; |
| 1813 | + } |
| 1814 | + }); |
| 1815 | + result = spel.getValue(context); |
| 1816 | + assertNotNull(result); |
| 1817 | + assertTrue(result.getClass().isArray()); |
| 1818 | + assertEquals(XYZ.X, Array.get(result, 0)); |
| 1819 | + assertEquals(XYZ.Y, Array.get(result, 1)); |
| 1820 | + assertEquals(XYZ.Z, Array.get(result, 2)); |
| 1821 | + } |
1780 | 1822 |
|
1781 | 1823 |
|
1782 | 1824 | private static enum ABC {A, B, C}
|
|
0 commit comments