0

My question is related to Java: Annotated Annotations (and passing values), but not entirely the same, so I thought I'd ask anyway. Especially since there were so few answers to that question.

Say I have written an annotation like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface NestedAnnotation {
    public String value();
    public Class<?> impl() default Void.class;
}

So if I want to use this, I have to do something like @NestedAnnotation("somevalue"). Now, what if I want to put that annotation inside another one:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@NestedAnnotation("need value here!")
public @interface OuterAnnotation {
    public String value();
    public Class<?> impl() default Void.class;
}

The NestedAnnotation needs a value, and adding a String (like above) works. But what if I wanted to pass on a value that was received by the OuterAnnotation? Is that possible?

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
Hieron
  • 439
  • 3
  • 10
  • 1
    if your goal is annotation inheritance then → [look this here](http://stackoverflow.com/questions/1624084/why-is-not-possible-to-extend-annotations-in-java) – ΦXocę 웃 Пepeúpa ツ May 03 '17 at 06:56
  • 1
    What you are asking is for dynamic values for annotation attributes. However annotations can only be set at compile time which is the reason why their values can only be compile time constants. You may only read them at runtime. – Jay Smith May 03 '17 at 06:59
  • Huh, ok, that makes sense. Good thing I found a different way of solving my problem. :) – Hieron May 03 '17 at 09:45
  • @Hieron Can you suggest me what is the solution for this problem I am also suffering through the same situation. – Pratik Bhajankar May 15 '20 at 05:30
  • I think I used a workaround that did not involve annotations, Rahul. So no real 'solution' I'm afraid – Hieron May 15 '20 at 07:46

0 Answers0