【发布时间】:2021-03-18 20:31:52
【问题描述】:
我想用 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;
这可能吗?
【问题讨论】:
标签: java annotations javaparser