質問

インターフェイスを表す Class オブジェクトが別のインターフェイスを拡張するかどうかを判断する必要があります。つまり、次のようになります。

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

によると 仕様 Class.getSuperClass() は、インターフェイスに対して null を返します。

このクラスがオブジェクトクラス、インターフェイス、プリミティブタイプ、またはvoidを表す場合、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