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

/**
 * Checks an object implements the given class
 * /*from w  ww  . j  a  v a2s . c o m*/
 * @param context
 *          the context
 * @param name
 *          the name to lookup
 * @param object
 *          the object
 * @param clazz
 *          the expected type
 * @throws Exception
 *           for any error
 */
protected static void checkObject(Context context, String name, Object object, Class clazz) throws Exception {
    Class objectClass = object.getClass();
    if (clazz.isAssignableFrom(objectClass) == false) {
        StringBuffer buffer = new StringBuffer(100);
        buffer.append("Object at '").append(name);
        buffer.append("' in context ").append(context.getEnvironment());
        buffer.append(" is not an instance of ");
        appendClassInfo(buffer, clazz);
        buffer.append(" object class is ");
        appendClassInfo(buffer, object.getClass());
        throw new ClassCastException(buffer.toString());
    }
}

From source file:com.expressui.core.util.SpringApplicationContext.java

/**
 * Finds al bean of a given type and whose class is declared with given generic argument type.
 *
 * @param type                type to search for
 * @param genericArgumentType generic argument type declared on the bean
 * @param <T>                 type to query
 * @return found beans//  w w w.ja  va 2  s  .  c o  m
 */
public static <T> Set<T> getBeansByTypeAndGenericArgumentType(Class<T> type, Class genericArgumentType) {
    Set<T> beans = getBeansByType(type);
    Set<T> beansWithGenericArgumentType = new HashSet<T>();

    for (T bean : beans) {
        Class argType = ReflectionUtil.getGenericArgumentType(bean.getClass());
        if (argType != null && genericArgumentType.isAssignableFrom(argType)) {
            beansWithGenericArgumentType.add(bean);
        }
    }

    return beansWithGenericArgumentType;
}

From source file:com.wavemaker.runtime.server.ServerUtils.java

public static Object invokeMethod(Object serviceObject, Method method, Object[] args)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Class<?>[] paramTypes = method.getParameterTypes();
    Object[] argsToSend = null;/*  w  ww  . j a  v a 2 s  .  c  o m*/
    if (args != null && paramTypes != null && args.length > 0) {
        argsToSend = new Object[args.length];
        int i = 0;
        for (Class<?> p : paramTypes) {
            Object arg = args[i];
            if (arg != null && !p.isAssignableFrom(arg.getClass())
                    && String.class.isAssignableFrom(arg.getClass())) {
                arg = convert(arg.toString(), p);
            }
            argsToSend[i] = arg;
            i++;
        }

    }
    return method.invoke(serviceObject, argsToSend);
}

From source file:io.pivotal.receptor.events.EventDispatcher.java

private static boolean supportsEventType(EventListener<?> listener, ReceptorEvent<?> event) {
    Class<?> declaredEventType = GenericTypeResolver.resolveTypeArgument(listener.getClass(),
            EventListener.class);
    return (declaredEventType == null || declaredEventType.isAssignableFrom(event.getClass()));
}

From source file:org.jboss.spring.vfs.VFSUtil.java

public static <T, E extends Exception> T invokeMethodWithExpectedExceptionType(Method method, Object target,
        Class<E> expectedExceptionType, Object... args) throws E {
    try {/*  www  .j  a v a 2s .co  m*/
        return (T) method.invoke(target, args);
    } catch (IllegalAccessException ex) {
        ReflectionUtils.handleReflectionException(ex);
    } catch (InvocationTargetException ex) {
        if (expectedExceptionType.isAssignableFrom(ex.getTargetException().getClass())) {
            throw (E) ex.getTargetException();
        }
        ReflectionUtils.handleInvocationTargetException(ex);
    }
    throw new IllegalStateException("Should never get here");
}

From source file:com.excilys.ebi.bank.util.Asserts.java

public static void isAssignable(Class<?> superType, Class<?> subType, String messagePattern, Object arg1,
        Object arg2) {//from   w  w  w  .j av a  2s  . c o m
    notNull(superType, "Type to check against must not be null");
    if (subType == null || !superType.isAssignableFrom(subType)) {
        throw new IllegalArgumentException(MessageFormatter.format(messagePattern, arg1, arg2).getMessage()
                + subType + " is not assignable to " + superType);
    }
}

From source file:com.hartveld.commons.test.swing.AbstractSwingFrameTest.java

private static <T> T lookup(final Container container, final String name, final Class<T> clazz) {
    LOG.trace("Looking up component of type {} in container {} ...", clazz.getName(), container.getName());

    for (final Component c : container.getComponents()) {
        if (clazz.isAssignableFrom(c.getClass())) {
            if (name == null || name.equals(c.getName())) {
                @SuppressWarnings("unchecked")
                final T target = (T) c;
                return target;
            }//from   w w  w. j av a 2 s . c  o  m
        } else if (c instanceof Container) {
            final T nested = lookup((Container) c, name, clazz);
            if (nested != null) {
                return nested;
            }
        }
    }

    return null;
}

From source file:com.shenit.commons.utils.DataUtils.java

/**
 * Cast?//  w  w  w. j  ava2 s .  c  o  m
 * @param o
 * @param clazz
 * @return
 */
public static <T> T cast(Object o, Class<? extends T> clazz) {
    if (o == null || clazz == null || !clazz.isAssignableFrom(o.getClass()))
        return null;
    return clazz.cast(o);
}

From source file:goja.initialize.ctxbox.ClassSearcher.java

private static List<Class<?>> extraction(Class<?> clazz, List<String> classFileList) {
    List<Class<?>> classList = Lists.newArrayList();
    for (String classFile : classFileList) {
        Class<?> classInFile = Reflect.on(classFile).get();
        if (clazz.isAssignableFrom(classInFile) && clazz != classInFile) {
            classList.add(classInFile);/*from  www  .j  a  va  2s  .c  om*/
        }
    }

    return classList;
}

From source file:it.cilea.osd.jdyna.web.tag.JDynATagLibraryFunctions.java

/**
 * Determina se il parametro superclasse passato come stringa e' un
 * superclasse di object; Nota chiama internamente
 * {@link Class#isAssignableFrom(Class)}
 * //from  ww  w . j  av a 2  s.c o m
 * <code> 
 * if(superclass.isAssignableFrom(object.getClass())) {
 *     return true;
 * }
 * </code>
 * 
 * @param object
 * @param superClassName
 * @return true se object1 se e' sottoclasse di superclass
 * @throws ClassNotFoundException
 */
public static boolean instanceOf(Object object, String superClassName) throws ClassNotFoundException {
    Class classe = Class.forName(superClassName);
    if (classe.isAssignableFrom(object.getClass())) {
        return true;
    }
    return false;
}