Tips on creating complicated live templates with groovy?

My team recently decided to explore switching to IntelliJ. Things are going pretty well, but someone discovered Live Templates and all of the complicated stuff they can do. Someone jokingly challenged me to write a live template which would spit out Preconditions checks for methods... A couple of days later I had figured this much out:

Template Text:
GROOVY - groovyScript("_1.collect { 'Preconditions.checkNotNull(' + it + ', \"'+ it +' cannot be null\");' }.join('\\n')",methodParameters())

This is pretty cool, and the output looks something like this:
            Preconditions.checkNotNull(id, "id cannot be null");
            Preconditions.checkNotNull(sourceKey, "sourceKey cannot be null");
            Preconditions.checkNotNull(partitionId, "partitionId cannot be null");
            Preconditions.checkNotNull(loadId, "loadId cannot be null");

Now, that's a good start, but definitely isn't perfect. This is fine if all of these variables are POJOs or other types which should only not be null, and are fine otherwise. However, we normally use other tools to help give us better control over input parameters and more informative output when things go wrong. Here's a better example of what the generated code above should look like:
            Preconditions.checkArgument(StringUtils.isEmpty(id), "id cannot be empty null");
            Preconditions.checkNotNull(sourceKey, "sourceKey cannot be null");
            Preconditions.checkArgument(StringUtils.isEmpty(partitionId), "partitionId cannot be empty or null");
            Preconditions.checkNotNull(loadId, "loadId cannot be null");

So, my main/core question is, how can I get both the method parameters and their type injected into my Groovy script, so that I can do conditional checks and generate preconditions checks accordingly?

But, more generally... Is there a better way to code these groovy script and smart template combos? There were a lot of hiccups while I was putting the above script together and instead of any really usable output/feedback/information, IntelliJ would just dump the errors/exceptions out into my source code after I had triggered the abbreviation.

Comment actions Permalink


So, my main/core question is, how can I get both the method parameters and their type injected into my Groovy script

At the moment you can't do it because methodParameters() macro returns strings only and knows nothing about its types. You can file a feature request about it to our issue tracker.

However, IMHO, LiveTemplates mechanism is not the best way for such imperative tasks. I believe that the most appropriate and flexible solution for that is a plugin for IDEA. E.g. you can implement Generator-action for generating preconditions or make an inspection that will complain about parameters that doesn't checked in method body and add needed preconditions by Alt+Enter.

If you'd like to implement a plugin there is a nice webinar about writing IntelliJ plugins that could help: and

Also in the part 2 you can find useful links to plugin development documentation.

Comment actions Permalink

Okay, thank you Alexander, I will look into creating a plugin next week and log a feature request soon.
Edit: seems like this feature request will work for my needs as well


Please sign in to leave a comment.