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.
You are trying to access a generic type at runtime, which does not work in this case, because of type erasure.
The simplest way to fix this is to take a Class<T>
in the constructor, which will give you the type at run time, you can then check if the List contains the value you have been given.
public Publisher(Class<T> clazz) {
if(!SUPPORTED_CLASSES.contains(clazz)) {
System.out.println("Class not supported!");
}
}
Your code does not currently support subtypes, which may cause issues, unless you are ok with this (you may work on Lists, but not necessarily ArrayLists), this does beak the LSP though.
Though some other answers are quite good, I would like to propose another workaround:
You could create an empty interface MyInterface
, and have all the classes in your list implement this interface. Then, you can change your class declaration to:
public class Publisher<T extends S, MyInterface>
which will achieve your purpose.
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