Annotations

87 views
Skip to first unread message

oracleruiz

unread,
Jul 11, 2012, 8:13:29 PM7/11/12
to javap...@googlegroups.com
Hello devs,
I'm new with the library and I'm wondering how to create Annotations, add them to a variable or method and then print the code.
There is no documentation about how to create them and the way to do it seems to be no simple. I tried with some methods that I found doing some code exploring...

Anyone of you know how to do it?

I have this:

private static class MethodChangerVisitor extends VoidVisitorAdapter {

        @Override
        public void visit(MethodDeclaration n, Object arg) {

                ArrayList<AnnotationExpr> anots = new ArrayList<AnnotationExpr>();

                AnnotationExpr anotation = new AnnotationExpr() {
                   
                    @Override
                    public <A> void accept(VoidVisitor<A> arg0, A arg1) {
                        // TODO Auto-generated method stub
                       
                    }
                   
                    @Override
                    public <R, A> R accept(GenericVisitor<R, A> arg0, A arg1) {
                        // TODO Auto-generated method stub
                        return null;
                    }
                };
               
                anots.add(anotation );
               
                n.setAnnotations(anots);

Where do I set the values of the Annotation properties??

Blake Miller

unread,
Sep 17, 2012, 10:07:03 PM9/17/12
to javap...@googlegroups.com
// this should give you @jibber
// an annotation with no parameters
new MarkerAnnotationExpr(new NameExpr("jibber"));

NormalAnnotationExpr accepts a list of MemberValuePair each of which consist of a nameexpr and a valueexpr, that's for "normal" annotations with member/values

SingleMemberAnnotationExpr is for the special case of a "value" value in which the token "value" and the "=" can be omitted

HTH
Reply all
Reply to author
Forward
0 new messages