Eclipse Encoding specification--code templates settings
The eclipse coding specification consists of three main areas: setting up code Templates, Eclipse formatter, Checkstyle,
This article mainly describes how to set up code Templates, the following steps:
Open Window->preferences->java->code Style->code Templates:
We can see the Eclipse Code templates interface middle Configure generated code and comments area contains two menu trees: Comment, Code,
Comments represents a comment template, and code represents a template for a tag, where each submenu represents a child item.
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)
/** * Project name:${project_name} * File name:${file_name} * Package Name:${package_name} * date:${ Date}${time} * Copyright (c) ${year} All rights Reserved. * */
Comments-->types (Java class comment)
/** * ClassName: ${type_name} <br/> * function: ${todo} ADD Function. <br/> * Reason: ${ TODO} ADD REASON <br/> * Date: ${date} ${time} <br/> * @author @version ${enclosing_type}${tags} @since JDK 1.6 */
Comments-->fields (class field comment)
/** @since JDK 1.6* /
Comments-->constructors (constructor comment)
/** * Creates a new instance of ${enclosing_type}. * * ${tags} */
Comments-->methods (Java method Comment)
/** * ${enclosing_method}:(here in a word describing the function of this method) * ${todo} (This method is described here to apply conditions – optional) * ${todo} (This method is described in the execution process – optional) * ${todo} (this Describe how this method is used – optional) * ${todo} (This method is described here for considerations – optional) * @author ${user} * ${tags} @since JDK 1.6 * /
Comments-->overriding methods (override method Comment)
/** * ${todo} simply describes the implementation of the method (optional). * ${see_to_overridden} */
Comments-->delegate methods (Proxy method Comment)
/** * ${tags} * ${see_to_target} */
Comments-->getters (Java getter method Comment)
/** * ${bare_field_name}. * @returnthe ${bare_field_name} @since JDK 1.6 */
Comments-->setters (Java Setters method Comment)
/** * ${param}. * @param ${param} The ${bare_field_name} to set @since JDK 1.6 * /
code-->new Java files (new Java file Code template /**
* 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} * Function: ${todo} * Reason: ${todo} * Date: ${date} ${time} @author ${user} @since JDK 1.6 */ ${typecomment}
Code-->method body (method body template)
// ${todo} auto-generated method Stub ${body_statement}
Code-->constructor body (constructor template)
${body_statement} // ${todo} auto-generated constructor stub
Code-->getter Body (field Getter method template)
return ${field};
Code-->setter Body (field Setter method template)
Code-->catch Block Body (Exception Catch code block template)
// ${todo} auto-generated catch block ${exception_var}.printstacktrace ();
Another way is to directly import the set of template file Codetemplates.xml,
The contents of Codetemplates.xml are as follows:
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"? ><templates><template autoinsert= "false" context= "Gettercomment_context" deleted= "false" description= "Comment for getter Method" enabled= "true" id= " Org.eclipse.jdt.ui.text.codetemplates.gettercomment "name=" Gettercomment ">/*** ${bare_field_name}. * * @returnThe ${bare_field_name} *@sinceJDK 1.6*/</template><template autoinsert= "false" context= "Settercomment_context" deleted= "false" description= " Comment for Setter method "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.settercomment "Name=" Settercomment ">/*** ${param}. * * @param${param} The ${bare_field_name} to set *@sinceJDK 1.6*/</template><template autoinsert= "false" context= "Constructorcomment_context" deleted= "false" description= "Comment for created constructors" enabled= "true" id= "org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name = "Constructorcomment" >/*** Creates a new instance of ${enclosing_type}. * * ${tags}*/</template><template autoinsert= "false" context= "Filecomment_context" deleted= "false" description= " Comment for created Java files "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.filecomment "Name=" Filecomment ">/*** Project name:${project_name} * File name:${file_name} * Package Name:${package_name} * Date:${date}${time} * Copyright (c) ${year}, [email protected] All rights Reserved. * */</template><template autoinsert= "false" context= "Typecomment_context" deleted= "false" description= " Comment for created types "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.typecomment "name=" Typecomment " >/*** 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} *@sinceJDK 1.6*/</template><template autoinsert= "false" context= "Fieldcomment_context" deleted= "false" description= " Comment for Fields "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.fieldcomment "name=" Fieldcomment ">/*** ${field}:${todo} (describe what this variable represents in a sentence). * @sinceJDK 1.6*/</template><template autoinsert= "false" context= "Methodcomment_context" deleted= "false" description= " Comment for non-overriding methods "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.methodcomment "Name=" Methodcomment ">/*** ${enclosing_method}:(here to describe the effect of this method). <br/> * ${todo} (This method is described here to apply the condition – optional) .<br/> * ${todo} (This method is described here for the execution process – optional) .<br/> * ${todo} (here is a description of how this method is used – optional) .<br/> * ${todo} (This method is described here for considerations – optional) .<br/> * * @author${user} * ${tags} *@sinceJDK 1.6*/</template><template autoinsert= "false" context= "Overridecomment_context" deleted= "false" description= " Comment for overriding methods "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.overridecomment "Name=" Overridecomment ">/*** ${todo} simply describes the implementation of the method (optional). * ${see_to_overridden}*/</template><template autoinsert= "true" context= "Delegatecomment_context" deleted= "false" description= " Comment for delegate methods "Enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.delegatecomment "Name=" Delegatecomment ">/*** ${tags} * ${see_to_target}*/</template><template autoinsert= "false" context= "Newtype_context" deleted= "false" description= "Newly Created files "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.newtype "name=" Newtype ">/*** 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 * @sinceJDK 1.6 *@see */${typecomment} ${type_declaration}</template><template autoinsert= "true" context= "Classbody_context" deleted= "false" description= "Code in New Class type Bodies "Enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.classbody "name=" Classbody "> </ Template><template autoinsert= "true" context= "Interfacebody_context" deleted= "false" description= "Code in New Interface type Bodies "Enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.interfacebody "name=" Interfacebody " > </template><template autoinsert= "true" context= "Enumbody_context" deleted= "false" description= "Code in New enum type Bodies "Enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.enumbody "name=" Enumbody "> </ Template><template autoinsert= "true" context= "Annotationbody_context" deleted= "false" description= "Code in New Annotation type bodies "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.annotationbody "Name=" Annotationbody "> </template><template autoinsert=" true "context=" CATCHBLOck_context "deleted=" false "description=" Code in new catch blocks "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.catchblock "name=" Catchblock ">//${todo} auto-generated catch block${exception_var}.printstacktrace (); </template><template autoinsert= "false" context= "Methodbody_context" deleted= "false" description= "Code in Created method stubs "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.methodbody "name=" MethodBody ">//${todo} auto-generated method Stub${body_statement}</template><template autoinsert= "true" context= "Constructorbody_context" deleted= "false "Description=" Code in created constructor stubs "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.constructorbody "name=" Constructorbody ">${body_statement}//${todo} auto-generated constructor stub</template><template autoinsert= "true" context= "Getterbody_context" deleted= "false" description= "Code in Created getters "enabled=" true "id=" Org.eclipse.jdt.ui.text.codetemplates.getterbody "name=" Getterbody ">return${field};</template><template autoinsert= "true" context= "Setterbody_context" deleted= "false" description= "Code in created Setters" enabled= "true" id= "Org.eclipse.jdt.ui.text.codetemplates.setterbody" name= "Setterbody" > ${field} = ${param};</template></templates>
The purpose of setting code templates is to unify the format of various annotations and the template of code;
Eclipse Encoding specification--code templates settings