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 {
@ -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

@ -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};
}
@ -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,11 +684,10 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
@Test
@SuppressWarnings("unchecked")
public void testMapOfMap_SPR7244() throws Exception {
Map<String,Object> map = new LinkedHashMap();
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("uri", "http:");
Map nameMap = new LinkedHashMap();
Map<String,String> nameMap = new LinkedHashMap<String,String>();
nameMap.put("givenName", "Arthur");
map.put("value", nameMap);
@ -725,7 +710,7 @@ public class SpringEL300Tests extends ExpressionTestCase {
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ls.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List value = (List)exp.getValue(ctx);
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);
@ -751,7 +736,7 @@ public class SpringEL300Tests extends ExpressionTestCase {
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ms.![key.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List value = (List)exp.getValue(ctx);
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);
@ -816,8 +801,9 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
/**
* 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 {
@ -1025,9 +1011,8 @@ public class SpringEL300Tests extends ExpressionTestCase {
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");
}
@ -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>();