您的位置:首页 > 编程语言 > Java开发

Eclipse 设置生成带有属性注释的getter/setter方法

2017-10-18 16:13 711 查看

1.  在开发中,一般类注释常注释在字段上面.但eclipse工具自动生成的getter和setter方法是没有注释的,而且还需要自己手动添加,这样比较麻烦.下面介绍如何通过修改eclipse的jar包类实现这样的功能.

首先看效果,在写字段时,我们带上了注释,当自动生成getter和setter方法时,也会自动把字段上面的注释生成了.这样方便我们的开发和以后维护.

   /**
* 创建人
*/
private String createdBy;
/**
* 创建时间
*/
private Date createdDate;

/**
* 获取: 创建人
*/
public String getCreatedBy() {
return createdBy;
}

/**
* 设置: 创建人
*/
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

/**
* 获取: 创建时间
*/
public Date getCreatedDate() {
return createdDate;
}

/**
* 设置: 创建时间
*/
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}

2.  好了,下面我们开始讲解如何实现这样的配置.首先,eclipse上面没有自带这样的功能,所以我们需要通过修改eclipse的jar进行修改生成setter和getter的方法.

  1.  我们先找到eclipse的安装目录,找到这个jar包 : org.eclipse.jdt.ui_xxxxx.jar , xxxxx是一个版本号.例如我的是org.eclipse.jdt.ui_3.12.2.v20160929-0804.jar

  该jar所在目录eclipse/plugins/

      2. 找到jar后,我们需要借用反编译工具,具体哪个工具都行,我使用的是jd-gui(下载地址:http://jd.benow.ca/).

      3. 打开反编译工具,把org.eclipse.jdt.ui_xxxxx.jar包进行反编译,然后找到org.eclipse.jdt.internal.corext.codemanipulation.GetterSetterUtil类,保存生成对应的java文件

  4. 打开eclipse,创建一个java工程,把生成的GetterSetterUtil.java文件放到工程中,注意:请保持改java文件所在的包目录为原来的目录: package org.eclipse.jdt.internal.corext.codemanipulation;

  5. 把该java文件所关联的一些jar包也引到项目中.具体使用了如下部分jar包,该部分的jar包都能在eclipse/plugins/目录下能找到.具体版本请使用自己的eclipse里面的即可:

  

  6. 到此,一切已准备就绪,接下来就是关键的时刻了. 在GetterSetterUtil.java类中找到getSetterStub和getGetterStub方法,然后分别在如下地方添加红框部分代码

红框的代码如下:

ISourceRange sr = field.getJavadocRange();
if (sr != null)
{
String fieldComment = field.getSource().substring(0, sr.getLength()).replaceAll("/", "").replaceAll("\\*", "").trim();
comment = comment.replaceAll("bare_field_comment", fieldComment);
}
else
{
String str = getLineComment((SourceField)field);
if (str != null) {
comment = comment.replaceAll("bare_field_comment", str);
}
}

  

  在该类后面添加该部分代码调用的getLineComment方法:

public static String getLineComment(SourceField field) throws JavaModelException {
ISourceRange range = field.getSourceRange();
if (range == null) {
return null;
}
IBuffer buf = null;
if (field.isBinary()) {
buf = field.getClassFile().getBuffer();
} else {
ICompilationUnit compilationUnit = field.getCompilationUnit();
if (!compilationUnit.isConsistent()) {
return null;
}
buf = compilationUnit.getBuffer();
}
int start = range.getOffset();
int length = range.getLength();
if ((length > 0) && (buf.getChar(start) == '/')) {
IScanner scanner = ToolFactory.createScanner(true, false, false, false);
try {
scanner.setSource(buf.getText(start, length).toCharArray());
int docOffset = -1;
int docEnd = -1;

int terminal = scanner.getNextToken();
switch (terminal) {
case 1003:
terminal = scanner.getNextToken();
break;
case 1001:
docOffset = scanner.getCurrentTokenStartPosition();
docEnd = scanner.getCurrentTokenEndPosition() + 1;
terminal = scanner.getNextToken();
break;
case 1002:
terminal = scanner.getNextToken();
}
if (docOffset != -1) {
SourceRange sr = new SourceRange(docOffset + start, docEnd - docOffset);
String str = field.getSource().substring(0, sr.getLength());
char[] c = str.toCharArray();
int beginIndex = 0;
char[] arrayOfChar1;
int j = (arrayOfChar1 = c).length;
for (int i = 0; i < j; i++) {
char cr = arrayOfChar1[i];
if (cr != '/') {
break;
}
beginIndex++;
}
return str.substring(beginIndex).trim();
}
} catch (InvalidInputException localInvalidInputException) {
} catch (IndexOutOfBoundsException localIndexOutOfBoundsException) {
}
}
return null;
}

  注意:加入的代码可能需要在GetterSetterUtil.java类引入一些包,为了大家方便,我把我引入的包贴一下方便大家查找:

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PostfixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.core.SourceField;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.NecessaryParenthesesChecker;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.JdtFlags;
import org.eclipse.jdt.ui.CodeGeneration;

修改完毕后,编译项目,然后找到编译出来的GetterSetterUtil.class文件.再通过压缩工具把GetterSetterUtil.class文件替换org.eclipse.jdt.ui_xxxxx.jar包中的GetterSetterUtil.class.

然后再把org.eclipse.jdt.ui_xxxxx.jar包替换eclipse/plugins/下的对应jar包.(注:替换前请备份,不然如果出问题了就悲剧了!!!)

7. 重启eclipse,打开Window > Preferences > Java > Code Style > Code Templates然后在comments中分别设置Getters和Setters,如下图:

 

 到此,大功告成啦! 

 

8. 如果觉得反编译麻烦,那大家也可以到网上找被人反编译修改好的GetterSetterUtil.class文件或者org.eclipse.jdt.ui_xxxxx.jar 包来替换自己eclipse下的jar包.但是一定要注意版本一致,不然就会出错的

 

我使用的eclipse Neon.3 Release (4.6.3),如果有和我一样的版本,那可以直接下载我修改后的使用:

GetterSetterUtil.class  (下载这个需要自己去替换org.eclipse.jdt.ui_3.12.2.v20160929-0804.jar包中的对应class文件)

org.eclipse.jdt.ui_3.12.2.v20160929-0804.jar (推荐)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco } p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px ".SF NS Text" }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