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:com.googlecode.jsonplugin.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./* ww  w  . ja  v  a2s  .  c  o  m*/
 * <p/>
 * 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:org.apache.roller.weblogger.ui.core.RollerContext.java

/**
 * Get an instance of AutoProvision, if available in roller.properties
 * @return AutoProvision// w  ww  . j  ava 2  s. c om
 */
public static AutoProvision getAutoProvision() {
    String clazzName = WebloggerConfig.getProperty("users.sso.autoProvision.className");

    if (null == clazzName) {
        return null;
    }

    Class clazz;
    try {
        clazz = Class.forName(clazzName);
    } catch (ClassNotFoundException e) {
        log.warn("Unable to found specified Auto Provision class.", e);
        return null;
    }

    if (null == clazz) {
        return null;
    }

    Class[] interfaces = clazz.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        if (interfaces[i].equals(AutoProvision.class)) {
            try {
                return (AutoProvision) clazz.newInstance();
            } catch (InstantiationException e) {
                log.warn("InstantiationException while creating: " + clazzName, e);
            } catch (IllegalAccessException e) {
                log.warn("IllegalAccessException while creating: " + clazzName, e);
            }
        }
    }
    return null;
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static int findCIOneLevelDistance(Class<?> lower, Class<?> upper) {
    if (!upper.isInterface() || lower.isInterface()) {
        throw new IllegalArgumentException(
                String.format("Invalid input class : upper=%s lower=%s", upper, lower));
    }//from  w  w  w . ja v  a 2 s .c  o m

    if (lower == upper) {
        return 0;
    }
    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    int distance = -1;
    Class<?>[] interfaces = lower.getInterfaces();
    for (Class<?> anInterface : interfaces) {
        int tmp = findIIDistance(anInterface, upper);
        if (tmp >= 0) {
            distance = distance < 0 ? tmp : Math.min(distance, tmp);
        }

        if (distance == 0) {
            break;
        }
    }

    return distance;
}

From source file:org.jdto.util.MethodUtils.java

/**
 * <p>Return an accessible method (that is, one that can be invoked via
 * reflection) that implements the specified method, by scanning through all
 * implemented interfaces and subinterfaces. If no such method can be found,
 * return/*from w  ww. j  a  va2  s.co  m*/
 * <code>null</code>.</p>
 *
 * <p> There isn't any good reason why this method must be private. It is
 * because there doesn't seem any reason why other classes should call this
 * rather than the higher level methods.</p>
 *
 * @param cls Parent class for the interfaces to be checked
 * @param methodName Method name of the method we wish to call
 * @param parameterTypes The parameter type signatures
 * @return the accessible method or
 * <code>null</code> if not found
 */
private static Method getAccessibleMethodFromInterfaceNest(Class cls, String methodName,
        Class[] parameterTypes) {
    Method method = null;

    // Search up the superclass chain
    for (; cls != null; cls = cls.getSuperclass()) {

        // Check the implemented interfaces of the parent class
        Class[] interfaces = cls.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            // Is this interface public?
            if (!Modifier.isPublic(interfaces[i].getModifiers())) {
                continue;
            }
            // Does the method exist on this interface?
            try {
                method = interfaces[i].getDeclaredMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e) {
                /*
                 * Swallow, if no method is found after the loop then this
                 * method returns null.
                 */
            }
            if (method != null) {
                break;
            }
            // Recursively check our parent interfaces
            method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes);
            if (method != null) {
                break;
            }
        }
    }
    return method;
}

From source file:org.apache.niolex.commons.reflect.MethodUtil.java

/**
 * Retrieve all the types, including class, super classes and interfaces of this class.
 *
 * @param clazz the class to be used/*ww  w.j a va2s  .  c om*/
 * @return the list contains all the types
 * @throws SecurityException if a security manager is present and the reflection is rejected
 */
