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:com.pros.jsontransform.expression.FunctionAbstract.java

@SuppressWarnings("rawtypes")
static Object getArgument(final int argPosition, final Class argClass, final Object... argArray)
        throws ObjectTransformerException {
    if (argPosition >= argArray.length) {
        throw new ObjectTransformerException("Wrong number of arguments.");
    }/* ww w.  j  a  v a2 s. c o m*/
    if (argArray[argPosition] != null && !argClass.isInstance(argArray[argPosition])) {
        throw new ObjectTransformerException("Wrong argument type.");
    }
    return argArray[argPosition];
}

From source file:hr.fer.spocc.grammar.Grammar.java

@SuppressWarnings("unchecked")
protected static <T, P extends ProductionRule<T>> Set<P> filterProductionTypes(
        Collection<? extends ProductionRule<T>> rules, Class<P> clazz) {
    Set<P> ret = new HashSet<P>();
    for (ProductionRule<T> rule : rules) {
        if (clazz.isInstance(rule)) {
            ret.add((P) rule);/*from w  w  w.j  a v a  2s  .  c  o  m*/
        }
    }
    return ret;
}

From source file:com.knowbout.hibernate.HibernateUtil.java

public static synchronized void addDeleteHandler(DeleteHandler<?> deleteHandler) {
    if (deleteHandlers == null) {
        deleteHandlers = new LinkedList<DeleteHandler<?>>();
        EventListeners els = config.getEventListeners();
        DeleteEventListener[] dels = els.getDeleteEventListeners();
        DeleteEventListener[] newDels = new DeleteEventListener[dels.length + 1];
        System.arraycopy(dels, 0, newDels, 1, dels.length);
        newDels[0] = new DeleteEventListener() {

            private static final long serialVersionUID = 0L;

            public void onDelete(DeleteEvent event) throws HibernateException {
                Object deleted = event.getObject();
                for (DeleteHandler<?> dh : deleteHandlers) {
                    Class<?> handledType = (Class) ((ParameterizedType) dh.getClass().getGenericSuperclass())
                            .getActualTypeArguments()[0];
                    if (handledType.isInstance(deleted)) {
                        /* The following should work but won't compile, so I'll invoke through reflection
                        dh.onDelete(handledType.cast(deleted));
                        *///from   ww  w . j  a v a2s.  com
                        Class clazz = dh.getClass();
                        try {
                            Method method = clazz.getMethod("onDelete", new Class[] { handledType });
                            method.invoke(dh, new Object[] { deleted });
                        } catch (Exception e) {
                            throw new Error("Bug in HibernateUtil.addDeleteHandler()");
                        }
                    }
                }
            }

        };
    }
    deleteHandlers.add(0, deleteHandler);
}

From source file:es.uvigo.ei.sing.gc.view.ZKUtils.java

public static void emptyComponent(Component component, Class<? extends Component>... childClasses) {
    final List<Object> children = new ArrayList<Object>(component.getChildren());

    for (Object child : children) {
        for (Class<? extends Component> childClass : childClasses) {
            if (childClass.isInstance(child)) {
                component.removeChild((Component) child);
                break;
            }/*from  w w w . j  a  va  2 s. c o  m*/
        }
    }
}

From source file:de.thorstenberger.taskmodel.complex.complextaskhandling.subtasklets.impl.SubTasklet_MCBuilder.java

/**
 * Return all instances of <code>clazz</code> from the given list.
 *
 * @param correctOrIncorrect/*  ww w.j a v  a 2s  .  c  o  m*/
 * @param clazz
 * @return
 */
static List filterList(List correctOrIncorrect, Class<?> clazz) {
    List result = new ArrayList();
    for (Object o : correctOrIncorrect) {
        if (clazz.isInstance(o)) {
            result.add(o);
        }
    }
    return result;
}

From source file:com.feilong.core.lang.ClassUtil.java

