Example usage for java.lang Class isAssignableFrom

List of usage examples for java.lang Class isAssignableFrom

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isAssignableFrom(Class<?> cls);

Source Link

Document

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Main cls = new Main();
    Class c = cls.getClass();//from   ww  w  .  ja  v a2s  . com

    Class bClass = BaseClass.class;

    boolean retval = bClass.isAssignableFrom(c);
    System.out.println("Return Value = " + retval);

    retval = c.isAssignableFrom(bClass);
    System.out.println("Return Value  = " + retval);
}

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 <T extends View> T findParentOfClass(View view, Class<T> theClass) {
    if (theClass.isAssignableFrom(view.getClass()))
        return (T) view;
    else if (view.getParent() != null && view.getParent() instanceof View)
        return findParentOfClass((View) view.getParent(), theClass);
    else/*from  w ww .ja v  a 2  s.  com*/
        return null;
}

From source file:Main.java

/**
 * The equivalent of instanceof, but for Class objects. 'cos I always forget
 * how to do this./*  www.ja va2  s .c  om*/
 * 
 * @param possSubType
 * @param superType
 * @return true if possSubType <i>is</i> a subType of superType
 */
public static boolean isa(Class<?> possSubType, Class<?> superType) {
    return superType.isAssignableFrom(possSubType);
}

From source file:Main.java

private static boolean isAssignableFrom(Class<?> clazz, Class<?> iClazz) {
    if (iClazz.isAssignableFrom(clazz)) {
        return true;
    } else if (clazz.getSuperclass() != null) {
        if (iClazz.isAssignableFrom(clazz.getSuperclass())) {
            return true;
        } else {/*  ww  w  .  j ava2  s .  c  o  m*/
            return isAssignableFrom(clazz.getSuperclass(), iClazz);
        }
    } else {
        return false;
    }
}

From source file:Main.java

/**
 * Get a class or interface from an object, if available.  This is useful
 * when an object may implement optional interfaces, and we want to
 * get those interfaces./*from   w  ww .java 2  s  .c om*/
 * 
 * As an example, see {@link AudioPlayerWithEvents}.
 * 
 * @param object the object to inspect
 * @param desiredClass the class to get
 * @return
 */
public static <T> T getIfAvailable(Object object, Class<T> desiredClass) {
    return (desiredClass.isAssignableFrom(object.getClass())) ? desiredClass.cast(object) : null;
}

From source file:Main.java

private static boolean ignoreClass(Object o, Class<?> c) {
    return c.isAssignableFrom(o.getClass());
}

From source file:Main.java

public static boolean isCollectionClass(Class clazz) {
    return Collection.class.isAssignableFrom(clazz) || clazz.isAssignableFrom(Collection.class)
            || Map.class.isAssignableFrom(clazz) || clazz.isAssignableFrom(Map.class);
}

From source file:Main.java

private static void collectAssignables(Class c1, Class c2, Set s) {
    if (c1.isAssignableFrom(c2)) {
        s.add(c1);/*from ww w.j a v  a 2  s.  c  om*/
    }
    Class sc = c1.getSuperclass();
    if (sc != null) {
        collectAssignables(sc, c2, s);
    }
    Class[] itf = c1.getInterfaces();
    for (int i = 0; i < itf.length; ++i) {
        collectAssignables(itf[i], c2, s);
    }
}

From source file:Main.java

/**
 * Filters all Views not within the given set.
 *
 * @param classSet contains all classes that are ok to pass the filter
 * @param viewList the Iterable to filter form
 * @return an ArrayList with filtered views
 *///  w  w  w. ja  v  a 2s .  c  om
public static ArrayList<View> filterViewsToSet(Class<View> classSet[], Iterable<View> viewList) {
    ArrayList<View> filteredViews = new ArrayList<View>();
    for (View view : viewList) {
        if (view == null)
            continue;
        for (Class<View> filter : classSet) {
            if (filter.isAssignableFrom(view.getClass())) {
                filteredViews.add(view);
                break;
            }
        }
    }
    return filteredViews;
}