让Myeclipse自动生成的get set方法 自动加上文本注释,并且注释内容包含字段中我们加的文档注释
在进行编码写实体类的时候发现,一个实体类有好多的字段要进行注释,他们都是私有的不能直接访问,我们在写的时候加入的文档注释也起不到效果,但是自动生成的get,set方法的文档注释有不符合我们要求(没有包含字段中的文档注释) 所以就很纠结。在网上看到了一些大神有解决方法就试了下可以,拿出来和大家分享下!
修改后的效果图:
步骤:
1:在myeclipse/eclisp中搜索找到org.eclipse.jdt.ui_*.jar(*是版本号)
2:将jar文件改成.rar格式并打开(修改前最好先备份一下这个jar文件)
3:找到\org\eclipse\jdt\internal\corext\codemanipulation这个目录下的GetterSetterUtil.class这个文件, 并下载我提供压缩文件下的文件替换掉这一个文件就可以了
4:开启开发工具找到Window->Preferences->Java->CodeStyle->Code Templates->Comments->Getters/Setters
getters:
/**
* 获取${bare_field_name}
* @return ${bare_field_name} ${bare_field_name}
*/
setters:
/**
* 设置${bare_field_name}
* @param ${bare_field_name} ${bare_field_name}
*/
注意:生成Get/Set方法时勾选上Generate method comments
上面的链接如果没有积分的话,可以新建一个.txt,把下面这些代码丢进去,直接改文件名为.class(代码为编译后的代码)
package org.eclipse.jdt.internal.corext.codemanipulation; import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.Flags;
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.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Assignment.Operator;
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.InfixExpression.Operator;
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.PostfixExpression.Operator;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression.Operator;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.JdtFlags;
import org.eclipse.jdt.ui.CodeGeneration; public class GetterSetterUtil
{
private static final String[] EMPTY = new String[0]; public static String getGetterName(IField field, String[] excludedNames)
throws JavaModelException
{
boolean useIs = StubUtility.useIsForBooleanGetters(field
.getJavaProject());
return getGetterName(field, excludedNames, useIs);
} private static String getGetterName(IField field, String[] excludedNames, boolean useIsForBoolGetters) throws JavaModelException
{
if (excludedNames == null)
excludedNames = EMPTY;
return getGetterName(field.getJavaProject(), field.getElementName(),
field.getFlags(), (useIsForBoolGetters) &&
(JavaModelUtil.isBoolean(field)), excludedNames);
} public static String getGetterName(IVariableBinding variableType, IJavaProject project, String[] excludedNames, boolean isBoolean)
{
boolean useIs = (StubUtility.useIsForBooleanGetters(project)) &&
(isBoolean);
return getGetterName(project, variableType.getName(),
variableType.getModifiers(), useIs, excludedNames);
} public static String getGetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames)
{
return NamingConventions.suggestGetterName(project, fieldName, flags,
isBoolean, excludedNames);
} public static String getSetterName(IVariableBinding variableType, IJavaProject project, String[] excludedNames, boolean isBoolean)
{
return getSetterName(project, variableType.getName(),
variableType.getModifiers(), isBoolean, excludedNames);
} public static String getSetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames)
{
return NamingConventions.suggestSetterName(project, fieldName, flags,
isBoolean, excludedNames);
} public static String getSetterName(IField field, String[] excludedNames) throws JavaModelException
{
if (excludedNames == null)
excludedNames = EMPTY;
return NamingConventions.suggestSetterName(field.getJavaProject(),
field.getElementName(), field.getFlags(),
JavaModelUtil.isBoolean(field), excludedNames);
} public static IMethod getGetter(IField field) throws JavaModelException {
String getterName = getGetterName(field, EMPTY, true);
IMethod primaryCandidate = JavaModelUtil.findMethod(getterName,
new String[0], false, field.getDeclaringType());
if ((!JavaModelUtil.isBoolean(field)) || ((primaryCandidate != null) &&
(primaryCandidate.exists()))) {
return primaryCandidate;
}
String secondCandidateName = getGetterName(field, EMPTY, false);
return JavaModelUtil.findMethod(secondCandidateName, new String[0],
false, field.getDeclaringType());
} public static IMethod getSetter(IField field) throws JavaModelException
{
String[] args = { field.getTypeSignature() };
return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args,
false, field.getDeclaringType());
} public static String getSetterStub(IField field, String setterName, boolean addComments, int flags)
throws CoreException
{
String fieldName = field.getElementName();
IType parentType = field.getDeclaringType();
String returnSig = field.getTypeSignature();
String typeName = Signature.toString(returnSig);
IJavaProject project = field.getJavaProject();
String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(project, fieldName, field.getFlags());
String argname = StubUtility.suggestArgumentName(project, accessorName, EMPTY);
boolean isStatic = Flags.isStatic(flags);
boolean isSync = Flags.isSynchronized(flags);
boolean isFinal = Flags.isFinal(flags);
String lineDelim = "\n";
StringBuffer buf = new StringBuffer();
if (addComments) {
String comment = CodeGeneration.getSetterComment(
field.getCompilationUnit(),
parentType.getTypeQualifiedName('.'),
setterName,
field.getElementName(),
typeName,
argname,
accessorName,
lineDelim);
ISourceRange sr = field.getJavadocRange();
if (sr != null) {
String filedComment = field.getSource();
filedComment = filedComment.substring(0, sr.getLength());
filedComment = filedComment.replaceAll("[\n,\r,*,/, ,\t]", ""); comment = comment.replaceFirst(field.getElementName(), filedComment); int i = comment.lastIndexOf(field.getElementName());
int j = getCount(comment, field.getElementName());
if ((i != -1) && (j >= 2)) {
comment = comment.substring(0, i) + filedComment + comment.substring(i + field.getElementName().length());
}
} if (comment != null) {
buf.append(comment);
buf.append(lineDelim);
}
}
buf.append(JdtFlags.getVisibilityString(flags));
buf.append(' ');
if (isStatic)
buf.append("static ");
if (isSync)
buf.append("synchronized ");
if (isFinal)
buf.append("final ");
buf.append("void ");
buf.append(setterName);
buf.append('(');
buf.append(typeName);
buf.append(' ');
buf.append(argname);
buf.append(") {");
buf.append(lineDelim);
boolean useThis = StubUtility.useThisForFieldAccess(project);
if ((argname.equals(fieldName)) || ((useThis) && (!isStatic)))
if (isStatic)
fieldName = parentType.getElementName() + '.' + fieldName;
else
fieldName = "this." + fieldName;
String body = CodeGeneration.getSetterMethodBodyContent(field
.getCompilationUnit(), parentType.getTypeQualifiedName('.'),
setterName, fieldName, argname, lineDelim);
if (body != null)
buf.append(body);
buf.append("}");
buf.append(lineDelim);
return buf.toString();
} public static String getGetterStub(IField field, String getterName, boolean addComments, int flags)
throws CoreException
{
String fieldName = field.getElementName();
IType parentType = field.getDeclaringType();
boolean isStatic = Flags.isStatic(flags);
boolean isSync = Flags.isSynchronized(flags);
boolean isFinal = Flags.isFinal(flags);
String typeName = Signature.toString(field.getTypeSignature());
String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(field.getJavaProject(), fieldName, field.getFlags());
String lineDelim = "\n";
StringBuffer buf = new StringBuffer();
if (addComments)
{
String comment = CodeGeneration.getGetterComment(
field.getCompilationUnit(),
parentType.getTypeQualifiedName('.'),
getterName,
field.getElementName(),
typeName,
accessorName,
lineDelim);
ISourceRange sr = field.getJavadocRange();
if (sr != null) {
String filedComment = field.getSource();
filedComment = filedComment.substring(0, sr.getLength());
filedComment = filedComment.replaceAll("[\n,\r,*,/, ,\t]", ""); comment = comment.replaceFirst(field.getElementName(), filedComment); int i = comment.lastIndexOf(field.getElementName());
int j = getCount(comment, field.getElementName());
if ((i != -1) && (j >= 2)) {
comment = comment.substring(0, i) + filedComment + comment.substring(i + field.getElementName().length());
}
} if (comment != null) {
buf.append(comment);
buf.append(lineDelim);
}
}
buf.append(JdtFlags.getVisibilityString(flags));
buf.append(' ');
if (isStatic)
buf.append("static ");
if (isSync)
buf.append("synchronized ");
if (isFinal)
buf.append("final ");
buf.append(typeName);
buf.append(' ');
buf.append(getterName);
buf.append("() {");
buf.append(lineDelim);
boolean useThis = StubUtility.useThisForFieldAccess(field.getJavaProject());
if ((useThis) && (!isStatic))
fieldName = "this." + fieldName;
String body = CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, lineDelim);
if (body != null)
buf.append(body);
buf.append("}");
buf.append(lineDelim);
return buf.toString();
} private static int getCount(String str, String sign)
{
if (str == null)
return 0;
double i = str.length();
str = str.replaceAll(sign, "");
return (int)(i - str.length()) / sign.length();
} public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher)
{
InfixExpression.Operator op = null;
AST ast = astRewrite.getAST();
if (isNotInBlock(node))
return null;
if (node.getNodeType() == 7) {
Assignment assignment = (Assignment)node;
Expression rightHandSide = assignment.getRightHandSide();
Expression copiedRightOp = (Expression)astRewrite
.createCopyTarget(rightHandSide);
if (assignment.getOperator() == Assignment.Operator.ASSIGN) {
ITypeBinding rightHandSideType = rightHandSide
.resolveTypeBinding();
copiedRightOp = createNarrowCastIfNessecary(copiedRightOp,
rightHandSideType, ast, variableType, is50OrHigher);
return copiedRightOp;
}
if (getterExpression != null) {
InfixExpression infix = ast.newInfixExpression();
infix.setLeftOperand(getterExpression);
infix.setOperator(ASTNodes.convertToInfixOperator(
assignment.getOperator()));
infix.setRightOperand(copiedRightOp);
ITypeBinding infixType = infix.resolveTypeBinding();
return createNarrowCastIfNessecary(infix, infixType, ast,
variableType, is50OrHigher);
}
} else if (node.getNodeType() == 37) {
PostfixExpression po = (PostfixExpression)node;
if (po.getOperator() == PostfixExpression.Operator.INCREMENT)
op = InfixExpression.Operator.PLUS;
if (po.getOperator() == PostfixExpression.Operator.DECREMENT)
op = InfixExpression.Operator.MINUS;
} else if (node.getNodeType() == 38) {
PrefixExpression pe = (PrefixExpression)node;
if (pe.getOperator() == PrefixExpression.Operator.INCREMENT)
op = InfixExpression.Operator.PLUS;
if (pe.getOperator() == PrefixExpression.Operator.DECREMENT)
op = InfixExpression.Operator.MINUS;
}
if ((op != null) && (getterExpression != null)) {
return createInfixInvocationFromPostPrefixExpression(op,
getterExpression, ast, variableType, is50OrHigher);
}
return null;
} private static boolean isNotInBlock(ASTNode parent) {
ASTNode statement = parent.getParent();
boolean isStatement = statement.getNodeType() != 21;
ASTNode block = statement.getParent();
boolean isBlock = (block.getNodeType() == 8) || (block.getNodeType() == 50);
boolean isControlStatemenBody =
ASTNodes.isControlStatementBody(statement.getLocationInParent());
return (isStatement) || ((!isBlock) && (!isControlStatemenBody));
} private static Expression createInfixInvocationFromPostPrefixExpression(InfixExpression.Operator operator, Expression getterExpression, AST ast, ITypeBinding variableType, boolean is50OrHigher)
{
InfixExpression infix = ast.newInfixExpression();
infix.setLeftOperand(getterExpression);
infix.setOperator(operator);
NumberLiteral number = ast.newNumberLiteral();
number.setToken("1");
infix.setRightOperand(number);
ITypeBinding infixType = infix.resolveTypeBinding();
return createNarrowCastIfNessecary(infix, infixType, ast, variableType,
is50OrHigher);
} private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher)
{
PrimitiveType castTo = null;
if (variableType.isEqualTo(expressionType))
return expression;
if (is50OrHigher) {
if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(
variableType))
castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(
variableType))
castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(
variableType))
castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
}
if (ast.resolveWellKnownType("char").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
if (ast.resolveWellKnownType("byte").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
if (ast.resolveWellKnownType("short").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
if (castTo != null) {
CastExpression cast = ast.newCastExpression();
if (ASTNodes.needsParentheses(expression)) {
ParenthesizedExpression parenthesized = ast
.newParenthesizedExpression();
parenthesized.setExpression(expression);
cast.setExpression(parenthesized);
} else {
cast.setExpression(expression);
}
cast.setType(castTo);
return cast;
}
return expression;
}
}
让Myeclipse自动生成的get set方法 自动加上文本注释,并且注释内容包含字段中我们加的文档注释的更多相关文章
- Eclipse,IDEA自动生成相应对象接收方法返回值的快捷键
@Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemM ...
- Eclipse用法和技巧七:自动生成get和set方法2
上一篇文章中我们介绍了自动批量生成get和set函数的方法.这个方法一般在声明完类的数据域之后使用,比较方便快捷.这里再补充几个自动生成get和set函数的方法. 步骤一:在声明的数据域中按Ctrl+ ...
- Eclipse用法和技巧六:自动生成get和set方法1
java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改了属性值.声 ...
- 基于注解处理器开发自动生成getter和setter方法的插件
昨天无意中,逛到了lombok的网站,并看到了首页的5分钟视频,视频中的作者只是在实体类中写了几个字段,就可以自动编译为含setter.getter.toString()等方法的class文件.看着挺 ...
- python--selenium实用的自动生成测试HTML报告方法--HTMLTestRunner
python--selenium实用的自动生成测试HTML报告方法--HTMLTestRunner 下面给大家介绍下用HTMLTestRunner模块自动生成测试报告的方法. 一.首先我们导入unit ...
- mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置
mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置 ============================== 蕃薯耀 2018年3月14 ...
- alt+shift+j 自动添加类的文档注释 Myeclipse
alt+shift+j 自动添加类的文档注释 Myeclipse ctrl+shift+y 将选中的内容大写换成小写 +x是转换成大写
- 将C#文档注释生成.chm帮助文档
由于最近需要把以前的一个项目写一个文档,但一时又不知道写成怎样的,又恰好发现了可以生成chm的工具,于是乎我就研究了下,感觉还不错,所以也给大家分享下.好了,不多废话,下面就来实现一下吧. 生成前的准 ...
- 工具分享——将C#文档注释生成.chm帮助文档
由于最近需要把以前的一个项目写一个文档,但一时又不知道写成怎样的,又恰好发现了可以生成chm的工具,于是乎我就研究了下,感觉还不错,所以也给大家分享下.好了,不多废话,下面就来实现一下吧. 生成前的准 ...
随机推荐
- Linux显示文件和目录的详细资料
Linux显示文件和目录的详细资料 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ls -l 总用量 56 -rw-r--r-- 1 youhaidong y ...
- 关于CS1061报错(XX不包含XXX的定义,并且找不到类型为XX的第一个参.....)的一种可能的解决的办法
在我编程中,我遇到了一个这样的报错, 可是我引用的product类中又确实定义了这么一个方法, protected void BindPageData(int categoryID) { Produc ...
- tablesorter 的使用
<table id="myTable" class="tablesorter"> <thead> <tr> <th&g ...
- JAVA简单的网格布局管理器--JAVA基础
网格布局管理器: GridLayoutDemo.java: import java.awt.GridLayout;import javax.swing.JButton;import javax.swi ...
- JAVA中String类的方法(函数)总结--JAVA基础
1.concat()方法,当参数为两字符串时,可实现字符串的连接: package cn.nxl123.www; public class Test { public static void main ...
- jquery中的事件与应用
mouseover和mouseenter的区别 mouseenter的定义是当鼠标穿过该元素才会被执行,而mouseover是当鼠标穿过该元素或者其子元素皆会触发该事件 mouseleave一般是与m ...
- ThreadPoolExecutor线程池参数设置技巧
一.ThreadPoolExecutor的重要参数 corePoolSize:核心线程数 核心线程会一直存活,及时没有任务需要执行 当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线 ...
- mybatis快速入门(七)-spring-mybatis整合
今天写写spring-mybatis整合吧,先写个原始dao类的整合,下一节在写个动态代理的整合,我就不写太详细了,因为前面的章节基本上都有了,我直接就一口气都写出来需要那些文件然后在直接贴代码,首先 ...
- 几道很Interesting的偏序问题
若干道偏序问题(STL,分块) 找了4道题目 BZOJ陌上花开(权限题,提供洛谷链接) Cogs2479偏序 Cogs2580偏序II Cogs2639偏序++ 作为一个正常人,肯定先看三维偏序 做法 ...
- AC自动机模板2(【CJOJ1435】)
题面 Description 对,这就是裸的AC自动机. 要求:在规定时间内统计出模版字符串在文本中出现的次数. Input 第一行:模版字符串的个数N. 第2->N+1行:N个字符串.(每个模 ...