ReflectionHelper's isFirstEntryInArray properly handles empty arrays

Issue: SPR-12522
This commit is contained in:
Juergen Hoeller 2014-12-22 19:54:16 +01:00
parent d40be6cfda
commit fa138d2c70
2 changed files with 24 additions and 52 deletions

View File

@ -310,54 +310,19 @@ public class ReflectionHelper {
return false; return false;
} }
Class<?> type = possibleArray.getClass(); Class<?> type = possibleArray.getClass();
if (type.isArray()) { if (!type.isArray() || Array.getLength(possibleArray) == 0 ||
Class<?> componentType = type.getComponentType(); !ClassUtils.isAssignableValue(type.getComponentType(), value)) {
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;
}
}
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 * Package up the arguments so that they correctly match what is expected in parameterTypes.
* parameterTypes is (int, String[]) because the second parameter was declared String... then if arguments is * For example, if parameterTypes is (int, String[]) because the second parameter was declared String...
* [1,"a","b"] then it must be repackaged as [1,new String[]{"a","b"}] in order to match the expected * then if arguments is [1,"a","b"] then it must be repackaged as [1,new String[]{"a","b"}] in order to
* parameterTypes. * match the expected parameterTypes.
* @param requiredParameterTypes the types of the parameters for the invocation * @param requiredParameterTypes the types of the parameters for the invocation
* @param args the arguments to be setup ready 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 * @return a repackaged array of arguments where any varargs setup has been done
@ -367,10 +332,8 @@ public class ReflectionHelper {
int parameterCount = requiredParameterTypes.length; int parameterCount = requiredParameterTypes.length;
int argumentCount = args.length; int argumentCount = args.length;
if (parameterCount == args.length) { // Check if repackaging is needed...
if (parameterCount != args.length ||
}
else if (parameterCount != args.length ||
requiredParameterTypes[parameterCount - 1] != requiredParameterTypes[parameterCount - 1] !=
(args[argumentCount - 1] != null ? args[argumentCount - 1].getClass() : null)) { (args[argumentCount - 1] != null ? args[argumentCount - 1].getClass() : null)) {

View File

@ -1879,13 +1879,22 @@ public class SpelReproTests extends AbstractExpressionTests {
} }
@Test @Test
public void SPR12502() throws Exception { public void SPR12502() {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("#root.getClass().getName()"); Expression expression = parser.parseExpression("#root.getClass().getName()");
assertEquals(UnnamedUser.class.getName(), expression.getValue(new UnnamedUser())); assertEquals(UnnamedUser.class.getName(), expression.getValue(new UnnamedUser()));
assertEquals(NamedUser.class.getName(), expression.getValue(new NamedUser())); 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 } private static enum ABC { A, B, C }