Skip to content

Commit

Permalink
ReflectionHelper's isFirstEntryInArray properly handles empty arrays
Browse files Browse the repository at this point in the history
Issue: SPR-12522
  • Loading branch information
jhoeller committed Dec 22, 2014
1 parent d40be6c commit fa138d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ static boolean convertArguments(TypeConverter converter, Object[] arguments, Obj
// 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
}
}
Expand Down Expand Up @@ -310,54 +310,19 @@ private static boolean isFirstEntryInArray(Object value, Object possibleArray) {
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
Expand All @@ -366,11 +331,9 @@ public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredPar
// 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)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1879,13 +1879,22 @@ public void SPR9735() {
}

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

Expand Down

0 comments on commit fa138d2

Please sign in to comment.