Live Templates in IntelliJ IDEA

Okay, I did the math. If you use all 38 of the Live Templates for Java that are available out of the box in IntelliJ IDEA 2020.2, you will save your finger pads approximately 2092 presses of wear and tear, and that’s just each one once, the reality is likely to be substantially higher. I will caveat this by saying that I counted the unresolved variables, yours may be longer or shorter. Still, it’s impressive!

What are Live Templates?

Java has had its fair share of grief over boilerplate code and verbosity. You could type it out manually (boring), switch to Kotlin (it’s an option), or take a look at using IntelliJ IDEA to save you both time and effort.

When I first came across the notion of Live Templates, I couldn’t figure out what was ‘live’ about them. Did they need feeding or something? It seems to be an industry-standard term, so I’m no longer devoting much energy to this quandary, but if you were wondering the same, you’re not alone.

There are lots of types of live templates in IntelliJ IDEA (38 in fact, as I mentioned). There’s the delightful four-finger taps `psvm` which will expand to 39 finger taps of:

public-static-void-main.png

And another of my favourites, also four-finger taps, is `sout` which expands to 20 finger taps:

system-out-println.png

The Live Templates even determine where the caret appears, which is why screenshots are useful here. There are lots of these ‘non-variable’ Live Templates. They’re quick to use and will quickly embed themselves in your muscle memory. You’ll wonder how you ever wrote:

public static final String

… instead of psfs

However, that’s not all live templates can do; they can also help you with something called ‘parameterised templates’ with variables that you can manipulate.

For example, fori gives you a rather lovely for loop template (29 finger taps):

for-loop-clean.png

By default, IntelliJ IDEA will put the caret on the first instance of i, and this is so you can change it to your preferred variable if required. If you then hit tab, the editor will jump you to the user input part between the less-than symbol and the semi-colon. Tab again will drop you into the body of the loop so you can step away from those arrow keys.

Other live templates for user input constructs include itar to iterate over elements of an array, itco to iterate over elements of a Collection as well as may other iteration operations. lazy is another win, quite literally, to perform a lazy initialisation.

Identifying a Live Template

IntelliJ IDEA is always trying to be helpful. One thing I struggled with was working out what was what when I was typing code. You can spot a live template from the lack of parenthesis and a helpful description. For example, the last four here are live templates, but only one is strictly from Java – fori.

live-template-list.png

Poking around Live Templates

You can take a look at Live Templates from your Preferences using ⌘, on macOS and Ctrl+Alt+S on Windows and Linux then start typing in Live Templates. You’ll see the list split by language. You can have a dig around in here until your heart’s content turning them off (and on again). I found it interesting to check out not only what Java Live Templates there were but also the Live Templates available in the other languages.

Making/Editing/Duplicating a Live Template

Manipulating Live Templates is easy to do in IntelliJ IDEA. As above, find your Live Templates in Preferences and then click this little plus icon:

If you want to duplicate one instead, use the icon a bit further down that looks like paste clipboard.

When you select 1. Live template you can then enter a keyboard abbreviation, a description, and the text that will form your live template.

Live Templates are very flexible in their creation, so here’s a couple of callouts. Firstly, know and use variables wisely. I found these a little bit confusing at first glance, but this documentation removes the mystery nicely and explains what each one does.

Let’s walk through the fori Live Template to see how it breaks down:

for-i-live-template.png

When we use it in a Java class we get this:

for-i-empty.png

How does that work then? Looking back at the Live Template definition, we can see the Edit Variables button. Clicking it yields this information:

edit-template-variables.png

This tells us that the variable $INDEX$ is defined as suggestIndexName() which we can see from the page I pointed you to earlier is defined as:

So $INDEX$ becomes i when we use the Live Template. $LIMIT$ didn’t get a definition in the Live Template so when it’s used in a Java class, that’s where the tab lands for the user to enter a value.

If you leave the value blank, the user can tab to it when the Live Template is used and enter their preferred value, as we saw in the fori example earlier.

Secondly, define your context carefully; this is where the Live Template will be available for use. For example, these are the available Java contexts:

java-contexts.png

Our fori example has a context of Statement which makes sense for a for loop:

for-i-context.png

Now you can go forth and create all the Live Templates that your heart desires and save even more finger taps!

How to Feel Good (about Live Templates)

It’s 2020, to be honest, we all need a little extra help with that warm fuzzy feeling this year. I found this thing called Productivity Guide in IntelliJ IDEA. It doesn’t just cover Live Templates; it covers everything that IntelliJ IDEA does to save your finger pads from wear and tear. Mine is pretty feeble because I have to reset my environment multiple times for various screencasts and stuff. I bet your IntelliJ IDEA statistics are substantially more impressive!

The Java SE Ecosystem

The Java SE ecosystem is strewn with acronyms that it has picked up over the last 25 years. Sometimes those acronyms even mean multiple things. Sometimes there are acronyms within acronyms! This post attempts to explain them all in terms of two main groupings:

  • OpenJDK

  • Java Development Kits (JDKs)

