• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I need metaClass advice for Groovy

 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need another method on String. I used metaClass to do this:

However, in all of the code examples I've seen on metaClass this trick has always been inside a script. In my application, though, I'm not running scripts - I have Groovy classes. I suppose I could place this metaClass thing in the beginning of any method that needs to call it... but that sure doesn't feel right.

I thought about moving it to some listener or bootstrapper class that loads when my container loads, but then I have to make sure it also gets called for my unit tests and that feels sloppy too.

Should metaClass not be used like this in an application (and keep it only in scripts)? Should I revert to the old school Java way and make a String utility class?
[ April 08, 2008: Message edited by: Marc Peabody ]
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*bump*
 
Author
Posts: 135
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you have this code in a static initialization block of a prominent class in your application? If this prominent class is guaranteed to be loaded pretty early in your application execution, you don't have to worry about making explicit calls to this code...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another approach is to use a category:

[ April 08, 2008: Message edited by: Jim Yingst ]
 
Venkat Subramaniam
Author
Posts: 135
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. Be aware, however, that Categories are more expensive and also injected methods are visible only within the same thread.
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static block sounds like a pretty good idea.

Thanks for the concise for loop, Jim.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic