java 确定类的扩展接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/116587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:04:28  来源:igfitidea点击:

Determining the extended interfaces of a Class

javareflectioninterface

提问by shsteimer

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

我需要确定表示一个接口的 Class 对象是否扩展了另一个接口,即:

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

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

根据规范Class.getSuperClass() 将为接口返回 null。

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

如果此类表示 Object 类、接口、基本类型或 void,则返回 null。

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?

有任何想法吗?

回答by Andreas Holstenson

Use Class.getInterfaces such as:

使用 Class.getInterfaces 如:

Class<?> c; // Your class
for(Class<?> i : c.getInterfaces()) {
     // test if i is your interface
}

Also the following code might be of help, it will give you a set with all super-classes and interfaces of a certain class:

此外,以下代码可能会有所帮助,它将为您提供一个包含某个类的所有超类和接口的集合:

public static Set<Class<?>> getInheritance(Class<?> in)
{
    LinkedHashSet<Class<?>> result = new LinkedHashSet<Class<?>>();

    result.add(in);
    getInheritance(in, result);

    return result;
}

/**
 * Get inheritance of type.
 * 
 * @param in
 * @param result
 */
private static void getInheritance(Class<?> in, Set<Class<?>> result)
{
    Class<?> superclass = getSuperclass(in);

    if(superclass != null)
    {
        result.add(superclass);
        getInheritance(superclass, result);
    }

    getInterfaceInheritance(in, result);
}

/**
 * Get interfaces that the type inherits from.
 * 
 * @param in
 * @param result
 */
private static void getInterfaceInheritance(Class<?> in, Set<Class<?>> result)
{
    for(Class<?> c : in.getInterfaces())
    {
        result.add(c);

        getInterfaceInheritance(c, result);
    }
}

/**
 * Get superclass of class.
 * 
 * @param in
 * @return
 */
private static Class<?> getSuperclass(Class<?> in)
{
    if(in == null)
    {
        return null;
    }

    if(in.isArray() && in != Object[].class)
    {
        Class<?> type = in.getComponentType();

        while(type.isArray())
        {
            type = type.getComponentType();
        }

        return type;
    }

    return in.getSuperclass();
}

Edit: Added some code to get all super-classes and interfaces of a certain class.

编辑:添加了一些代码来获取某个类的所有超类和接口。

回答by Matt

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

我一开始总是倒序排列,但最近意识到这与使用 instanceof 完全相反

if (extendedInterfaceA instanceof interfaceB) 

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

是一样的,但你必须有类的实例而不是类本身

回答by Dan Fleet

Does Class.isAssignableFrom() do what you need?

Class.isAssignableFrom() 是否满足您的需求?

Class baseInterface = Class.forName("a.b.c.d.IMyInterface");
Class extendedInterface = Class.forName("a.b.d.c.ISomeOtherInterface");

if ( baseInterface.isAssignableFrom(extendedInterface) )
{
  // do stuff
}

回答by user13664

Take a look at Class.getInterfaces();

看看 Class.getInterfaces();

List<Object> list = new ArrayList<Object>();
for (Class c : list.getClass().getInterfaces()) {
    System.out.println(c.getName());
}

回答by PhantomStr

Liast<Class> getAllInterfaces(Class<?> clazz){
    List<Class> interfaces = new ArrayList<>();
    Collections.addAll(interfaces,clazz.getInterfaces());
    if(!clazz.getSuperclass().equals(Object.class)){
        interfaces.addAll(getAllInterfaces(clazz.getSuperclass()));
    }
    return interfaces ;
}