经过多次尝试,我得到了解决方案。问题就像 Unni Kris 提到的那样,我必须使用ASTRewriter
它来操纵 AST 树。另一个问题是如何将更改保存回文件。我找到了一个解决方案,我不确定它是否 100% 正确,但至少它对我有用。
private void addAnnotations(final ICompilationUnit cu) throws MalformedTreeException, BadLocationException, CoreException {
// parse compilation unit
final ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(cu);
final CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
// create a ASTRewrite
final AST ast = astRoot.getAST();
final ASTRewrite rewriter = ASTRewrite.create(ast);
final ListRewrite listRewrite = rewriter.getListRewrite(astRoot, CompilationUnit.TYPES_PROPERTY);
final NormalAnnotation eventHandlerAnnotation = astRoot.getAST().newNormalAnnotation();
eventHandlerAnnotation.setTypeName(astRoot.getAST().newName("CustomAnnotation"));
eventHandlerAnnotation.values().add(createAnnotationMember(ast, "arg1", "Blup"));
eventHandlerAnnotation.values().add(createQualifiedAnnotationMember(ast, "arg2", "IsWorkbenchTest", "Blab"));
final SingleMemberAnnotation runWithFop = astRoot.getAST().newSingleMemberAnnotation();
runWithFop.setTypeName(astRoot.getAST().newName("SimpleAnnotation"));
final TypeLiteral newTypeLiteral = astRoot.getAST().newTypeLiteral();
newTypeLiteral.setType(astRoot.getAST().newSimpleType(astRoot.getAST().newSimpleName("Blop")));
runWithFop.setValue(newTypeLiteral);
listRewrite.insertAt(runWithFop, 0, null);
listRewrite.insertAt(eventHandlerAnnotation, 0, null);
final TextEdit edits = rewriter.rewriteAST();
// apply the text edits to the compilation unit
final Document document = new Document(cu.getSource());
edits.apply(document);
// this is the code for adding statements
cu.getBuffer().setContents(formatFileContent(document.get()));
cu.save(null, true);
}
protected MemberValuePair createQualifiedAnnotationMember(final AST ast, final String name, final String value, final String value2) {
final MemberValuePair mV = ast.newMemberValuePair();
mV.setName(ast.newSimpleName(name));
final TypeLiteral typeLiteral = ast.newTypeLiteral();
final QualifiedType newQualifiedName = ast.newQualifiedType(ast.newSimpleType(ast.newSimpleName(value)), ast.newSimpleName(value2));
typeLiteral.setType(newQualifiedName);
mV.setValue(typeLiteral);
return mV;
}
protected MemberValuePair createAnnotationMember(final AST ast, final String name, final String value) {
final MemberValuePair mV = ast.newMemberValuePair();
mV.setName(ast.newSimpleName(name));
final TypeLiteral typeLiteral = ast.newTypeLiteral();
typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(value)));
mV.setValue(typeLiteral);
return mV;
}
我还发现了一个插件,它显示了 Eclipse 中 java 文件的 AST 结构(ASTView)。这帮助我弄清楚了如何创建注释结构。只需创建一个具有所需结构的 java 文件,然后查看 AST 结构的外观。这是示例: