Andrea
Andrea

Reputation: 335

Call a method from a Groovy map transformed to a class with asType

Is it possible to call methodTwo() as in

def map = [methodOne: { return "${methodTwo()}" }, methodTwo: { return "Hey" }] as MyInterface

methodTwo cannot be found at runtime by groovy (it seems that it is searching its definition inside the class where the map was defined)

Upvotes: 2

Views: 258

Answers (1)

Will
Will

Reputation: 14559

You can call the method declaring the map variable before, and then referencing it:

interface MyInterface {
    def methodOne()
    def methodTwo()
}

def map
map = [
    methodOne: { return "${map.methodTwo()}" }, 
    methodTwo: { return "Hey" }
] as MyInterface

assert map.methodOne() == "Hey"

Upvotes: 2

Related Questions