I have following class:
public class Publisher<T> { private static final Class[] SUPPORTED_CLASSES = new Class[]{T1.class, T2.class}; public Publisher() { if(Arrays.asList(SUPPORTED_CLASSES).contains(T)) { // error: expression expected! System.out.println("Class not supported!"); } } } How can I check if class parameter conforms to the implementation?
In the above example I cannot use class parameter T as a parameter.
Sthat is only defined inT1andT2, use an interfaceIPublisher, add it to both class and thenPublish<T extends IPublisher>... you can remove that list and this condition. You know thatTwill be an instance ofIPublisher.