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:GenericsUtil.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> clazz) {
    if (clazz == null) {
        return Collections.emptyMap();
    }// w w  w  .  j  a v  a 2  s .  co m
    final Map<TypeVariable<?>, Type> map = new LinkedHashMap<TypeVariable<?>, Type>();
    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);
    }
    return map;
}

From source file:org.kuali.rice.krad.uif.util.CloneUtils.java

protected static List<Class<?>> getClassHierarchy(Class<?> c, boolean includeInterfaces) {
    List<Class<?>> classes = new ArrayList<Class<?>>();
    while (c != Object.class) {
        classes.add(c);//  w w w . j  av  a 2 s.  c o m
        if (includeInterfaces) {
            Class<?>[] interfaces = c.getInterfaces();
            for (Class<?> i : interfaces) {
                classes.add(i);
            }
        }
        c = c.getSuperclass();
        if (c == null) {
            break;
        }
    }

    return classes;
}

From source file:Util.java

/**
 * Append Class Info/*  w  w w.  j av  a2 s  . c  om*/
 * 
 * @param buffer
 *          the buffer to append to
 * @param clazz
 *          the class to describe
 */
protected static void appendClassInfo(StringBuffer buffer, Class clazz) {
    buffer.append("[class=").append(clazz.getName());
    buffer.append(" classloader=").append(clazz.getClassLoader());
    buffer.append(" interfaces={");
    Class[] interfaces = clazz.getInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        if (i > 0)
            buffer.append(", ");
        buffer.append("interface=").append(interfaces[i].getName());
        buffer.append(" classloader=").append(interfaces[i].getClassLoader());
    }
    buffer.append("}]");
}

From source file:Classes.java

/**
 * Describe the class//from w w  w  .ja v  a2s.  com
 * 
 * @param buffer
 *          the string buffer
 * @param clazz
 *          the clazz
 */
public static void describe(StringBuffer buffer, Class clazz) {
    if (clazz == null)
        buffer.append("**null**");
    else {
        buffer.append("{class=").append(clazz.getName());
        Class[] intfs = clazz.getInterfaces();
        if (intfs.length > 0) {
            buffer.append(" intfs=");
            for (int i = 0; i < intfs.length; ++i) {
                buffer.append(intfs[i].getName());
                if (i < intfs.length - 1)
                    buffer.append(", ");
            }
        }
        buffer.append("}");
    }
}

From source file:org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.java

private static Method extractMethodInheritanceChain(Class type, Method m) {
    if (m == null || Modifier.isPublic(type.getModifiers())) {
        return m;
    }// w  ww .ja  va2 s  .c  o  m
    Class[] inf = type.getInterfaces();
    Method mp;
    for (Class<?> iface : inf) {
        try {
            mp = iface.getMethod(m.getName(), m.getParameterTypes());
            mp = extractMethodInheritanceChain(mp.getDeclaringClass(), mp);
            if (mp != null) {
                return mp;
            }
        } catch (NoSuchMethodException e) {
            // do nothing
        }
    }
    Class<?> sup = type.getSuperclass();
    if (sup != null) {
        try {
            mp = sup.getMethod(m.getName(), m.getParameterTypes());
            mp = extractMethodInheritanceChain(mp.getDeclaringClass(), mp);
            if (mp != null) {
                return mp;
            }
        } catch (NoSuchMethodException e) {
            // do nothing
        }
    }
    return null;
}

From source file:org.gradle.internal.reflect.JavaPropertyReflectionUtil.java

private static <A extends Annotation> A getAnnotation(Class<?> type, Class<A> annotationType,
        boolean checkType) {
    A annotation;//w  w w  .j a v a2  s. com
    if (checkType) {
        annotation = type.getAnnotation(annotationType);
        if (annotation != null) {
            return annotation;
        }
    }

    if (annotationType.getAnnotation(Inherited.class) != null) {
        for (Class<?> anInterface : type.getInterfaces()) {
            annotation = getAnnotation(anInterface, annotationType, true);
            if (annotation != null) {
                return annotation;
            }
        }
    }

    if (type.isInterface() || type.equals(Object.class)) {
        return null;
    } else {
        return getAnnotation(type.getSuperclass(), annotationType, false);
    }
}

