Joint compilation of Groovy and Java – I

June 5, 2007

The first thing I’ve learned about Groovy was that you have to compile it separately from Java. OK, it is understandable because the languages are different.

But the next news was even worse. There is no way to compile it correctly if there are dependencies between Groovy and Java code. Again, of course we are software engineers, everything is possible(c) and there is a such possibility but…

Let me give you a sample. Imagine we have

  • Java interface JInterface
  • Groovy class GClass implementing JInterface
  • Java class JClass, which has to create instance of GClass (probably not thinking too much is it Groovy or Java class) and use it as instance of JInterface

I believe  everybody will agree that such design may be very logical in some situations. But how can we compile this code? Well, we do it in three steps of ant script

  • javac for JInterface
  • groovyc for GClass
  • javac for JClass

And here we are. Our problem resolved. May be the solution is not so elegant (carefully written ant script instead of IDE compilation) but it works. And it is what Groovy coders do every day.

But does the solution above really work? No, unfortunately it doesn’t!

Imagine that by some reasons JInterface has to know about GClass. We can argue long hours is it good design or bad one, so let just think it has to. Now we have circular dependencies between Groovy and Java code. To compile Java we need to compile Groovy first but to compile Groovy we need to compile Groovy. Ooops! We have dead lock.

Obviously, it is not new problem. Have a look on issue The problem is nobody knows how to solve it.

Again, we can refactor our design. Probably introduce some additional class. May be add additional step to our carefully written ant script and finally being able to compile our simple code. But does it make us more productive? I am not sure.

Fortunately I have some good news. There is a solution for joint compilation of Groovy and Java. Some nice trick was invented by guys from JetBrains And I can say even more – the’ve submitted patch, which probably will be accepted before Groovy 1.1 Beta 2 is out.

But I will describe this story tomorrow. Stay tuned 🙂


A groovy use case

June 5, 2007

Luc Dewavrin recently discovered Spring’s ability to declare beans that are script-based. He describes why this is great and helpful to to speed-up development. Sample declarations and scripts are also provided.

The article ends with interesting opinion “…, I like both Groovy and JRuby but now tend to prefer Groovy because it leverages my knowledge of Java API. With Ruby i spend too much time finding the reference web site how to achieve things that i do almost naturally in Java. OTH, Ruby has its core API use closures. Closures are tightly integrated and tend to favor loose coupling.”

Read full story…

Using Spring Factories with Groovy

June 5, 2007

Spring is an open-source framework created to address the complexity of Java enterprise application development. One of Spring’s goals is to help developers write simple, testable and loosely coupled systems while reducing the amount of scaffolding code required. In this respect, Groovy has a common goal. So, for simple systems, Groovy alone may be sufficient for your needs. However, as your system grows in size and complexity, and especially in hybrid Java/Groovy environments, you might find Spring’s facilities provide great value to your Groovy system development.

Read more in Groovy documentation…