如何在Eclipse中如何自动添加注释和自定义注释风格

2,355 阅读3分钟

背景简介

丰富的注释和良好的代码规范,对于代码的阅读性和可维护性起着至关重要的作用。几乎每个公司对这的要求还是比较严格的,往往会形成自己的一套编码规范。但是再实施过程中,如果全靠手动完成,不仅效率低下,还难以保证真正的符合规范。所以结合IDE环境,自动生成注释,还是很有必要的。今天我们就说一下,如何使用Eclipse给我们提供的自定义代码模版的功能来作业。

设置注释模板

设置注释模板的入口:Window->Preference->Java->Code Style->Code Template ,然后展开Comments节点就是所有需设置注释的元素了!


接下来,对每一个元素逐一介绍:

  • 文件(Files)注释标签

Files标签是对新建的文件的说明,出现在文件最上面


举栗子:

/**   
 * Copyright © ${year} eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Package: ${package_name} 
 * @author: ${user}   
 * @date: ${date} ${time} 
 */
  • 类型(Types)注释标签(类的注释)

Types标签是对类Class的一个说明,出现在类上面

举栗子:

 /** 
 * @ClassName: ${type_name} 
 * @Description: ${todo}
 * @author: ${user}
 * @date: ${date} ${time} 
 * ${tags} 
 */
  • 字段(Fields)注释标签

Fields标签是对变量字段的说明

举栗子:

// @Fields ${field} : ${todo}(用一句话描述这个变量表示什么)  
  • 构造函数(Constructors)标签

Constructors标签是对类的构造函数的说明

举栗子:

/** 
 * @Title:${enclosing_type}
 * @Description:${todo} 
 * ${tags} 
 */  
  • 方法(Methods)标签

Methods标签是对函数方法的说明

举栗子:

/** 
* @Title: ${enclosing_method} 
* @Description: ${todo} 
* ${tags} ${return_type}
* @author ${user}
* @date ${date}${time}
*/ 
  • 覆盖方法(Overriding Methods)标签

Overriding Methods标签是对覆盖方法的说明

举栗子:

/* (non Javadoc) 
 * @Title: ${enclosing_method}
 * @Description: ${todo}
 * ${tags} 
 * ${see_to_overridden} 
 */ 
  • 代表方法(Delegate Methods)标签

举栗子:

/**
 * ${tags}
 * ${see_to_target}
 */  
  • getter方法标签

举栗子:

/**  
* @return ${bare_field_name}  
*/  
  • setter方法标签

举栗子:

/**  
* @param ${param} 要设置的 ${bare_field_name}  
*/  

以上标签,只需要点击右侧面板上的按钮 – 编辑(Edit), 便可修改成自己的注释!

如何自动添加注释

可通过如下三种方法自动添加注释:
(1)输入“/**”并回车。
(2)用快捷键 Alt+Shift+J(先选中某个方法、类名或变量名)。
(3)在右键菜单中选择“Source > Generate ElementComment”。

另外,新建文件或类的时候,怎么自动生成文件(file)的注释呢?


只需要勾选Automatically and comments for new methods and types即可!

导入/导出代码格式模板

如果你辛辛苦苦定制好了自己的代码风格,然后换了台机器进行操作或重装了Eclipse,是不是要重新配置一遍呢?答案当然是No了,Eclipse提供了“导出”和“导入”功能,你可以把自己的模板导出来在其他机器上使用。


创建自定义注释模板

eclipse自带一些注释模板,如日期(@date)、文件名(@ClassName)、作者(@author)等,那么怎么自定义一些注释模板呢? codetemplates.xml模板内容,可直接导入eclipse

/**
* @Fields field:field:{todo}(用一句话描述这个变量表示什么)
*//**
* MIT License
* Copyright (c) 2018 haihua.liu
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*//**
* @param paramtheparamthe{bare_field_name} to set
*//**
* @return ${bare_field_name}
*//**
* @ClassName: ${type_name}
* @Description: ${todo}(这里用一句话描述这个类的作用)
* @author ${user}
* @date ${date}
*
* ${tags}
*//** (非 Javadoc)
*
*
* ${tags}
* ${see_to_overridden}
*//**
* ${tags}
* ${see_to_target}
*//**
* @Title: ${enclosing_method}
* @Description: ${todo}(这里用一句话描述这个方法的作用)
* @param ${tags} 参数
* @return ${return_type} 返回类型
* @throws
*/ /**
* 创建一个新的实例 ${enclosing_type}.
*
* ${tags}
*/