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.citytechinc.cq.component.maven.util.ComponentMojoUtil.java

/**
 * Retrieves a List of all classes which are annotated as Transformers and
 * are within the scope of the provided Reflections purview.
 * /*from   w  w  w  .  ja  va2  s .  co  m*/
 * @param classPool
 * @param reflections
 * @return A Map of transformer names to transformers
 * @throws ClassNotFoundException
 * @throws NotFoundException
 * @throws MalformedURLException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public static Map<String, ComponentNameTransformer> getAllTransformers(ClassPool classPool,
        Reflections reflections) throws ClassNotFoundException, NotFoundException, MalformedURLException,
        InstantiationException, IllegalAccessException {
    Map<String, ComponentNameTransformer> transformers = new HashMap<String, ComponentNameTransformer>();

    for (Class<?> c : reflections.getTypesAnnotatedWith(Transformer.class)) {
        if (Arrays.asList(c.getInterfaces()).contains(ComponentNameTransformer.class)) {
            CtClass ctclass = classPool.getCtClass(c.getName());
            Transformer transformer = (Transformer) ctclass.getAnnotation(Transformer.class);
            transformers.put(transformer.value(), (ComponentNameTransformer) c.newInstance());
        }
    }

    return transformers;
}

From source file:org.openspaces.test.client.executor.ExecutorUtils.java

/**
 * Returns an array of all declared interfaces of desired class (super-classes comes an
 * account). If <code>assignableClasses</code> is <code>null</code> all implemented interface
 * will be added to array, otherwise only <b>assignable</b> classes.
 *
 * @param claz The class to get all interfaces.
 **///  w  w  w . j  av a  2  s  . c om
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Class[] getDeclaredInterfaces(Class claz, Class... assignableClasses) {
    ArrayList<Class> interfList = new ArrayList<Class>();

    while (!claz.equals(Object.class)) {
        for (Class cl : claz.getInterfaces()) {
            /* append only assignable interfaces */
            if (assignableClasses.length > 0) {
                for (Class assignClaz : assignableClasses) {
                    if (assignClaz.isAssignableFrom(cl))
                        interfList.add(cl);
                }
            } else
                interfList.add(cl);
        }

        claz = claz.getSuperclass();
    }

    return interfList.toArray(new Class[interfList.size()]);
}

From source file:es.caib.zkib.jxpath.util.ValueUtils.java

/**
 * 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 <code>null</code>.
 *
 * @param clazz 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 Method/*www. j  a v  a2 s  .co  m*/
 */
private static Method getAccessibleMethodFromInterfaceNest(Class clazz, String methodName,
        Class[] parameterTypes) {

    Method method = null;

    // Check the implemented interfaces of the parent class
    Class[] interfaces = clazz.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) { //NOPMD
            //ignore
        }
        if (method != null) {
            break;
        }

        // Recursively check our parent interfaces
        method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes);
        if (method != null) {
            break;
        }
    }

    // Return whatever we have found
    return (method);
}

From source file:ReflectUtil.java

/**
 * <p>Attempts to find an accessible version of the method passed in, where accessible
 * is defined as the method itself being public and the declaring class being public.
 * Mostly useful as a workaround to the situation when
 * {@link PropertyDescriptor#getReadMethod()} and/or
 * {@link java.beans.PropertyDescriptor#getWriteMethod()} returns methods that are not
 * accessible (usually due to public implementations of interface methods in private
 * classes).</p>//from  w  w  w  .  jav a  2s .c  o m
 *
 * <p>Checks the method passed in and if it already meets these criteria it is returned
 * immediately. In general this leads to very little performance overhead</p>
 *
 * <p>If the method does not meet the criteria then the class' interfaces are scanned
 * for a matching method. If one is not found, then the class' superclass hierarchy
 * is searched. Finally, if no matching method can be found the original method is
 * returned.</p>
 *
 * @param m a method that may or may not be accessible
 * @return either an accessible version of the same method, or the method passed in if
 *         an accessible version cannot be found
 */
public static Method findAccessibleMethod(final Method m) {
    // If the passed in method is accessible, then just give it back.
    if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers()))
        return m;
    if (m.isAccessible())
        return m;

    final Class<?> clazz = m.getDeclaringClass();
    final String name = m.getName();
    final Class<?>[] ptypes = m.getParameterTypes();

    // Else, loop through the interfaces for the declaring class, looking for a
    // public version of the method that we can call
    for (Class<?> iface : clazz.getInterfaces()) {
        try {
            Method m2 = iface.getMethod(name, ptypes);
            if (m2.isAccessible())
                return m2;
            if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers()))
                return m2;
        } catch (NoSuchMethodException nsme) {
            /* Not Unexpected. */ }
    }

    // Else loop through the superclasses looking for a public method
    Class<?> c = clazz.getSuperclass();
    while (c != null) {
        try {
            Method m2 = c.getMethod(name, ptypes);
            if (m2.isAccessible())
                return m2;
            if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers()))
                return m2;
        } catch (NoSuchMethodException nsme) {
            /* Not Unexpected. */ }

        c = c.getSuperclass();
    }

    // If we haven't found anything at this point, just give up!
    return m;
}

From source file:com.aol.advertising.qiao.util.ContextUtils.java

/**
 * Supporting one parameter only./*from w ww.  ja  va2  s  .c  om*/
 *
 * @param clazz
 * @param methodName
 * @param paramType
 * @return
 */
