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:it.unibas.spicy.persistence.object.ClassUtility.java

private static boolean findInterface(Class currentClass, Class currentInterface) {
    Class[] interfaces = currentClass.getInterfaces();
    for (Class class1 : interfaces) {
        if (class1.equals(currentInterface)) {
            return true;
        }/*from   ww  w. j a  v a 2s.  c  o m*/
    }
    return false;
}

From source file:org.fusesource.meshkeeper.distribution.remoting.AbstractRemotingClient.java

private static void collectDistributableInterfaces(Class<?> clazz, Set<Class<?>> rc) throws Exception {
    for (Class<?> interf : clazz.getInterfaces()) {
        if (Distributable.class.isAssignableFrom(interf)) {
            validateInterface(interf);//from  www .ja v a  2 s  .c  om
            rc.add(interf);
        }
    }

    // Also slowOnewayOperations interfaces in the super classes...
    if (clazz.getSuperclass() != null) {
        collectDistributableInterfaces(clazz.getSuperclass(), rc);
    }
}

From source file:Main.java

/**
 * Get the interfaces for the specified class.
 *
 * @param cls  the class to look up, may be {@code null}
 * @param interfacesFound the {@code Set} of interfaces for the class
 *//*from   w w w . j a  va2  s . c  om*/
private static void getAllInterfaces(Class<?> cls, final HashSet<Class<?>> interfacesFound) {
    while (cls != null) {
        final Class<?>[] interfaces = cls.getInterfaces();

        for (final Class<?> i : interfaces) {
            if (interfacesFound.add(i)) {
                getAllInterfaces(i, interfacesFound);
            }
        }

        cls = cls.getSuperclass();
    }
}

From source file:com.jeroensteenbeeke.hyperion.events.DefaultEventDispatcher.java

@SuppressWarnings("unchecked")
static Class<? extends Event<?>> getEventClass(Class<?> handlerClass) {

    for (Class<?> i : handlerClass.getInterfaces()) {
        if (EventHandler.class.equals(i)) {
            for (Type t : handlerClass.getGenericInterfaces()) {
                if (t instanceof ParameterizedType) {
                    ParameterizedType pt = (ParameterizedType) t;
                    if (EventHandler.class.equals(pt.getRawType())) {

                        return (Class<? extends Event<?>>) pt.getActualTypeArguments()[0];

                    }/*from  ww  w  . j av a 2  s.  c  o m*/
                }
            }
        } else if (EventHandler.class.isAssignableFrom(i)) {
            return getEventClass((Class<? extends EventHandler<?>>) i);
        }
    }

    if (EventHandler.class.isAssignableFrom(handlerClass.getSuperclass())) {
        return getEventClass((Class<?>) handlerClass.getSuperclass());
    }

    return null;

}

From source file:Main.java

public static Set<Class<?>> getMinimalImplementedInterfaceNames(Class<?> klass) {
    Set<Class<?>> interfaces = new HashSet<Class<?>>();
    while (klass != null) {
        Class<?>[] localInterfaces = klass.getInterfaces();
        for (Class<?> intf : localInterfaces) {
            boolean subsumed = false;
            for (Class<?> i : new ArrayList<Class<?>>(interfaces)) {
                if (intf.isAssignableFrom(i)) {
                    subsumed = true;//from www  .  ja v  a2 s  .c o m
                    break;
                } else if (i.isAssignableFrom(intf)) {
                    interfaces.remove(i);
                }
            }
            if (subsumed) {
                continue;
            }
            interfaces.add(intf);
        }
        klass = klass.getSuperclass();
    }
    return interfaces;
}

From source file:Main.java

/**
 * Check out if it is a super interface of child
 * @param child child class type/* w  w w.  j  a  v  a 2 s .co m*/
 * @param sup the super interface class name
 * @return true if it is super interface or itself
 */
public static boolean isSuperInterface(Class child, String sup) {
    if (child == null)
        return false;
    if (child.getCanonicalName().equals(sup))
        return true;

    Class[] interfaces = child.getInterfaces();
    for (Class in : interfaces) {
        if (in.getCanonicalName().equals(sup)) {
            return true;
        }
    }
    return false;
}

From source file:de.xwic.appkit.core.model.EntityModelFactory.java

/**
 * Creates a new entity model based upon the specified entity.
 * @param entity//from w  ww  . j a va  2 s .  co  m
 * @return
 * @throws EntityModelException 
 */
