0
class EmailAssist {
    def abcService = "abc"
    String name

    EmailAssist(String name) {
        this.name = name
        println abcService
    }

    EmailAssist() {
    }

}

EmailAssist.metaClass.getAbcService = {->
    "test"
}

def e = new EmailAssist("Joe")
println e.abcService

This results in output of

abc
test

I'd think it would result in

test
test

Can anyone explain what goes into this exactly? Does the metaclass only modify after the constructor executes, as opposed to overwriting constructor methodology?

mrfixij
  • 132
  • 1
  • 8

1 Answers1

1

If you are in a class (not inner/nested class, not open block) that defines a field and you access the field using a qualified or unqualified this, then the access is done directly without considering the MOP. Thus you cannot shadow a field in this manner. Since this logic is done by the compiler and literal, instead of by value you can bypass this through def tmp = this; tmp.abcService

blackdrag
  • 6,413
  • 2
  • 26
  • 38