代码之家  ›  专栏  ›  技术社区  ›  shsteimer

确定类的扩展接口

  •  11
  • shsteimer  · 技术社区  · 17 年前

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

    根据 the spec

    如果该类表示 基元类型或void,则为null 返回。

    因此,以下方法不起作用。

    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
    }
    

    有什么想法吗?

    5 回复  |  直到 7 年前
        1
  •  16
  •   Andreas Holstenson    17 年前

    使用Class.GetInterface,例如:

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

    此外,以下代码可能会有所帮助,它将为您提供一组特定类的所有超类和接口:

    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();
    }
    

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

        2
  •  10
  •   Matt    17 年前
    if (interface.isAssignableFrom(extendedInterface))
    

    这就是你想要的

    if (extendedInterfaceA instanceof interfaceB) 
    

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

        3
  •  2
  •   Dan Fleet    17 年前

    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
    }
    
        4
  •  0
  •   user13664    17 年前

    看看Class.getInterfaces();

    List<Object> list = new ArrayList<Object>();
    for (Class c : list.getClass().getInterfaces()) {
        System.out.println(c.getName());
    }
    
        5
  •  0
  •   PhantomStr    8 年前
    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 ;
    }