Michael is a freelancing expert for DevOps, Continuous Delivery and SCM/ALM. Michael has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Seamless integration of Java and Groovy with IntelliJ IDEA

03.12.2008
| 14562 views |
  • submit to reddit

IntelliJ IDEA provides a seamless and all-embracing development environment for integrated Java and Groovy development with high-end, borderless Debugging and Refactoring support providing Smart Code Completion, advanced Editing, Inspections, Navigation, Search/Replace and Usage Search support and much more.

For a long time Java and the Java Virtual Machine were coupled inseparably: Java is compiled to bytecode and runs on the JVM. Meanwhile also dynamically typed "scripting" languages like Groovy provide bytecode for the same target deployment environment. Groovy and many other languages have become first-class-citizens on the JVM. They can interact with the Java artifacts, use and extend given Java APIs and libraries, modify and monitor them.

IDE vendors provide environments enabling development with the "new" language porting existing IDE features to it. Those IDE features are already known from Java coding and widely used for productive coding. That Java/Groovy-IDE is best-of-breed which is able to offer an environment integrating Java and Groovy as seamless as possible. From an IDE perspective the goal is that the developer isn't aware in which artifact he is currently working, a Java class or a Groovy class e.g.

JetBrains IntelliJ IDEA provides such an environment for Groovy too. You have to install the JetGroovy plugin (currently available in version 1.5) in the plugin manager to start taking off. The use case which is shown in the screenshots is impressive. Look at the setting: a Java interface is implemented by a Groovy class which itself is extended by a plain old Java object.

Interface implementations and overriding/overriden methods are marked with an icon regardless in which artifact type you are. In the Java extension we override the method getTitle. We use the context-sensitive Generate > Override Method (Ctrl + O) and IntelliJ shows us all possible/available methods. From this list we can also choose a method from a Groovy class which is taken and generated to the underlying artifact (available with IDEA 7.0.3). In our setting the main method creates an instance of the class. Beside the standard code completion the "Smart Code Completion" (Ctrl + Shift + Enter) offers available methods with coloring for the most likely cases.

The Groovy class shows the result of Groovy inspections. In our configuration we use the InspectorGroovy plugin to further enhance the rich standard IDEA Groovy inspection pool by 90 more inspections. Thrilling are code smell detections regarding the typing. We assign a java.lang.String to a javax.swing.JLabel which is kind of obscure. Instead of developing and waiting to detect defects during runtime IntelliJ helps to find bugs very soon i.e. during development time. The smells can be polished and released by using Intention Actions (Alt+Enter).

Beside the already discussed Java/Groovy-roundtrips also the Debugger is seamless. It does not make any differences and goes through the artifacts without barriers. You can also monitor and manipulate Groovy artifacts in debugging mode.

Another scenario where wheat is separated from the chaff is the Refactoring. IDEA enables advanced Refactorings bridging language borders. In the use case we want to rename the interface method from getTitle to getTitle2 (Shift + F6). The refactoring preview shows that all dependencies will be polished. Consider that this is also possible if there is a broken Java class or Groovy class at the same time (compiler errors).

Beside the comprehensive Editing, Refactoring and Debugging integration there are also many other scenarios where the seemless Java/Groovy integration is available e.g. in the scope of Navigation (e.g. "Go to Class", Ctrl + N), in Search/Replace activities (e.g. "Find in path", Ctrl +Shift +F), Usage Search tasks (e.g. "Show usages", Ctrl +Alt + F7) and many others.

With IntelliJ IDEA it is completely transparent in all artifacts and steps where exactly you are. In case of emergency you discover if you are coding in a Groovy or in a Java artifact while using semicolons or not.

Published at DZone with permission of its author, Michael Hüttermann.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)