What is ‘OpenJDK’?

The phrase OpenJDK is used to describe at least three fundamental things in the Java ecosystem. I’ve expanded the links here so you can see what I’m talking about.

First

First, there is a place called OpenJDK which lives at https://openjdk.java.net and is also known to some as The OpenJDK Project. This is where people collaborate on an open-source implementation of the Java specifications, https://www.oracle.com/java/technologies/java-se-glance.html, which are released every 6 months. Sun Microsystems open-sourced the majority of Java in 2006 under the GNU General Public License (GNU GPL) version 2 with a linking exception. OpenJDK serves as a continuation of that change. This is the use that I will refer to througout this blog. When I say OpenJDK I mean the place at https://openjdk.java.net unless otherwise specified.

Second

OpenJDK can also refer to just the source code repository on GitHub available at https://github.com/openjdk. This is the reference implementation of the Java specifications that I mentioned above. Many vendors use the term OpenJDK for their specific JDK binary, which is produced by building a binary off the OpenJDK code on GitHub. For example AdoptOpenJDK, which is available at https://adoptopenjdk.net.

Third

Lastly, OpenJDK can also refer to Oracle’s free JDKs which live at https://jdk.java.net. These are version specific binaries built from the source code which is available on GitHub at https://github.com/openjdk. Oracle provides fixes to this JDK for 6 months (until the next release of Java).

Who contributes to OpenJDK?

There are various channels to contribute to OpenJDK. This blog from Oracle has a graphic which breaks down committers for Java 15 – those that committed code to OpenJDK. To contribute to OpenJDK, you need to use a JDK Enhancement Proposal (JEP) to start with.

Correction with thanks to Marc Maathuis: You don’t always need a JEP to contribute to the OracleJDK. Bug fixes, for example may not. Contributors need to have signed the Oracle Contributor Agreement.

What are JEPs?

JDK Enhancement Proposal (JEP) is a proposed change to OpenJDK. You can think of them as the roadmap for Java. Like all good roadmaps, there’s no commitment to inclusion or timescales. Some JEPs require changes to the Java specifications. In this instance, a corresponding Java Specification Request (JSR) is required.

What is a JSR?

A Java Specification Request (JSR) may detail potential specification changes (where present) that arise from one or more JEPs. Not all JEPs will have JSRs; if the JEP doesn’t have changes to the specifications, you don’t need a JSR. A JSR may also be a suggested new specification for Java SE that does not require a JEP, such as a new specification for an API for computer vision. JSRs are considered for inclusion in the Java specifications by the Java Community Process (JCP).

What is the JCP?

The Java Community Process (JCP) is a process to facilitate the review and ultimate inclusion of changes to the Java specifications.

What are JDKs?

Java Development Kits (JDKs) are implementations of the Java SE platform specification by different vendors and groups of people, such as the open source community. Some of them are built from the OpenJDK source code. JDKs include the Java Runtime Environment (JRE), as well as other tools that help you develop Java.

What is the Java Runtime Environment (JRE)?

The Java Runtime Environment (JRE) is a component of the JDK that is required to run Java. It used to be a separate download from the JDK, but, since Java 11, it’s now part of the JDK itself rather than a separate entity meaning you can no longer download it separately.

Are all JDKs the same?

Vendors may introduce little implementation differences such as garbage collection, branding, and utilities, but they are all implementations of the Java platform specifications. For the purpose of this blog I’ve assumed the Java SE platform.

When can you call something a JDK?

To be called a JDK officially, the binaries need to have passed a Java Compatibility Kit (JCK) for that release which is a collection of Technology Compatibility Kits (TCK) that tests each JSR to ensure that the implementation of the specification behaves as expected. Oracle’s OpenJDK we spoke about earlier has passed a JCK, for example.

What’s a Technology Compatibility Kit (TCK)?

A Technology Compatibility Kit (TCK) is a set of tests that is applicable for a JSR. There has been controversy on the license for the TCK given it’s an open source project. There is now a specific license to allow the TCK to be run against the OpenJDK source code under the GPL license.

Who makes JDKs?

There are lots of JDKs out there. They all vary in terms of license, support, branding, and implementation differences. The list includes, but is not limited to AdoptOpenJDK, OracleJDK, Oracle OpenJDK, RedHat, and IBM. It is also possible to build your own JDK too. Talking of blogs, this one from the Java Champions is excellent and helps fill in some of the gaps as to how we got here.

Summary

‘OpenJDK’ is either:

Java Development Kits:

  • A binary which is an implementation of the Java platform specification that has passed a TCK.

  • Some JDKs are free to use, some have a cost associated with them for various things such as commercial use, fixes and support.

Contributions to Java and updates:

  • Anyone can contribute to OpenJDK.

  • The JCP is used to manage updates to the Java specifications. Anyone can join the JCP.

  • JEPs are the process for including changes to OpenJDK.

  • JSRs are the standards for Java SE, which may, or may not be implemented in the JDK itself.