/**
 *  <code>obj</code> ?? <code>klass</code> .
 * /*from  w  ww .j  a va 2 s  .  co  m*/
 * <h3>instanceof?/isAssignableFrom/isInstance(Object obj) </h3>
 * 
 * <blockquote>
 * <table border="1" cellspacing="0" cellpadding="4" summary="">
 * <tr style="background-color:#ccccff">
 * <th align="left"></th>
 * <th align="left"></th>
 * </tr>
 * <tr valign="top">
 * <td>instanceof?</td>
 * <td>,??????<br>
 * ?:oo instanceof TypeName<br>
 * ???,??????<br>
 * instanceofJava?,{@code ==,>,<}?,??,boolean?</td>
 * </tr>
 * <tr valign="top" style="background-color:#eeeeff">
 * <td>isAssignableFrom</td>
 * <td>class,?Class1?Class2????.<br>
 * ?Class1.isAssignableFrom(Class2)<br>
 * ?java.lang.Class.</td>
 * </tr>
 * <tr valign="top">
 * <td>isInstance(Object obj)</td>
 * <td>obj,objclass? ,true.<br>
 * instanceof? <span style="color:red">?</span></td>
 * </tr>
 * </table>
 * 
 * <p>
 * instanceof :? {@code ----->}  <br>
 * isAssignableFrom : {@code ----->} ?
 * </p>
 * </blockquote>
 * 
 * @param obj
 *            
 * @param klass
 *            
 * @return  obj , true;<br>
 *          <code>null == klass</code>  false
 * @see java.lang.Class#isInstance(Object)
 */
public static boolean isInstance(Object obj, Class<?> klass) {
    return null == klass ? false : klass.isInstance(obj);
}

From source file:Utils.java

/**
 * Create a typesafe filter of an unchecked iterator. {@link Iterator#remove}
 * will work if it does in the unchecked iterator.
 * // w w  w . j a  v a2 s  . co  m
 * @param rawIterator
 *          an unchecked iterator
 * @param type
 *          the desired enumeration type
 * @param strict
 *          if false, elements which are not null but not assignable to the
 *          requested type are omitted; if true, {@link ClassCastException}
 *          may be thrown from an iterator operation
 * @return an iterator guaranteed to contain only objects of the requested
 *         type (or null)
 */
public static <E> Iterator<E> checkedIteratorByFilter(Iterator rawIterator, final Class<E> type,
        final boolean strict) {
    return new CheckedIterator<E>(rawIterator) {
        protected boolean accept(Object o) {
            if (o == null) {
                return true;
            } else if (type.isInstance(o)) {
                return true;
            } else if (strict) {
                throw new ClassCastException(o + " was not a " + type.getName()); // NOI18N
            } else {
                return false;
            }
        }
    };
}

From source file:com.reachlocal.grails.plugins.cassandra.utils.OrmHelper.java

public static Object safeGetProperty(GroovyObject data, String name, Class clazz, Object defaultValue) {
    Object value;/* w  ww .j  a  va2 s . c om*/
    try {
        value = data.getProperty(name);
        if (!clazz.isInstance(value)) {
            value = defaultValue;
        }
    } catch (MissingPropertyException e) {
        value = defaultValue;
    } catch (MissingMethodException e) {
        value = defaultValue;
    }
    return value;
}

From source file:CollectionUtils.java

/**
 * Find a value of the given type in the given Collection.
 * @param collection the Collection to search
 * @param type the type to look for// w  w w. j  a  va 2 s  .com
 * @return a value of the given type found, or <code>null</code> if none
 * @throws IllegalArgumentException if more than one value of the given type found
 */
public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException {
    if (isEmpty(collection)) {
        return null;
    }
    Class typeToUse = (type != null ? type : Object.class);
    Object value = null;
    for (Iterator it = collection.iterator(); it.hasNext();) {
        Object obj = it.next();
        if (typeToUse.isInstance(obj)) {
            if (value != null) {
                throw new IllegalArgumentException(
                        "More than one value of type [" + typeToUse.getName() + "] found");
            }
            value = obj;
        }
    }
    return value;
}

From source file:com.link_intersystems.lang.Assert.java

/**
 * Assert that a value is an instance of one of expected classes or throw an
 * {@link IllegalArgumentException}./*  ww w . jav  a  2  s  .c  o  m*/
 *
 * @param name
 *            the name of the value.
 * @param value
 *            the value.
 * @param expectedInstanceOfs
 *            the expected classes that the value must be an instance of.
 * @since 1.2.0.0
 */
public static void instanceOf(String name, Object value, Class<?>... expectedInstanceOfs) {
    for (int i = 0; i < expectedInstanceOfs.length; i++) {
        Class<?> expectedInstanceOf = expectedInstanceOfs[i];
        if (expectedInstanceOf.isInstance(value)) {
            return;
        }
    }
    String exceptionMessage = String.format("%s must be an instance of %s", name,
            Arrays.toString(expectedInstanceOfs));
    throw new IllegalArgumentException(exceptionMessage);
}