@@ -141,18 +141,18 @@ public Object getResult(int index) throws InterruptedException, ExecutionExcepti
141141 */
142142 public boolean parameterTypesMatch (boolean varargs , Class <?>[] types ) throws InterruptedException , ExecutionException {
143143 // Check the number of parameters and replace the last param type with component type if needed
144+ Class <?> componentType = null ;
144145 if (types .length == results .length ) {
145146 if (varargs ) {
146- types [ types . length - 1 ] = types [types .length - 1 ].getComponentType ();
147+ componentType = boxType ( types [types .length - 1 ].getComponentType () );
147148 }
148149 } else {
149150 if (varargs ) {
150151 int diff = types .length - results .length ;
151152 if (diff > 1 ) {
152153 return false ;
153154 } else if (diff < 1 ) {
154- Class <?> varargsType = types [types .length - 1 ];
155- types [types .length - 1 ] = varargsType .getComponentType ();
155+ componentType = boxType (types [types .length - 1 ].getComponentType ());
156156 }
157157 // if diff == 1 then vargs may be empty and we need to compare the result types
158158 } else {
@@ -165,7 +165,11 @@ public boolean parameterTypesMatch(boolean varargs, Class<?>[] types) throws Int
165165 Object result = getResult (i );
166166 if (result != null ) {
167167 Class <?> resultClass = boxType (result .getClass ());
168- if (!paramType .isAssignableFrom (resultClass )) {
168+ if (!paramType .isAssignableFrom (resultClass )
169+ // For varargs we also try to match the component type
170+ && (componentType == null
171+ || i < (types .length - 1 )
172+ || !componentType .isAssignableFrom (resultClass ))) {
169173 return false ;
170174 }
171175 }
@@ -178,14 +182,30 @@ public boolean parameterTypesMatch(boolean varargs, Class<?>[] types) throws Int
178182
179183 public Object getVarargsResults (int numberOfParameters , Class <?> componentType )
180184 throws InterruptedException , ExecutionException {
185+ // For varargs we want to skip all previous args
181186 int skip = numberOfParameters - 1 ;
182- if (skip < 0 ) {
187+ if (skip < 0 || skip >= results . length ) {
183188 return Array .newInstance (componentType , 0 );
184189 }
190+ Object result = null ;
191+ int capacity = results .length - skip ;
192+ if (numberOfParameters == results .length ) {
193+ // If there is exactly one non-skipped argument
194+ // test if it's not a matching array
195+ result = getResult (skip );
196+ Class <?> resultClass = result .getClass ();
197+ if (resultClass .isArray () && resultClass .getComponentType ().equals (componentType )) {
198+ return result ;
199+ }
200+ skip ++;
201+ }
202+ Object array = Array .newInstance (componentType , capacity );
185203 int idx = 0 ;
186- Object array = Array .newInstance (componentType , results .length - skip );
204+ if (result != null ) {
205+ Array .set (array , idx ++, result );
206+ }
187207 for (int i = skip ; i < results .length ; i ++) {
188- Object result = getResult (i );
208+ result = getResult (i );
189209 Array .set (array , idx ++, result );
190210 }
191211 return array ;
0 commit comments