This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

I need metaClass advice for Groovy

 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
*bump*
 
Author
Posts: 109
5
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Another approach is to use a category:

[ April 08, 2008: Message edited by: Jim Yingst ]
 
Venkat Subramaniam
Author
Posts: 109
5
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
The static block sounds like a pretty good idea.

Thanks for the concise for loop, Jim.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic