Testing which interfaces a class/interface implements?
<p>I want to test which interfaces a class/interface is implementing.</p>

<p>So I have a basic interface <code>Adapter</code> and some interfaces base on it (e.g. <code>PermissionsAdapter</code>). Now I get a variable typed as <code>Adapter</code> and now I want to get every interface based on <code>Adapter</code> that this variable is implementing.</p>

<p>A simple implementation:</p>

<pre><code>import java.util.LinkedList;
import java.util.List;

public class Main {

    private interface A { }
    private interface BA extends A { }
    private interface CA extends A { }

    private interface X {}

    private class IB implements BA { }
    private class IC implements CA { }
    private class IBC implements BA, CA { }
    private class ICX implements CA, X { }
    private class IBCX implements BA, CA, X {}

    public Main() {
        this.test(new IB());
        this.test(new IC());
        this.test(new IBC());
        this.test(new ICX());
        this.test(new IBCX());
    }

    private void test(A a) {
        List&lt;Class&lt;? extends A&gt;&gt; result = this.getAdapterInterfaces(a);
        System.out.print(a.getClass().getSimpleName() + "{");
        for (Class&lt;? extends A&gt; class1 : result) {
            System.out.print(class1.getSimpleName() + ", ");
        }
        System.out.println("}");
    }

    @SuppressWarnings("unchecked")
    public List&lt;Class&lt;? extends A&gt;&gt; getAdapterInterfaces(A adapter) {
            List&lt;Class&lt;? extends A&gt;&gt; result = new LinkedList&lt;Class&lt;? extends A&gt;&gt;();
            Class&lt;?&gt;[] interfaces = adapter.getClass().getInterfaces();
            for (Class&lt;?&gt; clazz : interfaces) {
                    if (A.class.isInstance(clazz)) {
                        result.add((Class&lt;? extends A&gt;) clazz);
                    }
            }
            return result;
    }

    public static void main(String[] args) {
        new Main();
    }
}
</code></pre>

<p>Here is the <code>Adapter</code> the interface <code>A</code> and the <code>PermissionsAdapter</code> for example <code>BA</code> and <code>CA</code> is another adapter. <code>X</code> is only a sample interface, which the list shouldn't contain.</p>

<p>And the result:</p>

<pre><code>IB{}
IC{}
IBC{}
ICX{}
IBCX{}
</code></pre>

<p>So it doesn't seem to work. Now where is my mistake in <code>getAdapterInterfaces(A)</code>? I think it is the test, but how could I test, if a class is a specific class of <code>A</code>?</p>

<p>Fabian</p>
