Boost Java Productivity: Master IntelliJ IDEA Live Templates for Rapid Code Generation
This tutorial explains how Java developers can use IntelliJ IDEA Live Templates to automatically generate repetitive code such as private fields, loggers, and Spring beans, covering basic usage, custom templates, advanced functions, and practical examples that dramatically improve coding efficiency.
In Java development, repetitive code such as private fields, loggers, or beans can be generated automatically using IntelliJ IDEA's Live Templates. Although they look like simple snippets at first, they support variable functions for complex code generation.
Basic Usage
IDEA includes many common dynamic templates; typing fori and pressing Enter expands to a for‑loop skeleton.
for (int i = 0; i < ; i++) {
}Press Tab to jump between placeholders and fill values manually.
Custom Template
Built‑in templates may not satisfy personal coding style, so Live Templates provide variable functions for custom definitions.
Simple Usage
To add a custom template, specify the abbreviation (trigger word), optional description, define the Java context, and write the template body.
Here are a few commonly used simple templates:
==========
<out>
----------
System.out.println($END$)
==========
<pfs>
----------
private final static String $varName$ = "$var$";
==========
<privateField>
----------
/**
* $COMMENT$
*/
@Getter
@Setter
private $TYPE$ $NAME$;
==========
<main>
----------
public static void main(String[] args) {
$END$
}
==========Variables are denoted with $ symbols. $END$ is a special predefined variable that marks the final cursor position.
Advanced Usage
If you have used Vim snippet plugins, you know that templates can execute functions. IDEA can sense code semantics, such as the parameters of the current method, enabling powerful automation.
Quick Variable Declaration
A template for annotated private fields:
<osgiRef>
----------
/**
* $END$
*/
@OsgiReference
@Setter
private $TYPE$ $NAME$;The template differs from <privateField> by binding functions to variables.
Useful functions: clipboard(): returns the current clipboard string. decapitalize(): lower‑cases the first character of a string.
Quick Logger Declaration
Using the className() function to obtain the current class name:
<logger>
----------
/** logger */
private static final Logger LOGGER = LoggerFactory.getLogger($CLASS$.class);Powerful groovyScript()
The groovyScript() function can execute arbitrary Groovy code to process input and produce a string.
groovyScript("code", ...)
| code | A Groovy snippet or absolute path to a Groovy script |
| ... | Optional parameters bound to _1, _2, … in the script |Quick Bean Configuration
Generating Spring bean XML using clipboard() and groovyScript():
<bean>
----------
<bean id="$id$" class="$REF$" />The id is bound to
decapitalize(groovyScript("_1.tokenize('.')[-1]", clipboard())), which extracts the class name from the fully‑qualified reference and lower‑cases its first letter.
Print Current Context Information
When logging errors, you may want to print method parameters. The methodParameters() function returns the parameter list, which can be formatted with groovyScript():
<printContext>
---------------
LogUtil.$TYPE$(LOGGER, "$MSG$ " + $params$);Here $params$ is bound to
groovyScript("'\"' + _1.collect { it + ' = [\"' + it + '\"]'}.join(', ') + '\"'", methodParameters()), automatically formatting the arguments.
Summary
IDEA offers many template functions; mastering them can greatly increase productivity by eliminating repetitive coding. For a full list, refer to the official "Creating and Editing Template Variables" documentation. Additionally, the CodeMaker plugin can further streamline code generation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
