get Interfaces from Class - Java Reflection

Java examples for Reflection:Interface

Description

get Interfaces from Class

Demo Code


//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        System.out.println(java.util.Arrays.toString(getInterfaces(clazz)));
    }/*from ww w .j  a  v a  2 s  . com*/

    public static Class<?>[] getInterfaces(Class<?> clazz) {
        ArrayList<Class<?>> list = new ArrayList<Class<?>>();
        while (clazz != null) {
            list.addAll(Arrays.asList(clazz.getInterfaces()));
            clazz = clazz.getSuperclass();
        }
        return list.toArray(new Class[list.size()]);
    }
}

Related Tutorials