2008-09-22 11 views
10

मैं निर्धारण करने की आवश्यकता की विस्तारित इंटरफेस का निर्धारण करता है, तो एक कक्षा वस्तु एक इंटरफेस का प्रतिनिधित्व एक और इंटरफेस, यानी लागू होता है:एक कक्षा

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

the spec Class.getSuperClass के अनुसार() एक अंतरफलक के लिए अशक्त वापस आ जाएगी।

इस कक्षा या तो वस्तु वर्ग, एक अंतरफलक, एक आदिम प्रकार, या शून्य का प्रतिनिधित्व करता है, तो अशक्त वापस लौटा है।

इसलिए निम्नलिखित काम नहीं करेंगे।

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 
} 

कोई विचार?

उत्तर

15

उपयोग Class.getInterfaces जैसे:

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

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

इसके अलावा निम्नलिखित कोड मदद की हो सकता है, यह आप सभी सुपर वर्गों और एक निश्चित वर्ग के इंटरफेस के साथ एक सेट दे देंगे

संपादित करें: एक निश्चित वर्ग के सभी सुपर-क्लास और इंटरफेस प्राप्त करने के लिए कुछ कोड जोड़ा गया।

+1

मुझे यह अधिक जटिल बनाने की तरह लगता है तो यह है; जावा जो पहले से प्रदान करता है उसे पुन: कार्यान्वित करना। यहां सभी कोड मानना ​​सही है, यह वही उत्तर देगा जैसा कि एक-लाइनर है। अन्य उत्तरों से स्वीकार्य है। –

0

Class.getInterfaces() पर एक नज़र डालें;

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

क्या क्लास.इसAssignableFrom() क्या आपको चाहिए?

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

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

क्या आप

मैं हमेशा पहली बार में पीछे की ओर आदेश मिल चाहते हैं, लेकिन हाल ही में महसूस किया कि यह instanceof

if (extendedInterfaceA instanceof interfaceB) 

का उपयोग कर के ठीक विपरीत है एक ही बात है, लेकिन है आप कक्षाओं के बजाय कक्षाओं के उदाहरण होना चाहिए

0
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 ; 
} 
+1

आपको कोड प्रदान करने के बजाय अपने उत्तर को समझाते हुए कुछ संदर्भ जोड़ना चाहिए। – Jaquez

संबंधित मुद्दे