Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A warning that 'getAt' cannot be applied to 'Integer' in Groovy

In this line of my Groovy code:

def document = someQuery().Document[0]

Method someQuery will return a Json Array and this worked well. Since editor doesn't know property, it underlined Document, and shows a warning at [0], says:

'getAt' in 'org.codehaus.groovy.runtime.DefaultGroovyMethods' cannot be applied to '(java.lang.Integer)'

So what is the better way to do this to avoid this warning?

like image 968
Elderry Avatar asked Sep 25 '15 09:09

Elderry


2 Answers

Strictly for .getAt(0) and .getAt(-1), you can also use .first() and .last(), respectively, and Intellij will stop complaining.

Source: I just tried it. (See getAt doesn't expect an Integer here: code-completion)

    def shortId(def longId){
        longId?.toString()?.split('[/:]')?.last()
    }
like image 55
thehole Avatar answered Nov 03 '22 02:11

thehole


You probably have code that looks like:

def sql = //... (new/existing connection to the database)
sql.eachRow { row ->
    //Do something...
}

Replace this with:

def sql = //... (new/existing connection to the database)
sql.eachRow { GroovyResultSet row ->
    //Do something...
}

This will force your editor to look at methods from GroovyResultSet rather than methods with the same name attached to objects during groovy compilation.

like image 1
sf_jeff Avatar answered Nov 03 '22 02:11

sf_jeff