2

I know it's possible to get a class by name, using

public String className = "someName"; public Class<?> c = Class.forName(className); 

Is it possible to retrieve an annotation by name? I tried this:

public String annotationName = "someName"; public Class<?> c = Class.forName(annotationName); 

And then casting c to Class<? extends Annotation>, which works, but I get an Unchecked cast warning.

I also tried

public String annotationName = "someName"; public Class<? extends Annotation> c = Class.forName(annotationName); 

But when I do that I get the error

Incompatible types. Required: Class<? extends java.lang.annotation.Annotation> Found: Class<Capture<?>> 
6
  • Possible duplicate of How to get annotation class name, attribute values using reflection Commented May 12, 2019 at 9:01
  • @HadiJ If I understand correctly, that question deals with trying to retrieve all annotation names. My question is if I already have a specific annotation name, how to get to the annotation Commented May 12, 2019 at 9:05
  • 1
    The first way you showed is the way to do it. You can't do anything to the unchecked cast warning except to suppress it. That's just a limitation in Java's generics. Commented May 12, 2019 at 9:07
  • And then casting c to Class<? extends Annotation>, which works - can't be possible, bounded types only work if they involve an interface or class Commented May 12, 2019 at 10:01
  • @Eugene Annotation is an interface. Commented May 13, 2019 at 7:55

1 Answer 1

3

Use asSubclass. Unlike compiler generated type casts, which can only work with types known at compile-time, this will perform a check against the Class object retrieved at runtime. Since this is safe, it won’t generated an “Unchecked cast” warning. Note the existence of a similar operation, cast for casting an instance of a Class. These methods were added in Java 5, specifically to aid code using Generics.

String annotationName = "java.lang.Deprecated"; Class<? extends Annotation> c = Class.forName(annotationName).asSubclass(Annotation.class); boolean depr = Thread.class.getMethod("stop").isAnnotationPresent(c); System.out.println(depr); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.