java - Groovy 数组初始化

标签 java groovy

我有一个函数,它接受两个 BitSet 输入,然后,我通过 Groovy 中的评估来调用这个函数。 由于参数是两个 BitSet,我需要内联初始化它们并通过字符串传递所有内容。

public static void main(String[] args) {
    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);
    path(BitSet.valueOf(new long[] {0b00111111}),BitSet.valueOf(new long[] {0b00000011})); //s3
    //String s1 = "path(BitSet.valueOf([0b00111111L] as long[]),BitSet.valueOf([0b00111111L] as long[]))";
    //String s2 = "path(BitSet.valueOf( long[] o = [0b00111111L] ),BitSet.valueOf( long[] oo = [0b00111111L] ))";
    String s3 = "path(BitSet.valueOf(new long[] {0b00111111}),BitSet.valueOf(new long[] {0b00000011}))";
    Object value = shell.evaluate(s3);


}

public static LinkedList<BitSet> path(BitSet dstBitSet, BitSet srcBitSet) {
    LinkedList<BitSet> l = new LinkedList<>();
    l.add(srcBitSet);
    System.out.println("ok");
    return l;
}

第三行只需使用参数调用函数路径即可工作。 但是,Groovy 中完全相同的代码段 (s3 var) 不起作用,因为它返回以下错误:

Exception in thread "main"  org.codehaus.groovy.control.MultipleCompilationErrorsException: startup   failed:
Script1.groovy: 1: No expression for the array constructor call at   line: 1 column: 29. File: Script1.groovy @ line 1, column 29.
path(BitSet.valueOf(new long[] {0b00111111}),BitSet.valueOf(new long[]     {0b00000011}))
                            ^

谷歌搜索我发现Groovy接受以下格式的数组初始化:

  • [0b00111111] 长[]
  • 长[] o = [0b00111111]

正如你从代码中看到的,我尝试在 var s1 和 s2 中执行相同的操作,但仍然不起作用。 S1错误是:

Exception in thread "main" groovy.lang.MissingMethodException: No   signature of method: Script1.path() is applicable for argument types:   (java.util.BitSet, java.util.BitSet) values: [{0, 1, 2, 3, 4, 5}, {0, 1,   2, 3, 4, 5}]
Possible solutions: wait(), any(), with(groovy.lang.Closure), each(groovy.lang.Closure), run(), run()

S2 错误是:

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: Invalid use of declaration inside method call.

有办法解决这个问题吗?

最佳答案

试试这个:

path(BitSet.valueOf([0b00111111] as long[]), BitSet.valueOf([0b00111111] as long[]))

关于java - Groovy 数组初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40771574/

相关文章:

java - 在桌面应用程序中使用 Java 跟踪工具

spring - 创建自定义AuthenticationSuccessHandler并在完成后调用默认的AuthenticationSuccessHandler

grails - 在 Controller 中覆盖 Getter

java - 创建和检索 JIRA 远程问题链接

java - 根据某些关键字过滤字符串的最快方法

java - 为什么 DocumentBuilder.parse 抛出 SaxParserException

java - JVM 崩溃时异常中显示奇怪的路径

groovy - 创建一个新类 "on the fly"?

java - 二进制部分写入错误

java - Servlet 3.0 API - 注册一个 servlet