我想做什么
我正试图在用@lombok.Generated
注释的方法周围添加单行注释,以告诉Parasoft Jtest抑制报告方法中的发现,如下所示:
// parasoft-begin-suppress ALL
@lombok.Generated
void generatedMethod() {
}
// parasoft-end-suppress ALL
What I tried
为了添加这些注释,我编写了一个Java程序,使用JavaParser向Java源代码添加注释。我已经成功地在基于JavaParser样例项目的注释之前添加了以下代码:
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?
据我所知,注释是与AST节点相关联的,所以你不能做你想做的。
不是在方法后面添加单行注释,而是在方法的最后一行添加注释,代码如下:
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
}