简体   繁体   中英

To check a class or an interface implements or extends a particular interface

I would like to get all the classes or interfaces that implements or extends the Externalizable interface. For the same I used

Externalizable.class.isAssignableFrom(clasz)

But my intention is to get only the classes or interfaces that implements or extends Externalizable. I do not need clasz if Externalizable is not a superclass or superinterface of at the first level . It would be great if someone could help me with a solution.

Sounds like you want only classes and interfaces which directly implement Externalizable .

Class<?>[] interfaces = clasz.getInterfaces();
for (Class<?> c: interfaces) {
    if (c.equals(Externalizable.class)) {
        // clasz is a direct descendent of Externalizable
    }
}

If you just need to know the implementors in Java standard edition, they're listed in the Externalizable javadoc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM