警告"getAt"无法应用于Groovy中的"Integer"

Eld*_*rry 9 groovy intellij-idea

在我的Groovy代码的这一行:

def document = someQuery().Document[0]
Run Code Online (Sandbox Code Playgroud)

方法someQuery将返回一个Json数组,这很好用.由于编辑不知道财产,它强调Document并显示警告[0],说:

'getAt' in 'org.codehaus.groovy.runtime.DefaultGroovyMethods' cannot be applied to '(java.lang.Integer)'
Run Code Online (Sandbox Code Playgroud)

那么,为避免这种警告,更好的方法是什么?

the*_*ole 5

严格来说,对于.getAt(0).getAt(-1),您还可以分别使用.first().last(),Intellij 将停止抱怨。

来源:我刚刚尝试过。(请参阅此处 getAt 不需要 Integer:code-completion

    def shortId(def longId){
        longId?.toString()?.split('[/:]')?.last()
    }
Run Code Online (Sandbox Code Playgroud)


Joh*_*ier 0

您可以向 intellij 添加动态方法和属性,以便它了解它们。

http://confluence.jetbrains.com/display/GRVY/Dynamic+Methods+and+Properties

您还可以使用groovydsl让 intellij 知道您的代码应该如何工作。