Groovy数组初始化

时间:2016-11-23 18:14:27

标签: java groovy

我有一个输入两个BitSet的函数,然后,我在Groovy中通过 evaluate 调用这个函数。 由于params是两个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;
}

第三行只是用params调用函数路径并且它有效。 但是,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}))
                            ^

Google搜索我发现Groovy接受了以下格式的数组inizialitation:

  • [0b00111111] as long []
  • long [] 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.

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

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