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.paxml.util.ReflectUtils.java

/**
 * Traverse the inheritance tree of a class, including the class itself.
 * /*from   w w  w  . j  a v  a  2s .  c om*/
 * @param <T>
 *            the type to return as traversal result.
 * @param clazz
 *            the class to traverse
 * @param upTillClass
 *            the top level class to stop at
 * @param includeInterfaces
 *            true to traverse interfaces, false not to
 * @param visitor
 *            the visitor to be called on visiting each class on the
 *            inheritance tree. If the onVisit() method returns null, the
 *            traversal will continue, otherwise it will stop and return the
 *            value as overall result.
 * @return the value returned by the visitor's.
 */
public static <T> T traverseInheritance(Class<?> clazz, Class<?> upTillClass, boolean includeInterfaces,
        IClassVisitor<T> visitor) {
    if (!includeInterfaces && clazz.isInterface()) {
        return null;
    }
    if (upTillClass != null && upTillClass.equals(clazz)) {
        return visitor.onVisit(clazz);
    }
    if (Object.class.equals(clazz)) {
        return null;
    }
    T result = visitor.onVisit(clazz);
    if (result != null) {
        return result;
    }
    for (Class<?> intf : clazz.getInterfaces()) {
        result = traverseInheritance(intf, upTillClass, includeInterfaces, visitor);
        if (result != null) {
            return result;
        }
    }
    Class<?> parentClass = clazz.getSuperclass();
    if (parentClass != null) {
        result = traverseInheritance(parentClass, upTillClass, includeInterfaces, visitor);
    }
    return result;
}

From source file:org.apache.cocoon.servletservice.DispatcherServlet.java

private void getInterfaces(Set interfaces, Class clazz) {
    Class[] clazzInterfaces = clazz.getInterfaces();
    for (int i = 0; i < clazzInterfaces.length; i++) {
        //add all interfaces extended by this interface or directly
        //implemented by this class
        getInterfaces(interfaces, clazzInterfaces[i]);
    }// w  w  w .  j a v a 2  s  .  c o  m

    // the superclazz is null if class is instanceof Object, is
    // an interface, a primitive type or void
    Class superclazz = clazz.getSuperclass();
    if (superclazz != null) {
        //add all interfaces of the superclass to the list
        getInterfaces(interfaces, superclazz);
    }

    interfaces.addAll(Arrays.asList(clazzInterfaces));
}

From source file:org.dentaku.services.metadata.validator.ValidatingVisitorBase.java

private ListOrderedSet getVisitMethodsForClass(Class c) {
    ListOrderedSet result = (ListOrderedSet) superclassCache.get(c);
    if (result == null) {
        result = new ListOrderedSet();
        superclassCache.put(c, result);/*from ww  w  .j a  v  a  2s  .c  o  m*/
        Class current = c;
        while (current != null) {
            Class interfaces[] = current.getInterfaces();
            try {
                Method method = getClass().getMethod("visit", new Class[] { current, Object.class });
                if (!methodFilter.contains(method)) {
                    result.add(method);
                }
            } catch (NoSuchMethodException e) {
                // do nothing and loop
            }

            for (int i = 0; i < interfaces.length; i++) {
                Class thisInterface = interfaces[i];
                result.addAll(getVisitMethodsForClass(thisInterface));
            }

            current = current.getSuperclass();
        }
    }
    return result;
}

From source file:com.github.ljtfreitas.restify.http.spring.contract.metadata.reflection.SpringWebJavaTypeMetadata.java

public SpringWebJavaTypeMetadata(Class<?> javaType) {
    isTrue(javaType.isInterface(), "Your type must be a Java interface.");
    isTrue(javaType.getInterfaces().length <= 1, "Only single inheritance is supported.");

    RequestMapping mapping = AnnotatedElementUtils.getMergedAnnotation(javaType, RequestMapping.class);
    if (mapping != null) {
        isTrue(mapping.value().length <= 1, "Only single path is allowed.");
        isTrue(mapping.method().length == 0,
                "You must not set the HTTP method at the class level, only at the method level.");
    }/*from  ww  w.j av a 2  s  . co  m*/

    this.mapping = Optional.ofNullable(mapping).map(m -> new SpringWebRequestMappingMetadata(m));
    this.javaType = javaType;
    this.parent = javaType.getInterfaces().length == 1
            ? new SpringWebJavaTypeMetadata(javaType.getInterfaces()[0])
            : null;
}

