'Java Type Use annotation - How to read them

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ghoti and Chips