• Home
  • Events
  • About
  • IntelliJ IDEA 11 for the Groovy Developer

    IntelliJ IDEA 11 was released a few weeks ago, and it contains quite a few new features for the Groovy developer. Everything listed here is in the free and open source Community Edition of IntelliJ IDEA. There are plenty of new Grails features as well, but I wanted to separate out the Ultimate Edition features into a different post. So let’s jump in.

    Groovy 2.0 Support

    The big promoted feature of IDEA 11 is the Groovy 2.0 support, which is itself mostly Java 7 support. In case you’re confused: Java 7 added some small language feature to Java (more here), and Groovy naturally supports the same syntax. The Project Coin Java changes are by definition small language changes, so don’t expect anything too major in terms of productivity boosts.

    The first feature is underscores in numeric literals:

    Underscores in Number Literals

    Do you really need an explanation of this? OK, here it is then. You can put underscores into numbers and the compiler rips them out. They are just ignored. Just look at the .class file in a decompiler if you don’t believe me.

    The second feature is binary literals:

    Binary Literals

    This lets you specify integers using a binary format. As far as I can tell, this is mostly used if you’re constructing bit-masks to represent multiple states within one integer. You know, you could also use objects to do that and it might be clearer. But there are always times when you need this.

    The third feature is multi-catch, which is the most useful feature of the three:

    Java 7 Multi-Catch

    This lets you catch multiple exception types in one catch block. In Java, you can Alt+Enter on an old-style catch to convert it into a multi-catch, but you can’t yet do that in Groovy. Don’t worry though, I created a ticket and it will soon be implemented…


    For me the Groovy refactorings are one of the killer features of IDEA. There are a pair of nice upgrades in IDEA 11 in this area. The first is that Introduce Parameter works for closures now and not just methods. So if you start with something like this, where the literal 50 is hard-coded:

    Introduce Parameter for Closures

    You can highlight the 50 and press Ctrl+Alt+P to Introduce a Parameter:

    Ctrl+Alt+P to Introduce Parameter

    OK, so that is nice. A less well-known intention is the Unwrap Statement intention. Unwrap takes a statement that is wrapped in some sort of loop or conditional, and it removes that conditional. Witness:

    Start with an If statement, choose to unwrap it, and you’re left with only the enclosed logic. Now that you know this trick, you will start to use it more often. It’s surprisingly useful.

    Intentions and Inspections

    There are also a couple of good, new intentions and inspections in 11. On the inspection side is the new “Incompatible In” inspection. You can use the ‘in’ keyword in, which is normally the inverse of ‘contains’. Using the in keyword to do incompatible type comparisons now triggers an IDE warning:

    It’s a dynamically typed language, but as you can see the tools can easily catch your small type errors.

    Also in 11 is my favorite intention, Convert JUnit Assertion to Assert. This converts your old-style JUnit assertion methods into Groovy power-asserts:

    Just position the cursor at the method call and press Alt+Enter. And if you’re not sure why power asserts are an improvement then read this.

    Also in IDEA 11 is ‘Split If’ and ‘Invert If’. We wrote these intentions ourselves at Hackergarten in Devoxx. Horray for open source! Split If takes a compound boolean operation like ‘if (a && b) { … }’ and converts it into ‘if (a) { if (b) { … } }. Invert If takes the boolean conditional in the If and inverts it. Both intentions are invoked with Alt+Enter and they are both described in more detail over at the JetBrains IDEA blog.

    Besides that, the Import system got some new improvements as well. You can now use Alt+Enter to swap a qualified reference with an import, add a single-member static import, or add an on-demand static import. And my favorite: copy and paste within the IDE now carries the import statements with it. So if you copy a reference to the clipboard and paste it, then IDEA will ask you if you want to update the import statements. Whenever I switch back to a text editor I always miss the seamless import statement management provided by the IDE.

    Groovy Isms

    What else? Well, the IDE is now aware of @Category annotations and gives you correct code completion based on them. And you can finally specify a command line parameter when running a Groovy script from within IDEA. Hint: it’s the box marked ‘Script Parameters’:

    Managing dependencies with Jars and Grapes got easier. If you annotate a script with @Grape when IDEA has always imported that dependency into the project for you. Well now IDEA removes obsolete @Grapes references when you no longer need them.

    Lastly, there are many small UI improvements, such as overriding properties now have a gutter icon to show the relationship, multiple declarations on a single line now have better alignment, and pasting a slashy string now escapes the slashes. There are more as well, but they are even more minor.

    Plus Grails Plus Java Plus…

    And don’t forget, all the base improvements of IDEA, especially the speed improvements, will be felt in Groovy. The database UI has improved, the Navbar has improved, Git support has improved. It’s all there whether you’re in Java or Groovy. And like I said at the start, Grails has many improvements but they aren’t covered here… maybe in a later post after the holidays.

    So long and happy holidays!

    Need help with Groovy or Grails? Canoo Engineering offers training, consulting, and project delivery. Contact me directly at hamlet.darcy@canoo.com for more information.

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • email
    • Print
    • Twitter
    • LinkedIn
    • XING
    • Facebook
    • Google Bookmarks

    1 Comment »

    1. Peter Gromov said,

      December 20, 2011 @ 11:26

      Thanks for this very comprehensive post! A great place to point people to if one wants to convince them to use IDEA 🙂

    RSS feed for comments on this post · TrackBack URI

    Leave a Comment

    Time limit is exhausted. Please reload the CAPTCHA.