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:mase.mason.world.DistanceSensorArcs.java

protected WorldObject[] getCandidates() {
    if (objects != null) {
        return objects;
    } else {/* w  w w. ja  v  a  2  s  .c  om*/
        Bag neighbours = (Double.isInfinite(range) || field.allObjects.size() < 30) ? field.allObjects
                : field.getNeighborsWithinDistance(ag.getLocation(), range + ag.getRadius(), false, true);
        WorldObject[] objs = new WorldObject[neighbours.size()];
        int index = 0;
        for (Object n : neighbours) {
            if (n != ag) {
                for (Class type : types) {
                    if (type.isInstance(n)) {
                        objs[index++] = (WorldObject) n;
                        break;
                    }
                }
            }
        }
        if (index < objs.length) {
            objs = Arrays.copyOf(objs, index);
        }
        return objs;
    }
}

From source file:net.paslavsky.springrest.SpringAnnotationPreprocessor.java

private String getValue(Annotation[] annotations, Class<? extends Annotation> annotationClass) {
    for (Annotation annotation : annotations) {
        if (annotationClass.isInstance(annotation)) {
            Method valueMethod = ReflectionUtils.findMethod(annotationClass, "value");
            if (valueMethod != null) {
                return (String) ReflectionUtils.invokeMethod(valueMethod, annotation);
            } else {
                return null;
            }//w  w w  .  j a  va  2  s  .  co m
        }
    }

    return null;
}

From source file:com.conwet.silbops.model.AbstractMapping.java

@Override
public boolean equals(Object obj) {

    if (this == obj) {
        return true;
    }//from ww  w  .  j  a v  a 2s.c  o  m

    Class<T> clazz = getThisType();

    if (clazz.isInstance(obj)) {

        AbstractMapping<T> other = clazz.cast(obj);
        return attributes.equals(other.attributes);
    }

    return false;
}

From source file:com.wasteofplastic.acidisland.schematics.Schematic.java

/**
 * Get child tag of a NBT structure./*from   ww w. j a  va  2  s .  co m*/
 * 
 * @param items
 *            The parent tag map
 * @param key
 *            The name of the tag to get
 * @param expected
 *            The expected type of the tag
 * @return child tag casted to the expected type
 * @throws DataException
 *             if the tag does not exist or the tag is not of the
 *             expected type
 */
private static <T extends Tag> T getChildTag(Map<String, Tag> items, String key, Class<T> expected)
        throws IllegalArgumentException {
    if (!items.containsKey(key)) {
        throw new IllegalArgumentException("Schematic file is missing a \"" + key + "\" tag");
    }
    Tag tag = items.get(key);
    if (!expected.isInstance(tag)) {
        throw new IllegalArgumentException(key + " tag is not of tag type " + expected.getName());
    }
    return expected.cast(tag);
}

From source file:com.amalto.core.util.Util.java

public static <T> T getException(Throwable throwable, Class<T> cls) {
    if (cls.isInstance(throwable)) {
        return (T) throwable;
    }/*ww w.  j  a  v a 2  s  . c o m*/
    if (throwable.getCause() != null) {
        return getException(throwable.getCause(), cls);
    }
    return null;
}

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

/**
 * @see org.springmodules.validation.bean.conf.loader.annotation.handler.ClassValidationAnnotationHandler#supports(java.lang.annotation.Annotation, Class)
 *//*from  ww  w . java 2s  .c  o m*/
public boolean supports(Annotation annotation, Class clazz) {
    for (Class supportedType : supportedAnnotationTypes) {
        if (supportedType.isInstance(annotation)) {
            return true;
        }
    }
    return false;
}

From source file:com.github.aenygmatic.spring.osgi.registration.OsgiSpringComponentCollector.java

private Class<?> registration(Object bean) {
    Class<?> registration = bean.getClass().getAnnotation(OsgiService.class).registration();

    if (ByInterfaceRegistration.class.equals(registration)) {
        registration = findImplementedInterface(bean.getClass());
    } else if (!registration.isInstance(bean)) {
        throw new NotRegistrableServiceException(
                "@OsgiService registration must be impemented by the bean. Bean: " + bean.getClass()
                        + " registration: " + registration);
    }/*from   w  w w  . j a v a 2 s  .c o  m*/

    return registration;
}

From source file:rmblworx.tools.timey.AlarmEventsTest.java

/**
 * Stellt sicher, dass ein Element des Typs <code>type</code> in <code>items</code> vorkommt.
 * @param items Elemente/*from www .  j ava2 s  .  c  o m*/
 * @param type Typ
 */
protected final void assertContainsElementOfType(final Collection<?> items, final Class<?> type) {
    for (final Object item : items) {
        if (type.isInstance(item)) {
            return;
        }
    }

    fail(String.format("%s enthlt kein Element vom Typ %s.", items, type));
}

From source file:im.r_c.android.fusioncache.MemCache.java

/**
 * Special get method./*w ww  .  j a va  2  s .c om*/
 * Get value by class passed in.
 * <p>
 * Only used in this package.
 */
synchronized <T> T get(String key, Class<T> clz) {
    ValueWrapper wrapper = mCacheWrapper.get(key);
    if (wrapper == null || !clz.isInstance(wrapper.obj)) {
        return null;
    }
    return clz.cast(wrapper.obj);
}

From source file:org.jboss.spring.facade.ControllerBeanFactory.java

public boolean isTypeMatch(String name, Class clazz) throws NoSuchBeanDefinitionException {
    return clazz.isInstance(getBean(name));
}