设置注释模板的入口:
Window-<Preference-<Java-<CodeStyle-<Code Template 然后展开Comments节点就是所有需设置注释的元素。现就每一个元素逐一介绍:
文件(Files)注释标签:
/**
* Copyright © ${year}公司名字. All rights reserved.
*
* @Title: ${file_name}
* @Prject: ${project_name}
* @Package: ${package_name}
* @Description: ${todo}
* @author: ${user}
* @date: ${date} ${time}
* @version: V1.0
*/
类型(Types)注释标签(类的注释):
/**
* @ClassName: ${type_name}
* @Description: ${todo}
* @author: ${user}
* @date: ${date} ${time}
* ${tags}
*/
字段(Fields)注释标签:
/**
* @fieldName: ${field}
* @fieldType: ${field_type}
* @Description: ${todo}
*/
构造函数标签:
/**
* @Title:${enclosing_type}
* @Description:${todo}
* ${tags}
*/
方法(Constructor & Methods)标签:
/**
* @Title: ${enclosing_method}
* @Description: ${todo}
* ${tags}
* @return: ${return_type}
*/
覆盖方法(Overriding Methods)标签:
/**
* (non Javadoc)
* @Title: ${enclosing_method}
* @Description: ${todo}
* ${tags}
* ${see_to_overridden}
*/
代表方法(Delegate Methods)标签:
/**
* ${tags}
* ${see_to_target}
*/
getter方法标签:
/**
* @return the ${bare_field_name}
*/
setter方法标签:
/**
* @param ${param} the ${bare_field_name} to set
*/
或者可以从网上下载配置好的.xml文件导入也可以。
PS:在按照上面的方法设置后,一定要到windows->Preferences->general->Content
Types,右侧Context Types树,点开Text,选择Java Source File,在下面的Default
encoding输入框中输入UTF-8,点Update,则设置Java文件编码为UTF-8,否则会出现字符编码的问题。
经过如上的设置后,就可以使用快捷键Alt+Shift+J来快速生成文档的注释了。使用方法如下:
文件头注释:
使
用New->Class方法生成的文件会在创建类的对话框选项中提示是否生成注释,默认是选中的。这样生成的.java文件就有在文件头带有上面设
置的版权信息。若是使用New->File方法建立的java文件则不会自动生成文件头的版权信息,必须手动输入。
方法、类、字段等注释:
这一类的注释只需要选中方法名、类名、或字段名,然后使用Alt+Shift+J来自动生成注释,然后补全信息即可。
<?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">/** * @return the ${bare_field_name} */</template><template autoinsert="true" context="settercomment_context" deleted="false" description="Comment for setter method" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.settercomment" name="settercomment">/** * @param ${param} the ${bare_field_name} to set */</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">/** * @Title:${enclosing_type} * @Description:${todo} * ${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">/** * Copyright © ${year}gome. All rights reserved. * * @Title: ${file_name} * @Prject: ${project_name} * @Package: ${package_name} * @Description: ${todo} * @author: ${user} * @date: ${date} ${time} * @version: V1.0 */</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} * @Description: ${todo} * @author: ${user} * @date: ${date} ${time} * ${tags} */</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">/** * @fieldName: ${field} * @fieldType: ${field_type} * @Description: ${todo} */</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">/** * @Title: ${enclosing_method} * @Description: ${todo} * @author: ${user} * @date: ${date} ${time} * ${tags} */</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">/** * (non Javadoc) * @Title: ${enclosing_method} * @Description: ${todo} * ${tags} * ${see_to_overridden} */</template><template autoinsert="false" 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="true" context="newtype_context" deleted="false" description="Newly created files" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.newtype" name="newtype">${filecomment} ${package_declaration} ${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="true" 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>
转载请注明:学时网 » eclipse注释模版整理