alepuzio
alepuzio

Reputation: 1418

How to code an annotation inside an annotation in a class?

I'm trying to code, for fun, a class that use an annotion with another annotation inside, but I don't understand how to code.

My code

First annotation

@interface FirstAnnotation {
    String author();
}

Second annotation, contains the first annotation

public @interface SecondAnnotation {
    FirstAnnotation inside();
    int version();
}

Class with annotation

@FirstAnnotation(
    author = "alessandro"
)
@SecondAnnotation(
    version = 1,
    inside = /*Compilation code: what code? FirstAnnotation, this??*/   
)
public class GeneralClass {

    /*
     * a generic method
     */ 
    public void method() {
        System.out.println("method");
    }

}

What I have to put in row with string Compilation code, as reference to actual value of FirstAnnotation in the class?

Upvotes: 0

Views: 4205

Answers (1)

Slava Vedenin
Slava Vedenin

Reputation: 60094

You class should be:

@SecondAnnotation(
    version = 1,
    inside = @FirstAnnotation(
          author = "alessandro"
    )   
)
public class GeneralClass {

    /*
     * a generic method
     */ 
    public void method() {
        System.out.println("method");
    }

}

Upvotes: 7

Related Questions