views:

32

answers:

2

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?

A: 

I think the annotated interface should bounded as follows:

public @interface ServiceType {
        Class<? extends MyService<? extends Number>> value();
}
Bivas
+2  A: 

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.

ILMTitan