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

private static JComponent getComponentWithName(Container owner, String name, Class<?> clazz) {
    for (Component c : owner.getComponents()) {
        if (clazz.isInstance(c) && ((JComponent) c).getName() != null
                && ((JComponent) c).getName().equals(name))
            return (JComponent) c;
        else if (c instanceof JComponent) {
            JComponent b = getComponentWithName((JComponent) c, name, clazz);
            if (b != null)
                return b;
        }//  w w w.ja v  a2  s  . c o  m
    }
    return null;
}

From source file:Main.java

/**
 * Returns first parent which is instance of specified class type or null if
 * none found./*from w w w.ja  va 2 s.c  o  m*/
 *
 * @param component
 *            component to look parent for
 * @param parentClass
 *            parent component class
 * @param <T>
 *            parent component class type
 * @return first parent which is instance of specified class type or null if
 *         none found
 */
public static <T extends Container> T getFirstParent(final Component component, final Class<T> parentClass) {
    Component parent = component.getParent();
    while (!parentClass.isInstance(parent) && parent != null) {
        parent = parent.getParent();
    }
    return (T) parent;
}

From source file:org.ow2.proactive.procci.service.transformer.TransformerProvider.java

protected static <T> T castInstanceModel(Class<T> classe, InstanceModel instanceModel) {

    if (classe.isInstance(instanceModel)) {
        return classe.cast(instanceModel);
    } else {/*from   w  w w.j  av a2 s.  c  om*/
        logger.error("Error in castInstanceModel : the instance of " + instanceModel.getClass().getName()
                + " is not an instance of " + classe.getName());
        throw new ServerException();
    }

}

From source file:Main.java

public static <T> T getArgument(int index, Class<T> clazz, Object... objects) {
    T ret = null;/* ww  w . ja  va 2 s.  co m*/

    if (objects != null && index < objects.length && clazz != null && clazz.isInstance(objects[index])) {
        ret = clazz.cast(objects[index]);
    }
    return ret;
}

From source file:ExceptionUtils.java

/**
 * Locates a particular type of exception, working its way via the cause property of each exception in the exception
 * stack./*from   w  ww.  j a  v a 2s  .c om*/
 *
 * @param t    the outermost exception
 * @param type the type of exception to search for
 * @return the first exception of the given type, if found, or null
 */
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
    Throwable current = t;

    while (current != null) {
        if (type.isInstance(current))
            return type.cast(current);

        // Not a match, work down.

        current = current.getCause();
    }

    return null;
}

From source file:Main.java

/**
 * Convert the collection to a typed list of given type. 
 * @param <T>/*from   w  w w .j a  v  a2  s.  com*/
 * @param collection
 * @param type
 * @return
 */
public static <T> List<T> asList(Collection<?> collection, Class<T> type) {
    List<T> list = new ArrayList<T>();
    for (Object o : collection) {
        if (type.isInstance(o)) {
            list.add(type.cast(o));
        }
    }
    return list;
}

From source file:Main.java

/**
 * Modifies and returns the collection./*w w  w.  j a  v  a  2 s. co m*/
 */
public static Collection filterByClass(Collection collection, Class c) {
    for (Iterator i = collection.iterator(); i.hasNext();) {
        Object item = i.next();
        if (!c.isInstance(item)) {
            i.remove();
        }
    }

    return collection;
}

From source file:Main.java

/**
 * Finds the index of the first element in the given list of the given type.
 * /*from  w  w  w .  j  ava2  s .c  o  m*/
 * @param list
 *            the list to search through.
 * @param type
 *            the type to find.
 * @return the index of the first element in {@code list} that is an instance of {@code type}, or -1 if no such
 *         element is in {@code list}.
 */
public static <T> int indexOfInstance(List<T> list, Class<? extends T> type) {
    int k = 0;
    for (T t : list) {
        if (type.isInstance(t)) {
            return k;
        }
        k++;
    }
    return -1;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <X, Y extends X> List<Y> getObjectsOfType(Collection<X> objs, Class<Y> cls) {
    List<Y> ret = new ArrayList<Y>();
    if (objs != null) {
        for (X obj : objs) {
            if (cls.isInstance(obj)) {
                ret.add((Y) obj);//from www .  jav  a 2 s.  c  o m
            }
        }
    }
    return ret;
}

From source file:Main.java

public static int indexOfObject(Object[] array, Class<?> type, int sequence) {
    if (array == null) {
        return -1;
    }//from  w ww.  j ava 2 s  .  c  om
    while (sequence < array.length) {
        if (type.isInstance(array[sequence])) {
            return sequence;
        }
        sequence++;
    }
    return -1;
}