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

  • Parsing an XML file within a Jenkins pipeline
  • In jenkins job, create file using system groovy in current workspace
  • Get gradle variables in jenkins pipeline script
  • Run a remote command on all Jenkins slaves via Masters's script console
  • Setting an upstreamproject in Jenkins using groovy
  • how to modify/add/delete tool installations on a Jenkins slave using Groovy?
  • 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
    

  • How to use Jenkins pre-build branch merging only for branches I want to merge?
  • How to use environment variables in a groovy function using a Jenkinsfile
  • Starting Jenkins in Docker Container
  • Jenkins 2 - How to get user role (Role Strategy Plugin) from Jenkins Workflow - Pipeline Plugin
  • How do I deploy to private Maven repo from CloudBees?
  • Jenkins detects SVN changes but doesn't update workspace?
  • 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.