Use Eclipse to create templates and format code

Source: Internet
Author: User

How eclipse is set up:
Window-preferences-->java-code Style-formatter
Formatting your code with code formatter after you have written it facilitates comparison and file merging when versioning conflicts. Think of the current code writing format is somewhat too personalized, if you can create a code template, import it into all of the tools, and then all the code is written after the format, it avoids the code format is too confusing situation, it is helpful for everyone to read the code.
Eclipse has built-in code formatters and templates, or you can create your own templates:
In the menu bar, select Window-〉 preferences. In the left tree of the dialog box that appears, expand Code Styles under Java. There are two items, one is "code formatter" and the other is "code template". The main is to set up "code formatter", "code template" is set in the format of automatically generated code, such as setter and getter method code and related comments.
Set the code formatter: the Eclipse built-in template cannot be exported and needs to be created. Enter a template name and select the file to initialize the settings. This part mainly sets the format of the code after formatting, including indentation, blank lines, curly brackets and so on. Specific things in accordance with their own hobbies, you slowly determine it. Then click Export to Save as an. xml file.
There is nothing to set up "code template", mainly the annotation format for setting up some auto-generated methods, etc., such as the Getter/setter method. The inside can include some variables, can be generated when the corresponding content automatically fill, such as name, parameter name and so on. Once setup is complete, click Export All to Save As. xml file.
When using, of course, click the Import button separately, select the correct configuration file. Select formatting under source code in the menu bar, or use the shortcut CTRL shift f to format the code. Ok!
Method comments are used to describe the information of the method, such as the creator, creation date, key features, and description of the input and output. It is also understood that canonicalization method annotations can be used to automatically generate Javadoc, which looks like a project description document similar to the Java Help documentation.
Find out how to set up method comment templates and use method annotations today in Eclipse.
Set up a method comment Template: Select Preferences under Windows in the Eclipse menu bar, expand the left tree to the code template java-> code style, expand the comment-and-method in the dialog box that appears to the right, and click the Edit button on the right. Edit the content. You can also add a variable by clicking on the "Insert Variable" button below. For example:
/**
* @author ${user}
function
*${tags}
*/
Where @author is the indicator of Javadoc, the variable with the $ symbol is.
When you use the mouse, place it anywhere inside the method where you want to add the comment. When you click Add Javadoc Comment under source in the menu bar, or use the shortcut key Alt Shift J, Eclipse automatically adds a comment before the method, such as public static void Main (String[]args), which generates the following comment:
/**
* @authorhfm
function
* @param args
*/
Of course, you will need to add the contents of these items manually, such as the function of the method and the meaning of the parameters.

Use Eclipse to create templates and format code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.