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:net.paslavsky.springrest.HttpHeadersHelper.java

private <T> boolean instanceOf(Object o, Class<? extends T> type) {
    if (o == null) {
        return false;
    } else if (type.isInstance(o)) {
        return true;
    } else if (o instanceof List) {
        List l = (List) o;
        return l.size() > 0 && l.get(0) != null && type.isInstance(l.get(0));
    } else if (o instanceof Collection) {
        Collection c = (Collection) o;
        Iterator iterator = c.iterator();
        Object item = null;// w w  w.ja  v a 2 s.c o  m
        if (iterator.hasNext()) {
            item = iterator.next();
        }
        return item != null && item.getClass() == type;
    }
    return false;
}

From source file:net.logstash.logback.ConfigurationTest.java

private <T extends JsonProvider<ILoggingEvent>> T getInstance(List<JsonProvider<ILoggingEvent>> providers,
        Class<T> clazz) {
    for (JsonProvider<ILoggingEvent> jsonProvider : providers) {
        if (clazz.isInstance(jsonProvider)) {
            return clazz.cast(jsonProvider);
        }/*from  ww w.  j a v  a 2 s  . c  om*/
    }
    return null;
}

From source file:hudson.util.PersistedList.java

/**
 * Gets all instances that matches the given type.
 *///  w w w. ja  v a  2  s  .co m
public <U extends T> List<U> getAll(Class<U> type) {
    List<U> r = new ArrayList<U>();
    for (T t : data)
        if (type.isInstance(t))
            r.add(type.cast(t));
    return r;
}

From source file:org.springmodules.validation.bean.conf.loader.annotation.handler.AbstractPropertyValidationAnnotationHandler.java

/**
 * @see PropertyValidationAnnotationHandler#supports(java.lang.annotation.Annotation, Class, java.beans.PropertyDescriptor)
 *///from  w  ww  . j a  v  a  2  s  .  c o m
public boolean supports(Annotation annotation, Class clazz, PropertyDescriptor descriptor) {
    for (Class supportedType : supportedAnnotationTypes) {
        if (supportedType.isInstance(annotation)) {
            return true;
        }
    }
    return false;
}

From source file:be.fedict.eid.pkira.xkmsws.util.XMLMarshallingUtil.java

/**
 * Extracts the first element from the list matching the type.
 * //ww w.  ja  va2s .co m
 * @param <T>
 *            expected type.
 * @param clazz
 *            expected type.
 * @param list
 *            list with either this type of a JAXBElement with this type.
 * @return the first matching element.
 */
public <T> T getFromList(Class<T> clazz, List<Object> list) {
    for (Object object : list) {
        if (clazz.isInstance(object)) {
            return clazz.cast(object);
        }
        if (object instanceof JAXBElement<?>) {
            JAXBElement<?> jaxbElement = (JAXBElement<?>) object;
            if (jaxbElement.getDeclaredType().equals(clazz)) {
                return clazz.cast(jaxbElement.getValue());
            }
        }
    }

    return null;
}

From source file:com.quartercode.eventbridge.def.bridge.DefaultBridge.java

@Override
public <T extends BridgeModule> T getModule(Class<T> type) {

    Validate.notNull(type, "Module type for module retrieval cannot be null");

    for (BridgeModule module : modules) {
        if (type.isInstance(module)) {
            return type.cast(module);
        }// w w  w.ja va2  s .  c om
    }

    return null;
}

From source file:org.springmodules.validation.bean.conf.loader.annotation.handler.AbstractMethodValidationAnnotationHandler.java

/**
 * @see MethodValidationAnnotationHandler#supports(java.lang.annotation.Annotation, Class, java.lang.reflect.Method)
 *//*from   w w w . jav a 2 s. c  o m*/
public boolean supports(Annotation annotation, Class clazz, Method method) {
    for (Class supportedType : supportedAnnotationTypes) {
        if (supportedType.isInstance(annotation)) {
            return true;
        }
    }
    return false;
}

From source file:net.sf.morph.lang.languages.BaseLanguage.java

/**
 * {@inheritDoc}/*from  ww  w .  j  a  v  a2 s . com*/
 */
public void set(Object target, String expression, Object value, Locale locale)
        throws LanguageException, TransformationException {

    if (target == null) {
        throw new LanguageException("The target object cannot be null");
    }
    if (log.isTraceEnabled()) {
        log.trace("Setting '" + expression + "' to " + ObjectUtils.getObjectDescription(value) + " on target "
                + ObjectUtils.getObjectDescription(target));
    }
    // first do any needed type conversion
    Class type = getType(target, expression);
    Object converted = type.isInstance(value) ? value : getConverter().convert(type, value, locale);

    try {
        setImpl(target, expression, converted);
    } catch (LanguageException e) {
        throw e;
    } catch (Exception e) {
        throw new LanguageException(
                "Could not set '" + expression + "' to " + ObjectUtils.getObjectDescription(value)
                        + " on target " + ObjectUtils.getObjectDescription(target),
                e);
    }
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino.WrapFactoryConverter.java

/**
 * {@inheritDoc}//  w ww  . ja v a2  s.c  o m
 */
@Override
public Object convertValueForJava(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    final Object result;

    if (value instanceof Wrapper) {
        final Object unwrapped = ((Wrapper) value).unwrap();
        if (!expectedClass.isInstance(unwrapped)) {
            result = globalDelegate.convertValueForJava(unwrapped, expectedClass);
        } else {
            result = unwrapped;
        }
    } else {
        result = null;
    }

    return result;
}

From source file:com.swordlord.gozer.eventhandler.generic.GozerEventListenerList.java

/**
 * Adds the listener as a listener of the specified type.
 * @param t the type of the listener to be added
 * @param l the listener to be added/*w  w  w  . j  a  v a  2  s  .  com*/
 */
public synchronized <T extends GozerEventListener> void add(Class<T> t, T l) {
    if (l == null) {
        throw new NullArgumentException("Listener is null");
    }

    if (!t.isInstance(l)) {
        throw new IllegalArgumentException("Listener " + l + " is not of type " + t);
    }

    if (listenerList == NULL_ARRAY) {
        // if this is the first listener added,
        // initialize the lists
        listenerList = new Object[] { t, l };
    } else {
        // Otherwise copy the array and add the new listener
        int i = listenerList.length;
        Object[] tmp = new Object[i + 2];
        System.arraycopy(listenerList, 0, tmp, 0, i);

        tmp[i] = t;
        tmp[i + 1] = l;

        listenerList = tmp;
    }
}