我想用javaparser给一个字段添加一个注释。
public static String annotate() {
String classString = new StringJoiner("\n")
.add("class A {")
.add("")
.add(" @ExistingAnnotation")
.add(" private int i;")
.add("")
.add("}")
.toString();
CompilationUnit cu = StaticJavaParser.parse(classString);
LexicalPreservingPrinter.setup(cu);
cu.findFirst(FieldDeclaration.class).get()
.addAnnotation("Annotation")
.addAnnotation(Nonnull.class)
.addMarkerAnnotation("MarkerAnnotation");
return LexicalPreservingPrinter.print(cu);
}
但与我预期的相反,添加的注释不会放在自己的行上,而是附加到同一行上的现有注释上
import javax.annotation.Nonnull;
class A {
@ExistingAnnotation @Annotation() @Nonnull() @MarkerAnnotation
private int i;
}
相反,我想
@ExistingAnnotation
@Annotation()
@Nonnull()
@MarkerAnnotation
private int i;
这有可能吗?
发布于 2021-04-07 19:41:48
尝试使用以下命令打印:
new DefaultPrettyPrinter().print(cu);
https://stackoverflow.com/questions/66698723
复制相似问题