preface
Java development often needs to write code with a fixed format, such as declaring a private variable, logger, or bean, etc. For this kind of small-scale code generation, we can take advantage of the Live Templates feature provided by IDEA. I started out thinking it was just a simple Code Snippet, but then I realized that it supports variable function configuration and can support very complex Code generation. Let me introduce you to the use of Live Templates.
The basic use
IDEA comes with many commonly used dynamic templates. Type fori in Java code and press Enter
for (int i = 0; i < ; i++) {
}
Copy the code
fori
According to theTab
You can jump in each blank and fill in the value manually.
The custom Template
After all, native Templates don’t fit our personal coding style, so Live Templates provides a way to customize variable functions.
Simple usage
To add custom templates, first fill in the trigger word (i.e., Abbreviation), the description is optional, then define the context of the template, click Define to select Java, this will trigger the current template when editing Java, after defining the context, you can fill in the template.
new template
Here are a few simple templates I use frequently
= = = = = = = = = =
<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$
}
= = = = = = = = = =
Copy the code
Templates support the definition of variables, using? The enclosed character represents a variable. $END$is a special predefined variable that represents where the cursor last jumps. The location of each variable can be jumped over.
Advanced usage
If you’ve ever used vimCode Sinppet
Plugins, you’ll find templates that can execute functions, powerfulLive Templates
Yes, of course, and IDEA is aware of the semantics of the code, such as the arguments to the currently edited function. But that’s the one thing that makes us play. We examine the functionality of template functions from easy to difficult.
variables function
The variables mentioned earlier can be bound to functions, as shown in the figure above.
Quick declaration of variables
Declaring variables is a common operation, especially if you need to declare variables and add comments, which can be tedious to write. Here is the template I defined:
<osgiRef>
----------
/ * *
* $END$
* /
@OsgiReference
@Setter
private $TYPE$ $NAME$;
Copy the code
At first glance, this template looks similar to the privateField I defined above, the only difference being that I bind functions to these variables.
clipboard()
: Returns the string of the current stickboarddecapitalize()
: Changes the first letter of the entered string to lowercase
To demonstrate this, let’s copy the current class name and type itosgiRef
osgiRef
Quick declaration of Logger
Declaring logger is another common operation. We used the paste function to declare a variable quickly, but now we use another function className(), which, as the name suggests, returns the current className.
<logger>
----------
/** logger */
private static final Logger LOGGER = LoggerFactory.getLogger($CLASS$.class);
Copy the code
The most powerful groovyScript()
If the above functions don’t offer much in the way of power and flexibility, groovyScript() provides just about everything you could want by allowing you to execute Groovy scripts to process input and then output the processed string.
groovyScript("code", ...)
| code | a Groovy code or absolute path | Groovy script code
|... | can be selected to join, these parameters will be bound to a ` _1, _2, _3,... _n ', used in Groovy code. |
Copy the code
Let’s look at it in action.
Fast Bean configuration
To add a new service, you need to register a bean in Spring. Normally, this configuration will specify the ID and class. Since we are configuring in XML, we cannot use the className() function, but we can use the clipboard() function to get a full reference to the class. In IDEA, we can directly right-click the class name and click Copy Reference. Then execute the Groovy script to get the class name.
<bean>
----------
<bean id="$id$" class="$REF$" />
Copy the code
GroovyScript (“_1.tokenize(‘.’)[-1]”, clipBoard ()))). Then execute the Groovy code _1.tokenize(‘.’)[-1] (press. Split into an array of strings, then take the last one to get the class name, then lowercase the first letter with decapitalize() to get the ID.
bean
Print current context information quickly
The template function methodParameters() returns a list of the current function’s parameters, which we can’t use directly. It needs to be transformed with groovyScript.
<printContext>
---------------
LogUtil.$TYPE$(LOGGER, "$MSG$ " + $params$);
Copy the code
Params binding to groovyScript (” ‘\ “” + _1. Collect {it +’ = [\” + “+ it + + \] ‘}. Join (‘, ‘) + ‘\”‘ “, methodParameters ()), Automatically format the arguments of the current function for output.
conclusion
IDEA has many other Template functions, including Creating and Editing Template Variables. IDEA is a powerful tool that can greatly improve productivity by focusing on the key tasks rather than wasting time writing repetitive code. Some of the more advanced uses have yet to be discovered. Finally, promote a wave of code generation plug-in CodeMaker THAT I wrote, and make good use of it to save a lot of time to write code repeatedly.