Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to properly initialize Groovy metaclasses?

In a Groovy application, if you want to initialize metaclasses, where is the best place to put those initializations? In Grails apps, I've used the Bootstrap.groovy file. Is there something similar for an arbitrary Groovy app?

Edit: To clarify, I'm looking for a non-intrusive way of doing this that will guarantee it gets called when running the main application as well as in unit tests of the associated code.

like image 736
Alan Krueger Avatar asked Aug 01 '11 19:08

Alan Krueger


1 Answers

There's nothing like Bootstrap.groovy for standard groovy applications, but that doesn't stop you doing the same thing in your app and having a class or method that is called once on startup where you can add the metaClass initialization


Added from comments below:

You can write a DelegatingMetaClass for each class you wish to decorate, and place it in the package groovy.runtime.metaclass.[YOURPACKAGE].[YOURCLASS]MetaClass

This page explains the steps required to do this (2nd item on the page)

like image 176
tim_yates Avatar answered Sep 19 '22 09:09

tim_yates