简体   繁体   中英

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?

You class should be:

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM