理解java中的一些groovy代码

时间:2017-05-10 08:31:03

标签: java groovy

出于某种原因,我需要将groovy代码重写为java。如果重要的话,它是gradle插件。但我不了解一些代码。你可以一步一步地解释一下,可能是用java代码吗?

class DeployPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.with {
            apply plugin: 'org.hidetake.ssh'
            ssh.settings {
                identity = file(ssh_file)
                knownHosts = allowAnyHosts
                passphrase = ssh_passphrase
            }
    ...

据我所知project.with{ }表示为project实例调用内部的方法(?)。

apply plugin: 'org.hidetake.ssh' - 可以用project.getPlugins() .apply("org.hidetake.ssh")表达吗?

ssh.settings - 这里ssh是什么?如果它是变量,我怎样才能在java中获取它的实例?

ssh.settings {someExpressions} - 在此上下文中使用的花括号是什么?

1 个答案:

答案 0 :(得分:2)

代码在Java中可能看起来像这样:

public class DeployPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        project.getPlugins().apply("org.hidetake.ssh");
        org.hidetake.groovy.ssh.core.Service ssh = project.getExtensions().getByType(org.hidetake.groovy.ssh.core.Service.class);
        ssh.settings(new MethodClosure(this, "configureSettingsClosure"));
    }

    private void configureSettingsClosure(org.hidetake.groovy.ssh.core.settings.GlobalSettings settings) {
        settings.setIdentity(ssh_file);
        settings.setKnownHosts(settings.getAllowAnyHosts());
        settings.setPassphrase("p@ssw0rd");
    }

或者如果您不想依赖groovy-ssh可能是这样的,但我不推荐它:

public class DeployPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        project.getPlugins().apply("org.hidetake.ssh");
        Object ssh = project.getExtensions().getByName("ssh");
        try {
            Method settings = ssh.getClass().getDeclaredMethod("settings", Closure.class);
            settings.invoke(ssh, new MethodClosure(this, "configureSettingsClosure"));
        } catch (IllegalAccessException | NoSuchMethodException e) {
            throw new AssertionError("Should not happen except by using a different groovy-ssh version that changed incompatibly", e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    private void configureSettingsClosure(Object settings) throws InvocationTargetException {
        try {
            Method setIdentity = settings.getClass().getDeclaredMethod("setIdentity", Object.class);
            setIdentity.invoke(settings, ssh_file);
            Method getAllowAnyHosts = settings.getClass().getDeclaredMethod("getAllowAnyHosts");
            Method setKnownHosts = settings.getClass().getDeclaredMethod("setKnownHosts", File.class);
            setKnownHosts.invoke(settings, getAllowAnyHosts.invoke(settings));
            Method setPassphrase = settings.getClass().getDeclaredMethod("setPassphrase", String.class);
            setPassphrase.invoke(settings, "p@ssw0rd");
        } catch (NoSuchMethodException | IllegalAccessException e) {
            throw new AssertionError("Should not happen except by using a different groovy-ssh version that changed incompatibly", e);
        }
    }