Example usage for java.lang Class isInstance

List of usage examples for java.lang Class isInstance

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isInstance(Object obj);

Source Link

Document

Determines if the specified Object is assignment-compatible with the object represented by this Class .

Usage

From source file:Main.java

public static Component findFirstComponentOfType(Component comp, Class c) {
    if (c.isInstance(comp))
        return comp;

    if (comp instanceof Container) {
        Container container = (Container) comp;
        for (int i = 0; i < container.getComponentCount(); i++) {
            Component comp2 = findFirstComponentOfType(container.getComponent(i), c);
            if (comp2 != null)
                return comp2;
        }/*from  w  w w  .j a  v a 2s .  co m*/
    }
    return null;
}

From source file:Main.java

@Nullable
public static <T> T firstChildOfType(@NonNull View root, @NonNull Class<T> type) {
    if (type.isInstance(root)) {
        return type.cast(root);
    }/* w w  w  . j  av a2s  .  c o m*/
    if (root instanceof ViewGroup) {
        final ViewGroup rootGroup = (ViewGroup) root;
        for (int i = 0; i < rootGroup.getChildCount(); i++) {
            final View child = rootGroup.getChildAt(i);
            final T childResult = firstChildOfType(child, type);
            if (childResult != null) {
                return childResult;
            }
        }
    }
    return null;
}

From source file:Main.java

protected static void fillAllDescendants(Container c, Class findClass, boolean deep, Collection results) {
    if (findClass.isInstance(c)) {
        results.add(c);//  ww  w  . j  ava  2 s . com
        if (!deep)
            return;
    }
    for (int i = 0; i < c.getComponentCount(); i++) {
        Component comp = c.getComponent(i);
        if (comp instanceof Container)
            fillAllDescendants((Container) comp, findClass, deep, results);
    }
}

From source file:Main.java

public static void checkContent(final Collection<?> collection, final Class<?> clazz) {
    for (final Object o : collection) {
        if (!clazz.isInstance(o)) {
            throw new IllegalArgumentException(
                    "Collection has an instance of " + o.getClass() + ", which doesn't inherit from " + clazz);
        }/* ww  w .  ja v a2s .co m*/
    }
}

From source file:Main.java

public static boolean allInstancesOf(Class<?> clazz, Object... args) {
    for (Object arg : args)
        if (!clazz.isInstance(arg))
            return false;
    return true;// www.  j a v  a  2s  .  c o m
}

From source file:com.yoncabt.ebr.executor.jasper.Convert.java

/**
 * buras jrxml e gidecek verilerin tip dnmn yapar.
 *
 * @param <T>/*ww w  . j av  a  2  s .  c om*/
 * @param val
 * @param valueClass
 * @return
 */
public static <T> T to(Object val, Class<T> valueClass) {
    if (valueClass.isInstance(val)) {
        return (T) val;
    }
    if (ResourceBundle.class == valueClass) { //bunu dar kartaym
        return (T) val;
    }
    if (val instanceof String) {
        if (Number.class.isAssignableFrom(valueClass))
            return str2Number((String) val, valueClass);
    }
    throw new IllegalArgumentException(val.getClass() + "->" + valueClass.getName());
}

From source file:Main.java

/**
 * Returns whether all members of the list are either null or of the specific type
 *//*from  w ww .  j  a v a 2s  . co  m*/
public static boolean contains(List<?> list, Class<?> type) {
    for (Object member : list) {
        if (member != null && !(type.isInstance(member))) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Copy the source collection into the destination one filtering source elements by the given type.
 * //from   ww w.j  a va 2 s . c o m
 * @param source
 *          the source collection
 * @param destination
 *          the destination collection
 * @param clazz
 *          the filtering type
 */
@SuppressWarnings("unchecked")
public static <T, E extends T> void filterCollectionByType(Collection<T> source, Collection<E> destination,
        Class<E> clazz) {
    for (T element : source) {
        if (clazz.isInstance(element)) {
            destination.add((E) element);
        }
    }
}

From source file:Main.java

static public <T extends Container> T findAncestorOfType(Container acomp, Class<T> type) {
    Container parent = acomp;/*from w ww.  j a v a2  s . c  om*/
    while (parent != null) {
        if (type.isInstance(parent))
            return type.cast(parent);
        parent = parent.getParent();
    }
    return null;
}

From source file:com.mycollab.configuration.logging.ExceptionFilter.java

private static boolean isInstanceInBlackList(Class cls, Throwable throwable) {
    return cls.isInstance(throwable)
            || throwable.getCause() != null && isInstanceInBlackList(cls, throwable.getCause());
}