In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use the IDEA code generator", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the IDEA code generator.
Basic use
IDEA comes with many commonly used dynamic templates. Enter fori into the Java code and enter will appear.
For (int I = 0; I <; iTunes +) {}
Press Tab to jump in each blank and fill in the value manually.
Custom Template
After all, the official template can not meet the needs of our personal coding style, Live Templates provides a way for us to customize variable functions.
Simple usage
To add a custom template, you first need to enter the trigger word (that is, Abbreviation), and the description is optional, then define the context of the template, click define to select Java, so that the current template will be triggered when editing the Java. After defining the context, you can fill in the template.
Here are a few simple templates that I often use
=-System.out.println ($END$) =-privatefinalstatic String $varName$ = "$var$"; `=-/ * * $COMMENT$ @ Getter @ Setter private $TYPE$ $NAME$; =-public static void main (String [] args) {$END$} =
The template supports the definition of variables, using characters surrounded by $$to represent a variable. Is a special predefined variable that represents the last jump position of the cursor. The position of each variable can be skipped.
Advanced usage
If you have used vim's Code Sinppet plug-in, you will find that the template can execute functions, of course, powerful Live Templates also supports, and IDEA can sense the semantics of the code, such as the parameters of the currently edited function. But this can make us play with flowers. We study the function of template function from easy to difficult.
The variables we mentioned earlier can be bound to functions, as shown in the figure above.
Quickly declare variables
Declaring variables is a common operation, especially when you need to declare variables that need to be annotated, and the code is boring to write. Here is the template I defined:
-/ * $END$ @ OsgiReference @ Setter private $TYPE$ $NAME$
At first glance, this template looks similar to the privateField I defined above, except that I bind functions to these variables.
1.clipboard (): returns the string of the current pasteboard
2.decapitalize (): lowercase the first letter of the entered string
Let's demonstrate that we first copy the current class name, and then type osgiRef
Quickly declare logger
Declaring logger is also a common operation, above we use the paste function to quickly declare variables, now let's use another function className (), as the name implies, its function is to return the current class name.
-/ * * logger * / privatestaticfinal Logger LOGGER = LoggerFactory.getLogger ($CLASS$.class)
Most powerful groovyScript ()
If the functions used above provide limited and inflexible capabilities, groovyScript () provides everything you want, allowing you to execute Groovy scripts to process the input and then output the processed strings.
GroovyScript ("code",...) | code | absolute path of a piece of Groovy code or Groovy script code | |. | optional input parameters. These parameters are bound to `_ 1, _ 2, _ 3,. _ n`, which are used in Groovy code. | |
Let's take a look at its practical application.
Rapid bean configuration
For a new service, you have to register a bean in Spring. Generally, this configuration will only specify id and class. Because we configure it in xml, we cannot use the className () function, but we can use the clipboard () function to get the full reference of the class. In IDEA, we directly right-click the class name and click Copy Reference. Then execute the groovy script to get the class name.
-
Id binds decapitalize (groovyScript ("_ 1.tokenize ('.') [- 1]", clipboard ()), first takes the full reference of clipboard () to the class, and then executes the groovy code _ 1.tokenize ('.') [- 1] (press. Split it into an array of strings, then take the last one to get the class name, and then lowercase the first letter with decapitalize () to get id.
Quickly print current context information
When printing the error log, you need to print the current context information, such as input parameters. Sometimes, when there are many input parameters, it is very painful to write. Fortunately, there is a template function methodParameters (), which returns the list of parameters of the current function. Of course, we can not use this list directly, we need to convert it with groovyScript.
-LogUtil.$TYPE$ (LOGGER, "$MSG$" + $params$)
If you bind params to groovyScript ("'" + _ 1.collect {it +'= ["+'+ it +'+"]'} .join (',') +'"", methodParameters ()), you can automatically format and output the parameters of the current function.
At this point, I believe you have a deeper understanding of "how to use the IDEA code generator". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.