Java - Use Class parameter in method parameter

FazoM :

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.

jrtapsell :

Why this doesn't work

You are trying to access a generic type at runtime, which does not work in this case, because of type erasure.

How to fix

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.

Example code

public Publisher(Class<T> clazz) {
    if(!SUPPORTED_CLASSES.contains(clazz)) {
        System.out.println("Class not supported!");
    }
}

Possible issues

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=443702&siteId=1