From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java

/**
 * Get a single {@link java.lang.annotation.Annotation} of {@code annotationType} from the supplied {@link java.lang.reflect.Method},
 * traversing its super methods if no annotation can be found on the given method itself.
 * <p>Annotations on methods are not inherited by default, so we need to handle this explicitly.
 * @param method the method to look for annotations on
 * @param annotationType the annotation class to look for
 * @return the annotation found, or {@code null} if none found
 *//*from w w  w.  ja  va2 s . c  o m*/
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
    A annotation = getAnnotation(method, annotationType);
    Class<?> clazz = method.getDeclaringClass();
    if (annotation == null) {
        annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
    }
    while (annotation == null) {
        clazz = clazz.getSuperclass();
        if (clazz == null || clazz.equals(Object.class)) {
            break;
        }
        try {
            Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
            annotation = getAnnotation(equivalentMethod, annotationType);
        } catch (NoSuchMethodException ex) {
            // No equivalent method found
        }
        if (annotation == null) {
            annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
        }
    }
    return annotation;
}

From source file:cn.fql.utility.ClassUtility.java

/**
 * Return is one class implements a class or extends a class
 *
 * @param currClass  current class//www .  ja  v a  2s .  c o  m
 * @param superClass the super class or interface
 * @return the result
 */
public static boolean isRelationClass(Class currClass, Class superClass) {
    boolean result = false;
    if (currClass != null) {
        if (currClass.equals(superClass)) {
            result = true;
        } else {
            // find in interface
            Class[] interfaces = currClass.getInterfaces();
            for (int i = 0; i < interfaces.length; i++) {
                Class currInterface = interfaces[i];
                if (currInterface.equals(superClass)) {
                    result = true;
                }
            }

            if (!result) {
                //find in superclass
                if (currClass.equals(Object.class)) {
                    result = false;
                } else {
                    List superClasses = ClassUtils.getAllSuperclasses(currClass);
                    if (!CollectionUtils.isEmpty(superClasses)) {
                        Class currSuperClass = (Class) superClasses.get(0);
                        if (currSuperClass == null) {
                            System.out.println("Super Class is null");
                            result = false;
                        } else {
                            result = isRelationClass(currSuperClass, superClass);
                        }
                    }
                }
            }
        }
    }
    return result;
}

From source file:com.proofpoint.jaxrs.JaxrsModule.java

private static boolean isJaxRsType(Class<?> type) {
    if (type == null) {
        return false;
    }/*from w w w .j  a  v  a 2  s  .  com*/

    if (type.isAnnotationPresent(Provider.class)) {
        return true;
    } else if (type.isAnnotationPresent(Path.class)) {
        return true;
    }
    if (isJaxRsType(type.getSuperclass())) {
        return true;
    }
    for (Class<?> typeInterface : type.getInterfaces()) {
        if (isJaxRsType(typeInterface)) {
            return true;
        }
    }

    return false;
}

From source file:com.mawujun.utils.AnnotationUtils.java

/**
 * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method},
 * traversing its super methods if no annotation can be found on the given method itself.
 * <p>Annotations on methods are not inherited by default, so we need to handle this explicitly.
 * @param method the method to look for annotations on
 * @param annotationType the annotation class to look for
 * @return the annotation found, or {@code null} if none found
 *//*from   ww w  .ja  v a2s.  c o m*/
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
    A annotation = getAnnotation(method, annotationType);
    Class<?> cl = method.getDeclaringClass();
    if (annotation == null) {
        annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
    }
    while (annotation == null) {
        cl = cl.getSuperclass();
        if (cl == null || cl == Object.class) {
            break;
        }
        try {
            Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            annotation = getAnnotation(equivalentMethod, annotationType);
        } catch (NoSuchMethodException ex) {
            // No equivalent method found
        }
        if (annotation == null) {
            annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
        }
    }
    return annotation;
}