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 <T> void assertInstanceOf(@NonNull T object, @NonNull Class<?> clazz,
        @NonNull String parameterName) throws AssertionError {
    check(!clazz.isInstance(object), parameterName + " is not instance of " + clazz.getName() + ".");
}

From source file:Main.java

/**
 * Return the first ancestor of <code>component</code> (beginning with itself) that is an
 * {@link Class#isInstance(Object) instance of} <code>c</code>. If <code>component</code> is in
 * a popup, {@link JPopupMenu#getInvoker()} is used instead of {@link Component#getParent()}.
 * //from  www  .ja  v a2 s  .  c  o m
 * @param <C> type of class.
 * @param c the searched for class.
 * @param component the component.
 * @return the first ancestor or <code>null</code> if none match.
 */
public static final <C> C getAncestorOrSelf(final Class<C> c, Component component) {
    while (component != null && !c.isInstance(component)) {
        if (component instanceof JPopupMenu) {
            // popups are in a JLayeredPane in the root pane
            component = ((JPopupMenu) component).getInvoker();
        } else {
            component = component.getParent();
        }
    }
    return c.cast(component);
}

From source file:Main.java

@NonNull
public static <T> List<T> childrenOfType(@NonNull View root, @NonNull Class<T> type) {
    final List<T> children = new ArrayList<>();
    if (type.isInstance(root)) {
        children.add(type.cast(root));//from w  ww.j  ava  2  s .  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);
            children.addAll(childrenOfType(child, type));
        }
    }
    return children;
}

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

private static boolean isInstanceInBlackList(Class cls, Throwable throwable) {
    if (cls.isInstance(throwable)) {
        return true;
    }// w w w  .j  a  v  a  2 s  .  c o m
    if (throwable.getCause() != null) {
        return isInstanceInBlackList(cls, throwable.getCause());
    }
    return false;
}

From source file:Main.java

public static <T> List<T> subListByType(Collection<?> input, Class<T> type) {
    List<T> result = new ArrayList<T>();
    for (Object s : input) {
        if (type.isInstance(s)) {
            T t = type.cast(s);/*from ww w  .jav a2  s . co m*/
            result.add(t);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Get specified parent.//  w  ww.  j  av  a  2s. c  o m
 * @param <T>
 * @param component
 * @param clazz
 * @return
 */
public static <T> T getParent(Component component, Class<T> clazz) {

    if (component == null) {
        return null;
    }

    if (clazz.isInstance(component)) {
        return (T) component;
    }

    return getParent(component.getParent(), clazz);
}

From source file:Main.java

/**
 * Filters the src collection and puts the objects matching the
 * clazz into the dest collection.//www  .jav a2 s.  c o m
 */
public static <T> void filter(Class<T> clazz, Collection<?> src, Collection<T> dest) {
    for (Object o : src) {
        if (clazz.isInstance(o)) {
            dest.add(clazz.cast(o));
        }
    }
}

From source file:fr.inria.atlanmod.neoemf.core.impl.NeoEMFEObjectAdapterFactoryImpl.java

@SuppressWarnings("unchecked")
public static <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType.isInstance(adaptableObject)) {
        return (T) adaptableObject;
    } else if (adapterType.isAssignableFrom(NeoEMFInternalEObject.class)
            && adaptableObject instanceof InternalEObject) {
        {//from  w  ww.j a v a 2 s. co  m
            EObject adapter = adaptedObjects.get(adaptableObject);
            if (adapter != null) {
                if (adapterType.isAssignableFrom(adapter.getClass())) {
                    return (T) adapter;
                }
            }
        }
        {
            // Compute the interfaces that the proxy has to implement
            // These are the current interfaces + NeoEMFEObject
            List<Class<?>> interfaces = new ArrayList<>();
            interfaces.addAll(ClassUtils.getAllInterfaces(adaptableObject.getClass()));
            interfaces.add(NeoEMFInternalEObject.class);
            // Create the proxy
            Enhancer enhancer = new Enhancer();
            enhancer.setClassLoader(NeoEMFEObjectAdapterFactoryImpl.class.getClassLoader());
            enhancer.setSuperclass(adaptableObject.getClass());
            enhancer.setInterfaces(interfaces.toArray(new Class[] {}));
            enhancer.setCallback(new NeoEMFEObjectProxyHandlerImpl((InternalEObject) adaptableObject));
            T adapter = (T) enhancer.create();
            adaptedObjects.put((InternalEObject) adaptableObject, (NeoEMFInternalEObject) adapter);
            return adapter;
        }
    }
    return null;
}

From source file:fr.inria.atlanmod.neoemf.core.impl.NeoEObjectAdapterFactoryImpl.java

@SuppressWarnings("unchecked")
public static <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType.isInstance(adaptableObject)) {
        return (T) adaptableObject;
    } else if (adapterType.isAssignableFrom(InternalPersistentEObject.class)
            && adaptableObject instanceof InternalEObject) {
        {/*from w w w. j  ava2  s  .c om*/
            EObject adapter = adaptedObjects.get(adaptableObject);
            if (adapter != null) {
                if (adapterType.isAssignableFrom(adapter.getClass())) {
                    return (T) adapter;
                }
            }
        }
        {
            // Compute the interfaces that the proxy has to implement
            // These are the current interfaces + PersistentEObject
            List<Class<?>> interfaces = new ArrayList<>();
            interfaces.addAll(ClassUtils.getAllInterfaces(adaptableObject.getClass()));
            interfaces.add(InternalPersistentEObject.class);
            // Create the proxy
            Enhancer enhancer = new Enhancer();
            enhancer.setClassLoader(adaptableObject.getClass().getClassLoader());
            enhancer.setSuperclass(adaptableObject.getClass());
            enhancer.setInterfaces(interfaces.toArray(new Class[] {}));
            enhancer.setCallback(new NeoEObjectProxyHandlerImpl((InternalEObject) adaptableObject));
            T adapter = (T) enhancer.create();
            adaptedObjects.put((InternalEObject) adaptableObject, (InternalPersistentEObject) adapter);
            return adapter;
        }
    }
    return null;
}

From source file:Main.java

public static <T> int getIndexOf(List<T> configurationSources, Class<? extends T> clazz) {
    int i = 0;//from ww  w . java 2s .  co m
    for (; i < configurationSources.size(); i++) {
        if (clazz.isInstance(configurationSources.get(i))) {
            return i;
        }
    }
    return -1;
}