Let's say I've got an annotation interface of foo
@interface foo {
String bar();
}
I want to be able to pass foo
to a generic T
in a class, let's call it someClass
class someClass<T> {
T ourAnnotation = getClass().getAnnotation(T); // this won't work
}
The reason I'd pass this is that later on, when creating a base class, we can pass this generic
@foo(bar = ...)
public class MyClass extends someClass<foo> {
public MyClass() {
ourAnnotation.bar // ... do stuff
}
}
How would I be able to pass this?
Edit: I've figured out a solution
Within someClass
, I've made the following modifications:
class someClass<T> {
final T info;
public someClass(Class<T> tClass) {
info = getClass().getAnnotation(tClass);
}
}
Then, by passing foo
(make sure it's retention policy is set to runtime), you can easily grab the values from MyClass
.