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:org.rapla.rest.gwtjsonrpc.server.JsonServlet.java

private static Class findInterface(Class<?> c) {
    while (c != null) {
        if (c.getAnnotation(WebService.class) != null) {
            return c;
        }/*from   w  w w  .  java 2  s .  com*/
        for (final Class<?> i : c.getInterfaces()) {
            final Class r = findInterface(i);
            if (r != null) {
                return r;
            }
        }
        c = c.getSuperclass();
    }
    return null;
}

From source file:org.jdbcluster.JDBClusterUtil.java

/**
 * tries to validate interface instances through Super Interfaces
 * /*w  ww .j  a  v  a  2  s  .  co m*/
 * @param count
 *            recursive parameter. Use 0 for the first call
 * @param superInterface
 * @param interfaceClasses
 * @return -1 for not found. Counter for superInterfaces above parameter
 *         superInterface
 */
static private int countSuperInterface(int count, Class<?> superInterface, Class<?>[] interfaceClasses) {
    int ret = 0;

    if (interfaceClasses == null)
        return -1;

    for (Class<?> c : interfaceClasses) {
        if (superInterface == c)
            return count;
    }
    ++count;
    for (Class<?> c : interfaceClasses) {
        ret = countSuperInterface(count, superInterface, c.getInterfaces());
        if (ret != -1)
            break;
    }
    return ret;
}

From source file:com.struts2ext.json.JSONUtil.java

/**
 * Recursive method to visit all the interfaces of a class (and its
 * superclasses and super-interfaces) if they haven't already been visited.
 * <p/>/*from   w  w  w .  j av  a 2 s  .c  om*/
 * Always visits itself if it hasn't already been visited
 * 
 * @param thisClass
 *            the current class to visit (if not already done so)
 * @param classesVisited
 *            classes already visited
 * @param visitor
 *            this vistor is called for each class/interface encountered
 * @return true if recursion can continue, false if it should be aborted
 */
private static boolean visitUniqueInterfaces(Class<?> thisClass, ClassVisitor visitor,
        List<Class<?>> classesVisited) {
    boolean okayToContinue = true;

    if (!classesVisited.contains(thisClass)) {
        classesVisited.add(thisClass);
        okayToContinue = visitor.visit(thisClass);

        if (okayToContinue) {
            Class<?>[] interfaces = thisClass.getInterfaces();
            int index = 0;
            while ((index < interfaces.length) && (okayToContinue)) {
                okayToContinue = visitUniqueInterfaces(interfaces[index++], visitor, classesVisited);
            }

            if (okayToContinue) {
                Class<?> superClass = thisClass.getSuperclass();
                if ((superClass != null) && (!Object.class.equals(superClass))) {
                    okayToContinue = visitUniqueInterfaces(superClass, visitor, classesVisited);
                }
            }
        }
    }
    return okayToContinue;
}

From source file:Main.java

/**
 * Sorts objects in the given collection into different types by Class.
 * @param <E> the element type//from  ww w .  j  a va2s.  c o m
 * @param objects a Collection of objects
 * @param inherit if true, objects are put into all their class's superclasses as well, except
 * Object - the original Collection can be used in that case
 * @return a Map from Class to List of objects in that class
 */
public static <E> Map<Class<?>, List<E>> groupByClass(Collection<E> objects, boolean inherit) {
    Map<Class<?>, List<E>> retval = new HashMap<Class<?>, List<E>>();
    for (E o : objects) {
        Class<?> c = o.getClass();
        if (inherit) {
            Set<Class<?>> done = new HashSet<Class<?>>();
            done.add(Object.class);
            Stack<Class<?>> todo = new Stack<Class<?>>();
            todo.push(c);
            while (!todo.empty()) {
                c = todo.pop();
                if ((c != null) && !done.contains(c)) {
                    done.add(c);
                    List<E> l = retval.get(c);
                    if (l == null) {
                        l = new ArrayList<E>();
                        retval.put(c, l);
                    }
                    l.add(o);
                    todo.push(c.getSuperclass());
                    Class<?>[] classes = c.getInterfaces();
                    for (int i = 0; i < classes.length; i++) {
                        todo.push(classes[i]);
                    }
                }
            }
        } else {
            List<E> l = retval.get(c);
            if (l == null) {
                l = new ArrayList<E>();
                retval.put(c, l);
            }
            l.add(o);
        }
    }
    return retval;
}

From source file:org.compass.core.accessor.BasicPropertyAccessor.java

