小编典典

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

jenkins

我正在尝试使用Groovy在Jenkins中编写插件的配置脚本。某些插件(例如GitHub Pull Request
Builder插件)
并未提供任何简单的方法来在Jenkins
Web界面(使用名为Stapler的库将HTML表单绑定到对象实例)之外配置插件。

`Stabl

该插件实现了需要接口实现的org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl#configure方法StaplerRequest。该测试用例通过null的要求,所以我想我会做同样的在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

阅读 325

收藏
2020-07-25

共1个答案

小编典典

在第一个示例中,您尝试将实际的Class实例(不为null)传递给第一个方法。第二个示例是正确的,只需为装订器变量赋null即可,它被称为罚款。至于“是否是装订器请求”,则null
instanceof会返回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的形式。

2020-07-25