Play with Eclipse - Java Code Template for automatic code generation

 When the code is written to a certain level, you will find that a lot of code has been repeatedly typed N times, and it is no exaggeration to say that you can type it out with your eyes closed. A lot of typing these repetitive codes has basically no other benefits except to exercise the speed of typing on the keyboard, but it will waste a lot of time in the long run and reduce the efficiency of software development. For example: in order to output logger information during program execution, you need to add such a piece of code to each class:

 

[java]  view plain copy  
 
  1. import org.slf4j.Logger;  
  2. import org.slf4j.LoggerFactory;  
  3.   
  4. publicclass XXXClass {   
  5.     /** 
  6.      * Logging mechanism. 
  7.      */  
  8.     privatestatic Logger logger = LoggerFactory.getLogger(XXXClass.class);   
  9.       
  10. }  

        The only difference in this code in different class files is the name of the class: XXXClass. The average person may type it letter by letter; the lazy person will Ctrl + C and Ctrl + V, and then change the XXXClass; the smart person can do it with a few letters and a shortcut key. This article takes the above logger as an example to introduce a more convenient and quick way for smart people to use Java Code Template.

 

1. Template settings

Select Window -> Preferences ->  Java  -> Editor -> Templates         on the Eclipse toolbar to enter the following interface:

        In the list is the Code Template that comes with Eclipse. The last column is " on ", which means it is valid, and if it is not, it means it is invalid. If you want the built-in Code Template to take effect, you can select the desired Code Template, click " Edit... ", in the Edit Template dialog box that opens, select the " Automatically insert " check box, and click "OK" to save the settings. . Use these effective built-in Code Template methods in the same way as the logger demonstrated later.

        Click " New... " to create a new template. The detailed settings of the Template are as follows:

        Where you need to declare a logger, enter the template name logger, and then press the Ctrl + Space shortcut key to display the prompt box of the template logger as shown in the following figure:

        Select the template logger to be added, and the code in the template will be automatically added, including imported packages, comments, and logger declarations.

2. Ctrl + Space does not work

        In some cases, after pressing the Ctrl + Space shortcut, there is no response and no prompt box is displayed. Two situations may exist:

 

  • Case 1: Content Assist shortcut is not set

 

Select Window -> Preferences -> General -> Keys         on the Eclipse toolbar to enter the following shortcut key setting interface:

        Enter " content assist " in the edit box, and the "Content Assist" command will be automatically searched. After selecting it, enter " Ctrl+Space " in the "Binding" edit box below, or directly press the Ctrl + Space shortcut key, Then click "OK".

 

  • Case 2: Ctrl + Space shortcut key conflict

 

        If Ctrl + Space has been set as the shortcut key of Content Assist, but still has no effect, the possible reason is that the shortcut key of Ctrl + Space of Eclipse conflicts with the shortcut key of input method switching in Windows system. Change the shortcut key of input method switching to other, as shown in the following figure:

3. There is no template in the Ctrl + Space prompt box

        If you press Ctrl + Space, the template logger is not displayed in the prompt box, as shown in the following figure:

        This is because Template Proposals is not selected in Content Assist. Select Window -> Preferences -> Java -> Editor -> Content Assist -> Advanced on the Eclipse toolbar to set the following:

        Select the checkbox in front of "Template Proposals" in both boxes. If the "Template Proposals" in the box below is not the first one, you can select it, and then click the "Up" button until it is in the first position, so that the first one in each prompt box is the required Code Template.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326714189&siteId=291194637