private static BasicGetter getGetterOrNull(Class theClass, String propertyName) {

    if (theClass == Object.class || theClass == null)
        return null;

    Method method = getterMethod(theClass, propertyName);

    if (method != null) {
        if (!ClassUtils.isPublic(theClass, method))
            method.setAccessible(true);//  w  ww . j  a v  a2 s .  c  om
        return new BasicGetter(theClass, method, propertyName);
    } else {
        BasicGetter getter = getGetterOrNull(theClass.getSuperclass(), propertyName);
        if (getter == null) {
            Class[] interfaces = theClass.getInterfaces();
            for (int i = 0; getter == null && i < interfaces.length; i++) {
                getter = getGetterOrNull(interfaces[i], propertyName);
            }
        }
        return getter;
    }
}

From source file:org.jboss.dashboard.commons.misc.ReflectionUtils.java

public static Class[] getClassHierarchyInterfaces(Class clazz) {
    if (clazz == null)
        return new Class[] {};
    if (clazz.equals(java.lang.Object.class))
        return new Class[] {};

    List<Class> results = new ArrayList<Class>();
    Class[] ifaces = clazz.getInterfaces();
    for (int i = 0; i < ifaces.length; i++) {
        if (!results.contains(ifaces[i]))
            results.add(ifaces[i]);//from   ww w  .ja  v a  2 s.c o  m
    }

    ifaces = getClassHierarchyInterfaces(clazz.getSuperclass());
    for (int i = 0; i < ifaces.length; i++) {
        if (!results.contains(ifaces[i]))
            results.add(ifaces[i]);
    }

    int index = 0;
    Class[] array = new Class[results.size()];
    for (Class result : results)
        array[index++] = result;
    return array;
}

From source file:com.google.gwtjsonrpc.server.JsonServlet.java

@SuppressWarnings("unchecked")
private static Class<? extends RemoteJsonService> findInterface(Class<?> c) {
    while (c != null) {
        if (c.isInterface() && RemoteJsonService.class.isAssignableFrom(c)) {
            return (Class<RemoteJsonService>) c;
        }//from  w w  w  . j a  va  2  s .  c  om
        for (final Class<?> i : c.getInterfaces()) {
            final Class<? extends RemoteJsonService> r = findInterface(i);
            if (r != null) {
                return r;
            }
        }
        c = c.getSuperclass();
    }
    return null;
}

From source file:SystemUtils.java

public static boolean DoesClassImplement(Class targetClass, Class interfaceClass) {
    boolean implemented = false;
    Class[] interfaces;/*from  w w  w  .  j av  a2s.co  m*/

    if (targetClass != null && interfaceClass != null) {

        do {
            interfaces = targetClass.getInterfaces();

            for (Class intf : interfaces) {
                if (intf.getName().equals(interfaceClass.getName())) {
                    implemented = true;
                    break;
                }
            }

            targetClass = targetClass.getSuperclass();
        } while (!implemented && !targetClass.getName().equals("java.lang.Object"));
    } //End null class parameters check

    return implemented;
}

From source file:org.craftercms.commons.ebus.config.EBusBeanAutoConfiguration.java

private static Set<Method> findHandlerMethods(final Class<?> handlerType,
        final ReflectionUtils.MethodFilter listenerMethodFilter) {

    final Set<Method> handlerMethods = new LinkedHashSet<Method>();

    if (handlerType == null) {
        return handlerMethods;
    }/*from   w  w w.j a v  a2  s .c om*/

    Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>();
    Class<?> specifiedHandlerType = null;
    if (!Proxy.isProxyClass(handlerType)) {
        handlerTypes.add(handlerType);
        specifiedHandlerType = handlerType;
    }
    handlerTypes.addAll(Arrays.asList(handlerType.getInterfaces()));
    for (Class<?> currentHandlerType : handlerTypes) {
        final Class<?> targetClass = (specifiedHandlerType != null ? specifiedHandlerType : currentHandlerType);
        ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(final Method method) throws IllegalArgumentException, IllegalAccessException {
                Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
                Method bridgeMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
                if (listenerMethodFilter.matches(specificMethod)
                        && (bridgeMethod == specificMethod || !listenerMethodFilter.matches(bridgeMethod))) {
                    handlerMethods.add(specificMethod);
                }
            }
        }, ReflectionUtils.USER_DECLARED_METHODS);
    }

    return handlerMethods;
}

From source file:com.espertech.esper.event.bean.BeanEventType.java

private static void getSuperInterfaces(Class clazz, Set<Class> result) {
    Class interfaces[] = clazz.getInterfaces();

    for (int i = 0; i < interfaces.length; i++) {
        result.add(interfaces[i]);/* ww  w  . j a v  a2 s. c  o m*/
        getSuperInterfaces(interfaces[i], result);
    }
}