grailsgroovygrails-ormgrails-domain-class

grails4 migration Traits lazy load issue - HHH000142: Bytecode enhancement failed


I have migrated my application from Grails-3.x to Grails-4.1.1 Most of my Domain classes implemented the following Traits class (DynamicProperties), which has an implementation of GormEntity for some reason - to override the propertyMissing method.

trait DynamicProperties<D> implements GormEntity<D> {

def dynamic = [:]

def propertyMissing(String name, value) {
    if (!propertyIsDatasource(name)) {
        dynamic[name] = value
    }
}

def propertyMissing(String name) {
    if (propertyIsDatasource(name)) {
        super.propertyMissing(name)
    } else {
        dynamic[name]
    }
}

boolean propertyIsDatasource(String name) {
    false
} 
}

The above trait has been implemented by many domain classes like this

class Customer implements DynamicProperties<Customer> {

String customerCode
String customerName
Address address
....
}

Now, when I run my application, It is throwing the following exception

HHH000142: Bytecode enhancement failed: com.apps.billing.Customer.
Caused by: java.lang.NullPointerException
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:37)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:75)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
at **com.apps.billing.common.DynamicProperties$Trait$Helper.$init$**(DynamicProperties.groovy:7)

It used to work fine with Grails3.x


Solution

  • I had a similar problem when migrating a project to Grails 4.0.13.

    I tracked the trigger of this problem down to having default values for member variables in a trait class. I see you have the same with the property dynamic being initialized with the empty map.

    I recommend lazy initializing it in one of your propertyMissing methods.