首页
论坛
课程
招聘
[原创]getset自动生成注释
2022-7-29 17:53 3796

[原创]getset自动生成注释

2022-7-29 17:53
3796

更多好资源:https://bbs.pediy.com/user-854079.htm
Eclipse下自动添加注释如图
网络上有许多,但经测试不太好用,在此分享一下
get set中的注释自动生成

需要修改一个jar
路径如下:
C:\eclipse\plugins\org.eclipse.jdt.core.manipulation_1.14.0.v20200526-0740.jar
文件名:
package org.eclipse.jdt.internal.corext.codemanipulation.GetterSetterUtil;
如有觉得麻烦可以使用下方class进行替换,jar用压缩工具打开,用此class文件替换原有的即可,如想自己编译,可把有依赖包加到编译器中,jar所有路径如上,不是所有jar都需要添加
其中的两个方法需要被修改:
getSetterStub方法
getGetterStub方法

 

两方法修改比较类似

 

getSetterStub()改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//模板
        comment = CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, "\n"); 
        if (addComments && comment!=null) {
            ISourceRange sr = field.getJavadocRange();
            if(sr!=null) {
                //属性中的注释
                String annotation = field.getSource().substring(0, sr.getLength());
                annotation = annotation.replaceAll("/", "").replace("*", "").trim();
                //用注释替换模板中的值
                if(comment.contains("_bare_field_comment")) {
                    comment = comment.replace("_bare_field_comment", annotation);//替换值
                }else if(annotation.length()>0){
                    comment = annotation;
                }
            }else { //注释为  “//
                String annotation = getLineComment(field);
                if(annotation!=null && annotation.length()>0) {
                    annotation = annotation.trim();
                    if(comment.contains("_bare_field_comment")) {
                        comment = comment.replace("_bare_field_comment", annotation);//替换值
                    }else if(annotation.length()>0){
                        comment = annotation;//替换值
                    }
                }else {
                    comment = comment.replace("_bare_field_comment",field.getElementName());
                }
            }
            buf.append(comment);
            buf.append("\n");
        }

getGetterStub()改为:
//默认注释中的模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
comment = CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, "\n"); 
if (addComments && comment!=null) {
    //属性中的注释
    ISourceRange sr = field.getJavadocRange();
    if(sr!=null) {
        //属性中的注释
        String annotation = field.getSource().substring(0, sr.getLength());
        annotation = annotation.replaceAll("/", "").replace("*", "").trim();
        //用注释替换模板中的值
        if(comment.contains("_bare_field_comment")) {
            comment = comment.replace("_bare_field_comment", annotation);//替换值
        }else if(annotation.length()>0){
            comment = annotation;
        }
    }else {//注释为  “//
        String annotation = getLineComment(field);
        if(annotation!=null && annotation.length()>0) {
            annotation = annotation.trim();
            if(comment.contains("_bare_field_comment")) {
                comment = comment.replace("_bare_field_comment", annotation);//替换值
            }else if(annotation.length()>0){
                comment = annotation;//替换值
            }
        }else {//没有注释
            comment = comment.replace("_bare_field_comment",field.getElementName());
        }
    }
    buf.append(comment);
    buf.append("\n");
}

添加一个辅助方法;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public static String getLineComment(IField field) throws JavaModelException {
          ISourceRange range = field.getSourceRange();
          if (range == null) {
              return null;
          }
          IBuffer buf;
        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 (Exception ex) {
              }
          }
          return null;
      }

到此已完成一大半
配置Eclipse中的模式

如果生成get set时没有出现注释参考下图
图片描述
如果觉得麻烦可以从下方下载class文件
觉得不安全自己逆向下,看源码吧


[2023春季班]《安卓高级研修班(网课)》月薪两万班招生中~

上传的附件:
收藏
点赞0
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回