Now the project is generally a team development, and everyone has their own coding habits, in order to unify the format, the project team will develop a series of specifications before the project development. The purpose of the code templates is to unify the format of the various annotations and the template of the codes, as long as you have set up code templates and then use Eclipse to easily generate our own custom annotations . So we have to take some steps to help us unify the coding style of the project developers. Mainly includes three aspects: set up code Templates, Eclipse formatter, this article mainly describes how to set up code Templates, the following steps:
Open Window->preferences->java->code Style->code Templates
Click "Import" to import the template Codetemplates.xml file.
Codetemplates.xml content is our own pre-defined, here is not described in detail, we can see the Eclipse code templates interface between configure generated code and The Comments area contains two menu trees: Comment, Code, as shown in:
Comments represents a comment template, and code represents a template for a tag, where each submenu represents a child item.
As soon as we click on a subkey, we see the template content and format we defined in the pattern area below the interface, as shown in:
As shown, when we click on the Files submenu under comments, the following pattern shows the header comment for the Java file.
The following is a detailed list of the template formats for each subkey:
Comments-->files (Java file comment)
Java code
- /**
- * Project Name:${project_name}
- * File Name:${file_name}
- * Package Name:${package_name}
- * Date:${date}${time}
- * Copyright (c) ${year}, [email protected] All rights Reserved.
- *
- */
Comments-->types (Java class comment)
Java code
- /**
- * ClassName: ${type_name} <br/>
- * Function: ${todo} ADD Function. <br/>
- * Reason: ${todo} ADD Reason (optional). <br/>
- * Date: ${date} ${time} <br/>
- *
- * @author ${user}
- * @version ${enclosing_type}${tags}
- * @since JDK 1.6
- */
Comments-->fields (class field comment)
Java code
- /**
- * ${field}:${todo} (describe what this variable represents in a sentence).
- * @since JDK 1.6
- */
Comments-->constructors (constructor comment)
Java code
- /**
- * Creates a new instance of ${enclosing_type}.
- *
- * ${tags}
- */
Comments-->methods (Java method Comment)
Java code
- /**
- * ${enclosing_method}:(Here is a word describing the effect of this method). <br/>
-
-
-
-
-
- * @author ${user}
- span class= "comment" > * ${tags}
- * @since jdk 1.6
- */
Comments-->overriding methods (override method Comment)
Java code
- /**
- * ${todo} simply describes the implementation of the method (optional).
- * ${see_to_overridden}
- */
Comments-->delegate methods (Proxy method Comment)
Java code
- /**
- * ${tags}
- * ${see_to_target}
- */
Comments-->getters (Java getter method Comment)
Java code
- /**
- * ${bare_field_name}.
- *
- * @return The ${bare_field_name}
- * @since JDK 1.6
- */
Comments-->setters (Java Setters method Comment)
Java code
- /**
- * ${param}.
- *
- * @param ${param} The ${bare_field_name} to set
- * @since JDK 1.6
- */
code-->new Java files (new Java file Code template)
Java code
- /**
- * Project Name:${project_name}
- * File Name:${file_name}
- * Package Name:${package_name}
- * Date:${date}${time}
- * Copyright (c) ${year}, [email protected] All rights Reserved.
- *
- */
- ${filecomment}
- ${package_declaration}
- /**
- * Classname:${type_name} <br/>
- * Function: ${todo} ADD Function. <br/>
- * Reason: ${todo} ADD Reason. <br/>
- * Date: ${date} ${time} <br/>
- * @author ${user}
- * @version
- * @since JDK 1.6
- * @see
- */
- ${typecomment}
- ${type_declaration}
Code-->method body (method body template)
Java code
- ${todo} auto-generated Method stub
- ${body_statement}
Code-->constructor body (constructor template)
Java code
- ${body_statement}
- ${todo} auto-generated Constructor stub
Code-->getter Body (field Getter method template)
Java code
- Return ${field};
Code-->setter Body (field Setter method template)
Java code
- ${field} = ${param};
Code-->catch Block Body (Exception Catch code block template)
Java code
- ${todo} auto-generated Catch block
- ${exception_var}.printstacktrace ();
Code specification: Eclipse Code templates settings