Eclipse自动生成的注释不能满足要求,于是需要通过修改Eclipse源码来达到自定义注释的目的。
类似于教程:Eclipse解决自动补全变量名并自定义上屏按键为TAB
具体操作步骤
1.获取Eclipse源码
下载一个带源码的Eclipse,或者下载一个Eclipse SDK全搞定:eclipse project archived downloads
最后需要修改的文件为: GetterSetterUtil.java,就是这个文件:GetterSetterUtil.java
或者你可以直接在eclipse中安装 Eclipse plug-in development environment 和 Eclipse JDT Plug-in Developer Resources。
2.在 plug-in development 中将 org.eclipse.jdt.ui 导出为工程项目(具体导出步骤,见此文章开头的链接)。或者直接解压源码包加入Eclipse工程。
3.修改文件
打开 org.eclipse.jdt.internal.corext.codemanipulation -> GetterSetterUtil.java 文件。
搜索 CodeGeneration.getSetterComment 和 CodeGeneration.getGetterComment 两个位置,分别在下一行添加如下代码:
1 2 3 4 5 6 |
ISourceRange sr= field.getJavadocRange(); if (null != sr) { String filedComment= field.getSource(); filedComment= filedComment.substring(0, sr.getLength()).replaceAll("[\n,\r,*,/,\t]", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$ comment= comment.replace("#{bare_field_comment}", filedComment); //$NON-NLS-1$ } |
需要导入的包为: org.eclipse.jdt.core.ISourceRange
4.保存并导出为jar包,替换Eclipse plugins 目录下相应的jar包。
修改Eclipse注释模板
1.打开Window > Preferences > Java > Code Style > Code Templates
2.展开 Comments > Getters 设置 Pattern(设置仅供参考,请自行设置为需要的格式)
1 2 3 4 |
/** * 获取 #{bare_field_comment} //#{bare_field_comment} 必须,用于显示成员变量名称 * @return ${bare_field_name} */ |
3.设置Setters的Pattern
1 2 3 4 |
/** * 设置 #{bare_field_comment} * @param ${param} #{bare_field_comment} */ |
使用方法
1.新建成员变量并设置注释(需要使用 /** 形式的注释):
1 2 3 4 |
/** * 用户名 */ private String userName; |
2.使用快捷键 shift+alt+s 打开源码控制面板,选择 Generate Getters and Setters…,然后勾选要生成的 get/set 方法,注意勾选 Generate method comments。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * 获取 用户名 * @return userName */ public String getUserName() { return userName; } /** * 设置 用户名 * @param userName 用户名 */ public void setUserName(String userName) { this.userName = userName; } |
文件下载
文件名:org.eclipse.jdt.ui_3.10.2.v20141014-1419.jar Eclispe-comments
文件名:org.eclipse.jdt.ui_3.11.2.v20151123-1510.jar https://soft.kuoruan.com/eclipse/autoComment/
文章来源:Eclipse中setter/getter方法自动添加属性注释
- 本文固定链接: https://blog.kuoruan.com/42.html
- 转载请注明: Index 于 扩软博客 发表
Eclispe生成getter/setter时自动添加中文注释
| 扩软博客 http://beritapialadunia.site
群主,请问下32bit和64bit的eclipse修改起来有什么地方不同吗?为什么我用sts-3.8.3.RELEASE里的org.eclipse.jdt.ui_3.12.2.v20160929-0804.jar我自己修改的,无效呢
好久没用 Eclipse 了,现在我都是用 IDEA,具体的问题只有你自己试试了
厉害! 来点个赞!