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:ReflectionUtils.java

/**
 * Determine whether the given method explicitly declares the given exception
 * or one of its superclasses, which means that an exception of that type
 * can be propagated as-is within a reflective invocation.
 * @param method the declaring method//from  w  w  w .  j  ava 2s.c om
 * @param exceptionType the exception to throw
 * @return <code>true</code> if the exception can be thrown as-is;
 * <code>false</code> if it needs to be wrapped
 */
public static boolean declaresException(Method method, Class exceptionType) {

    Class[] declaredExceptions = method.getExceptionTypes();
    for (int i = 0; i < declaredExceptions.length; i++) {
        Class declaredException = declaredExceptions[i];
        if (declaredException.isAssignableFrom(exceptionType)) {
            return true;
        }
    }
    return false;
}

From source file:com.berkgokden.mongodb.SpringMongoDBConverter.java

public static boolean isStandardClass(Class clazz) {
    if (clazz.isAssignableFrom(Date.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Number.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(String.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(ObjectId.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(BSONObject.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Boolean.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Double.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Integer.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Long.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Pattern.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(UUID.class)) // standard, pass
        return true;

    return false;
}

From source file:de.micromata.genome.util.runtime.ExceptionUtils.java

/**
 * Rethrow an exception into Error, RuntimeException or Exception declared. If non, a RuntimeException will be used.
 *
 * @param ex the ex//  ww  w.jav  a  2  s .  co  m
 * @param declared the declared
 * @throws Exception the exception
 */
public static void wrappException(Throwable ex, Class<? extends Exception>... declared) throws Exception {
    if (ex instanceof Error) {
        throw (Error) ex;
    }
    if (ex instanceof RuntimeException) {
        throw (RuntimeException) ex;
    }
    for (Class<? extends Exception> ce : declared) {
        if (ce.isAssignableFrom(ex.getClass()) == true) {
            throw (Exception) ex;
        }
    }
    throw new RuntimeException(ex);
}

From source file:DebugUtilities.java

public static void printContainerComponents(java.awt.Container cont, PrintWriter writer, String prefix,
        boolean recurse) {
    java.awt.Component[] comps = cont.getComponents();

    if (comps.length < 1) {
        writer.println(prefix + "Contains no components.");
    }//from  www.  j av a 2s  . c o m

    for (int i = 0; i < comps.length; ++i) {
        DebugUtilities.printClassHierarchy(comps[i].getClass(), writer, prefix + "[" + i + "]");

        if (recurse) {
            Class c = java.awt.Container.class;
            if (c.isAssignableFrom(comps[i].getClass())) {
                DebugUtilities.printContainerComponents((java.awt.Container) comps[i], writer,
                        (prefix + "[" + i + "] "), recurse);
            }
        }
    }
}

From source file:Main.java

public static Object getResource(Context context, Field field, int value) {
    Resources resources = context.getResources();
    Class type = field.getType();

    if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
        return resources.getBoolean(value);
    else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
        return resources.getInteger(value);
    } else if (type.isAssignableFrom(ColorStateList.class))
        return resources.getColorStateList(value);
    else if (type.isAssignableFrom(XmlResourceParser.class))
        return resources.getXml(value);
    else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
        return resources.getDimension(value);
    else if (type.isAssignableFrom(Drawable.class))
        return resources.getDrawable(value);
    else if (type.isAssignableFrom(Animation.class))
        return AnimationUtils.loadAnimation(context, value);
    else if (type.isAssignableFrom(Movie.class))
        return resources.getMovie(value);
    else if (type.isAssignableFrom(String.class))
        return resources.getString(value);
    else if (type.isArray()) {
        if (type.getName().equals("[I")) {
            return resources.getIntArray(value);
        } else if (type.isAssignableFrom(String[].class)) {
            return resources.getStringArray(value);
        }/*from   www.  j a va2  s . c o  m*/
    }

    return null;
}

From source file:Main.java

public static <T extends Component> T findParentComponentOfType(Component component, Class<T> type) {
    do {/*from  w w  w  . j a  v a 2  s .  c om*/
        component = component.getParent();
        if (component != null && type.isAssignableFrom(component.getClass())) {
            return (T) component;
        }
    } while (component != null);

    return null;
}

From source file:com.yahoo.elide.utils.coerce.CoerceUtil.java

/**
 * Convert value to target class.//from  w ww. ja v  a  2 s . com
 *
 * @param value value to convert
 * @param cls class to convert to
 * @return coerced value
 */
public static Object coerce(Object value, Class<?> cls) {
    if (value == null || cls == null || cls.isAssignableFrom(value.getClass())) {
        return value;
    }

    try {
        return ConvertUtils.convert(value, cls);

    } catch (ConversionException | InvalidAttributeException | IllegalArgumentException e) {
        throw new InvalidValueException(value);
    }
}

From source file:io.rhiot.utils.Reflections.java

public static boolean isInstanceOfOrWrappable(Class<?> type, Class<?> instanceOf) {
    if (instanceOf.isAssignableFrom(type)) {
        return true;
    } else {/* w  ww  . j a  va  2s .  c o m*/
        type = wrapperClasses.get(type);
        return instanceOf.isAssignableFrom(type);
    }
}

From source file:Main.java

/**
 * Check if the given <code>java.lang.Throwable</code> is a declared
 * throwable of the given method.//from  www .  jav a 2  s .  c o m
 * @param p_e The trowable.
 * @param p_method The method.
 * @return <code>true</code> if <code>p_method</code> declares to throw
 *    <code>p_e</code>, or <code>false</code> otherwise.
 * @throws IllegalArgumentException
 *    If either of the method parameters is <code>null</code>.
 */
public static boolean isDeclaredThrowable(Throwable p_e, Method p_method) {
    final Class<? extends Throwable> l_clsThrowable;

    if (p_e == null)
        throw new IllegalArgumentException("No throwable.");

    if (p_method == null)
        throw new IllegalArgumentException("No method.");

    l_clsThrowable = p_e.getClass();

    for (final Class<?> l_clsException : p_method.getExceptionTypes()) {
        if (l_clsException.isAssignableFrom(l_clsThrowable))
            return true;
    }

    return false;
}

From source file:Main.java

protected static boolean isTypeOf(Type type, Class<?> clazz) {
    if (Class.class.isInstance(type)) {
        return clazz.isAssignableFrom(Class.class.cast(type));
    }/*from w  w  w .  j a  va  2 s . c  om*/
    if (ParameterizedType.class.isInstance(type)) {
        final ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
        return isTypeOf(parameterizedType.getRawType(), clazz);
    }
    return false;
}