当前位置: 首页 > 面试题库 >

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

邹玄裳
2023-03-14
问题内容

我正在尝试使用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

问题答案:

在第一个示例中,您尝试将实际的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的形式。



 类似资料:
  • 问题内容: 问题摘要: 我想将具有类型参数(例如)的类作为类型参数传递给泛型方法。 假设我有一个方法: 当然,此方法对于任何类型的类都可以正常使用。我可以这样调用该方法,例如: 问题: 我发现我不能这样做: 从句法上讲,这显然是无效的。但是,我不确定如何实现这样的目标。我当然可以通过,但是泛型类型的添加使其在语法上不再有效,并且我想不出解决方法。 唯一的直接解决方案是这样的事情(看起来很愚蠢):

  • 我有办法 我想知道如果它真的创建了用户,我是否可以对其进行单元测试。但是它没有参数。 我尝试了以下方法: 然而,这实际上让我通过了与扫描仪的争论,这显然是我在测试中无法做到的。还尝试了其他逻辑。我也尝试过使用when(),,,但我找不到解决这个问题的方法,因为我对模仿还比较陌生。 有人能和我分享一些想法吗?

  • 让我们假设下面的方法(从番石榴的Iterables说): 这个系列: 然后编译以下代码并正确导出泛型 (在Guava中,这将返回< code>objs中所有字符串的iterable。) 但是现在让我们假设以下类: 我不知道如何调用并获取

  • 我正在编写一个方法,如果我想将一个类传递给一个方法,其中代码的一部分包括检查对象是否属于某种类型。这就是我想要的(但显然不行): 关于如何做到这一点有什么提示吗?谢谢!

  • 问题内容: 以下代码的输出是因为从beginIndex到EndIndex-1。但是,令我惊讶的是这里的3(int)因为是两个整数。这背后的概念是什么? 问题答案: 一直到C,从本质上讲是一个窄整数类型,并在需要时隐式转换为C。 在Java中,这在技术上称为“扩展原始转换”,在JLS的5.1.2节中进行了介绍。

  • int是byte、short、int、long的默认文字,然后是为什么byte c=15被接受而不进行强制转换,但不接受方法参数。 提前谢谢你。