문제

인터페이스를 나타내는 클래스 객체가 다른 인터페이스를 확장하는지 확인해야 합니다. 즉, 다음과 같습니다.

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

~에 따르면 사양 Class.getSuperClass()는 인터페이스에 대해 null을 반환합니다.

이 클래스가 객체 클래스, 인터페이스, 원시 유형 또는 공극을 나타내는 경우 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))

당신이 원하는 것입니다

나는 항상 처음에는 순서를 거꾸로 잡았지만 최근에 그것이 instanceof를 사용하는 것과 정반대라는 것을 깨달았습니다.

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