From source file:energy.usef.core.workflow.step.WorkflowStepLoader.java

private boolean hasWorkflowStepInterface(Class<?> clazz) {
    return Stream.of(clazz.getInterfaces())
            .anyMatch(interfaceClazz -> interfaceClazz.toString().equals(WorkflowStep.class.toString()));
}

From source file:net.solarnetwork.central.dras.aop.EntityChangeNotificationAspect.java

private String getServiceClassName(JoinPoint jp) {
    Class<?> targetClass = jp.getTarget().getClass();
    Class<?>[] interfaces = targetClass.getInterfaces();
    String className;/*from  w  w  w. j  av  a  2s  .  co  m*/
    if (interfaces != null && interfaces.length > 0) {
        className = interfaces[0].getName();
    } else {
        className = targetClass.getName();
    }
    return className;
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Return all interfaces that the given class implements as Set,
 * including ones implemented by superclasses.
 * <p>If the class itself is an interface, it gets returned as sole interface.
 * @param clazz the class to analyze for interfaces
 * @param classLoader the ClassLoader that the interfaces need to be visible in
 * (may be {@code null} when accepting all declared interfaces)
 * @return all interfaces that the given object implements as Set
 *///from w w  w .  ja  v  a 2  s.  c  o m
public static Set<Class> getAllInterfacesForClassAsSet(Class clazz, ClassLoader classLoader) {
    Assert.notNull(clazz, "Class must not be null");
    Set<Class> interfaces = new LinkedHashSet<Class>();
    while (clazz != null) {
        Class<?>[] ifcs = clazz.getInterfaces();
        for (Class<?> ifc : ifcs) {
            interfaces.add(ifc);
            interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader));
        }
        clazz = clazz.getSuperclass();
    }
    return interfaces;
}

From source file:fr.mtlx.odm.ClassAssistant.java

public boolean isCollection(Type type) {
    Class<?> c = type.getClass();

    return asList(c.getInterfaces()).contains(Collection.class) || c.equals(Collection.class);
}

From source file:org.kametic.specifications.reflect.ClassMethodsAnnotatedWith.java

@SuppressWarnings("unchecked")
Class<?>[] getAllInterfacesAndClasses(Class<?>[] classes) {
    if (0 == classes.length) {
        return classes;
    } else {//from w  ww .  j  a  v  a  2s. c om
        List<Class<?>> extendedClasses = new ArrayList<Class<?>>();
        // all interfaces hierarchy
        for (Class<?> clazz : classes) {
            if (clazz != null) {
                Class<?>[] interfaces = clazz.getInterfaces();
                if (interfaces != null) {
                    extendedClasses.addAll((List<? extends Class<?>>) Arrays.asList(interfaces));
                }
                Class<?> superclass = clazz.getSuperclass();
                if (superclass != null && superclass != Object.class) {
                    extendedClasses.addAll((List<? extends Class<?>>) Arrays.asList(superclass));
                }
            }
        }

        //Class::getInterfaces() gets only interfaces/classes implemented/extended directly by a given class.
        //We need to walk the whole way up the tree.
        return (Class[]) ArrayUtils.addAll(classes,
                getAllInterfacesAndClasses(extendedClasses.toArray(new Class[extendedClasses.size()])));
    }
}

From source file:org.apache.olingo.ext.proxy.context.EntityUUID.java

public EntityUUID(final URI entitySetURI, final Class<?> type, final Object key) {
    this.entitySetURI = entitySetURI;
    this.key = key;
    this.tempKey = (int) (Math.random() * 1000000);

    if (type == null || !Serializable.class.isAssignableFrom(type)) {
        throw new IllegalArgumentException("Invalid Entity type class: " + type);
    }/*  w  w w .j  a v a  2 s. co m*/
    if (this.type == null) {
        for (Class<?> clazz : ClassUtils.hierarchy(type, ClassUtils.Interfaces.INCLUDE)) {
            if (ArrayUtils.contains(clazz.getInterfaces(), EntityType.class)) {
                this.type = clazz;
            }
        }
    }
}