Check if a generic T implements an interface

后端 未结 3 782
南旧
南旧 2021-02-05 07:28

so I have this class in Java:

public class Foo{
}

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

The

3条回答
  •  失恋的感觉
    2021-02-05 08:10

    you can check it using isAssignableFrom

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

    or to get the array of interface as

    Class[] intfs = clazz.getInterfaces();
    

提交回复
热议问题