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:com.github.ibole.infrastructure.common.exception.MoreThrowables.java

/**
 * ??./*from  w w w .  j  av a 2s. co m*/
 */
@SuppressWarnings("unchecked")
public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) {
    Throwable cause = ex.getCause();
    while (cause != null) {
        for (Class<? extends Exception> causeClass : causeExceptionClasses) {
            if (causeClass.isInstance(cause)) {
                return true;
            }
        }
        cause = cause.getCause();
    }
    return false;
}

From source file:Main.java

private static void searchFor(Container c, Class cls, List list, String name) {
    if (!isEmpty(c.getComponents())) {
        for (Component comp : c.getComponents()) {
            if (cls.isInstance(comp)
                    && (name == null ? true : (comp.getName() != null && comp.getName().matches(name)))) {
                list.add(comp);/*from  ww w . jav a  2s.c  om*/
            } else if (comp instanceof Container) {
                searchFor((Container) comp, cls, list, name);
            }
        }
    }
}

From source file:com.jaeksoft.searchlib.util.ExceptionUtils.java

@SuppressWarnings("unchecked")
public static <T extends Exception> T throwException(Exception exception, Class<T> exceptionClass) throws T {
    if (exception == null)
        return null;
    if (exceptionClass.isInstance(exception))
        throw (T) exception;
    try {/*from  w ww  .ja  va  2  s  .  c o m*/
        return (T) exceptionClass.getConstructor(Exception.class).newInstance(exception);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.ovgu.featureide.core.typecheck.helper.FujiWrapper.java

@SuppressWarnings("rawtypes")
public static <T> T getParentByType(ASTNode node, Class<T> type) {
    if (node == null) {
        return null;
    }/* w ww .ja v a 2  s . co  m*/
    if (type.isInstance(node.getParent())) {
        return type.cast(node.getParent());
    } else {
        return getParentByType(node.getParent(), type);
    }
}

From source file:org.carewebframework.cal.api.ClientUtil.java

/**
 * Returns a resource of the specified type given a resource reference. If the resource has not
 * been previously fetched, it will be fetched from the server. If the referenced resource is
 * not of the specified type, null is returned.
 * /*www . j a  v  a 2  s  .  c  om*/
 * @param reference A resource reference.
 * @param clazz The desired resource class.
 * @return The corresponding resource.
 */
@SuppressWarnings("unchecked")
public static <T extends IResource> T getResource(ResourceReferenceDt reference, Class<T> clazz) {
    IResource resource = getResource(reference);
    return clazz.isInstance(resource) ? (T) resource : null;
}

From source file:Main.java

static public <T> T getAncestorOfType(JComponent component, Class<T> type) {
    Container ancestor = component.getParent();
    while (ancestor != null) {
        System.out.println("Check  parent : " + ancestor);
        if (type.isInstance(ancestor))
            return (T) ancestor;
        ancestor = ancestor.getParent();
    }//  w  w w . j ava 2 s.c om
    return null;
}

From source file:Main.java

public static <T> List<T> filterByType(Iterable<?> collection, Class<T> expectedType) {
    List<T> filtered = new LinkedList<T>();
    if (collection != null) {
        for (Object item : collection) {
            if (item != null && expectedType.isInstance(item)) {
                filtered.add(expectedType.cast(item));
            }/*from   w w w  . java 2  s.  c o m*/
        }
    }
    return filtered;
}

From source file:com.comcast.cereal.impl.ReflectionHelper.java

/**
 * Convert the given object to be castable to the given <code>goal</code> type.
 * //from   w w  w  . jav  a  2  s  .co m
 * @param obj
 *            the object to convert
 * @param goal
 *            the goal type
 * 
 * @return the converted object
 */
public static Object convert(Object obj, Class<?> goal) {
    if (null == obj) {
        return obj;
    } else {
        if (goal.isInstance(obj)) {
            return obj;
        } else if (asPrimitive(goal).equals(asPrimitive(obj.getClass()))) {
            return obj;
        } else {
            Converter converter = ConvertUtils.lookup(goal);
            return (null != converter) ? converter.convert(goal, obj) : obj;
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> List<T> getAllComponents(final Container c, Class<T> classType) {
    Component[] comps = c.getComponents();
    List<T> compList = new ArrayList<T>();
    for (Component comp : comps) {
        if (classType.isInstance(comp)) {
            compList.add((T) comp);/* w  ww .  j a va2 s  .  com*/
        }
        if (comp instanceof Container) {
            compList.addAll(getAllComponents((Container) comp, classType));
        }
    }
    return compList;
}

From source file:Main.java

/**
 * Assert that the provided object is an instance of the provided class.
 * <p/>/*from   w w w  .ja  v  a2  s.com*/
 * <pre class="code">
 * Assert.instanceOf(Foo.class, foo);
 * </pre>
 *
 * @param type    the type to check against
 * @param obj     the object to check
 * @param message a message which will be prepended to the message produced by
 *                the function itself, and which may be used to provide context.
 *                It should normally end in a ": " or ". " so that the function
 *                generate message looks ok when prepended to it.
 * @throws IllegalArgumentException if the object is not an instance of clazz
 * @see Class#isInstance
 */
public static void isInstanceOf(Class<?> type, Object obj, String message) {
    notNull(type, "Type to check against must not be null");
    if (!type.isInstance(obj)) {
        throw new IllegalArgumentException(message + "Object of class ["
                + (obj != null ? obj.getClass().getName() : "null") + "] must be an instance of " + type);
    }
}