Example usage for java.lang Class getInterfaces

List of usage examples for java.lang Class getInterfaces

Introduction

In this page you can find the example usage for java.lang Class getInterfaces.

Prototype

public Class<?>[] getInterfaces() 

Source Link

Document

Returns the interfaces directly implemented by the class or interface represented by this object.

Usage

From source file:ClassUtil.java

/**
 * Builds an <b>unordered</b> set of all interface and object classes that
 * are generalizations of the provided class.
 * @param classObject the class to find generalization of.
 * @return a Set of class objects./* ww  w  .  java  2s.c  om*/
 */
public static Set getGeneralizations(Class classObject) {
    Set generalizations = new HashSet();

    generalizations.add(classObject);

    Class superClass = classObject.getSuperclass();
    if (superClass != null) {
        generalizations.addAll(getGeneralizations(superClass));
    }

    Class[] superInterfaces = classObject.getInterfaces();
    for (int i = 0; i < superInterfaces.length; i++) {
        Class superInterface = superInterfaces[i];
        generalizations.addAll(getGeneralizations(superInterface));
    }

    return generalizations;
}

From source file:ReflectApp.java

static void describeClassOrInterface(Class className, String name) {
    displayModifiers(className.getModifiers());
    displayFields(className.getDeclaredFields());
    displayMethods(className.getDeclaredMethods());

    if (className.isInterface()) {
        System.out.println("Interface: " + name);
    } else {/*w w w.j a  v a2s  . c om*/
        System.out.println("Class: " + name);
        displayInterfaces(className.getInterfaces());
        displayConstructors(className.getDeclaredConstructors());
    }
}

From source file:com.zfsoft.util.reflect.ReflectionUtils.java

/**
 * clazz???/*from   w  ww.j  ava 2  s. c om*/
 * @param clazz Class
 * @param targetInterface ??
 * @return
 */
public static boolean isInterface(Class clazz, Class targetInterface) {
    Class[] face = clazz.getInterfaces();
    for (int i = 0, j = face.length; i < j; i++) {
        if (face[i].getName().equals(targetInterface.getName())) {
            return true;
        } else {
            Class[] face1 = face[i].getInterfaces();
            for (int x = 0; x < face1.length; x++) {
                if (face1[x].getName().equals(targetInterface.getName())) {
                    return true;
                } else if (isInterface(face1[x], targetInterface)) {
                    return true;
                }
            }
        }
    }
    if (null != clazz.getSuperclass()) {
        return isInterface(clazz.getSuperclass(), targetInterface);
    }
    return false;
}

From source file:Main.java

/**
 * Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found in the super
 * classes/interfaces of inClass, then the generic type corresponding to inClass and its TypeVariables is returned,
 * otherwise null. For example :/*from  w  w  w .  ja  v a2  s.c  om*/
 * 
 * <pre>
 * abstract class MyClass implements Serializer&lt;Number&gt; {
 * 
 * }
 * 
 * // type value will be the parameterized type Serializer&lt;Number&gt;
 * Type type = lookupGenericType(Serializer.class, MyClass.class);
 * </pre>
 */
public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) {
    if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass))
        return null;
    if (ofClass.equals(inClass))
        return inClass;

    if (ofClass.isInterface()) {
        // lets look if the interface is directly implemented by fromClass
        Class<?>[] interfaces = inClass.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {
            // do they match?
            if (ofClass.equals(interfaces[i])) {
                return inClass.getGenericInterfaces()[i];
            } else {
                Type superType = lookupGenericType(ofClass, interfaces[i]);
                if (superType != null)
                    return superType;
            }
        }
    }

    // ok it's not one of the directly implemented interfaces, lets try extended class
    Class<?> superClass = inClass.getSuperclass();
    if (ofClass.equals(superClass))
        return inClass.getGenericSuperclass();
    return lookupGenericType(ofClass, inClass.getSuperclass());
}

