Determining the extended interfaces of a Class

前端 未结 5 756
太阳男子
太阳男子 2020-12-20 13:48

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.         


        
相关标签:
5条回答
  • 2020-12-20 14:32

    Use Class.getInterfaces such as:

    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.

    0 讨论(0)
  • 2020-12-20 14:34

    Does Class.isAssignableFrom() do what you need?

    Class baseInterface = Class.forName("a.b.c.d.IMyInterface");
    Class extendedInterface = Class.forName("a.b.d.c.ISomeOtherInterface");
    
    if ( baseInterface.isAssignableFrom(extendedInterface) )
    {
      // do stuff
    }
    
    0 讨论(0)
  • 2020-12-20 14:37

    Take a look at Class.getInterfaces();

    List<Object> list = new ArrayList<Object>();
    for (Class c : list.getClass().getInterfaces()) {
        System.out.println(c.getName());
    }
    
    0 讨论(0)
  • 2020-12-20 14:37
    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 ;
    }
    
    0 讨论(0)
  • 2020-12-20 14:42
    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

    0 讨论(0)
提交回复
热议问题