/** * Check whether given class implements an interface with the same name. * It returns true even when the implemented interface is loaded by a different * classloader and hence the class is not assignable into it. * * An interface is considered as implemented when either: * <ul> * <li>The class directly implements the interface</li> * <li>The class implements an interface which extends the original interface</li> * <li>One of superclasses directly implements the interface</li> * <li>One of superclasses implements an interface which extends the original interface</li> * </ul> * * This is useful for logging purposes. * * @param clazz class to check whether implements the interface * @param iface interface to be implemented * @return <code>true</code> when the class implements the inteface with the same name */ public static boolean implementsInterfaceWithSameName(Class<?> clazz, Class<?> iface) { Class<?>[] interfaces = getAllInterfaces(clazz); for (Class implementedInterface : interfaces) { if (implementedInterface.getName().equals(iface.getName())) { return true; } } return false; }
/** * Check whether given class implements an interface with the same name. * It returns true even when the implemented interface is loaded by a different * classloader and hence the class is not assignable into it. * * An interface is considered as implemented when either: * <ul> * <li>The class directly implements the interface</li> * <li>The class implements an interface which extends the original interface</li> * <li>One of superclasses directly implements the interface</li> * <li>One of superclasses implements an interface which extends the original interface</li> * </ul> * * This is useful for logging purposes. * * @param clazz class to check whether implements the interface * @param iface interface to be implemented * @return <code>true</code> when the class implements the inteface with the same name */ public static boolean implementsInterfaceWithSameName(Class<?> clazz, Class<?> iface) { Class<?>[] interfaces = getAllInterfaces(clazz); for (Class implementedInterface : interfaces) { if (implementedInterface.getName().equals(iface.getName())) { return true; } } return false; }