Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:52:16+00:00 2026-06-12T22:52:16+00:00

I have an IFile with the following content: package com.example; //@SimpleAnnotation(Blab.ckass) //@CustomAnnotation(arg1 = Blup.class

  • 0

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:52:17+00:00Added an answer on June 12, 2026 at 10:52 pm

    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:

    Eclipse Screenshot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following httphandler: public class NewHandler : IHttpHandler { [Inject] public IFile
I have a class in c++ like the following: class myCls { public: myCls();
I have a CSV file in the following format: id,code,date,address,complete_url Example data from the
I have the following code inside a class. (It's coffeescript-- and it's for a
I have the following lines in a file. Here is an example of one
have written this little class, which generates a UUID every time an object of
i have several files whose content need to be merged into a single file.
I have the following SQL statement which returns a single record as expected: select
I have the following query...how would I go about troubleshooting why it is not
I have the following code: //Comp454 program 2 #include <iostream> #include <string> #include <fstream>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.