|
1 | 1 | /* |
2 | | - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
40 | 40 | public class ReflectionHelper { |
41 | 41 |
|
42 | 42 | /** |
43 | | - * Compare argument arrays and return information about whether they match. A supplied |
44 | | - * type converter and conversionAllowed flag allow for matches to take into account |
45 | | - * that a type may be transformed into a different type by the converter. |
46 | | - * @param expectedArgTypes the array of types the method/constructor is expecting |
47 | | - * @param suppliedArgTypes the array of types that are being supplied at the point of invocation |
| 43 | + * Compare argument arrays and return information about whether they match. |
| 44 | + * A supplied type converter and conversionAllowed flag allow for matches to take |
| 45 | + * into account that a type may be transformed into a different type by the converter. |
| 46 | + * @param expectedArgTypes the types the method/constructor is expecting |
| 47 | + * @param suppliedArgTypes the types that are being supplied at the point of invocation |
48 | 48 | * @param typeConverter a registered type converter |
49 | | - * @return a MatchInfo object indicating what kind of match it was or null if it was not a match |
| 49 | + * @return a MatchInfo object indicating what kind of match it was, |
| 50 | + * or {@code null} if it was not a match |
50 | 51 | */ |
51 | 52 | static ArgumentsMatchInfo compareArguments( |
52 | 53 | List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) { |
@@ -90,7 +91,7 @@ public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List< |
90 | 91 | int result = 0; |
91 | 92 | for (int i = 0; i < paramTypes.size(); i++) { |
92 | 93 | TypeDescriptor paramType = paramTypes.get(i); |
93 | | - TypeDescriptor argType = argTypes.get(i); |
| 94 | + TypeDescriptor argType = (i < argTypes.size() ? argTypes.get(i) : null); |
94 | 95 | if (argType == null) { |
95 | 96 | if (paramType.isPrimitive()) { |
96 | 97 | return Integer.MAX_VALUE; |
@@ -127,13 +128,15 @@ else if (ClassUtils.isAssignable(paramTypeClazz, superClass)) { |
127 | 128 | } |
128 | 129 |
|
129 | 130 | /** |
130 | | - * Compare argument arrays and return information about whether they match. A supplied type converter and |
131 | | - * conversionAllowed flag allow for matches to take into account that a type may be transformed into a different |
132 | | - * type by the converter. This variant of compareArguments also allows for a varargs match. |
133 | | - * @param expectedArgTypes the array of types the method/constructor is expecting |
134 | | - * @param suppliedArgTypes the array of types that are being supplied at the point of invocation |
| 131 | + * Compare argument arrays and return information about whether they match. |
| 132 | + * A supplied type converter and conversionAllowed flag allow for matches to |
| 133 | + * take into account that a type may be transformed into a different type by the |
| 134 | + * converter. This variant of compareArguments also allows for a varargs match. |
| 135 | + * @param expectedArgTypes the types the method/constructor is expecting |
| 136 | + * @param suppliedArgTypes the types that are being supplied at the point of invocation |
135 | 137 | * @param typeConverter a registered type converter |
136 | | - * @return a MatchInfo object indicating what kind of match it was or null if it was not a match |
| 138 | + * @return a MatchInfo object indicating what kind of match it was, |
| 139 | + * or {@code null} if it was not a match |
137 | 140 | */ |
138 | 141 | static ArgumentsMatchInfo compareArgumentsVarargs( |
139 | 142 | List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) { |
@@ -246,12 +249,14 @@ public static boolean convertAllArguments(TypeConverter converter, Object[] argu |
246 | 249 | * @param converter the type converter to use for attempting conversions |
247 | 250 | * @param arguments the actual arguments that need conversion |
248 | 251 | * @param methodOrCtor the target Method or Constructor |
249 | | - * @param varargsPosition the known position of the varargs argument, if any (null if not varargs) |
250 | | - * @return true if some kind of conversion occurred on an argument |
| 252 | + * @param varargsPosition the known position of the varargs argument, if any |
| 253 | + * ({@code null} if not varargs) |
| 254 | + * @return {@code true} if some kind of conversion occurred on an argument |
251 | 255 | * @throws EvaluationException if a problem occurs during conversion |
252 | 256 | */ |
253 | 257 | static boolean convertArguments(TypeConverter converter, Object[] arguments, Object methodOrCtor, |
254 | 258 | Integer varargsPosition) throws EvaluationException { |
| 259 | + |
255 | 260 | boolean conversionOccurred = false; |
256 | 261 | if (varargsPosition == null) { |
257 | 262 | for (int i = 0; i < arguments.length; i++) { |
@@ -320,9 +325,9 @@ private static boolean isFirstEntryInArray(Object value, Object possibleArray) { |
320 | 325 |
|
321 | 326 | /** |
322 | 327 | * Package up the arguments so that they correctly match what is expected in parameterTypes. |
323 | | - * For example, if parameterTypes is (int, String[]) because the second parameter was declared String... |
324 | | - * then if arguments is [1,"a","b"] then it must be repackaged as [1,new String[]{"a","b"}] in order to |
325 | | - * match the expected parameterTypes. |
| 328 | + * For example, if parameterTypes is {@code (int, String[])} because the second parameter |
| 329 | + * was declared {@code String...}, then if arguments is {@code [1,"a","b"]} then it must be |
| 330 | + * repackaged as {@code [1,new String[]{"a","b"}]} in order to match the expected types. |
326 | 331 | * @param requiredParameterTypes the types of the parameters for the invocation |
327 | 332 | * @param args the arguments to be setup ready for the invocation |
328 | 333 | * @return a repackaged array of arguments where any varargs setup has been done |
@@ -374,10 +379,11 @@ static enum ArgumentsMatchKind { |
374 | 379 |
|
375 | 380 |
|
376 | 381 | /** |
377 | | - * An instance of ArgumentsMatchInfo describes what kind of match was achieved between two sets of arguments - |
378 | | - * the set that a method/constructor is expecting and the set that are being supplied at the point of invocation. |
379 | | - * If the kind indicates that conversion is required for some of the arguments then the arguments that require |
380 | | - * conversion are listed in the argsRequiringConversion array. |
| 382 | + * An instance of ArgumentsMatchInfo describes what kind of match was achieved |
| 383 | + * between two sets of arguments - the set that a method/constructor is expecting |
| 384 | + * and the set that are being supplied at the point of invocation. If the kind |
| 385 | + * indicates that conversion is required for some of the arguments then the arguments |
| 386 | + * that require conversion are listed in the argsRequiringConversion array. |
381 | 387 | */ |
382 | 388 | static class ArgumentsMatchInfo { |
383 | 389 |
|
|
0 commit comments