I am writing a method which is passed a Class
as an argument, like this:
public void doStuff(Class<?> clazz) {
/* do stuff */
}
I can then call the method like this:
doStuff(FooBar.class);
How do I enforce at the compiler level that the argument can only be a class which implements a particular interface?
I’ve tried the following—
public <T extends FooInterface> void doStuff(Class<T> clazz) {
/* do stuff */
}
—but get a compiler error for the following statement:
doStuff(FooBar.class); // FooBar implements FooInterface
How do I do this correctly?