Browse Source

ReflectionHelper's isFirstEntryInArray properly handles empty arrays

Issue: SPR-12522
pull/709/head
Juergen Hoeller 10 years ago
parent
commit
fa138d2c70
  1. 65
      spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java
  2. 11
      spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

65
spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java

@ -281,8 +281,8 @@ public class ReflectionHelper { @@ -281,8 +281,8 @@ public class ReflectionHelper {
// 1) the input argument was already compatible (ie. array of valid type) and nothing was done
// 2) the input argument was correct type but not in an array so it was made into an array
// 3) the input argument was the wrong type and got converted and put into an array
if (argument != arguments[varargsPosition] &&
!isFirstEntryInArray(argument, arguments[varargsPosition])) {
if (argument != arguments[varargsPosition] &&
!isFirstEntryInArray(argument, arguments[varargsPosition])) {
conversionOccurred = true; // case 3
}
}
@ -310,54 +310,19 @@ public class ReflectionHelper { @@ -310,54 +310,19 @@ public class ReflectionHelper {
return false;
}
Class<?> type = possibleArray.getClass();
if (type.isArray()) {
Class<?> componentType = type.getComponentType();
if (componentType.isPrimitive()) {
if (componentType == Boolean.TYPE) {
return value instanceof Boolean &&
((boolean[])possibleArray)[0] == (Boolean)value;
}
else if (componentType == Double.TYPE) {
return value instanceof Double &&
((double[])possibleArray)[0] == (Double)value;
}
else if (componentType == Float.TYPE) {
return value instanceof Float &&
((float[])possibleArray)[0] == (Float)value;
}
else if (componentType == Integer.TYPE) {
return value instanceof Integer &&
((int[])possibleArray)[0] == (Integer)value;
}
else if (componentType == Long.TYPE) {
return value instanceof Long &&
((long[])possibleArray)[0] == (Long)value;
}
else if (componentType == Short.TYPE) {
return value instanceof Short &&
((short[])possibleArray)[0] == (Short)value;
}
else if (componentType == Character.TYPE) {
return value instanceof Character &&
((char[])possibleArray)[0] == (Character)value;
}
else if (componentType == Byte.TYPE) {
return value instanceof Byte &&
((byte[])possibleArray)[0] == (Byte)value;
}
}
else {
return ((Object[])possibleArray)[0] == value;
}
if (!type.isArray() || Array.getLength(possibleArray) == 0 ||
!ClassUtils.isAssignableValue(type.getComponentType(), value)) {
return false;
}
return false;
Object arrayValue = Array.get(possibleArray, 0);
return (type.getComponentType().isPrimitive() ? arrayValue.equals(value) : arrayValue == value);
}
/**
* Package up the arguments so that they correctly match what is expected in parameterTypes. For example, if
* parameterTypes is (int, String[]) because the second parameter was declared String... then if arguments is
* [1,"a","b"] then it must be repackaged as [1,new String[]{"a","b"}] in order to match the expected
* parameterTypes.
* Package up the arguments so that they correctly match what is expected in parameterTypes.
* For example, if parameterTypes is (int, String[]) because the second parameter was declared String...
* then if arguments is [1,"a","b"] then it must be repackaged as [1,new String[]{"a","b"}] in order to
* match the expected parameterTypes.
* @param requiredParameterTypes the types of the parameters for the invocation
* @param args the arguments to be setup ready for the invocation
* @return a repackaged array of arguments where any varargs setup has been done
@ -366,11 +331,9 @@ public class ReflectionHelper { @@ -366,11 +331,9 @@ public class ReflectionHelper {
// Check if array already built for final argument
int parameterCount = requiredParameterTypes.length;
int argumentCount = args.length;
if (parameterCount == args.length) {
}
else if (parameterCount != args.length ||
// Check if repackaging is needed...
if (parameterCount != args.length ||
requiredParameterTypes[parameterCount - 1] !=
(args[argumentCount - 1] != null ? args[argumentCount - 1].getClass() : null)) {

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

@ -1879,13 +1879,22 @@ public class SpelReproTests extends AbstractExpressionTests { @@ -1879,13 +1879,22 @@ public class SpelReproTests extends AbstractExpressionTests {
}
@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()));
assertEquals(NamedUser.class.getName(), expression.getValue(new NamedUser()));
}
@Test
public void SPR12522() {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("T(java.util.Arrays).asList('')");
Object value = expression.getValue();
assertTrue(value instanceof List);
assertTrue(((List) value).isEmpty());
}
private static enum ABC { A, B, C }

Loading…
Cancel
Save