BuddyJoe
BuddyJoe

Reputation: 71161

Grails - Making Methods Globally Available and Metaclass Programming

I inserted this line into my init() of my BootStrap class

Integer.metaClass.minutes = { 60000L * delegate }

I was then not able to use it from a Job class (Quartz plugin). Do I put this line of code somewhere else to make it a global modification?

I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way?

Upvotes: 3

Views: 1249

Answers (1)

Dónal
Dónal

Reputation: 187379

Do I put this line of code somewhere else to make it a global modification?

Use the DelegatingMetaClass

I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way?

If you want the function to be an instance method of all classes, then you must add it to the metaClass of Object (see above). If not, simply add the function as a static method of a class, i.e. the same way you make functions globally accessible in Java.

Upvotes: 1

Related Questions