1

I'm using custom type use annotation. I can't read them from an object like any other regular annotation:

public class TestingAnnotations {



public static void main(final String[] args) {
     final @CustomAnnotation TypeAnnotated b = new @CustomAnnotation TypeAnnotated();
     System.out.println(b.getClass().getAnnotation(CustomAnnotation.class)); //<-- prints null :(
   }
}

@Target({ElementType.TYPE_USE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {

}

class TypeAnnotated {

}

So, how can I check b instance is annotated?

Thanks

2 Answers2

0

you are actually not annotating the class.... a class is annotated when it looks like:

@CustomAnnotation
class TypeAnnotated {

}

after that you will get the annotation doing:

TypeAnnotated b = new TypeAnnotated();
System.out.println(b.getClass().getAnnotation(CustomAnnotation.class));
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

It looks like you actually want a local variabel annotation

@Target({ElementType.TYPE_USE, ElementType.LOCAL_VARIABLE})
public @interface CustomAnnotation {
}

Then this compiles just fine:

enter image description here