public static final List<Class<?>> getAllTypes(Class<?> clazz) {
    // Store all the classes and interfaces here.
    List<Class<?>> list = new ArrayList<Class<?>>();
    List<Class<?>> interfaces = new ArrayList<Class<?>>();
    // Step 1. Add this class and all the super classes.
    do {
        list.add(clazz);
        CollectionUtil.addAll(interfaces, clazz.getInterfaces());
        clazz = clazz.getSuperclass();
    } while (clazz != null);
    // Step 2. Add all the interfaces.
    HashSet<Class<?>> clsSet = new HashSet<Class<?>>();
    for (int i = 0; i < interfaces.size(); ++i) {
        clazz = interfaces.get(i);
        // Use this hash set to filter duplicates.
        if (clsSet.contains(clazz)) {
            continue;
        }
        clsSet.add(clazz);
        list.add(clazz);
        CollectionUtil.addAll(interfaces, clazz.getInterfaces());
    }
    return list;
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

/**
 * Find a single {@link Annotation} of <code>annotationType</code> from the supplied {@link Class}, traversing its
 * interfaces and superclasses if no annotation can be found on the given class itself.
 * <p>/*w ww  .  j ava2  s  . co  m*/
 * This method explicitly handles class-level annotations which are not declared as
 * {@link java.lang.annotation.Inherited inherited} <i>as well as annotations on interfaces</i>.
 * <p>
 * The algorithm operates as follows: Searches for an annotation on the given class and returns it if found. Else
 * searches all interfaces that the given class declares, returning the annotation from the first matching
 * candidate, if any. Else proceeds with introspection of the superclass of the given class, checking the superclass
 * itself; if no annotation found there, proceeds with the interfaces that the superclass declares. Recursing up
 * through the entire superclass hierarchy if no match is found.
 * 
 * @param clazz
 *            the class to look for annotations on
 * @param annotationType
 *            the annotation class to look for
 * @return A tuple {@link Pair} containing the annotation on the left hand side and the class on the right hand side
 *         or <code>null</code> if none found
 */
public static <A extends Annotation> Pair<A, Class<?>> findAnnotation(final Class<?> clazz,
        final Class<A> annotationType) {
    Validate.notNull(clazz, "Class must not be null");
    A annotation = clazz.getAnnotation(annotationType);
    if (annotation != null) {
        return new ImmutablePair<A, Class<?>>(annotation, clazz);
    }
    for (final Class<?> ifc : clazz.getInterfaces()) {
        final Pair<A, Class<?>> pair = findAnnotation(ifc, annotationType);
        if (pair != null) {
            annotation = pair.getLeft();
            if (annotation != null) {
                return new ImmutablePair<A, Class<?>>(annotation, ifc);
            }
        }
    }
    if (!Annotation.class.isAssignableFrom(clazz)) {
        for (final Annotation ann : clazz.getAnnotations()) {
            final Pair<A, Class<?>> pair = findAnnotation(ann.annotationType(), annotationType);
            if (pair != null) {
                annotation = pair.getLeft();
                if (annotation != null) {
                    return new ImmutablePair<A, Class<?>>(annotation, ann.annotationType());
                }
            }
        }
    }
    final Class<?> superClass = clazz.getSuperclass();
    if ((superClass == null) || (superClass == Object.class)) {
        return null;
    }
    return findAnnotation(superClass, annotationType);
}

From source file:com.sworddance.util.WeakProxy.java

@SuppressWarnings("unchecked")
public static <T> T newProxyInstance(final Object referent, Callable<T> restoreCallable,
        Class<?>... interfaces) {
    if (referent == null && restoreCallable == null) {
        return null;
    } else {//  w w  w.ja  v a2  s . c o m
        Class<?> clazz = getFirst(interfaces);
        T actualReferent = (T) getActual(referent);
        if (clazz == null) {
            if (actualReferent == null) {
                actualReferent = invokeCallable(restoreCallable);
            }
            ApplicationIllegalArgumentException.notNull(actualReferent,
                    "referent must be not null if there are no listed classes");
            clazz = actualReferent.getClass();
            interfaces = clazz.getInterfaces();
        }
        Reference<T> objectRef = newWeakReference(actualReferent);
        ProxyInvocationHandler<T> invocationHandler = new ProxyInvocationHandler<T>(objectRef, restoreCallable,
                interfaces);
        T t = invocationHandler.newProxyInstance(clazz.getClassLoader());
        return t;
    }
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static int findIIDistance(Class<?> lower, Class<?> upper) {
    if (!upper.isInterface() || !lower.isInterface()) {
        throw new IllegalArgumentException(
                String.format("Invalid input class : cannot be interfaces. upper=%s lower=%s", upper, lower));
    }/*w  ww  .j a v a  2 s. c o  m*/

    if (lower == upper) {
        return 0;
    }
    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    int distance = -1;
    Class<?>[] interfaces = lower.getInterfaces();
    for (Class<?> anInterface : interfaces) {
        int tmp = findIIDistance(anInterface, upper);
        if (tmp >= 0) {
            tmp++;
            distance = distance < 0 ? tmp : Math.min(distance, tmp);
        }

        if (distance == 0) {
            //cannot do better
            break;
        }
    }

    return distance;
}

From source file:net.ymate.platform.commons.util.ClassUtils.java

/**
 * @param clazz /*from  w  ww  .j  a  va  2s . co  m*/
 * @param interfaceClass ?
 * @return  clazz?interfaceClass?
 */
public static boolean isInterfaceOf(Class<?> clazz, Class<?> interfaceClass) {
    boolean _flag = false;
    do {
        for (Class<?> cc : clazz.getInterfaces()) {
            if (cc.equals(interfaceClass)) {
                _flag = true;
            }
        }
        clazz = clazz.getSuperclass();
    } while (!_flag && (clazz != null && clazz != Object.class));
    return _flag;
}

From source file:android.bus.EventBus.java

/** Recurses through super interfaces. */
static void addInterfaces(List<Class<?>> eventTypes, Class<?>[] interfaces) {
    for (Class<?> interfaceClass : interfaces) {
        if (!eventTypes.contains(interfaceClass)) {
            eventTypes.add(interfaceClass);
            addInterfaces(eventTypes, interfaceClass.getInterfaces());
        }/*from   w  w w .  ja v  a  2  s  .  c  o m*/
    }
}