How do you pass null from Groovy to a Java method with a parameter that is typed as an interface?

I’m trying to use Groovy to script the config of plugins in Jenkins. A few of the plugins, like the GitHub Pull Request Builder plugin don’t expose any straightforward way to configure the plugin outside of the Jenkins web interface (which uses a library named Stapler for binding HTML forms to object instances).

The `Stabl

The plugin implements a org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl#configure method that requires a StaplerRequest interface implementation. The test cases pass null for the request, so I figured I would do the same in Groovy.

However, I can’t convince Groovy to coerce null or NullObject as the first argument to configure(request, formData).

Can Groovy do this?

import org.codehaus.groovy.runtime.NullObject

def descriptor  = org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl
net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject() as net.sf.json.JSONObject;
org.kohsuke.stapler.StaplerRequest stapler = NullObject as org.kohsuke.stapler.StaplerRequest

println ("Is it a stapler request? ${stapler instanceof org.kohsuke.stapler.StaplerRequest}.")

println descriptor.configure(stapler, jsonObject)

Output:

Is it a stapler request? true.
groovy.lang.MissingMethodException: No signature of method: static org.jenkinsci.plugins.ghprb.GhprbTrigger$DescriptorImpl.configure() is applicable for argument types: (Class_delegateProxy, net.sf.json.JSONObject) values: [Class_delegateProxy@31433174, [:]]
Possible solutions: configure(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject), configure(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject), configure(org.kohsuke.stapler.StaplerRequest)

Update

So, Jenkins’ API can be very complicated and less than obvious at times. My reference to the static DescriptorImpl type was wrong. I’d still love to know why the above failed.

def descriptor = jenkins.model.Jenkins.getInstance().getDescriptorByType(org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.class)

//def plugin = org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl
net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
org.kohsuke.stapler.StaplerRequest stapler = null

println ("Is it a stapler request? ${stapler instanceof org.kohsuke.stapler.StaplerRequest}.")

jsonObject.put("serverAPIUrl", "https://api.github.com");
jsonObject.put("username", "user");
jsonObject.put("password", "1111");
jsonObject.put("accessToken", "accessToken");
jsonObject.put("adminlist", "user");
jsonObject.put("allowMembersOfWhitelistedOrgsAsAdmin", "false");
jsonObject.put("publishedURL", "");
jsonObject.put("requestForTestingPhrase", "test this");
jsonObject.put("whitelistPhrase", "");
jsonObject.put("okToTestPhrase", "ok to test");
jsonObject.put("retestPhrase", "retest this please");
jsonObject.put("skipBuildPhrase", "[skip ci]");
jsonObject.put("cron", "*/1 * * * *");
jsonObject.put("useComments", "true");
jsonObject.put("logExcerptLines", "0");
jsonObject.put("unstableAs", "");
jsonObject.put("testMode", "true");
jsonObject.put("autoCloseFailedPullRequests", "false");
jsonObject.put("msgSuccess", "Success");
jsonObject.put("msgFailure", "Failure");

println descriptor.configure(stapler, jsonObject)

Output

Is it a stapler request? false.
true

  • Determine Failed Stage in Jenkins Declaritive Pipeline
  • Jenkins workflow - tests/validation/build before push to remote
  • Is it possible to trigger Jenkins from one specific branch only?
  • Integration-Manager Git Workflow using Jenkins/Hudson
  • Accessing docker host from (jenkins) docker container
  • Jenkins Cron Expression Not Scheduled at Right time
  • How to set up “Git Changelog” in Jenkins?
  • Unable to get Jenkins and Git to play nice
  • One Solution collect form web for “How do you pass null from Groovy to a Java method with a parameter that is typed as an interface?”

    In the first example, you tried to pass in an actual Class instance, not null, into the first method. The second example is right, just assign null to the stapler variable and it gets called fine. As for the “is it a stapler request”, null instanceof anything returns false.

    Another issue is that org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl is a Class instance, but jenkins.model.Jenkins.getInstance().getDescriptorByType(org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.class) returns an instance of org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.

    Git Baby is a git and github fan, let's start git clone.