private static Method findMethod(Class<?> clazz, String methodName, Class<?> paramType) {
    // trace
    //System.out.println("findMethod> class=" + clazz.getSimpleName()
    //        + ",method=" + methodName + ",param="
    //        + paramType.getSimpleName());
    Method mth = ReflectionUtils.findMethod(clazz, methodName, paramType);
    if (mth == null) {

        // check param's interfaces
        Class<?>[] list = paramType.getInterfaces();
        if (list != null && list.length > 0) {
            for (Class<?> c : list) {
                mth = findMethod(clazz, methodName, c);
                if (mth != null)
                    return mth;
            }
        }

        // check param's super class
        Class<?> param_super = paramType.getSuperclass();
        if (param_super != null) {
            mth = findMethod(clazz, methodName, param_super);
            if (mth != null)
                return mth;
        }

        if (mth == null) {
            // check object's super class
            Class<?> parent_clz = clazz.getSuperclass();
            if (parent_clz != null) {
                mth = findMethod(parent_clz, methodName, paramType);
            }
        }
    }

    return mth;
}

From source file:org.ebayopensource.twin.ElementImpl.java

/** Is c an interface that directly extends i? */
static final boolean isInterfaceExtending(Class<?> c, Class<?> i) {
    if (!c.isInterface())
        return false;
    for (Class<?> d : c.getInterfaces())
        if (i.equals(d))
            return true;
    return false;
}

From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java

public static boolean isResource(Class<?> clazz) {

    if (Modifier.isInterface(clazz.getModifiers()) || Modifier.isAbstract(clazz.getModifiers())) {
        return false;
    }//from w  w w .j  ava 2  s  . c o m

    if (clazz.getAnnotation(Path.class) != null && clazz.getAnnotation(Group.class) != null) {
        return true;
    }

    Class<?> declaringClass = clazz;

    while (!declaringClass.equals(Object.class)) {
        // try a superclass
        Class<?> superclass = declaringClass.getSuperclass();
        if (superclass.getAnnotation(Path.class) != null && clazz.getAnnotation(Group.class) != null) {
            return true;
        }

        // try interfaces
        Class<?>[] interfaces = declaringClass.getInterfaces();
        for (Class<?> interfaceClass : interfaces) {
            if (interfaceClass.getAnnotation(Path.class) != null && clazz.getAnnotation(Group.class) != null) {
                return true;
            }
        }
        declaringClass = declaringClass.getSuperclass();
    }
    return false;
}

From source file:gr.interamerican.bo2.utils.JavaBeanUtils.java

/**
 * Finds the properties of all interfaces implemented by an abstract class
 * and returns them in a {@link HashMap} with
 * the property name as key.//from  w  w w.j  av  a  2s .c  o  m
 * 
 * @param type
 *            the bean
 * @return Returns the property descriptors of a java bean.
 * 
 * @see #getBeansProperties(Class)
 * 
 */
@SuppressWarnings("rawtypes")
public static Map<String, PropertyDescriptor> getBeansPropertiesOfAbstractClass(Class<?> type) {
    List<Class> types = CollectionUtils.addAll(new ArrayList<Class>(), (Class[]) type.getInterfaces());
    types.add(type);
    HashMap<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
    for (Class infc : types) {
        Map<String, PropertyDescriptor> subMap = JavaBeanUtils.getBeansPropertiesMap(infc);
        properties.putAll(subMap);
    }
    return properties;
}

From source file:ShowClass.java

/**
 * Display the modifiers, name, superclass and interfaces of a class or
 * interface. Then go and list all constructors, fields, and methods.
 *///from   www . j a  v a 2  s .  c o m
public static void print_class(Class c) {
    // Print modifiers, type (class or interface), name and superclass.
    if (c.isInterface()) {
        // The modifiers will include the "interface" keyword here...
        System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c));
    } else if (c.getSuperclass() != null) {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends "
                + typename(c.getSuperclass()));
    } else {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c));
    }

    // Print interfaces or super-interfaces of the class or interface.
    Class[] interfaces = c.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
        if (c.isInterface())
            System.out.print(" extends ");
        else
            System.out.print(" implements ");
        for (int i = 0; i < interfaces.length; i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(typename(interfaces[i]));
        }
    }

    System.out.println(" {"); // Begin class member listing.

    // Now look up and display the members of the class.
    System.out.println("  // Constructors");
    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
        // Display constructors.
        print_method_or_constructor(constructors[i]);

    System.out.println("  // Fields");
    Field[] fields = c.getDeclaredFields(); // Look up fields.
    for (int i = 0; i < fields.length; i++)
        // Display them.
        print_field(fields[i]);

    System.out.println("  // Methods");
    Method[] methods = c.getDeclaredMethods(); // Look up methods.
    for (int i = 0; i < methods.length; i++)
        // Display them.
        print_method_or_constructor(methods[i]);

    System.out.println("}"); // End class member listing.
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Collect all super implementing.//from   w w  w  .j  a  v  a 2  s  .c  o m
 *
 * @param <T> the generic type
 * @param clazz the clazz
 * @param implementing the implementing
 * @param list the list
 */
public static <T> void collectAllSuperImplementing(Class<?> clazz, Class<T> implementing, //
        Set<Class<? extends T>> list) {
    if (clazz == null) {
        return;
    }
    if (implementing.isAssignableFrom(clazz) == false) {
        return;
    }
    list.add((Class<? extends T>) clazz);
    collectAllSuperImplementing(clazz.getSuperclass(), implementing, list);
    for (Class<?> ifaces : clazz.getInterfaces()) {
        collectAllSuperImplementing(ifaces, implementing, list);
    }
}