Boost Java Productivity with IntelliJ IDEA Live Templates
This guide explains how to use IntelliJ IDEA Live Templates to automate repetitive Java code, covering built‑in snippets, creating custom templates with variables, advanced functions like clipboard and groovyScript, and practical examples for declaring loggers, beans, and printing method parameters.
Preface
Java development often requires writing repetitive code such as declaring a private logger or a bean. IDEA's Live Templates feature can generate these small code fragments automatically. Initially it looks like a simple code snippet, but it supports variable functions for complex generation.
Basic Usage
IDEA comes with many common dynamic templates. Typing fori in a Java file and pressing Tab expands to a basic for loop:
for (int i = 0; i < ; i++) {
}Pressing Tab moves the cursor to each placeholder for manual filling.
Custom Templates
The built‑in templates may not match personal coding style, so Live Templates allows custom definitions using variables. To create a template, provide an abbreviation (trigger word), optional description, select the context (e.g., Java), and then write the template body.
Simple Example
After defining a template, you can use it by typing its abbreviation. Below are some frequently 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 enclosed in $$. $END$ is a predefined variable that marks the final cursor position.
Advanced Usage
Live Templates support functions such as clipboard() (returns clipboard content) and decapitalize() (lower‑cases the first letter). The powerful groovyScript() function can execute arbitrary Groovy code to process input.
Quick Variable Declaration
A template for declaring a variable with annotations:
<osgiRef>
----------
/**
* $END$
*/
@OsgiReference
@Setter
private $TYPE$ $NAME$;Quick Logger Declaration
Using the className() function to insert the current class name:
<logger>
----------
/** logger */
private static final Logger LOGGER = LoggerFactory.getLogger($CLASS$.class);Most Powerful groovyScript()
groovyScript()can run Groovy code with optional parameters bound to _1, _2, …. Example syntax:
groovyScript("code", ...)
| code | a Groovy script or path to a script |
| ... | optional parameters bound to _1, _2, ... |Quick Bean Configuration
When adding a Spring bean in XML, you can generate the id and class attributes using clipboard(), groovyScript(), and decapitalize():
<bean>
----------
<bean id="$id$" class="$REF$" />Here $id$ is bound to
decapitalize(groovyScript("_1.tokenize('.')[-1]", clipboard())), which extracts the class name from the fully‑qualified reference and lower‑cases the first letter.
Quickly Print Current Context Information
To log method parameters, use the methodParameters() function together with groovyScript to format them:
<printContext>
---------------
LogUtil.$TYPE$(LOGGER, "$MSG$ " + $params$);Bind $params$ to
groovyScript('"' + _1.collect { it + " = [" + it + "]" }.join(', ') + '"', methodParameters())to automatically generate a formatted parameter list.
Conclusion
IDEA offers many more template functions beyond those shown here. By mastering Live Templates, developers can dramatically reduce repetitive coding, focus on core logic, and improve overall productivity.
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.
