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.seedstack.seed.core.utils.BaseClassSpecifications.java

/**
 * Checks if the candidate has interface.
 *
 * @return the specification//www. j av a 2  s .c  o m
 */
public static Specification<Class<?>> classHasSuperInterfaces() {
    return new AbstractSpecification<Class<?>>() {
        @Override
        public boolean isSatisfiedBy(Class<?> candidate) {
            Class<?>[] interfaces = {};
            if (candidate != null) {
                interfaces = candidate.getInterfaces();
            }
            return candidate != null && interfaces != null && interfaces.length > 0;
        }
    };
}

From source file:org.apache.marmotta.commons.sesame.facading.util.FacadeUtils.java

public static <C extends Annotation, D> C getFacadeAnnotation(Class<D> clazz, Class<C> annotation) {
    if (clazz.isAnnotationPresent(annotation)) {
        return clazz.getAnnotation(annotation);
    } else {/*from w ww .  j av a  2 s  .co  m*/
        for (final Class<?> iface : clazz.getInterfaces()) {
            if (iface.isAnnotationPresent(annotation)) {
                return iface.getAnnotation(annotation);
            }
        }
        if (clazz.getSuperclass() != null) {
            return getFacadeAnnotation(clazz.getSuperclass(), annotation);
        }
        return null;
    }

}

From source file:unquietcode.tools.beanmachine.AnnotationUtils.java

/**
 * Find a single {@link java.lang.annotation.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>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./*from w ww .  j a  v a2s .co m*/
 * @param clazz the class to look for annotations on
 * @param annotationType the annotation class to look for
 * @return the annotation found, or <code>null</code> if none found
 */
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
    Assert.notNull(clazz, "Class must not be null");
    A annotation = clazz.getAnnotation(annotationType);
    if (annotation != null) {
        return annotation;
    }
    for (Class<?> ifc : clazz.getInterfaces()) {
        annotation = findAnnotation(ifc, annotationType);
        if (annotation != null) {
            return annotation;
        }
    }
    if (!Annotation.class.isAssignableFrom(clazz)) {
        for (Annotation ann : clazz.getAnnotations()) {
            annotation = findAnnotation(ann.annotationType(), annotationType);
            if (annotation != null) {
                return annotation;
            }
        }
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass == null || superClass == Object.class) {
        return null;
    }
    return findAnnotation(superClass, annotationType);
}

From source file:org.dozer.util.MappingUtils.java

@SuppressWarnings("unchecked")
public static List<Class<?>> getInterfaceHierarchy(Class<?> srcClass) {
    final List<Class<?>> result = new LinkedList<Class<?>>();
    Class<?> realClass = getRealClass(srcClass);

    final LinkedList<Class> interfacesToProcess = new LinkedList<Class>();

    Class[] interfaces = realClass.getInterfaces();

    interfacesToProcess.addAll(Arrays.asList(interfaces));

    while (!interfacesToProcess.isEmpty()) {
        Class<?> iface = interfacesToProcess.remove();
        if (!result.contains(iface)) {
            result.add(iface);/* w ww.ja va2 s .  c  om*/
            for (Class subiface : iface.getInterfaces()) {
                // if we haven't processed this interface yet then add it to be processed
                if (!result.contains(subiface)) {
                    interfacesToProcess.add(subiface);
                }
            }
        }
    }

    return result;

}

From source file:com.github.dozermapper.core.util.MappingUtils.java

@SuppressWarnings("unchecked")
public static List<Class<?>> getInterfaceHierarchy(Class<?> srcClass, BeanContainer beanContainer) {
    final List<Class<?>> result = new LinkedList<>();
    Class<?> realClass = getRealClass(srcClass, beanContainer);

    final LinkedList<Class> interfacesToProcess = new LinkedList<>();

    Class[] interfaces = realClass.getInterfaces();

    interfacesToProcess.addAll(Arrays.asList(interfaces));

    while (!interfacesToProcess.isEmpty()) {
        Class<?> iface = interfacesToProcess.remove();
        if (!result.contains(iface)) {
            result.add(iface);//  w  w w .  jav a  2  s .c  o  m
            for (Class subiface : iface.getInterfaces()) {
                // if we haven't processed this interface yet then add it to be processed
                if (!result.contains(subiface)) {
                    interfacesToProcess.add(subiface);
                }
            }
        }
    }

    return result;

}

From source file:org.fuusio.api.flow.AbstractFlow.java

protected static Class<? extends View> getClass(final View view) {

    final Class viewClass = view.getClass();

    if (viewClass.isInterface() && View.class.isAssignableFrom(viewClass)) {
        return viewClass;
    }//  w  w w .j  a v  a 2  s .c o m

    final Class[] interfaceClasses = viewClass.getInterfaces();

    for (int i = 0; i < interfaceClasses.length; i++) {
        if (View.class.isAssignableFrom(interfaceClasses[i])) {
            return interfaceClasses[i];
        }
    }
    return null;
}

From source file:com.smart.utils.ReflectionUtils.java

private static List<Class> getInterfaceOfClass(Class clz) {
    Class[] clazzs = clz.getInterfaces();
    List<Class> classFamilyTree = Arrays.asList(clazzs);
    return new LinkedList(classFamilyTree);
}

From source file:org.seedstack.seed.core.utils.BaseClassSpecifications.java

/**
 * @param interfaceClass the requested interface
 * @return a specification which check if one candidate ancestor implements the given interface
 *//*from  ww  w . ja v  a2s . c  o m*/
public static Specification<Class<?>> ancestorImplements(final Class<?> interfaceClass) {
    return new AbstractSpecification<Class<?>>() {

        @Override
        public boolean isSatisfiedBy(Class<?> candidate) {
            if (candidate == null) {
                return false;
            }

            boolean result = false;

            Class<?>[] allInterfacesAndClasses = getAllInterfacesAndClasses(candidate);

            for (Class<?> clazz : allInterfacesAndClasses) {
                if (!clazz.isInterface()) {
                    for (Class<?> i : clazz.getInterfaces()) {
                        if (i.equals(interfaceClass)) {
                            result = true;
                            break;
                        }
                    }
                }
            }

            return result;
        }

    };
}

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

/**
 * <p>//  www . ja v  a2  s.  c  om
 * Returns 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 <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, final String methodName,
        final Class<?>... parameterTypes) {
    Method method = null;

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

        // Check the implemented interfaces of the parent class
        final 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 (final NoSuchMethodException e) { // NOPMD
                /*
                 * 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:GenericsUtil.java

private static void gatherTypeVariables(final Class<?> clazz, final Type type,
        final Map<TypeVariable<?>, Type> map) {
    if (clazz == null) {
        return;/*from  w w w  .ja v a  2  s. c o  m*/
    }
    gatherTypeVariables(type, map);

    final Class<?> superClass = clazz.getSuperclass();
    final Type superClassType = clazz.getGenericSuperclass();
    if (superClass != null) {
        gatherTypeVariables(superClass, superClassType, map);
    }
    final Class<?>[] interfaces = clazz.getInterfaces();
    final Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
    }
}