How to determine whether a Java class implements a specific interface

I'm trying to use reflection to determine whether the incoming class implements the iswdidget interface:

public boolean isAWidget(Class<?> clzz) {
    Class<?> runtimeClass = ClassLoader.getSystemClassLoader().loadClass(clzz.getName());
    Class<?>[] impls = runtimeClass.getInterfaces();
    for(Class<?> clz : impls)
        if(clz.getName().equals(IsWidget.class.getName()))
            return true;

    return false;
}

Is this the best / most effective way to determine? I also saw an iswidget class. Isassignablefrom (class ) method

Solution

I will use the isassignablefrom method to determine whether the iswidget is a super interface:

return IsWidget.class.isAssignableFrom(clzz);

Reference the Javadoc linked above:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>