My project’s copy contains the following snippet in HibernateGrailsPlugin.groovy :
def doWithDynamicMethods = {
def dynamicMethods = HibernatePluginSupport.doWithDynamicMethods
dynamicMethods.delegate = delegate
dynamicMethods.call(it)
// aids in generating appropriate documentation in plugin.xml since
// domain class methods are lazily loaded we initialize them here
if(plugin.basePlugin) {
try {
def clz = application.classLoader.loadClass("org.grails.Behavior")
clz.count()
}
catch(e) {
// ignore
}
}
}
I’m new to grails/groovy, but if I understand correctly, this closure is delegating the add/removal of dynamic methods to the doWithDynamicMethods closure of the HibernatePluginSupport class.
It seems the HibernatePluginSupport class is compiled with the rest of grails specific code, if the code for HibernatedPluginSupport lived in my project’s copy of the plugin I could easily customize it to my needs.
My question is, how do I modify the autogenerated methods for specific classes? The only way I see is rewriting the doWithDynamicMethods closure in the groovy file, but I don’t want to do that since I’m only customizing it for some domain classes. More specifically, How do I remove/replace dynamic methods added by the ‘HibernatePluginSupport.doWithDynamicMethods’ closure?
first of all: dynamic finders are based on Groovy’s meta-programming features.
You should not modify the HibernateGrailsPlugin.groovy. This file is part of the Grails Hibernate plugin and will be overridden when executing certain Grails commands.
Since you are free to modify your Groovy objects meta-classes at any time from within your application you could
a) create your own grails plugin that sneaks in custom dynamic finders (with a dependency on the Hibernate Grails plugin) or
b) throw your meta-class modifications into Bootstrap.groovy to apply them on application startup