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.apache.wookie.feature.FeatureLoader.java

/**
 * Validates a feature for the supplied parameters, or throws
 * an exception if the feature specified is not a valid.
 * @param name the name of the feature, which must be a valid IRI
 * @param klass the Class name of the feature, which must implement the IFeature interface
 */// w w w  .  j a  va 2s .co  m
@SuppressWarnings("unchecked")
public static void validateFeature(String name, String klass) throws Exception {
    // Are required parameters missing?
    if (name == null || klass == null) {
        throw new Exception("Invalid feature");
    }
    // Does the class exist?
    Class theClass;
    try {
        theClass = Class.forName(klass);
    } catch (Exception e) {
        throw new Exception("Invalid feature: class not found");
    }
    // Does the class implement IFeature?
    boolean implementsFeature = false;
    Class[] interfaces = theClass.getInterfaces();
    if (interfaces != null) {
        if (interfaces.length > 0) {
            if (interfaces[0].getName().equals("org.apache.wookie.feature.IFeature"))
                implementsFeature = true;
        }
    }
    if (!implementsFeature)
        throw new Exception("Invalid feature: class is not a Feature class");

    // Does the feature name match that in the class?
    if (!((IFeature) theClass.newInstance()).getName().equals(name))
        throw new Exception("Invalid feature: feature name supplied and name in the class do not match");
    ;

    // Is the feature name a valid IRI?
    if (!IRIValidator.isValidIRI(name)) {
        throw new Exception("Invalid feature: name is not a valid IRI");
    }
}

From source file:org.nebulaframework.core.job.archive.GridArchive.java

/**
 * Detects whether a given {@code Class} implements
 * {@code GridJob interface}, using Reflection API.
 * //ww w . j a  v  a  2 s. co  m
 * @param cls
 *            {@code Class} to be checked
 * @return if {@code GridJob} class, {@code true}, otherwise {@code false}
 */
protected static boolean isGridJobClass(Class<?> cls) {

    // Get all interfaces, and process each
    for (Class<?> iface : cls.getInterfaces()) {
        // If class implements GridJob interfaces
        if (isGridJobInterface(iface)) {
            log.debug("[GridArchive] Found GridJob Class " + cls.getName());
            return true;
        }
    }
    return false;
}

From source file:org.lenskit.util.TypeUtils.java

/**
 * Return the supertype closure of a type (the type and all its transitive
 * supertypes).//from ww  w . j a  v a2s  . c  o  m
 *
 * @param type The type.
 * @return All supertypes of the type, including the type itself.
 */
public static Set<Class<?>> typeClosure(Class<?> type) {
    if (type == null) {
        return Collections.emptySet();
    }

    Set<Class<?>> supertypes = new HashSet<>();
    supertypes.add(type);
    supertypes.addAll(typeClosure(type.getSuperclass()));
    for (Class<?> iface : type.getInterfaces()) {
        supertypes.addAll(typeClosure(iface));
    }

    return supertypes;
}

From source file:edu.mayo.cts2.framework.webapp.service.ServiceUtils.java

/**
 * Find annotations./*from  w  ww  .j a v a2s  .  c  om*/
 *
 * @param <A> the generic type
 * @param clazz the clazz
 * @param annotationType the annotation type
 * @return the collection
 */
public static <A extends Annotation> Collection<A> findAnnotations(Class<?> clazz, Class<A> annotationType) {
    Collection<A> returnCollection = new HashSet<A>();

    Assert.notNull(clazz, "Class must not be null");
    A annotation = clazz.getAnnotation(annotationType);
    if (annotation != null) {
        returnCollection.add(annotation);
    }

    for (Class<?> ifc : clazz.getInterfaces()) {
        Collection<A> annotations = findAnnotations(ifc, annotationType);
        if (annotations != null) {
            returnCollection.addAll(annotations);
        }
    }
    if (!Annotation.class.isAssignableFrom(clazz)) {
        for (Annotation ann : clazz.getAnnotations()) {
            Collection<A> annotations = findAnnotations(ann.annotationType(), annotationType);
            if (annotations != null) {
                returnCollection.addAll(annotations);
            }
        }
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass == null || superClass == Object.class) {
        return returnCollection;
    }

    returnCollection.addAll(findAnnotations(superClass, annotationType));

    return returnCollection;
}

From source file:org.red5.server.api.persistence.PersistenceUtils.java

/**
 * Returns persistence store object class constructor
 * /*from   ww w.  jav  a2s  .c o  m*/
 * @param theClass
 *            Persistence store class
 * @param interfaces
 *            Interfaces that are being implemented by persistence store object class
 * @return Constructor
 * @throws Exception
 */
private static Constructor<?> getPersistenceStoreConstructor(Class<?> theClass, Class<?>[] interfaces)
        throws Exception {
    Constructor<?> constructor = null;
    for (Class<?> interfaceClass : interfaces) {
        try {
            constructor = theClass.getConstructor(new Class[] { interfaceClass });
        } catch (NoSuchMethodException err) {
            // Ignore this error
        }
        if (constructor != null) {
            break;
        }
        constructor = getPersistenceStoreConstructor(theClass, interfaceClass.getInterfaces());
        if (constructor != null) {
            break;
        }
    }
    return constructor;
}

