I have an IFile with the following content:
package com.example;
//@SimpleAnnotation(Blab.ckass)
//@CustomAnnotation(arg1 = Blup.class , arg2 = Blup.Row.class)
public class SimpleClassWithAnnotation {
}
How can I add an Annotation(the commented one) to the class? I’ve tried to use the solution from this thread, but the source doesn’t change. Here is the code snippet, that I used:
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"));
That’s a try to add a simple annotation @AnnotationTest. However I need something like this: @CustomAnnotation(arg1 = Blup.class , arg2 = Blup.Row.class)
Thanks in advance !
After alot of tries I got to the solution. The problem was like Unni Kris mentioned is that I have to use
ASTRewriterin order to manipulate the AST Tree. The other problem was how to save the changes back to the file. I’Ve found a solution, which I’m not sure that is 100% correct, but at least it work for me.I’ve also found a plugin that shows the AST Structure (ASTView) of the java files in eclipse. That helped me to figure it out how to create the annotation structure. Just create an java file with the needed structure and see how the AST Structure looks like. Here is the example: