3

我有一个包含以下内容的 IFile:

package com.example;
//@SimpleAnnotation(Blab.ckass)
//@CustomAnnotation(arg1 = Blup.class , arg2 = Blup.Row.class)
public class SimpleClassWithAnnotation {

}

如何在课堂上添加注释(注释的)?我尝试使用线程中的解决方案,但来源没有改变。这是我使用的代码片段:

    final ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(this.cu);
    ICompilationUnit cu = JavaCore.createCompilationUnitFrom(this.ifile);
    final CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
    final AST ast = astRoot.getAST();
    astRoot.recordModifications();
    final NormalAnnotation newNormalAnnotation = astRoot.getAST().newNormalAnnotation();
    newNormalAnnotation.setTypeName(astRoot.getAST().newName("AnnotationTest"));

这是尝试添加一个简单的注释@AnnotationTest。但是我需要这样的东西:@CustomAnnotation(arg1 = Blup.class , arg2 = Blup.Row.class)

提前致谢 !

4

3 回答 3

6

经过多次尝试,我得到了解决方案。问题就像 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 结构的外观。这是示例:

Eclipse 截图

于 2012-10-11T09:02:12.710 回答
1

所做的修改仅适用于 AST,不会记录到 Java 源代码文件中。

检查下面的链接。这提供了有关如何将 AST 更改写入文件的见解。

http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html

更新:

您可以添加注释值,如下所示:

MemberValuePair mV = astRoot.getAST().newMemberValuePair();
mV.setName(astRoot.getAST().newSimpleName("name"));
TypeLiteral typeLiteral = astRoot.getAST().newTypeLiteral();
typeLiteral.setType(astRoot.getAST().newSimpleType(astRoot.getAST().newSimpleName("Blup")));
mV.setValue(typeLiteral);
newNormalAnnotation.values().add(mV);

然后,您可以将 NormalAnnotation 添加到 Class 的修饰符列表中。

于 2012-10-09T06:24:06.787 回答
1

With your solution using:

final ListRewrite listRewrite = rewriter.getListRewrite(astRoot, CompilationUnit.TYPES_PROPERTY);

I get one line space between annotation and class. So I tried to add annotation as Modifier and it seems working well. Below is an example how to do it:

final MarkerAnnotation javaTaskInfoA = ast.newMarkerAnnotation();
javaTaskInfoA.setTypeName(ast.newSimpleName(JavaTaskInfo.class.getSimpleName()));
acu.accept(new ASTVisitor() { public boolean visit(TypeDeclaration node) {
    rewriter.getListRewrite(node, node.getModifiersProperty()).insertAt(javaTaskInfoA, 0, null);
    return false;
}});

Marek

于 2014-04-11T00:46:50.203 回答