From source file:au.com.dw.testdatacapturej.reflection.util.ReflectionUtil.java

/**
 * Check if a class has a setter method with the parameter signature for the interface that may be implemented by a field.
 * e.g. instead of 'class.setField(class type)', would look for 'class.setField(interface)'
 *
 * @param clazz//from w ww .j  a  v a 2 s  .com
 * @param methodName
 * @param implementationClass
 * @return
 */
private static boolean hasSetterMethodForInterfaces(Class<?> clazz, String methodName,
        Class<?> implementationClass) {
    boolean hasSetterMethodForInterfaces = false;

    Class<?>[] theInterfaces = implementationClass.getInterfaces();
    for (int i = 0; i < theInterfaces.length; i++) {
        hasSetterMethodForInterfaces = hasSetterMethodForParameterClass(clazz, methodName, theInterfaces[i]);
        if (hasSetterMethodForInterfaces) {
            break;
        } else {
            // try to find setter in super-interfaces, note that getSuperClass() doesn't return super interfaces
            // due to possible multiple inheritance for interfaces
            hasSetterMethodForInterfaces = hasSetterMethodForInterfaces(clazz, methodName, theInterfaces[i]);
            if (hasSetterMethodForInterfaces) {
                break;
            }
        }
    }
    return hasSetterMethodForInterfaces;
}

From source file:ReflectUtils.java

private static synchronized Method findMethod(Object obj, String property, Object value) {
    Method m = null;/*from w  ww.  ja  va 2  s . c  o  m*/
    Class<?> theClass = obj.getClass();
    String setter = String.format("set%C%s", property.charAt(0), property.substring(1));
    Class paramType = value.getClass();
    while (paramType != null) {
        try {
            m = theClass.getMethod(setter, paramType);
            return m;
        } catch (NoSuchMethodException ex) {
            // try on the interfaces of this class
            for (Class iface : paramType.getInterfaces()) {
                try {
                    m = theClass.getMethod(setter, iface);
                    return m;
                } catch (NoSuchMethodException ex1) {
                }
            }
            paramType = paramType.getSuperclass();
        }
    }
    return m;
}

From source file:com.googlecode.struts2gwtplugin.interceptor.GWTServlet.java

/**
 * Find the invoked method on either the specified interface or any super.
 */// w w  w  .  j av  a2 s. c  o  m
private static Method findInterfaceMethod(Class<?> intf, String methodName, Class<?>[] paramTypes,
        boolean includeInherited) {
    try {
        return intf.getDeclaredMethod(methodName, paramTypes);
    } catch (NoSuchMethodException e) {
        if (includeInherited) {
            Class<?>[] superintfs = intf.getInterfaces();
            for (int i = 0; i < superintfs.length; i++) {
                Method method = findInterfaceMethod(superintfs[i], methodName, paramTypes, true);
                if (method != null) {
                    return method;
                }
            }
        }

        return null;
    }
}

From source file:com.lucidtechnics.blackboard.util.Utility.java

public static ArrayList getAllTypes(ArrayList _searchDomainList, ArrayList _allTypesList) {
    for (int i = 0; i < _searchDomainList.size(); i++) {
        Class searchClass = (Class) _searchDomainList.get(i);
        _searchDomainList.remove(searchClass);

        Class superClass = searchClass.getSuperclass();

        if (superClass != null && (_allTypesList.contains(superClass) == false)) {
            _searchDomainList.add(superClass);
            _allTypesList.add(superClass);
        }//w  w  w .j  a  v a 2  s  .com

        Class[] interfaceArray = searchClass.getInterfaces();

        if (interfaceArray != null) {
            for (int j = 0; j < interfaceArray.length; j++) {
                if (interfaceArray[j] != null && (_allTypesList.contains(interfaceArray[j]) == false)) {
                    _searchDomainList.add(interfaceArray[j]);
                    _allTypesList.add(interfaceArray[j]);
                }
            }
        }
    }

    if (_searchDomainList.isEmpty() == false) {
        _allTypesList = getAllTypes(_searchDomainList, _allTypesList);
    }

    return _allTypesList;
}

From source file:org.apache.sling.scripting.sightly.impl.compiler.CompileTimeObjectModel.java

private static Method extractMethodInheritanceChain(Class type, Method m) {
    if (m == null || Modifier.isPublic(type.getModifiers())) {
        return m;
    }/*from w  ww .j a  va  2 s .  co m*/
    Class[] iFaces = type.getInterfaces();
    Method mp;
    for (Class<?> iFace : iFaces) {
        mp = getClassMethod(iFace, m);
        if (mp != null) {
            return mp;
        }
    }
    return getClassMethod(type.getSuperclass(), m);
}