Search code examples
javagenericsgradlegroovydocker-java

Groovy does not executes Java-Method (signature includes generics)


i have the following Java-Code that i want to convert to 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();

My current version in groovy is this:

 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()

My problem is, that i get the following error, when i try to run the groovy code:

* 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)

The Java-exec-Method has the signature:

public <T extends ResultCallback<Frame>> T exec(T resultCallback);

I tried to cast "new ExecStartResultCallback(System.out, System.err)" to "ResultCallback", but it did not work.

Is there any way to force Groovy to handle the instance as a ResultCallback-Instance so that the correct method is called?

Regards, marbon


Solution

  • A colleague helped with this problem and we found out, that the instance dockerClient used a custom classloader, which my has some problems. It could be solved by instantiating the new ExecStartResultCallback(System.out, System.err) with the same classloader from dockerInstance:

        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()