groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gerald Wiltse <>
Subject Confirming getProperties() works differently (inside vs outside)
Date Mon, 29 Feb 2016 02:23:00 GMT
Is there a way for the Chameleon class to ever see that it has a
"lastColor" property?

class Chameleon{
    String color = "green"

    void printAllMyProperties(){{println it}


trait ColorChanging {
    String lastColor
    def changeColor = {newcolor ->
        lastColor = this.color
        this.color = newcolor

def mylizard = new Chameleon().withTraits(ColorChanging)


mylizard.printAllMyProperties(){println it}

The outputs of the two above lines don't match. lastColor isn't available
from inside the class.

This seems like it should be possible, but I tried several things including
the declaredFields property and could not get the desired effect.

In this script, I'm looking for a way for my instance to reference all the
properties it's received from the trait. I guess it's same situation with
inheritance, and properties added by propertymissing.

Gerald R. Wiltse

View raw message