Lambda Lounge Presentation: Groovy Categories and Mixins

There was an excellent turnout at the inaugural meeting of the Lambda Lounge last night at Appistry in St. Louis. I think we were all surprised at how many über-geeks could fit into one small room. Alex Miller did a great job organizing the event, so kudos to him. I’ve already seen a few blog and twitter reactions to the meeting, and I hope to see more folks talking about the event so just as many people show up next time.

I gave a presentation on metaprogramming in Groovy using categories and mixins.

View SlideShare presentation (tags: groovy metaprogramming)

I also ran through a bit of example code live, and I’ve zipped it up here for you to download at your leisure:

There were some good questions last night about metaprogramming with Groovy, and I’m happy to say I have some answers to them this morning.

How far can Groovy go when manipulating Java classes? Can it change private methods? It is restricted by the SecurityManager?

People are obviously concerned about the security of letting their programmers use Groovy, especially when it has seemingly unlimited access to all the existing Java code within an application. This is a very viable concern. The short answer is: You can only do with Groovy what you can do with Java. Groovy is implemented in Java, so it makes sense that you are restricted by the same rules as Java when it comes to access, reflection, etc. That being said, Alex pointed out last night that you can do a lot with Java.

Groovy will allow you to change existing Java classes, public or private, at any time. It is, however, bound by restrictions of the SecurityManager. Although this seems somewhat dangerous, you must understand that any manipulations Groovy applies will not be applied when other Java code in the application uses it.

In other words, Groovy ‘decorates’ third party libraries and the JDK when it metaprograms over them, but the real bytecode is not modified at all. So only from the “Groovy side of things” will you see the changes in behavior. This means that Groovy metaprogramming will not affect any Java code that uses the libraries Groovy has touched.

Can I use categories or mixins to add or change static methods and properties?

No. I mistakenly assumed that it was possible, but for the current Groovy, it is not. It has been talked about, however, but unless there is a firm requirement for it, I’m not sure that it will be implemented.

I want to thank everyone who came out for the meeting last night and made it such a success. I hope to keep in contact with you all.

This entry was posted in uncategorized and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted December 6, 2008 at 2:12 am | Permalink

    Very cool! I wasn’t aware of the new mixin stuff previously, but it looks powerful. It’ll make all those apache commons libraries much more groovy to use:

    import org.apache.commons.lang.StringUtils

    String.metaClass.mixin StringUtils

    assert “foo …” == “foo bar baz quux”.abbreviate(7)

One Trackback

  1. By Great first meeting! « Lambda Lounge on December 5, 2008 at 10:41 am

    [...] Matt Taylor Possibly related posts: (automatically generated)December meeting topicsClique-ity clack, or Ridin’ the Bozo busMontana “Big Brother” GOP Keeps a Database on YouMathematical impossibility: Men say they have more partners than women [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
Check out the latest GroovyMag to see an interview with me about the 1.1 release of GrailsUI: