Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a parameterized type from an annotation

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?

like image 640
Jeremy Gustie Avatar asked Aug 20 '26 05:08

Jeremy Gustie


1 Answers

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.

like image 199
ILMTitan Avatar answered Aug 22 '26 19:08

ILMTitan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!