Link to parameterized type from annotation

I am trying to figure out how to refer to a parameterized interface as an annotation attribute:

public class Example {
    public interface MyService<T extends Number> {
        T someNumber();
    }

    public class BaseServiceImpl<T extends Number> implements MyService<T> {
        @Override
        public T someNumber() {
            return null;
        }
    }

    public @interface ServiceType {
        Class<? extends MyService<?>> value();
    }

    @ServiceType(BaseServiceImpl.class)
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

The above code crashes with type mismatch in the annotation @ServiceType. I believe the problem is related to the restrictions that I have specified for the attribute value. I also noticed that when a type is not generic, it works fine; for example the @ServiceType(IntegerService.class)given works:

    public class IntegerService extends BaseServiceImpl<Integer> { /* ... */ }

What am I missing to get rid of a mismatch error when trying to provide a generic type?

+3
source share
2 answers

, . BaseServiceImpl.class Class<BaseServiceImpl>, Class<BaseServiceImpl<? extends Number>>. Class<? extends MyService> .

+3

, :

public @interface ServiceType {
        Class<? extends MyService<? extends Number>> value();
}
0

Source: https://habr.com/ru/post/1770411/


All Articles