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

  • Jenkins Groovy: What triggered the build
  • Jenkins pipeline MissingMethodException: No signature of method:
  • Jenkins Pipeline continue latest build at certain time
  • groovy.lang.MissingPropertyException: No such property: manager for class: Script1
  • How to get currently running job's node name in jenkins using groovy
  • How would I produce JUnit test report for groovy tests, suitable for consumption by Jenkins/Hudson?
  • 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
    

  • Send build artifacts over SSH - could not create directory
  • Can I import a groovy script from a relative directory from a Jenkinsfile?
  • How do I trigger another job from hudson as a pre-build step?
  • How to make few locations use the same svn revision in jenkins
  • Git clone on jenkins master, copy to slave
  • Jenkins and Office365 email notification settings
  • 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.