如何将null从Groovy传递给带有类型化为接口的参数的Java方法?

如何将null从Groovy传递给带有类型化为接口的参数的Java方法?,groovy,jenkins,Groovy,Jenkins,我正在尝试使用Groovy在Jenkins中编写插件配置脚本。一些插件,如,没有在Jenkins web界面(使用名为Stapper的库将HTML表单绑定到对象实例)之外公开任何直接的方式来配置插件 马厩 该插件实现了一种需要StaplerRequest接口实现的方法。是的,所以我想在Groovy中也会这样做 但是,我无法说服Groovy强制null或null对象作为configure(request,formData)的第一个参数 Groovy能做到这一点吗 import org.codeha

我正在尝试使用Groovy在Jenkins中编写插件配置脚本。一些插件,如,没有在Jenkins web界面(使用名为Stapper的库将HTML表单绑定到对象实例)之外公开任何直接的方式来配置插件

马厩

该插件实现了一种需要
StaplerRequest
接口实现的方法。是的,所以我想在Groovy中也会这样做

但是,我无法说服Groovy强制
null
null对象
作为
configure(request,formData)
的第一个参数

Groovy能做到这一点吗

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)
输出:

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)

更新 因此,詹金斯的API可能非常复杂,有时不那么明显。我对静态
描述符rimpl
类型的引用是错误的。我仍然很想知道为什么上面的失败

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)
输出


在第一个示例中,您试图将一个实际的类实例(不是null)传递到第一个方法中。第二个例子是正确的,只需将null赋值给stapler变量,它就会被称为fine。至于“这是订书机请求吗”,null instanceof anything返回false


另一个问题是org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl是一个类实例,但是jenkins.model.jenkins.getInstance().getDescriptorByType(org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.Class)返回org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl的实例。

Hi,我还没有将这个答案标记为可接受的答案,因为仍然不清楚为什么我使用
jenkins.model.jenkins.getInstance().getDescriptorByType(org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.class)
,但是Groovy运行时失败了`org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl`。据我所知,两者都返回对同一实例的引用。org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl返回一个类实例,但getDescriptorByType返回一个DescriptorImpl实例。他们不一样。这是String.class和新字符串(“abc”)之间的区别。但是在上面的代码中,创建stapler的方式也是错误的,因为您分配了一个类实例而不是null。在更新中你解决了这两个问题。太棒了-现在我明白了!非常感谢。
Is it a stapler request? false.
true