Warning that 'getAt' cannot be applied to 'Integer' in Groovy

In this line of my Groovy code:

def document = someQuery().Document[0] 

The someQuery method will return a Json Array, and this worked fine. Since the editor does not know the property, it underlines Document and displays a warning in [0] , says:

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

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

+8
source share
3 answers

You can add dynamic methods and properties for intellij so that they know about them.

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

You can also use groovydsl so that intellij knows how your code should work.

0
source

You probably have code that looks like this:

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

Replace it with:

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

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

0
source

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 does not expect an integer here: code completion )

  def shortId(def longId){ longId?.toString()?.split('[/:]')?.last() } 
0
source

Source: https://habr.com/ru/post/1232254/


All Articles