Java custom annotations takes another annotation

Reddi :

how can I write a custom annotation that takes another annotation and the values?

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation{
  Class<? extends TestAnnotationChild> annotation();
}

The second annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotationChild{

}

And I would like to do something like

@TestAnnotation(@TestAnnotationChild={values})

How can I do something like that?

LppEdd :

This is how it is done.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
   TestAnnotationChild child();

   // Or for an array
   TestAnnotationChild[] children();
}

Usage

@TestAnnotation(
        @TestAnnotationChild(
               value = "42",
               anotherValue = 42
        )
)

However this part of your statement

and the values

does make me think you want to do something non-ordinary.
Could you clarify?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=99213&siteId=1