• 让Myeclipse自动生成的get set方法 自动加上文本注释,并且注释内容包含字段中我们加的文档注释


    在进行编码写实体类的时候发现,一个实体类有好多的字段要进行注释,他们都是私有的不能直接访问,我们在写的时候加入的文档注释也起不到效果,但是自动生成的get,set方法的文档注释有不符合我们要求(没有包含字段中的文档注释) 所以就很纠结。在网上看到了一些大神有解决方法就试了下可以,拿出来和大家分享下!

    修改后的效果图:

    步骤:

    1:在myeclipse/eclisp中搜索找到org.eclipse.jdt.ui_*.jar(*是版本号)

    2:将jar文件改成.rar格式并打开(修改前最好先备份一下这个jar文件)

    3:找到orgeclipsejdtinternalcorextcodemanipulation这个目录下的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(代码为编译后的代码)

      1 package org.eclipse.jdt.internal.corext.codemanipulation;
      2 
      3 import org.eclipse.core.runtime.CoreException;
      4 import org.eclipse.jdt.core.Flags;
      5 import org.eclipse.jdt.core.IField;
      6 import org.eclipse.jdt.core.IJavaProject;
      7 import org.eclipse.jdt.core.IMethod;
      8 import org.eclipse.jdt.core.ISourceRange;
      9 import org.eclipse.jdt.core.IType;
     10 import org.eclipse.jdt.core.JavaModelException;
     11 import org.eclipse.jdt.core.NamingConventions;
     12 import org.eclipse.jdt.core.Signature;
     13 import org.eclipse.jdt.core.dom.AST;
     14 import org.eclipse.jdt.core.dom.ASTNode;
     15 import org.eclipse.jdt.core.dom.Assignment;
     16 import org.eclipse.jdt.core.dom.Assignment.Operator;
     17 import org.eclipse.jdt.core.dom.CastExpression;
     18 import org.eclipse.jdt.core.dom.Expression;
     19 import org.eclipse.jdt.core.dom.ITypeBinding;
     20 import org.eclipse.jdt.core.dom.IVariableBinding;
     21 import org.eclipse.jdt.core.dom.InfixExpression;
     22 import org.eclipse.jdt.core.dom.InfixExpression.Operator;
     23 import org.eclipse.jdt.core.dom.NumberLiteral;
     24 import org.eclipse.jdt.core.dom.ParenthesizedExpression;
     25 import org.eclipse.jdt.core.dom.PostfixExpression;
     26 import org.eclipse.jdt.core.dom.PostfixExpression.Operator;
     27 import org.eclipse.jdt.core.dom.PrefixExpression;
     28 import org.eclipse.jdt.core.dom.PrefixExpression.Operator;
     29 import org.eclipse.jdt.core.dom.PrimitiveType;
     30 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
     31 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
     32 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
     33 import org.eclipse.jdt.internal.corext.util.JdtFlags;
     34 import org.eclipse.jdt.ui.CodeGeneration;
     35 
     36 public class GetterSetterUtil
     37 {
     38   private static final String[] EMPTY = new String[0];
     39 
     40   public static String getGetterName(IField field, String[] excludedNames)
     41     throws JavaModelException
     42   {
     43     boolean useIs = StubUtility.useIsForBooleanGetters(field
     44       .getJavaProject());
     45     return getGetterName(field, excludedNames, useIs);
     46   }
     47 
     48   private static String getGetterName(IField field, String[] excludedNames, boolean useIsForBoolGetters) throws JavaModelException
     49   {
     50     if (excludedNames == null)
     51       excludedNames = EMPTY;
     52     return getGetterName(field.getJavaProject(), field.getElementName(), 
     53       field.getFlags(), (useIsForBoolGetters) && 
     54       (JavaModelUtil.isBoolean(field)), excludedNames);
     55   }
     56 
     57   public static String getGetterName(IVariableBinding variableType, IJavaProject project, String[] excludedNames, boolean isBoolean)
     58   {
     59     boolean useIs = (StubUtility.useIsForBooleanGetters(project)) && 
     60       (isBoolean);
     61     return getGetterName(project, variableType.getName(), 
     62       variableType.getModifiers(), useIs, excludedNames);
     63   }
     64 
     65   public static String getGetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames)
     66   {
     67     return NamingConventions.suggestGetterName(project, fieldName, flags, 
     68       isBoolean, excludedNames);
     69   }
     70 
     71   public static String getSetterName(IVariableBinding variableType, IJavaProject project, String[] excludedNames, boolean isBoolean)
     72   {
     73     return getSetterName(project, variableType.getName(), 
     74       variableType.getModifiers(), isBoolean, excludedNames);
     75   }
     76 
     77   public static String getSetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames)
     78   {
     79     return NamingConventions.suggestSetterName(project, fieldName, flags, 
     80       isBoolean, excludedNames);
     81   }
     82 
     83   public static String getSetterName(IField field, String[] excludedNames) throws JavaModelException
     84   {
     85     if (excludedNames == null)
     86       excludedNames = EMPTY;
     87     return NamingConventions.suggestSetterName(field.getJavaProject(), 
     88       field.getElementName(), field.getFlags(), 
     89       JavaModelUtil.isBoolean(field), excludedNames);
     90   }
     91 
     92   public static IMethod getGetter(IField field) throws JavaModelException {
     93     String getterName = getGetterName(field, EMPTY, true);
     94     IMethod primaryCandidate = JavaModelUtil.findMethod(getterName, 
     95       new String[0], false, field.getDeclaringType());
     96     if ((!JavaModelUtil.isBoolean(field)) || ((primaryCandidate != null) && 
     97       (primaryCandidate.exists()))) {
     98       return primaryCandidate;
     99     }
    100     String secondCandidateName = getGetterName(field, EMPTY, false);
    101     return JavaModelUtil.findMethod(secondCandidateName, new String[0], 
    102       false, field.getDeclaringType());
    103   }
    104 
    105   public static IMethod getSetter(IField field) throws JavaModelException
    106   {
    107     String[] args = { field.getTypeSignature() };
    108     return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args, 
    109       false, field.getDeclaringType());
    110   }
    111 
    112   public static String getSetterStub(IField field, String setterName, boolean addComments, int flags)
    113     throws CoreException
    114   {
    115     String fieldName = field.getElementName();
    116     IType parentType = field.getDeclaringType();
    117     String returnSig = field.getTypeSignature();
    118     String typeName = Signature.toString(returnSig);
    119     IJavaProject project = field.getJavaProject();
    120     String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(project, fieldName, field.getFlags());
    121     String argname = StubUtility.suggestArgumentName(project, accessorName, EMPTY);
    122     boolean isStatic = Flags.isStatic(flags);
    123     boolean isSync = Flags.isSynchronized(flags);
    124     boolean isFinal = Flags.isFinal(flags);
    125     String lineDelim = "
    ";
    126     StringBuffer buf = new StringBuffer();
    127     if (addComments) {
    128       String comment = CodeGeneration.getSetterComment(
    129         field.getCompilationUnit(), 
    130         parentType.getTypeQualifiedName('.'), 
    131         setterName, 
    132         field.getElementName(), 
    133         typeName, 
    134         argname, 
    135         accessorName, 
    136         lineDelim);
    137       ISourceRange sr = field.getJavadocRange();
    138       if (sr != null) {
    139         String filedComment = field.getSource();
    140         filedComment = filedComment.substring(0, sr.getLength());
    141         filedComment = filedComment.replaceAll("[
    ,
    ,*,/, ,	]", "");
    142 
    143         comment = comment.replaceFirst(field.getElementName(), filedComment);
    144 
    145         int i = comment.lastIndexOf(field.getElementName());
    146         int j = getCount(comment, field.getElementName());
    147         if ((i != -1) && (j >= 2)) {
    148           comment = comment.substring(0, i) + filedComment + comment.substring(i + field.getElementName().length());
    149         }
    150       }
    151 
    152       if (comment != null) {
    153         buf.append(comment);
    154         buf.append(lineDelim);
    155       }
    156     }
    157     buf.append(JdtFlags.getVisibilityString(flags));
    158     buf.append(' ');
    159     if (isStatic)
    160       buf.append("static ");
    161     if (isSync)
    162       buf.append("synchronized ");
    163     if (isFinal)
    164       buf.append("final ");
    165     buf.append("void ");
    166     buf.append(setterName);
    167     buf.append('(');
    168     buf.append(typeName);
    169     buf.append(' ');
    170     buf.append(argname);
    171     buf.append(") {");
    172     buf.append(lineDelim);
    173     boolean useThis = StubUtility.useThisForFieldAccess(project);
    174     if ((argname.equals(fieldName)) || ((useThis) && (!isStatic)))
    175       if (isStatic)
    176         fieldName = parentType.getElementName() + '.' + fieldName;
    177       else
    178         fieldName = "this." + fieldName;
    179     String body = CodeGeneration.getSetterMethodBodyContent(field
    180       .getCompilationUnit(), parentType.getTypeQualifiedName('.'), 
    181       setterName, fieldName, argname, lineDelim);
    182     if (body != null)
    183       buf.append(body);
    184     buf.append("}");
    185     buf.append(lineDelim);
    186     return buf.toString();
    187   }
    188 
    189   public static String getGetterStub(IField field, String getterName, boolean addComments, int flags)
    190     throws CoreException
    191   {
    192     String fieldName = field.getElementName();
    193     IType parentType = field.getDeclaringType();
    194     boolean isStatic = Flags.isStatic(flags);
    195     boolean isSync = Flags.isSynchronized(flags);
    196     boolean isFinal = Flags.isFinal(flags);
    197     String typeName = Signature.toString(field.getTypeSignature());
    198     String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(field.getJavaProject(), fieldName, field.getFlags());
    199     String lineDelim = "
    ";
    200     StringBuffer buf = new StringBuffer();
    201     if (addComments)
    202     {
    203       String comment = CodeGeneration.getGetterComment(
    204         field.getCompilationUnit(), 
    205         parentType.getTypeQualifiedName('.'), 
    206         getterName, 
    207         field.getElementName(), 
    208         typeName, 
    209         accessorName, 
    210         lineDelim);
    211       ISourceRange sr = field.getJavadocRange();
    212       if (sr != null) {
    213         String filedComment = field.getSource();
    214         filedComment = filedComment.substring(0, sr.getLength());
    215         filedComment = filedComment.replaceAll("[
    ,
    ,*,/, ,	]", "");
    216 
    217         comment = comment.replaceFirst(field.getElementName(), filedComment);
    218 
    219         int i = comment.lastIndexOf(field.getElementName());
    220         int j = getCount(comment, field.getElementName());
    221         if ((i != -1) && (j >= 2)) {
    222           comment = comment.substring(0, i) + filedComment + comment.substring(i + field.getElementName().length());
    223         }
    224       }
    225 
    226       if (comment != null) {
    227         buf.append(comment);
    228         buf.append(lineDelim);
    229       }
    230     }
    231     buf.append(JdtFlags.getVisibilityString(flags));
    232     buf.append(' ');
    233     if (isStatic)
    234       buf.append("static ");
    235     if (isSync)
    236       buf.append("synchronized ");
    237     if (isFinal)
    238       buf.append("final ");
    239     buf.append(typeName);
    240     buf.append(' ');
    241     buf.append(getterName);
    242     buf.append("() {");
    243     buf.append(lineDelim);
    244     boolean useThis = StubUtility.useThisForFieldAccess(field.getJavaProject());
    245     if ((useThis) && (!isStatic))
    246       fieldName = "this." + fieldName;
    247     String body = CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, lineDelim);
    248     if (body != null)
    249       buf.append(body);
    250     buf.append("}");
    251     buf.append(lineDelim);
    252     return buf.toString();
    253   }
    254 
    255   private static int getCount(String str, String sign)
    256   {
    257     if (str == null)
    258       return 0;
    259     double i = str.length();
    260     str = str.replaceAll(sign, "");
    261     return (int)(i - str.length()) / sign.length();
    262   }
    263 
    264   public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher)
    265   {
    266     InfixExpression.Operator op = null;
    267     AST ast = astRewrite.getAST();
    268     if (isNotInBlock(node))
    269       return null;
    270     if (node.getNodeType() == 7) {
    271       Assignment assignment = (Assignment)node;
    272       Expression rightHandSide = assignment.getRightHandSide();
    273       Expression copiedRightOp = (Expression)astRewrite
    274         .createCopyTarget(rightHandSide);
    275       if (assignment.getOperator() == Assignment.Operator.ASSIGN) {
    276         ITypeBinding rightHandSideType = rightHandSide
    277           .resolveTypeBinding();
    278         copiedRightOp = createNarrowCastIfNessecary(copiedRightOp, 
    279           rightHandSideType, ast, variableType, is50OrHigher);
    280         return copiedRightOp;
    281       }
    282       if (getterExpression != null) {
    283         InfixExpression infix = ast.newInfixExpression();
    284         infix.setLeftOperand(getterExpression);
    285         infix.setOperator(ASTNodes.convertToInfixOperator(
    286           assignment.getOperator()));
    287         infix.setRightOperand(copiedRightOp);
    288         ITypeBinding infixType = infix.resolveTypeBinding();
    289         return createNarrowCastIfNessecary(infix, infixType, ast, 
    290           variableType, is50OrHigher);
    291       }
    292     } else if (node.getNodeType() == 37) {
    293       PostfixExpression po = (PostfixExpression)node;
    294       if (po.getOperator() == PostfixExpression.Operator.INCREMENT)
    295         op = InfixExpression.Operator.PLUS;
    296       if (po.getOperator() == PostfixExpression.Operator.DECREMENT)
    297         op = InfixExpression.Operator.MINUS;
    298     } else if (node.getNodeType() == 38) {
    299       PrefixExpression pe = (PrefixExpression)node;
    300       if (pe.getOperator() == PrefixExpression.Operator.INCREMENT)
    301         op = InfixExpression.Operator.PLUS;
    302       if (pe.getOperator() == PrefixExpression.Operator.DECREMENT)
    303         op = InfixExpression.Operator.MINUS;
    304     }
    305     if ((op != null) && (getterExpression != null)) {
    306       return createInfixInvocationFromPostPrefixExpression(op, 
    307         getterExpression, ast, variableType, is50OrHigher);
    308     }
    309     return null;
    310   }
    311 
    312   private static boolean isNotInBlock(ASTNode parent) {
    313     ASTNode statement = parent.getParent();
    314     boolean isStatement = statement.getNodeType() != 21;
    315     ASTNode block = statement.getParent();
    316     boolean isBlock = (block.getNodeType() == 8) || (block.getNodeType() == 50);
    317     boolean isControlStatemenBody = 
    318       ASTNodes.isControlStatementBody(statement.getLocationInParent());
    319     return (isStatement) || ((!isBlock) && (!isControlStatemenBody));
    320   }
    321 
    322   private static Expression createInfixInvocationFromPostPrefixExpression(InfixExpression.Operator operator, Expression getterExpression, AST ast, ITypeBinding variableType, boolean is50OrHigher)
    323   {
    324     InfixExpression infix = ast.newInfixExpression();
    325     infix.setLeftOperand(getterExpression);
    326     infix.setOperator(operator);
    327     NumberLiteral number = ast.newNumberLiteral();
    328     number.setToken("1");
    329     infix.setRightOperand(number);
    330     ITypeBinding infixType = infix.resolveTypeBinding();
    331     return createNarrowCastIfNessecary(infix, infixType, ast, variableType, 
    332       is50OrHigher);
    333   }
    334 
    335   private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher)
    336   {
    337     PrimitiveType castTo = null;
    338     if (variableType.isEqualTo(expressionType))
    339       return expression;
    340     if (is50OrHigher) {
    341       if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(
    342         variableType))
    343         castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
    344       if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(
    345         variableType))
    346         castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
    347       if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(
    348         variableType))
    349         castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
    350     }
    351     if (ast.resolveWellKnownType("char").isEqualTo(variableType))
    352       castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
    353     if (ast.resolveWellKnownType("byte").isEqualTo(variableType))
    354       castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
    355     if (ast.resolveWellKnownType("short").isEqualTo(variableType))
    356       castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
    357     if (castTo != null) {
    358       CastExpression cast = ast.newCastExpression();
    359       if (ASTNodes.needsParentheses(expression)) {
    360         ParenthesizedExpression parenthesized = ast
    361           .newParenthesizedExpression();
    362         parenthesized.setExpression(expression);
    363         cast.setExpression(parenthesized);
    364       } else {
    365         cast.setExpression(expression);
    366       }
    367       cast.setType(castTo);
    368       return cast;
    369     }
    370     return expression;
    371   }
    372 }
  • 相关阅读:
    DB2保存图片并读取动态显示图片
    DIV布局之position详解
    dojo之配置dojoconfig
    JAVA虚拟机内存架构
    UrlConnection的代理和返回状态码的问题
    自定义网站的icon和收藏夹图标
    DB2建库简单例子
    python学习笔记之函数(方法)
    python学习笔记之基础数据和控制
    MVC3学习:实现文章上一篇下一篇链接
  • 原文地址:https://www.cnblogs.com/hejianjun/p/7027138.html
Copyright © 2020-2023  润新知