Loading...

June 10, 2010

Groovy Goodness: Get Hints About Missing Methods or Properties

Groovy helps us if for example we make a small mistake in our code. We don't only get an error message, but also a hint about a possible solution. This works also for classes we create ourselves.

In the following example we use toUppercase() instead of toUpperCase(). Notice the suggestion we get from Groovy:

$ groovy -e "println 'mrhaki'.toUppercase()"
Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.toUppercase() is applicable for argument types: () values: []
Possible solutions: toUpperCase(), toUpperCase(java.util.Locale), toLowerCase(), toLowerCase(java.util.Locale)
 at script_from_command_line.run(script_from_command_line:1)

Suppose we define the following script:

class User {
    String name
    String giveName() { name }
}

def u = new User(name: 'mrhaki')
println u.givenName()

When we run this script we get the following output:

$ groovy UserScript.groovy 
Caught: groovy.lang.MissingMethodException: No signature of method: User.givenName() is applicable for argument types: () values: []
Possible solutions: giveName(), getName(), setName(java.lang.String)
 at UserScript.run(UserScript.groovy:7)