1

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.

Frontear
  • 1,150
  • 12
  • 25

1 Answers1

0

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.

This allows gson to find the values

Frontear
  • 1,150
  • 12
  • 25