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 ASTRewriter
in 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.
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;
}
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: