【问题标题】:How do you pass null from Groovy to a Java method with a parameter that is typed as an interface?如何将 null 从 Groovy 传递给带有类型为接口的参数的 Java 方法?
【发布时间】:2014-09-16 02:14:42
【问题描述】:

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

“马厩”

插件实现了一个org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl#configure 方法,该方法需要一个StaplerRequest 接口实现。 test cases pass null for the request,所以我想我会在 Groovy 中做同样的事情。

但是,我无法说服 Groovy 强制将 nullNullObject 作为 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)

更新

因此,Jenkins 的 API 有时可能非常复杂且不太明显。我对静态 DescriptorImpl 类型的引用是错误的。我仍然很想知道上述失败的原因。

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)

输出

Is it a stapler request? false.
true

【问题讨论】:

    标签: groovy jenkins


    【解决方案1】:

    在第一个示例中,您尝试将一个实际的 Class 实例而不是 null 传递给第一个方法。第二个例子是正确的,只需将 null 分配给订书机变量,它就可以正常调用。至于“是否是订书机请求”,null instanceof any 返回false。

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

    【讨论】:

    • 嗨,我还没有将此答案标记为已接受的答案,因为仍然不清楚为什么当我使用 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 返回一个 Class 实例,但 getDescriptorByType 返回一个 DescriptorImpl 实例。他们不一样。这是 String.class 和 new String("abc") 之间的区别。但是在顶部代码中,您创建订书机的方式也是错误的,因为您分配了一个 Class 实例而不是 null。在更新中,您修复了这两个问题。
    • 太棒了 - 现在我明白了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多