Comparing Java to Groovy NFJS talk online
The slides for my NFJS talk “Comparing Java and Groovy”, along with source code are now online in the Presentations section.
Matt Secoske’s intermittent ramblings on software and life
{ Category Archives }
The slides for my NFJS talk “Comparing Java and Groovy”, along with source code are now online in the Presentations section.
I have been doing a lot of digging into Groovy and DSLs lately, getting ready for my OSCON talk next week.
The Groovy language has great built-in support for creating DSLs. Along with its dynamic nature, Groovy has the “use” keyword for working with categories, and coming in 1.1, the ExpandoMetaClass if you want to complete.
Categories help you do scoped “extension” to normal every day objects. An example is in order:
So, inside the use block, there is now (effectively) a new property on the Integer class, called tacos, that will return a string describing your delectable entree. This is a very powerful technique, and is very useful for developing your own expressive syntax.
One drawback to this technique is that you have to define where you are using it. Use can be extended into closures, but does not always extend into predefined methods.
I was talking about Groovy Categories with Blaine Buxton, and was telling him that I did not like having the “use” block inside my DSL code, and wanted a more transparent approach. He suggested that perhaps an Annotation and an Aspect would do what I was looking for.
With the 1.1-Beta1 Release, Groovy has built in support for Java Annotations, and since Groovy compiles down to byte code, there is implicit support for Aspects. Putting these two things together allows us to create an annotation for a more powerful use:
With a simple annotation, @Uses(category=[list of category classes]), we now can define them for any method we want, without having to put them into the method body.
Here is the Annotation code:
And here is the Aspect code:
One thing to note when working with Annotations / Aspects in Groovy: you have to keep track of the build order. I struggled for a while to figure out why I was not seeing the annotation in my Groovy classes. It was because it had not been built yet! I ended up having to split my compilation into 4 parts:
I put my InfoTec 2007 presentations online last night. If you don’t want to hear about how much fun I had giving them, here is the link.
My first talk was a 4 hour “Introduction to Ruby on Rails”. It had a decent turnout, and was a lot of fun to give. Thanks to a IM chat with Harish the night before the talk, I gave the best line I’ve given so far in a talk (IMHO): “So, what type of web application would you like to build today?”
My second talk was “Agile Java Web Frameworks”. There were twice as many people in this talk that had signed up before the conference started, though slightly fewer than in my rails talk. A slightly disjointed talk, I hit upon some of the more interesting web frameworks in the java world: Struts2, Spring MVC/WebFlow, Click, Rife, and then the dynamic contenders: Grails, Rails, and Lift.
Click was an interesting experience, as I had only learned about it the night before from Stephen Haberman. It looks pretty promising.
We only had enough time to quickly dive into one of the frameworks, and the audience chose Grails. So we delved into the bowels of GroovyQuiz and I showed them how it works.
Overall I had a lot of fun, and am looking forward to next years InfoTec.
The other day I needed to remove a mess of .svn directories from a project as I moved it from my personal box (using svn as the soure control) to the client’s source control system. This is a fairly frequent task, so attempting to be a “productive programmer” I decided that I would take the time to write a script to do this for me. After testing, it took about an hour to write… mainly because I tried to be too fancy, and tried a few things in Groovy I had not seen before. Overall, it was an hour well spent:
14 lines. I really like the eachDir and the eachFile methods on the File object… and there is even an eachFileRecurse. All I can say is: where the heck is the eachDirRecurse??? Oh well. It was still a good learning experience, and now I have a malleable tool next time I run into something similar.