简体   繁体   English

用Java处理Groovy脚本的结果

[英]Processing the result of a Groovy script in Java

I followed the answer to this SO post to write my own script. 我按照这个SO帖子的答案来编写自己的脚本。 However, I do not understand how to convert from GStringImpl to String[] . 但是,我不明白如何从GStringImpl转换为String[] How do I do that? 我怎么做?

Thanks 谢谢

public class TestGroovy {
    public static void main(String[] args) {
        Binding binding = new Binding();
        GroovyShell shell = new GroovyShell(binding);
        binding.setVariable("b", "a|b|c");
        GStringImpl value = (GStringImpl) shell.evaluate("return \"${b.split('|')}\";");// return "b.split('|')";}
        System.out.println(value);
    }
}

This prints 此打印

[a, |, b, |, c]

I rewrote my code as follows. 我将代码重写如下。 I use properties instead 我改用属性

public static void main(String[] args) {
    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);
    binding.setProperty("b", "a|b|c");
    shell.evaluate("result = b.tokenize('|');");// return "b.split('|')";}
    List<String> property = (List<String>) shell.getProperty("result");
    System.out.println(property);
    for (String s : property) {
        System.out.println(s);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM