Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the extended interfaces of a Class

I need to determine if a Class object representing an interface extends another interface, ie:

 package a.b.c.d;
    public Interface IMyInterface extends a.b.d.c.ISomeOtherInterface{
    }

according to the spec Class.getSuperClass() will return null for an Interface.

If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned.

Therefore the following won't work.

Class interface = Class.ForName("a.b.c.d.IMyInterface")
Class extendedInterface = interface.getSuperClass();
if(extendedInterface.getName().equals("a.b.d.c.ISomeOtherInterface")){
    //do whatever here
}

any ideas?

like image 450
shsteimer Avatar asked Sep 22 '08 18:09

shsteimer


1 Answers

if (interface.isAssignableFrom(extendedInterface))

is what you want

i always get the ordering backwards at first but recently realized that it's the exact opposite of using instanceof

if (extendedInterfaceA instanceof interfaceB) 

is the same thing but you have to have instances of the classes rather than the classes themselves

like image 124
Matt Avatar answered Nov 16 '22 03:11

Matt