【问题标题】:Determining the extended interfaces of a Class确定类的扩展接口
【发布时间】:2010-09-12 02:36:42
【问题描述】:

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

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

根据the spec Class.getSuperClass() 将为接口返回null。

如果这个类代表 对象类,一个接口,一个 原始类型或 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
}

有什么想法吗?

【问题讨论】:

    标签: java reflection interface


    【解决方案1】:

    使用 Class.getInterfaces 如:

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

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

    【讨论】:

    • 在我看来好像让它变得更复杂了;重新实现 Java 已经提供的功能。假设这里的所有代码都是正确的,它只会给出与其他答案中的单行 isAssignableFrom 相同的答案。
    【解决方案2】:
    if (interface.isAssignableFrom(extendedInterface))
    

    是你想要的

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

    if (extendedInterfaceA instanceof interfaceB) 
    

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

    【讨论】:

      【解决方案3】:

      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】:

        看看 Class.getInterfaces();

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

        【讨论】:

          【解决方案5】:
          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 ;
          }
          

          【讨论】:

          • 您应该添加一些上下文来解释您的答案,而不仅仅是提供代码。
          猜你喜欢
          • 1970-01-01
          • 2017-01-21
          • 2021-08-07
          • 2010-12-24
          • 2018-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多