@SuppressWarnings("unchecked")
public static IEntityModel createModel(IEntity entity) {

    // create model and InvocationHandler
    Set<Class<?>> interfacesSet = new HashSet<Class<?>>();
    Class<? extends IEntity> clazz = entity.getClass();
    interfacesSet.add(IEntityModel.class);

    // recursivly add all interfaces
    Class<?> c = clazz;
    while (c != null) {
        Class<?>[] orgInterfaces = c.getInterfaces();
        for (int i = 0; i < orgInterfaces.length; i++) {
            interfacesSet.add(orgInterfaces[i]);
        }
        c = c.getSuperclass();
    }
    Class[] interfaces = new Class[interfacesSet.size()];
    int idx = 0;
    for (Iterator<Class<?>> it = interfacesSet.iterator(); it.hasNext();) {
        interfaces[idx++] = it.next();
    }

    EntityModelInvocationHandler ih = new EntityModelInvocationHandler(entity);
    return (IEntityModel) Proxy.newProxyInstance(classLoader, interfaces, ih);

}

From source file:org.kuali.rice.core.api.util.EqualsAndHashCodeUtils.java

/**
 * This method provides an equals comparison of two objects by evaluating the results of compareTo across specified
 * internal fields of the class of the two objects being compared.
 * <p/>//w  w w.  ja  v a  2  s  .  c o m
 * This method should be used where evaluating equality on fields of two instances of type T using .equals() yields
 * false, but for the purposes of determining equality of the two instances of type T, should be true.  An example
 * is where a class has internal fields of type Calendar that need equality determined using only its time value
 * and not other internal fields of Calendar.
 *
 * @param o1         The first object used in an equality operation using compareTo
 * @param o2         The second object used in an equality operation using compareTo
 * @param fieldNames All field names within type T that should be determined equal or not using compareTo
 * @param <T>        Type of both o1 and o2 parameters.  Guarantees both o1 and o2 are the same reference type.
 * @return true if (o1.field.compareTo(o2.field) == 0) is true for all passed in fieldNames.  Otherwise false
 *         is returned.  False is also returned if any fields specified in fieldNames are not of type Comparable or if one
 *         (but not both) of the passed in objects are null references.
 */
public static <T> boolean equalsUsingCompareToOnFields(T o1, T o2, String... fieldNames) {
    if (o1 == o2) {
        return true;
    }
    if (o1 == null || o2 == null) {
        return false;
    }

    boolean isEqual = true;
    Class<?> targetClass = o1.getClass();
    try {
        for (String fieldName : fieldNames) {
            Field field = targetClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            Class<?> fieldClass = field.getType();

            if (ArrayUtils.contains(fieldClass.getInterfaces(), Comparable.class)) {
                @SuppressWarnings("unchecked")
                Comparable<Object> c1 = (Comparable<Object>) field.get(o1);
                @SuppressWarnings("unchecked")
                Comparable<Object> c2 = (Comparable<Object>) field.get(o2);
                if (c1 == c2) {
                    continue;
                }
                if (c1 == null || c2 == null) {
                    isEqual = false;
                } else {
                    isEqual = (c1.compareTo(c2) == 0);
                }
            } else {
                isEqual = false;
            }

            if (!isEqual) {
                break;
            }
        }

        return isEqual;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.netsteadfast.greenstep.util.ManualDataSourceFactory.java

private static boolean checkDataSourceClass(Class<?> clazz) throws Exception {
    if (null == clazz) {
        return false;
    }/*  w  ww. j  ava 2  s  . com*/
    boolean isDataSource = false;
    Class<?>[] ifs = clazz.getInterfaces();
    for (int i = 0; ifs != null && i < ifs.length; i++) {
        if (ifs[i].getSimpleName().equals("DataSource")) {
            isDataSource = true;
        }
    }
    return isDataSource;
}

From source file:org.polymap.core.runtime.WeakListener.java

/**
 * Creates a proxy for the given listener. The proxy forwards all method calls to the
 * delegate. But it holds just a {@link WeakReference} to the delegate, so that the
 * reference in the {@link ListenerList} does not prevent the listener from being
 * reclaimed by the GC./*from  www.  j  a va 2  s . c  o  m*/
 * <p/>
 * The returned proxy always implements {@link ListenerReference}.
 * 
 * @param delegate
 * @return A proxy that implements all interfaces from the delegate.
 */
public static <T> T forListener(T delegate) {
    assert delegate != null;
    log.debug("forListener(): " + delegate.getClass().getName());

    // find all interfaces
    Set<Class> interfaces = new HashSet();
    Class<? extends Object> cl = delegate.getClass();
    while (cl != Object.class) {
        Class[] ifs = cl.getInterfaces();
        for (int i = 0; i < ifs.length; i++) {
            //log.info( "    " + ifs[i] );
            interfaces.add(ifs[i]);
        }
        cl = cl.getSuperclass();
    }
    interfaces.add(ListenerReference.class);

    // build the proxy
    Object proxy = Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
            interfaces.toArray(new Class[interfaces.size()]), new WeakListener(delegate));
    return (T) proxy;
}