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 void main(final String[] args) {
    Class cls = new Integer("0").getClass();
    cls.isInstance(new Double("1.2").getClass());
    Number.class.isInstance(123);
    cls = Number.class;
    cls.isAssignableFrom(new Double("1.2").getClass());
}

From source file:Main.java

public static void main(String[] args) {
    Class cls = Long.class;

    Long l = new Long(8);
    Double d = new Double(3.5);

    // checking for Long instance
    boolean retval = cls.isInstance(l);
    System.out.println(l + " is Long ? " + retval);

    // checking for Long instance
    retval = cls.isInstance(d);/* w w  w  .j a  va  2  s .com*/
    System.out.println(d + " is Long ? " + retval);
}

From source file:Main.java

public static <T> T as(Object obj, Class<T> tClass) {
    return tClass.isInstance(obj) ? tClass.cast(obj) : null;
}

From source file:Main.java

public static <T> T as(Class<T> t, Object o) {
    return t.isInstance(o) ? t.cast(o) : null;
}

From source file:Main.java

static public <T> void findDescentdantsOfType(List<T> holder, Container acomp, Class<T> type) {
    if (type.isInstance(acomp))
        holder.add((T) acomp);//from w ww.  j  a  v a 2 s.  co  m
    for (int i = 0; i < acomp.getComponentCount(); i++) {
        Component child = acomp.getComponent(i);
        if (child instanceof Container) {
            findDescentdantsOfType(holder, (Container) child, type);
        }
    }
}

From source file:Main.java

public static boolean instanceOfAny(Object o, Class<?>... clazzes) {
    for (Class<?> clazz : clazzes)
        if (clazz.isInstance(o))
            return true;
    return false;
}

From source file:Main.java

public static boolean instanceOfAll(Object o, Class<?>... clazzes) {
    for (Class<?> clazz : clazzes)
        if (!clazz.isInstance(o))
            return false;
    return true;//w w  w  .j  ava  2  s  .  com
}

From source file:Main.java

static public <T> void findDescendantOfType(List<T> holder, Container parent, Class<T> type) {
    if (type.isInstance(parent)) {
        holder.add((T) parent);//from   w  w  w. j a  va 2s.  c o  m
    }
    for (Component child : parent.getComponents()) {
        if (child instanceof Container) {
            findDescendantOfType(holder, (Container) child, type);
        } else {
            if (type.isInstance(child)) {
                holder.add((T) child);
            }
        }
    }
}

From source file:Main.java

private static <T> boolean isMatch(Component component, Class<T> type, Predicate<T> predicate) {
    return type.isInstance(component) && predicate.test(type.cast(component));
}

From source file:Main.java

/**
 * Returns the first parent of given component which is an instance of given class.
 *//*from  w  ww . j  a  va2  s .c  o m*/
public static <T> T getParent(Component aComponent, Class<T> aClass) {
    while (aComponent != null && !aClass.isInstance(aComponent))
        aComponent = aComponent.getParent();
    return (T) aComponent;
}