Browse Source

Consistent resolution of Class methods and static methods

Issue: SPR-12502
(cherry picked from commit 9ef0bdc)
pull/718/head
Juergen Hoeller 11 years ago
parent
commit
4c5e17ec3e
  1. 27
      spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java
  2. 20
      spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,12 +17,13 @@ @@ -17,12 +17,13 @@
package org.springframework.expression.spel.support;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@ -106,7 +107,7 @@ public class ReflectiveMethodResolver implements MethodResolver { @@ -106,7 +107,7 @@ public class ReflectiveMethodResolver implements MethodResolver {
try {
TypeConverter typeConverter = context.getTypeConverter();
Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
List<Method> methods = new ArrayList<Method>(Arrays.asList(getMethods(type, targetObject)));
List<Method> methods = new ArrayList<Method>((getMethods(type, targetObject)));
// If a filter is registered for this type, call it
MethodFilter filter = (this.filters != null ? this.filters.get(type) : null);
@ -201,14 +202,22 @@ public class ReflectiveMethodResolver implements MethodResolver { @@ -201,14 +202,22 @@ public class ReflectiveMethodResolver implements MethodResolver {
}
}
private Method[] getMethods(Class<?> type, Object targetObject) {
private Collection<Method> getMethods(Class<?> type, Object targetObject) {
if (targetObject instanceof Class) {
Set<Method> methods = new HashSet<Method>();
methods.addAll(Arrays.asList(getMethods(type)));
methods.addAll(Arrays.asList(getMethods(targetObject.getClass())));
return methods.toArray(new Method[methods.size()]);
Set<Method> result = new LinkedHashSet<Method>();
result.addAll(Arrays.asList(getMethods(targetObject.getClass())));
// Add these also so that static result 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);
}
}
return result;
}
else {
return Arrays.asList(getMethods(type));
}
return getMethods(type);
}
/**

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

@ -1835,6 +1835,14 @@ public class SpelReproTests extends ExpressionTestCase { @@ -1835,6 +1835,14 @@ public class SpelReproTests extends ExpressionTestCase {
assertEquals(1, exp.getValue(sec));
}
@Test
public void SPR12502() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("#root.getClass().getName()");
assertEquals(UnnamedUser.class.getName(), expression.getValue(new UnnamedUser()));
assertEquals(NamedUser.class.getName(), expression.getValue(new NamedUser()));
}
private static enum ABC { A, B, C }
@ -1976,4 +1984,16 @@ public class SpelReproTests extends ExpressionTestCase { @@ -1976,4 +1984,16 @@ public class SpelReproTests extends ExpressionTestCase {
public static final int X = 1;
}
public static class UnnamedUser {
}
public static class NamedUser {
public String getName() {
return "foo";
}
}
}

Loading…
Cancel
Save