سؤال

أحتاج إلى تحديد ما إذا كان كائن الفئة الذي يمثل واجهة يمتد إلى واجهة أخرى، على سبيل المثال:

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

وفق المواصفات سوف يُرجع Class.getSuperClass() قيمة فارغة للواجهة.

إذا كانت هذه الفئة تمثل إما فئة الكائن أو واجهة أو نوع بدائي أو باطل ، فسيتم إرجاع 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
}

أيه أفكار؟

هل كانت مفيدة؟

المحلول

استخدم 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();
}

يحرر:تمت إضافة بعض التعليمات البرمجية للحصول على جميع الفئات الفائقة والواجهات الخاصة بفئة معينة.

نصائح أخرى

if (interface.isAssignableFrom(extendedInterface))

هو ما تريد

أحصل دائمًا على الترتيب بشكل عكسي في البداية ولكني أدركت مؤخرًا أنه عكس استخدام مثيل

if (extendedInterfaceA instanceof interfaceB) 

هو نفس الشيء ولكن يجب أن يكون لديك مثيلات للفئات بدلاً من الفئات نفسها

هل يقوم 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
}

ألق نظرة على Class.getInterfaces();

List<Object> list = new ArrayList<Object>();
for (Class c : list.getClass().getInterfaces()) {
    System.out.println(c.getName());
}
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 ;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top