groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edd Grant <...@eddgrant.com>
Subject Fwd: @Immutable and MissingPropertyException
Date Wed, 01 Jun 2016 16:00:02 GMT
Hi Folks,

I am trying to control the behaviour exhibited by some of our POGOs when
they are created using a Map constructor which contains keys that do NOT
map to their properties e.g.

class Person {
  String name
}

def person = new Person([name: 'Edd', age: 35])

Normally the above would call propertyMissing() however I have discovered
that I can define a trait (which the class then implements) and provide a
default implementation of this method e.g.

trait LogsUnknownProperties implements GroovyInterceptable {

    private static final Logger LOGGER =
Logger.getLogger(LogsUnknownProperties)

    def propertyMissing(String name, value){
        LOGGER.warn("Class: ${this.class.name} - Could not set property
with name '${name}' having value '${value}' as property does not exist.")
    }
}

This works brilliantly in mutable POGOs, however for POGOs which are
annotated with @Immutable it doesn't work. From looking at the code in
ImmutableASTTransformation.java this seems to be because the
checkPropNames() method throws a MissingPropertyException:

https://github.com/groovy/groovy-eclipse/blob/master/base/org.codehaus.groovy20/src/org/codehaus/groovy/transform/ImmutableASTTransformation.java#L556

Is there any way I can intercept the throwing of this exception so I can
control the behaviour for @Immutable classes in the same way I can for
mutable ones? I wondered if it could be achieved with a sprinkling of
meta-programming but I'm not sure where to start looking?

Many thanks,

Edd
-- 
Web: http://www.eddgrant.com
Email: edd@eddgrant.com
Mobile: +44 (0) 7861 394 543

Mime
View raw message