2008-09-22 32 views
10

tôi cần phải xác định xem một đối tượng Class đại diện cho một giao diện mở rộng giao diện khác, ví dụ:Xác định các giao diện mở rộng của một Class

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

theo the spec Class.getSuperClass() sẽ trả về null cho một giao diện.

Nếu lớp này đại diện cho một trong hai lớp Object, một giao diện, một loại nguyên thủy , hoặc bãi bỏ, sau đó null là trả lại.

Vì vậy, các thao tác sau sẽ không hoạt động.

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 
} 

bất kỳ ý tưởng nào?

Trả lời

15

Sử dụng Class.getInterfaces như:

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

Ngoài ra mã sau đây có thể giúp đỡ, nó sẽ cung cấp cho bạn một bộ với tất cả các siêu lớp và các giao diện của một lớp nhất định:

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

Chỉnh sửa: Đã thêm một số mã để có được tất cả các lớp siêu và giao diện của một lớp nhất định.

+1

Trông với tôi như làm cho nó phức tạp hơn sau đó nó được; reimplementing những gì Java đã cung cấp. Giả sử tất cả các mã ở đây là chính xác, nó sẽ chỉ cung cấp cho câu trả lời tương tự như một lớp lót làAssignableTừ các câu trả lời khác. –

0

Hãy xem Class.getInterfaces();

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

Class.isAssignableFrom() có làm những gì bạn cần không?

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)) 

là những gì bạn muốn

tôi luôn luôn nhận được đặt hàng ngược lúc đầu nhưng thời gian gần đây nhận ra rằng nó hoàn toàn trái ngược của việc sử dụng instanceof

if (extendedInterfaceA instanceof interfaceB) 

được điều tương tự nhưng bạn phải có các phiên bản của các lớp học chứ không phải là các lớp tự mình

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

Bạn nên thêm một số ngữ cảnh giải thích câu trả lời của mình thay vì chỉ cung cấp mã. – Jaquez

Các vấn đề liên quan