Check if a generic T implements an interface

36,517

Solution 1

Generics, oddly enough, use extends for interfaces as well.1 You'll want to use:

public class Foo<T extends SomeInterface>{
    //use T as you wish
}

This is actually a requirement for the implementation, not a true/false check.

For a true/false check, use unbounded generics(class Foo<T>{) and make sure you obtain a Class<T> so you have a refiable type:

if(SomeInterface.class.isAssignableFrom(tClazz));

where tClazz is a parameter of type java.lang.Class<T>.

If you get a parameter of refiable type, then it's nothing more than:

if(tParam instanceof SomeInterface){

but this won't work with just the generic declaration.

1If you want to require extending a class and multiple interfaces, you can do as follows: <T extends FooClass & BarInterface & Baz> The class(only one, as there is no multiple inheritance in Java) must go first, and any interfaces after that in any order.

Solution 2

you can check it using isAssignableFrom

if (YourInterface.class.isAssignableFrom(clazz)) {
    ...
}

or to get the array of interface as

Class[] intfs = clazz.getInterfaces();
Share:
36,517

Related videos on Youtube

Budius
Author by

Budius

Updated on September 14, 2020

Comments

  • Budius
    Budius over 3 years

    so I have this class in Java:

    public class Foo<T>{
    }
    

    and inside this class I want to know if T implements certain interface.

    The following code DOES NOT work but it's the idea of what I want to accomplish:

    if(T.class implements SomeInterface){
        // do stuff
    }
    

    so I want to check if the class T that was passed to Foo have implements SomeInterface on its signature.

    Is it possible? How?

  • nanofarad
    nanofarad over 10 years
    The OP needs a refiable type to get clazz, perhaps as accepting Class<T> as a parameter or at construction.
  • nanofarad
    nanofarad over 10 years
    Doesn't work, type erasure. And T Class object isn't valid syntax. Do you mean an instance of t?
  • Budius
    Budius over 10 years
    hi. I really need the true/false check because I'm modifying an existing class. I'm trying your second code but still not sure where tClass come from. Any help on that ?
  • nanofarad
    nanofarad over 10 years
    @Budius Your user needs to at some time, pass in either Class<T> from someInstanceOfT.getClass() or to pass in an instance of T itself. Type erasure won't let you get it otherwise.
  • Budius
    Budius over 10 years
    so I can't use the actual generic T to test for interface, I'll only be able to use it during public void add(T object){...} and test it on the object.
  • Budius
    Budius over 10 years
    Sadly indeed, not sure it will match my requirements as I need to generate a filename on constructor. Any other way you can think to get the serialVersionUID of a possible Serializable ?
  • Naveen Kumar Alone
    Naveen Kumar Alone over 10 years
    @hexafraction modified my answer
  • Budius
    Budius over 10 years
    ok, I think I'm saved at the end. The guy who originally created this class created a public abstract Class<T> myclass(); so I could use instanceof. Thanks again.