Skip to content
Permalink
Browse files

[FIXED JENKINS-47159] Set proper vargs location

We shouldn't be starting looking for vargs until we've got to the
index of the last parameter type *and* that last parameter type is an
array. So...tada.
  • Loading branch information...
abayer committed Sep 28, 2017
1 parent 2876383 commit fdf28858e4309f4e094b30bde47c10e9b5889f6e
@@ -27,7 +27,6 @@
import com.google.common.primitives.Primitives;
import groovy.lang.GString;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -95,12 +94,13 @@ private static boolean matches(@Nonnull Class<?>[] parameterTypes, @Nonnull Obje
Class<?> componentType = parameterTypes[fixedLen].getComponentType();
assert componentType != null;
int arrayLength = parameters.length - fixedLen;

if (arrayLength >= 0) {
if (arrayLength == 1 && parameterTypes[fixedLen].isInstance(parameters[fixedLen])) {
// not a varargs call
return parameters;
} else {
Object array = DefaultTypeTransformation.castToVargsArray(parameters, 0, parameterTypes[fixedLen]);
Object array = DefaultTypeTransformation.castToVargsArray(parameters, arrayLength, parameterTypes[fixedLen]);
Object[] parameters2 = new Object[fixedLen + 1];
System.arraycopy(parameters, 0, parameters2, 0, fixedLen);
parameters2[fixedLen] = array;
@@ -29,9 +29,18 @@
import groovy.lang.GString;
import groovy.lang.Script;
import hudson.EnvVars;
import hudson.model.BooleanParameterValue;
import hudson.model.Hudson;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.StringParameterValue;
import jenkins.model.Jenkins;
import org.codehaus.groovy.runtime.GStringImpl;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.EnumeratingWhitelistTest;
@@ -93,6 +102,18 @@ public static void m2(long x) {}
@Test public void constructorVarargs() throws Exception {
assertEquals(EnvVars.class.getConstructor(), GroovyCallSiteSelector.constructor(EnvVars.class, new Object[0]));
assertEquals(EnvVars.class.getConstructor(String[].class), GroovyCallSiteSelector.constructor(EnvVars.class, new Object[] {"x"}));
Map<String, String> myMap = new HashMap<>();
myMap.put("ONE", "one");
myMap.put("TWO", "two");
assertEquals(EnvVars.class.getConstructor(Map.class), GroovyCallSiteSelector.constructor(EnvVars.class, new Object[] {myMap}));
List<ParameterValue> params = new ArrayList<>();
params.add(new StringParameterValue("someParam", "someValue"));
params.add(new BooleanParameterValue("someBool", true));
params.add(new StringParameterValue("someOtherParam", "someOtherValue"));
assertEquals(ParametersAction.class.getConstructor(List.class),
GroovyCallSiteSelector.constructor(ParametersAction.class, new Object[]{params}));
assertEquals(ParametersAction.class.getConstructor(ParameterValue[].class),
GroovyCallSiteSelector.constructor(ParametersAction.class, params.toArray()));
}

}

0 comments on commit fdf2885

Please sign in to comment.
You can’t perform that action at this time.