简体   繁体   English

groovy.lang.MissingMethodException 用于 Groovy 中的“收集”关闭

[英]groovy.lang.MissingMethodException for 'collect' closure in Groovy

I am getting this error:我收到此错误:

No signature of method: ClassPath$_method_closure3.doCall() is applicable for argument types: ([AncestorOfFile]) values: [...values]
Possible solutions: doCall(ru.naumen.modules.portal.types.File), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)

For this code对于此代码

pageFiles = (someCollection as Collection<File>).collect {File file ->
    ...
    some logic
    ...
}

Works perfectly fine without type declaration "File" for inner variable.无需内部变量的类型声明“文件”即可正常工作。

I thought i can use type declaration for collect closure like that because i work with typed Collection.我想我可以像这样使用类型声明来收集闭包,因为我使用类型化的 Collection。

Am i doing something wrong?难道我做错了什么?

You need to double-check the assumption that the variable someCollection is of type Collection<File> .您需要仔细检查变量someCollectionCollection<File>类型的假设。 It looks like you expect that every element of that collection is of type ru.naumen.modules.portal.types.File , and if that was correct, then you could apply it to the closure you defined for the collect() method.看起来您希望该集合的每个元素都是ru.naumen.modules.portal.types.File类型,如果这是正确的,那么您可以将其应用于您为collect()方法定义的闭包。

The reason you don't see a similar error when you don't define File file parameter for that closure is that in such a case, the closure you define accepts any type of argument.当您没有为该闭包定义File file参数时,您看不到类似错误的原因是,在这种情况下,您定义的闭包接受任何类型的参数。 Let me show you this by example.让我通过示例向您展示这一点。

def someCollection = [[1,2,3,4,5]]

(someCollection as Collection<Integer>).collect { 
    // do something...
}

In this example, someCollection is of the type List<List<Integer>> .在此示例中, someCollection的类型为List<List<Integer>> There are no errors, the code seems to work fine.没有错误,代码似乎工作正常。 But it is not if you expect that the collect method gets every number one by one.但是,如果您期望collect方法一个一个地获取每个数字,则并非如此。 Instead, it gets the whole list of numbers.相反,它会获取整个数字列表。 This problem can be discovered if we declare a desired type of argument for that closure:如果我们为该闭包声明所需类型的参数,则可以发现此问题:

def someCollection = [[1,2,3,4,5]]

(someCollection as Collection<Integer>).collect { Integer it ->
    // do something...
}

If we try to run this example, we get an error that is similar to yours:如果我们尝试运行此示例,我们会收到与您的错误类似的错误:

Caught: groovy.lang.MissingMethodException: No signature of method: test$_run_closure1.doCall() is applicable for argument types: (ArrayList) values: [[1, 2, 3, 4, 5]]
Possible solutions: doCall(java.lang.Integer), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
groovy.lang.MissingMethodException: No signature of method: test$_run_closure1.doCall() is applicable for argument types: (ArrayList) values: [[1, 2, 3, 4, 5]]
Possible solutions: doCall(java.lang.Integer), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
    at test.run(test.groovy:3)

To fix the problem with your code you will need to find out what is the type of the argument passed to the closure with no type definition.要解决代码问题,您需要找出传递给没有类型定义的闭包的参数的类型。 One way to do it would be to print the it.dump() result and see what is the type of the value.一种方法是打印it.dump()结果并查看值的类型。 This is what it looks like for the example I shown you earlier:这就是我之前向您展示的示例的样子:

def someCollection = [[1,2,3,4,5]]

(someCollection as Collection<Integer>).collect {
    println it.dump()
}

Output: Output:

<java.util.ArrayList@1c3e4a2 elementData=[1, 2, 3, 4, 5] size=5 modCount=1>

You will see that the value you expect to be of type ru.naumen.modules.portal.types.File is something else in reality.您将看到您期望的ru.naumen.modules.portal.types.File类型的值实际上是另一回事。 Maybe this is a list of files, or maybe this is something else.也许这是一个文件列表,或者这是别的东西。 And then if you want to make the closure with specific type to work, you will have to make sure that someCollection is a collection of those values in reality.然后,如果您想让特定类型的闭包起作用,您将必须确保someCollection是这些值的集合。

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

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