Check if a generic T implements an interface

后端 未结 3 784
南旧
南旧 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:01

    Use isAssignableFrom()

    isAssignableFrom() determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

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

提交回复
热议问题