/*
N o w J a v a . c o m
*/
//package com.nowjava;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] argv) throws Exception {
Class clazz = String.class;
System.out.println(java.util.Arrays
.toString(getImplementedInterfaces(clazz)));
}
/* from
n o w j a v a . c o m - 时 代 Java*/
/**
* Returns all interfaces implemented by the specified class
* including all interfaces implemented by super classes.
* If the specified class is itself an interfaces or the
* specified class does not implement any interfaces, this
* method returns an empty array.
* @param clazz the Class
* @return all interfaces implemented by the specified class
*/
public static Class[] getImplementedInterfaces(Class clazz) {
if (clazz.isInterface())
return new Class[0];
Class[] classes = getInheritanceHierarchy(clazz);
Set interfaceSet = new HashSet();
for (int ii = 0; ii < classes.length; ii++) {
interfaceSet.addAll(Arrays.asList(classes[ii].getInterfaces()));
}
return (Class[]) interfaceSet
.toArray(new Class[interfaceSet.size()]);
}
/**
* Returns the inheritance hierarchy of the specified class.
* The returned array includes all superclasses of the specified class
* starting with the most general superclass, which is
* <code>java.lang.Object</code>. The returned array also
* includes the class itself as the last element. Implemented
* interfaces are not included.
* @param clazz the Class
* @return all superclasses, most general superclass first
*/
public static Class[] getInheritanceHierarchy(Class clazz) {
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。