Polish ReflectiveMethodResolver and unit tests

- Update Javadoc
 - Fix whitespace errors (tabs v. spaces, trailing spaces)
 - Break at 90 chars where sensible
 - Remove dead test code
 - Fix generics warnings, remove @SuppressWarnings
This commit is contained in:
Chris Beams 2012-02-01 17:19:42 +01:00
parent 8980ce712d
commit c7fd03be69
2 changed files with 275 additions and 327 deletions

View File

@ -38,11 +38,14 @@ import org.springframework.expression.spel.SpelMessage;
import org.springframework.util.CollectionUtils;
/**
* A method resolver that uses reflection to locate the method that should be invoked.
* Reflection-based {@link MethodResolver} used by default in
* {@link StandardEvaluationContext} unless explicit method resolvers have been specified.
*
* @author Andy Clement
* @author Juergen Hoeller
* @author Chris Beams
* @since 3.0
* @see StandardEvaluationContext#addMethodResolver(MethodResolver)
*/
public class ReflectiveMethodResolver implements MethodResolver {
@ -92,10 +95,10 @@ public class ReflectiveMethodResolver implements MethodResolver {
// If a filter is registered for this type, call it
MethodFilter filter = (this.filters != null ? this.filters.get(type) : null);
if (filter != null) {
List<Method> methodsForFiltering = new ArrayList<Method>();
for (Method method: methods) {
methodsForFiltering.add(method);
}
List<Method> methodsForFiltering = new ArrayList<Method>();
for (Method method: methods) {
methodsForFiltering.add(method);
}
List<Method> methodsFiltered = filter.filter(methodsForFiltering);
if (CollectionUtils.isEmpty(methodsFiltered)) {
methods = NO_METHODS;
@ -124,7 +127,7 @@ public class ReflectiveMethodResolver implements MethodResolver {
continue;
}
if (method.getName().equals(name)) {
Class[] paramTypes = method.getParameterTypes();
Class<?>[] paramTypes = method.getParameterTypes();
List<TypeDescriptor> paramDescriptors = new ArrayList<TypeDescriptor>(paramTypes.length);
for (int i = 0; i < paramTypes.length; i++) {
paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, i)));

View File

@ -132,8 +132,8 @@ public class SpringEL300Tests extends ExpressionTestCase {
static class Spr5899Class {
public Spr5899Class() {}
public Spr5899Class(Integer i) { }
public Spr5899Class(Integer i, String... s) { }
public Spr5899Class(Integer i) { }
public Spr5899Class(Integer i, String... s) { }
public Integer tryToInvokeWithNull(Integer value) { return value; }
public Integer tryToInvokeWithNull2(int i) { return new Integer(i); }
@ -179,10 +179,9 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
}
@SuppressWarnings("unchecked")
@Test
public void testSPR5804() throws Exception {
Map m = new HashMap();
Map<String,String> m = new HashMap<String,String>();
m.put("foo", "bar");
StandardEvaluationContext eContext = new StandardEvaluationContext(m); // root is a map instance
eContext.addPropertyAccessor(new MapAccessor());
@ -239,11 +238,11 @@ public class SpringEL300Tests extends ExpressionTestCase {
static class MapAccessor implements PropertyAccessor {
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
return (((Map) target).containsKey(name));
return (((Map<?,?>) target).containsKey(name));
}
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name));
return new TypedValue(((Map<?,?>) target).get(name));
}
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
@ -252,10 +251,10 @@ public class SpringEL300Tests extends ExpressionTestCase {
@SuppressWarnings("unchecked")
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
((Map) target).put(name, newValue);
((Map<String, Object>) target).put(name, newValue);
}
public Class[] getSpecificTargetClasses() {
public Class<?>[] getSpecificTargetClasses() {
return new Class[] {Map.class};
}
@ -338,8 +337,8 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
private final Resource resource;
public Resource getResource() {
return resource;
}
return resource;
}
}
static class Resource {
@ -376,7 +375,6 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
/** $ related identifiers */
@SuppressWarnings("unchecked")
@Test
public void testDollarPrefixedIdentifier_SPR7100() {
Holder h = new Holder();
@ -431,7 +429,10 @@ public class SpringEL300Tests extends ExpressionTestCase {
Assert.assertEquals("wobble",name);
}
/** Should be accessing (setting) Goo.wibble field because 'bar' variable evaluates to "wibble" */
/**
* Should be accessing (setting) Goo.wibble field because 'bar' variable evaluates to
* "wibble"
*/
@Test
public void testIndexingAsAPropertyAccess_SPR6968_4() {
Goo g = Goo.instance;
@ -509,7 +510,7 @@ public class SpringEL300Tests extends ExpressionTestCase {
static class Holder {
public Map map = new HashMap();
public Map<String,String> map = new HashMap<String,String>();
}
// ---
@ -553,21 +554,6 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
};
// @Test
// public void testFails() {
//
// StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
// evaluationContext.setVariable("target", new Foo2());
// for (int i = 0; i < 300000; i++) {
// evaluationContext.addPropertyAccessor(new MapAccessor());
// ExpressionParser parser = new SpelExpressionParser();
// Expression expression = parser.parseExpression("#target.execute(payload)");
// Message message = new Message();
// message.setPayload(i+"");
// expression.getValue(evaluationContext, message);
// }
// }
static class Foo2 {
public void execute(String str){
System.out.println("Value: " + str);
@ -698,64 +684,63 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
@Test
@SuppressWarnings("unchecked")
public void testMapOfMap_SPR7244() throws Exception {
Map<String,Object> map = new LinkedHashMap();
map.put("uri", "http:");
Map nameMap = new LinkedHashMap();
nameMap.put("givenName", "Arthur");
map.put("value", nameMap);
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("uri", "http:");
Map<String,String> nameMap = new LinkedHashMap<String,String>();
nameMap.put("givenName", "Arthur");
map.put("value", nameMap);
StandardEvaluationContext ctx = new StandardEvaluationContext(map);
ExpressionParser parser = new SpelExpressionParser();
String el1 = "#root['value'].get('givenName')";
Expression exp = parser.parseExpression(el1);
Object evaluated = exp.getValue(ctx);
Assert.assertEquals("Arthur", evaluated);
StandardEvaluationContext ctx = new StandardEvaluationContext(map);
ExpressionParser parser = new SpelExpressionParser();
String el1 = "#root['value'].get('givenName')";
Expression exp = parser.parseExpression(el1);
Object evaluated = exp.getValue(ctx);
Assert.assertEquals("Arthur", evaluated);
String el2 = "#root['value']['givenName']";
exp = parser.parseExpression(el2);
evaluated = exp.getValue(ctx);
Assert.assertEquals("Arthur",evaluated);
}
String el2 = "#root['value']['givenName']";
exp = parser.parseExpression(el2);
evaluated = exp.getValue(ctx);
Assert.assertEquals("Arthur",evaluated);
}
@Test
public void testProjectionTypeDescriptors_1() throws Exception {
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ls.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List value = (List)exp.getValue(ctx);
// value is list containing [true,false]
Assert.assertEquals(Boolean.class,value.get(0).getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
Assert.assertEquals(null, evaluated.getElementTypeDescriptor());
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ls.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List<?> value = (List<?>)exp.getValue(ctx);
// value is list containing [true,false]
Assert.assertEquals(Boolean.class,value.get(0).getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
Assert.assertEquals(null, evaluated.getElementTypeDescriptor());
}
@Test
public void testProjectionTypeDescriptors_2() throws Exception {
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "as.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
Object[] value = (Object[])exp.getValue(ctx);
// value is array containing [true,false]
Assert.assertEquals(Boolean.class,value[0].getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
Assert.assertEquals(Boolean.class, evaluated.getElementTypeDescriptor().getType());
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "as.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
Object[] value = (Object[])exp.getValue(ctx);
// value is array containing [true,false]
Assert.assertEquals(Boolean.class,value[0].getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
Assert.assertEquals(Boolean.class, evaluated.getElementTypeDescriptor().getType());
}
@Test
public void testProjectionTypeDescriptors_3() throws Exception {
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ms.![key.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List value = (List)exp.getValue(ctx);
// value is list containing [true,false]
Assert.assertEquals(Boolean.class,value.get(0).getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
Assert.assertEquals(null, evaluated.getElementTypeDescriptor());
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ms.![key.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List<?> value = (List<?>)exp.getValue(ctx);
// value is list containing [true,false]
Assert.assertEquals(Boolean.class,value.get(0).getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
Assert.assertEquals(null, evaluated.getElementTypeDescriptor());
}
static class C {
@ -795,29 +780,30 @@ public class SpringEL300Tests extends ExpressionTestCase {
list.add(new D(null));
list.add(new D("zzz"));
StandardEvaluationContext ctx = new StandardEvaluationContext(list);
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext(list);
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "#root.?[a < 'hhh']";
SpelExpression exp = parser.parseRaw(el1);
Object value = exp.getValue(ctx);
assertEquals("[D(aaa), D(bbb), D(null), D(ccc), D(null)]",value.toString());
String el1 = "#root.?[a < 'hhh']";
SpelExpression exp = parser.parseRaw(el1);
Object value = exp.getValue(ctx);
assertEquals("[D(aaa), D(bbb), D(null), D(ccc), D(null)]",value.toString());
String el2 = "#root.?[a > 'hhh']";
SpelExpression exp2 = parser.parseRaw(el2);
Object value2 = exp2.getValue(ctx);
assertEquals("[D(zzz)]",value2.toString());
String el2 = "#root.?[a > 'hhh']";
SpelExpression exp2 = parser.parseRaw(el2);
Object value2 = exp2.getValue(ctx);
assertEquals("[D(zzz)]",value2.toString());
// trim out the nulls first
String el3 = "#root.?[a!=null].?[a < 'hhh']";
SpelExpression exp3 = parser.parseRaw(el3);
Object value3 = exp3.getValue(ctx);
assertEquals("[D(aaa), D(bbb), D(ccc)]",value3.toString());
// trim out the nulls first
String el3 = "#root.?[a!=null].?[a < 'hhh']";
SpelExpression exp3 = parser.parseRaw(el3);
Object value3 = exp3.getValue(ctx);
assertEquals("[D(aaa), D(bbb), D(ccc)]",value3.toString());
}
/**
* Test whether {@link ReflectiveMethodResolver} follows Java Method Invocation Conversion order. And more precisely
* that widening reference conversion is 'higher' than a unboxing conversion.
* Test whether {@link ReflectiveMethodResolver} follows Java Method Invocation
* Conversion order. And more precisely that widening reference conversion is 'higher'
* than a unboxing conversion.
*/
@Test
public void testConversionPriority_8224() throws Exception {
@ -957,109 +943,108 @@ public class SpringEL300Tests extends ExpressionTestCase {
public class ReflectionUtil<T extends Number> {
public Object methodToCall(T param) {
System.out.println(param+" "+param.getClass());
return "Object methodToCall(T param)";
}
public Object methodToCall(T param) {
System.out.println(param+" "+param.getClass());
return "Object methodToCall(T param)";
}
public void foo(int... array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(float...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(double...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(short...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(long...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(boolean...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(char...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(byte...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(int... array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(float...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(double...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(short...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(long...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(boolean...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(char...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void foo(byte...array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void bar(int... array) {
if (array.length==0) {
throw new RuntimeException();
}
}
public void bar(int... array) {
if (array.length==0) {
throw new RuntimeException();
}
}
}
@Test
public void testReservedWords_8228() throws Exception {
// "DIV","EQ","GE","GT","LE","LT","MOD","NE","NOT"
@SuppressWarnings("unused")
class Reserver {
public Reserver getReserver() {
return this;
}
public String NE = "abc";
public String ne = "def";
@SuppressWarnings("unused")
class Reserver {
public Reserver getReserver() {
return this;
}
public String NE = "abc";
public String ne = "def";
public int DIV = 1;
public int div = 3;
public int DIV = 1;
public int div = 3;
public Map m = new HashMap();
public Map<String,String> m = new HashMap<String,String>();
@SuppressWarnings("unchecked")
Reserver() {
m.put("NE","xyz");
}
}
StandardEvaluationContext ctx = new StandardEvaluationContext(new Reserver());
SpelExpressionParser parser = new SpelExpressionParser();
String ex = "getReserver().NE";
SpelExpression exp = null;
exp = parser.parseRaw(ex);
String value = (String)exp.getValue(ctx);
Assert.assertEquals("abc",value);
m.put("NE","xyz");
}
}
StandardEvaluationContext ctx = new StandardEvaluationContext(new Reserver());
SpelExpressionParser parser = new SpelExpressionParser();
String ex = "getReserver().NE";
SpelExpression exp = null;
exp = parser.parseRaw(ex);
String value = (String)exp.getValue(ctx);
Assert.assertEquals("abc",value);
ex = "getReserver().ne";
exp = parser.parseRaw(ex);
value = (String)exp.getValue(ctx);
Assert.assertEquals("def",value);
ex = "getReserver().ne";
exp = parser.parseRaw(ex);
value = (String)exp.getValue(ctx);
Assert.assertEquals("def",value);
ex = "getReserver().m[NE]";
exp = parser.parseRaw(ex);
value = (String)exp.getValue(ctx);
Assert.assertEquals("xyz",value);
ex = "getReserver().m[NE]";
exp = parser.parseRaw(ex);
value = (String)exp.getValue(ctx);
Assert.assertEquals("xyz",value);
ex = "getReserver().DIV";
exp = parser.parseRaw(ex);
Assert.assertEquals(1,exp.getValue(ctx));
ex = "getReserver().DIV";
exp = parser.parseRaw(ex);
Assert.assertEquals(1,exp.getValue(ctx));
ex = "getReserver().div";
exp = parser.parseRaw(ex);
Assert.assertEquals(3,exp.getValue(ctx));
ex = "getReserver().div";
exp = parser.parseRaw(ex);
Assert.assertEquals(3,exp.getValue(ctx));
exp = parser.parseRaw("NE");
Assert.assertEquals("abc",exp.getValue(ctx));
exp = parser.parseRaw("NE");
Assert.assertEquals("abc",exp.getValue(ctx));
}
/**
@ -1089,21 +1074,6 @@ public class SpringEL300Tests extends ExpressionTestCase {
expressionParser.parseExpression("shouldBeFourth").getValue(evaluationContext));
}
// test not quite complete, it doesn't check that the list of resolvers at the end of
// PropertyOrFieldReference.getPropertyAccessorsToTry() doesn't contain duplicates, which
// is what it is trying to test by having a property accessor that returns specific
// classes Integer and Number
// @Test
// public void testPropertyAccessorOrder_8211_2() {
// ExpressionParser expressionParser = new SpelExpressionParser();
// StandardEvaluationContext evaluationContext =
// new StandardEvaluationContext(new Integer(42));
//
// evaluationContext.addPropertyAccessor(new TestPropertyAccessor2());
//
// assertEquals("42", expressionParser.parseExpression("x").getValue(evaluationContext));
// }
class TestPropertyAccessor implements PropertyAccessor {
private String mapName;
public TestPropertyAccessor(String mapName) {
@ -1140,31 +1110,6 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
// class TestPropertyAccessor2 implements PropertyAccessor {
//
// public Class[] getSpecificTargetClasses() {
// return new Class[]{Integer.class,Number.class};
// }
//
// public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
// return true;
// }
//
// public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
// return new TypedValue(target.toString(),TypeDescriptor.valueOf(String.class));
// }
//
// public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
// return false;
// }
//
// public void write(EvaluationContext context, Object target, String name, Object newValue)
// throws AccessException {
// throw new UnsupportedOperationException();
// }
//
// }
class ContextObject {
public Map<String, String> firstContext = new HashMap<String, String>();
public Map<String, String> secondContext = new HashMap<String, String>();