Groovy 不执行 Java-方法(签名包括泛型)
Groovy does not executes Java-Method (signature includes generics)
我有以下 Java- 我想转换成 groovy 的代码:
String containerId = "545cdc81a969";
ExecCreateCmdResponse execCreateCmdResponse = dockerClient
.execCreateCmd(containerId)
.withAttachStdout(true)
.withCmd("sh", "-c", "sleep 5 && exit 5")
.exec();
ExecStartResultCallback execStartCmd =
dockerClient.execStartCmd(execCreateCmdResponse.getId())
.exec(new ExecStartResultCallback(System.out, System.err))
.awaitCompletion();
我在 groovy 中的当前版本是这样的:
String id = "545cdc81a969";
def execCreateCmdResponse = dockerClient
.execCreateCmd(id)
.withAttachStdout(true)
.withCmd('sh','-c','sleep 5 && exit 5')
.exec()
dockerClient.execStartCmd(execCreateCmdResponse.getId())
.withDetach(false)
.exec(new ExecStartResultCallback(System.out, System.err))
.awaitCompletion()
我的问题是,当我尝试 运行 groovy 代码时出现以下错误:
* What went wrong:
Execution failed for task ':werner'.
> No signature of method: com.github.dockerjava.core.command.ExecStartCmdImpl.exec() is applicable for argument types: (com.github.dockerjava.core.command.ExecStartResultCallback) values: [com.github.dockerjava.core.command.ExecStartResultCallback@6ce82155]
Possible solutions: exec(com.github.dockerjava.api.async.ResultCallback), exec(com.github.dockerjava.api.async.ResultCallback), every(), grep(), every(groovy.lang.Closure), grep(java.lang.Object)
Java-exec-方法具有签名:
public <T extends ResultCallback<Frame>> T exec(T resultCallback);
我尝试将 "new ExecStartResultCallback(System.out, System.err)" 转换为 "ResultCallback",但没有成功。
有什么方法可以强制 Groovy 将实例作为 ResultCallback-Instance 来处理,以便调用正确的方法吗?
此致,
马邦
一位同事帮助解决了这个问题,我们发现实例 dockerClient 使用了自定义类加载器,我的有一些问题。可以通过使用来自 dockerInstance 的相同类加载器实例化新的 ExecStartResultCallback(System.out, System.err) 来解决:
ClassLoader dockerClientClassLoader = dockerClient.getClass().getClassLoader()
Class callbackClass = dockerClientClassLoader.loadClass("com.github.dockerjava.core.command.ExecStartResultCallback")
def callback = callbackClass.getDeclaredConstructor(OutputStream.class, OutputStream.class).newInstance(System.out, System.err);
dockerClient.execStartCmd(execCreateCmdResponse.getId())
.withDetach(false)
.exec(callback)
.awaitCompletion()
我有以下 Java- 我想转换成 groovy 的代码:
String containerId = "545cdc81a969";
ExecCreateCmdResponse execCreateCmdResponse = dockerClient
.execCreateCmd(containerId)
.withAttachStdout(true)
.withCmd("sh", "-c", "sleep 5 && exit 5")
.exec();
ExecStartResultCallback execStartCmd =
dockerClient.execStartCmd(execCreateCmdResponse.getId())
.exec(new ExecStartResultCallback(System.out, System.err))
.awaitCompletion();
我在 groovy 中的当前版本是这样的:
String id = "545cdc81a969";
def execCreateCmdResponse = dockerClient
.execCreateCmd(id)
.withAttachStdout(true)
.withCmd('sh','-c','sleep 5 && exit 5')
.exec()
dockerClient.execStartCmd(execCreateCmdResponse.getId())
.withDetach(false)
.exec(new ExecStartResultCallback(System.out, System.err))
.awaitCompletion()
我的问题是,当我尝试 运行 groovy 代码时出现以下错误:
* What went wrong:
Execution failed for task ':werner'.
> No signature of method: com.github.dockerjava.core.command.ExecStartCmdImpl.exec() is applicable for argument types: (com.github.dockerjava.core.command.ExecStartResultCallback) values: [com.github.dockerjava.core.command.ExecStartResultCallback@6ce82155]
Possible solutions: exec(com.github.dockerjava.api.async.ResultCallback), exec(com.github.dockerjava.api.async.ResultCallback), every(), grep(), every(groovy.lang.Closure), grep(java.lang.Object)
Java-exec-方法具有签名:
public <T extends ResultCallback<Frame>> T exec(T resultCallback);
我尝试将 "new ExecStartResultCallback(System.out, System.err)" 转换为 "ResultCallback",但没有成功。
有什么方法可以强制 Groovy 将实例作为 ResultCallback-Instance 来处理,以便调用正确的方法吗?
此致, 马邦
一位同事帮助解决了这个问题,我们发现实例 dockerClient 使用了自定义类加载器,我的有一些问题。可以通过使用来自 dockerInstance 的相同类加载器实例化新的 ExecStartResultCallback(System.out, System.err) 来解决:
ClassLoader dockerClientClassLoader = dockerClient.getClass().getClassLoader()
Class callbackClass = dockerClientClassLoader.loadClass("com.github.dockerjava.core.command.ExecStartResultCallback")
def callback = callbackClass.getDeclaredConstructor(OutputStream.class, OutputStream.class).newInstance(System.out, System.err);
dockerClient.execStartCmd(execCreateCmdResponse.getId())
.withDetach(false)
.exec(callback)
.awaitCompletion()