【发布时间】: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