Example usage for java.lang Class isAssignableFrom

List of usage examples for java.lang Class isAssignableFrom

Introduction

In this page you can find the example usage for java.lang Class isAssignableFrom.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isAssignableFrom(Class<?> cls);

Source Link

Document

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Usage

From source file:Main.java

@SuppressWarnings(value = { "unchecked" })
public static <U, F extends U> Collection<F> filterByClass(Collection<U> unfiltered, final Class<F> cls) {
    return (Collection<F>) Collections2.filter(unfiltered, new Predicate<U>() {

        @Override//from www .  j av  a  2 s  .c o  m
        public boolean apply(U superclass) {
            return cls.isAssignableFrom(superclass.getClass());
        }
    });
}

From source file:com.hortonworks.streamline.streams.layout.ConfigFieldValidation.java

public static boolean isSubType(Class clazz, Object value) {
    boolean result = false;
    if (value != null) {
        result = clazz.isAssignableFrom(value.getClass());
    }//from  w  w  w . ja  va2 s.  c  o  m
    return result;
}

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   ww w . j a  va2s . 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:com.crossbusiness.resiliency.aspect.AbstractFallbackAspect.java

static boolean isFallbackableException(Throwable t, Class<? extends Throwable>[] fallbackableExceptions) {
    for (Class<? extends Throwable> throwable : fallbackableExceptions) {
        if (throwable.isAssignableFrom(t.getClass())) {
            return true;
        }/* www .  j  a  v a2 s.co m*/
    }
    return false;
}

From source file:Main.java

private static final boolean callableWith(Class<?>[] formalArgs, List<Class<?>> actualArgs) {
    if (formalArgs.length != actualArgs.size())
        return false;
    int i = 0;//from w w  w  .  j  a v  a 2  s .co  m
    for (final Class<?> argClass : formalArgs) {
        final Class<?> actualArg = actualArgs.get(i);
        // null match everything
        if (actualArg != null && !argClass.isAssignableFrom(actualArg) && argClass != getPrimitive(actualArg))
            return false;
        i++;
    }

    return true;
}

From source file:Main.java

/**
 * Extracts the {@link Element} for the given name from the parent.
 * // ww w  .  java 2 s .c  o  m
 * @param parent
 * @param name
 * @return
 */
public static <T extends Node> T findNode(Node parent, Class<T> type, String name) {
    final NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node child = childNodes.item(i);
        if (type.isAssignableFrom(child.getClass()) && name.equals(child.getNodeName())) {
            return type.cast(child);
        }
    }
    return null;
}

From source file:Main.java

public static <T extends Throwable> T catchException(Class<T> exceptionClass, Runnable behavior) {
    Throwable caught = null;//ww  w  . jav a2  s . c  o m

    try {
        behavior.run();
    } catch (Throwable ex) {
        caught = ex;
    }

    if (caught != null && exceptionClass.isAssignableFrom(caught.getClass()))
        //noinspection unchecked
        return (T) caught;

    return null;
}

From source file:Main.java

/**
 * Determine the common ancestor of the given classes, if any.
 *
 * @param clazz1 the class to introspect
 * @param clazz2 the other class to introspect
 * @return the common ancestor (i.e. common superclass, one interface
 * extending the other), or {@code null} if none found. If any of the
 * given classes is {@code null}, the other class will be returned.
 * @since 3.2.6/*from  w ww  .jav  a 2  s .  c  o m*/
 */
public static Class<?> determineCommonAncestor(Class<?> clazz1, Class<?> clazz2) {
    if (clazz1 == null) {
        return clazz2;
    }
    if (clazz2 == null) {
        return clazz1;
    }
    if (clazz1.isAssignableFrom(clazz2)) {
        return clazz1;
    }
    if (clazz2.isAssignableFrom(clazz1)) {
        return clazz2;
    }
    Class<?> ancestor = clazz1;
    do {
        ancestor = ancestor.getSuperclass();
        if (ancestor == null || Object.class.equals(ancestor)) {
            return null;
        }
    } while (!ancestor.isAssignableFrom(clazz2));
    return ancestor;
}

From source file:TypeUtils.java

/**
 * Check if the right-hand side type may be assigned to the left-hand side
 * type, assuming setting by reflection. Considers primitive wrapper
 * classes as assignable to the corresponding primitive types.
 * @param lhsType the target type//  w ww  .jav  a 2s  .co m
 * @param rhsType the value type that should be assigned to the target type
 * @return if the target type is assignable from the value type
 * @see TypeUtils#isAssignable
 */
public static boolean isAssignable(Class lhsType, Class rhsType) {

    return (lhsType.isAssignableFrom(rhsType) || lhsType.equals(primitiveWrapperTypeMap.get(rhsType)));
}

From source file:com.sonicle.webtop.core.app.util.ClassHelper.java

public static boolean isImplementingInterface(Class clazz, Class interfaceClass) {
    if (clazz == null)
        return false;
    if (interfaceClass == null)
        return false;
    return interfaceClass.isAssignableFrom(clazz);
}