【问题标题】:How to add a line comment just after a method with JavaParser如何在 JavaParser 的方法之后添加行注释
【发布时间】:2022-12-18 19:33:32
【问题描述】:

我想做的事

我正在尝试在用 @lombok.Generated 注释的方法周围添加单行 cmets,以告诉 Parasoft Jtest 禁止报告方法中的结果,如下所示:

// parasoft-begin-suppress ALL
@lombok.Generated
void generatedMethod() {
}
// parasoft-end-suppress ALL

我试过的

为了添加这些 cmets,我编写了一个 Java 程序,使用 JavaParser 将 cmets 添加到 Java 源。 我已经使用基于 JavaParser sample project 的代码在注释之前成功添加了注释:

public static void main(String[] args) throws IOException {
    Log.setAdapter(new Log.StandardOutStandardErrorAdapter());

    Path inPath = Paths.get("/path/to/input/source");
    SourceRoot sourceRoot = new SourceRoot(inPath);
    List<ParseResult<CompilationUnit>> p = sourceRoot.tryToParseParallelized();

    Iterator<ParseResult<CompilationUnit>> it = p.iterator();
    while (it.hasNext()) {
        ParseResult<CompilationUnit> pr = it.next();
        pr.getResult().ifPresent(cu -> {
            cu.accept(new ModifierVisitor<Void>() {

                @Override
                public Visitable visit(MethodDeclaration n, Void arg) {
                    List<MarkerAnnotationExpr> list = n.findAll(MarkerAnnotationExpr.class);
                    Iterator<MarkerAnnotationExpr> it = list.iterator();
                    while (it.hasNext()) {
                        MarkerAnnotationExpr ann = it.next();
                        if (ann.getNameAsString().equals("lombok.Generated")) {
                            ann.setLineComment("// parasoft-begin-suppress ALL");
                            List<Node> childNodeList = n.getChildNodes();
                            // childNodeList.add(new LineComment("// parasoft-end-suppress ALL"));
                        }
                    }
                    return super.visit(n, arg);
                }
            }, null);
        });
    }

    Path outPath = Paths.get("/path/to/output/source");
    sourceRoot.saveAll(outPath);
}

问题

我无法在childNodeList.add(new LineComment("// parasoft-end-suppress ALL")); 的方法后添加评论。 Node#getChildNodes 的 Javadoc 说 You can add and remove nodes from this list by adding or removing nodes from the fields of this node.,但是当我调用 childNodeList.add() 时我得到了 UnsupportedOperationException

问题

如何在使用 JavaParser 的方法之后添加行注释?

【问题讨论】:

    标签: java javaparser


    【解决方案1】:

    据我所知,cmets 与 AST 节点相关联,所以你不能做你想做的事。

    【讨论】:

      【解决方案2】:

      我没有在方法之后添加单行注释,而是使用以下代码在方法的最后一行添加注释:

      public static void main(String[] args) throws IOException {
          Log.setAdapter(new Log.StandardOutStandardErrorAdapter());
      
          Path inPath = Paths.get("/path/to/input/source");
          SourceRoot sourceRoot = new SourceRoot(inPath);
          List<ParseResult<CompilationUnit>> p = sourceRoot.tryToParseParallelized();
      
          Iterator<ParseResult<CompilationUnit>> it = p.iterator();
          while (it.hasNext()) {
              ParseResult<CompilationUnit> pr = it.next();
              pr.getResult().ifPresent(cu -> {
                  cu.accept(new ModifierVisitor<Void>() {
                      @Override
                      public Visitable visit(MethodDeclaration n, Void arg) {
                          List<MarkerAnnotationExpr> list = n.findAll(MarkerAnnotationExpr.class);
                          Iterator<MarkerAnnotationExpr> it = list.iterator();
                          while (it.hasNext()) {
                              MarkerAnnotationExpr ann = it.next();
                              if (ann.getNameAsString().equals("lombok.Generated") || ann.getNameAsString().equals("Generated")) {
                                  ann.setLineComment("parasoft-begin-suppress ALL");
                                  List<Node> childNodeList = n.getChildNodes();
                                  // The last child node will be "{ ~ }"
                                  Node lastChildNode = childNodeList.get(childNodeList.size() - 1);
                                  // This line comment will be inserted at the last line of the method
                                  lastChildNode.addOrphanComment(new LineComment("parasoft-end-suppress ALL"));
                              }
                          }
                          return super.visit(n, arg);
                      }
                  }, null);
              });
          }
      
          Path outPath = Paths.get("/path/to/output/source");
          sourceRoot.saveAll(outPath);
      }
      

      使用此代码和以下源代码,

      @lombok.Generated
      void generatedMethod() {
          System.out.println("DUMMY");
      }
      

      我得到以下结果(Parasoft Jtest 将忽略此方法中的所有语句):

      // parasoft-begin-suppress ALL
      @lombok.Generated
      void generatedMethod() {
          System.out.println("DUMMY");
          // parasoft-end-suppress ALL
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-07
        • 1970-01-01
        • 2020-07-28
        • 2014-09-18
        • 2011-05-28
        相关资源
        最近更新 更多