From source file:de.unisb.cs.st.javalanche.mutation.runtime.testDriver.junit.Junit4MutationTestDriver.java

private static boolean runnerImplementsFilterable(Class<? extends Runner> runner) {
    for (Class<?> interfaze : runner.getInterfaces()) {
        if (Filterable.class.equals(interfaze))
            return true;
    }// w ww .  j  a  v  a2s . co m
    return false;
}

From source file:net.landora.video.utils.UIUtils.java

public static MultiValueMap createCompleteContextByClass(Collection<?> context) {
    Collection<Object> fullContext = UIUtils.createCompleteContext(context);

    MultiValueMap valuesByClass = new MultiValueMap();
    Set<Class<?>> allClasses = new HashSet<Class<?>>();
    for (Object obj : fullContext) {
        Class<?> clazz = obj.getClass();
        allClasses.clear();/*from   w w w . j a va  2s.c  om*/
        while (clazz != null) {
            allClasses.add(clazz);
            allClasses.addAll(Arrays.asList(clazz.getInterfaces()));
            clazz = clazz.getSuperclass();
        }

        for (Class<?> c : allClasses) {
            valuesByClass.put(c, obj);
        }
    }
    return valuesByClass;
}

From source file:architecture.common.util.ClassUtils.java

/**
 * Finds all super classes and interfaces for a given class
 * //from   ww  w .ja  va2 s . com
 * @param cls
 *            The class to scan
 * @param types
 *            The collected related classes found
 */
public static void findAllTypes(Class cls, Set<Class> types) {
    if (cls == null) {
        return;
    }

    // check to ensure it hasn't been scanned yet
    if (types.contains(cls)) {
        return;
    }

    types.add(cls);

    findAllTypes(cls.getSuperclass(), types);
    for (int x = 0; x < cls.getInterfaces().length; x++) {
        findAllTypes(cls.getInterfaces()[x], types);
    }
}

From source file:Main.java

public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> aClazz) {
    //Check class hierarchy
    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
        T anno = c.getAnnotation(aClazz);
        if (anno != null) {
            return anno;
        }/*  www  .j a  va2s .  c o m*/
    }

    //Check interfaces (breadth first)
    Queue<Class<?>> q = new LinkedList<Class<?>>();
    q.add(clazz);
    while (!q.isEmpty()) {
        Class<?> c = q.remove();
        if (c != null) {
            if (c.isInterface()) {
                T anno = c.getAnnotation(aClazz);
                if (anno != null) {
                    return anno;
                }
            } else {
                q.add(c.getSuperclass());
            }
            q.addAll(Arrays.asList(c.getInterfaces()));
        }
    }

    return null;
}

From source file:fi.foyt.foursquare.api.JSONFieldParser.java

/**
 * Returns whether class is derived from FoursquareEntity interface
 * //from  w  ww .  java 2 s .  com
 * @param clazz class
 * @return whether class is derived from FoursquareEntity interface
 */
private static boolean isFoursquareEntity(Class<?> clazz) {
    for (Class<?> intrf : clazz.getInterfaces()) {
        if (intrf.equals(FoursquareEntity.class)) {
            return true;
        }
    }

    Class<?> superClass = clazz.getSuperclass();
    if (!superClass.equals(Object.class)) {
        return isFoursquareEntity(superClass);
    }

    return false;
}

From source file:Main.java

private static void _addSuperTypes(Class<?> cls, Class<?> endBefore, Collection<Class<?>> result,
        boolean addClassItself) {
    if (cls != endBefore && cls != null && cls != Object.class) {
        if (addClassItself) {
            if (!result.contains(cls)) {
                result.add(cls);//from   w w w.j a  v  a  2 s  .co  m
            } else {
                return;
            }
        }
        for (Class<?> intCls : cls.getInterfaces()) {
            _addSuperTypes(intCls, endBefore, result, true);
        }
        _addSuperTypes(cls.getSuperclass(), endBefore, result, true);
    }
}