I am trying to figure out how to reference 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 fails with a type mismatch on the @ServiceType annotation. I believe the problem is with the bounds I have specified for the value attribute. I also noticed that when a type is not generic it works fine; e.g. @ServiceType(IntegerService.class) works given:
public class IntegerService extends BaseServiceImpl<Integer> { /* ... */ }
What am I missing to get the rid of the mismatch error when trying to supply the generic type?
The problem is that class literals refer to raw class types. That is BaseServiceImpl.class is of type Class<BaseServiceImpl>, not Class<BaseServiceImpl<? extends Number>>. Change the return type of value to Class<? extends MyService> and it should compile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With