Reputation: 23
I searched longer than 30 mins for this simple question and could not find anything helpful.
I want to write a method which accepts a Class as parameter. Additionally I have to guarantee that the Class is not any kind of class but an Interface (e.g. Action.class, Collection.class,...).
So it has to look similar to the lines below. But what do I need to specify where it put in HELP?
public void registerInterface(Class<HELP> anInterfaceClass) {
// do stuff with anInterfaceClass
}
I can't put in Object. Maybe Class :-) So easy but I can't find the solution....
Upvotes: 2
Views: 90
Reputation: 12924
You can check using Class#isInterface() method in Class. It checks if the specified Class object represents an interface type.
Collection.class.isInterface()
Upvotes: 0
Reputation: 523224
I don't think there is compile-time check if a generic argument is an interface. You could put a runtime-check though.
public void registerInterface(Class<?> anInterfaceClass) {
if (!anInterfaceClass.isInterface()) {
throw new IllegalArgumentException("An interface is expected!");
}
Upvotes: 6
Reputation: 14025
There is no type that is a superclass of all interfaces but not of non-interface classes. So there's nothing you can put where you've marked HELP
that will work.
Upvotes: 1