groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edd Grant <>
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 =

    def propertyMissing(String name, value){
        LOGGER.warn("Class: ${} - 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 this seems to be because the
checkPropNames() method throws a MissingPropertyException:

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,

Mobile: +44 (0) 7861 394 543

View raw message