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.bstek.dorado.data.entity.BeanEntityEnhancer.java

@Override
protected void internalWriteProperty(Object entity, String property, Object value) throws Exception {
    BeanMap beanMap = getBeanMap(entity);
    Class<?> propertyType = beanMap.getPropertyType(property);
    if (!propertyType.isInstance(value)) {
        value = tryConvertValue(propertyType, value);
    }/*from ww w.jav  a2 s  .  co m*/
    PropertyUtils.setSimpleProperty(entity, property, value);
}

From source file:net.malariagen.gatk.test.WalkerTest.java

/**
 * execute the test, given the following:
 * @param name     the name of the test/* w w  w . java  2  s . c  o  m*/
 * @param args     the argument list
 * @param expectedException the expected exception or null
 */
public static void executeTest(String name, String args, Class<?> expectedException) {
    CommandLineGATK instance = new CommandLineGATK();
    String[] command = Utils.escapeExpressions(args);
    // add the logging level to each of the integration test commands
    //       command = Utils.appendArray(command, "-et", ENABLE_REPORTING ? "STANDARD" : "NO_ET");
    // run the executable
    boolean gotAnException = false;
    try {
        System.out.println(
                String.format("Executing test %s with GATK arguments: %s", name, Utils.join(" ", command)));
        CommandLineExecutable.start(instance, command);
    } catch (Exception e) {
        gotAnException = true;
        if (expectedException != null) {
            // we expect an exception
            System.out.println(String.format("Wanted exception %s, saw %s", expectedException, e.getClass()));
            if (expectedException.isInstance(e)) {
                // it's the type we expected
                System.out.println(String.format("  => %s PASSED", name));
            } else {
                e.printStackTrace();
                Assert.fail(String.format("Test %s expected exception %s but got %s instead", name,
                        expectedException, e.getClass()));
            }
        } else {
            // we didn't expect an exception but we got one :-(
            throw new RuntimeException(e);
        }
    }

    // catch failures from the integration test
    if (expectedException != null) {
        if (!gotAnException)
            // we expected an exception but didn't see it
            Assert.fail(String.format("Test %s expected exception %s but none was thrown", name,
                    expectedException.toString()));
    } else {
        if (CommandLineExecutable.result != 0) {
            throw new RuntimeException("Error running the GATK with arguments: " + args);
        }
    }
}

From source file:com.googlecode.psiprobe.tools.logging.tomcatSlf4jLogback.TomcatSlf4jLogbackFactoryAccessor.java

/**
 * Attempts to initialize a TomcatSlf4jLogback logger factory via the given class loader.
 *  //from  w  w  w  .j  a  v  a 2  s  . c  o  m
 * @param cl the ClassLoader to use when fetching the factory
 */
public TomcatSlf4jLogbackFactoryAccessor(ClassLoader cl) throws ClassNotFoundException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException {
    // Get the singleton SLF4J binding, which may or may not be Logback, depending on the binding.
    Class clazz = cl.loadClass("org.apache.juli.logging.org.slf4j.impl.StaticLoggerBinder");
    Method m1 = MethodUtils.getAccessibleMethod(clazz, "getSingleton", new Class[] {});
    Object singleton = m1.invoke(null, null);
    Method m = MethodUtils.getAccessibleMethod(clazz, "getLoggerFactory", new Class[] {});
    Object loggerFactory = m.invoke(singleton, null);

    // Check if the binding is indeed Logback
    Class loggerFactoryClass = cl.loadClass("org.apache.juli.logging.ch.qos.logback.classic.LoggerContext");
    if (!loggerFactoryClass.isInstance(loggerFactory)) {
        throw new RuntimeException("The singleton SLF4J binding was not Logback");
    }
    setTarget(loggerFactory);
}

From source file:EventListenerList.java

/**
 * Adds the given listener as a listener of the given type.
 *///from ww w . j  a  va  2 s .  c om

public void add(Class listenerType, EventListener listener) {
    if (listener == null)
        return;
    if (!listenerType.isInstance(listener))
        throw new IllegalArgumentException("The listener is not an instance of the listener class.");

    Object[] tempArr = new Object[listenerList.length + 2];
    System.arraycopy(listenerList, 0, tempArr, 0, listenerList.length);

    tempArr[tempArr.length - 2] = listenerType;
    tempArr[tempArr.length - 1] = listener;

    listenerList = tempArr;
}

From source file:org.ocelotds.marshalling.ArgumentServices.java

void checkClass(Object object, Class cls) throws JsonUnmarshallingException {
    if (!cls.isInstance(object)) {
        throw new JsonUnmarshallingException(object + " is not instance of " + cls);
    }/*from w  ww . ja v a 2  s  .c om*/
}

From source file:edu.umd.cs.psl.util.graph.memory.MemoryNode.java

@Override
public Property createProperty(String type, Object attribute) {
    Integer pt = graph.getPropertyType(type);
    if (pt != null) {
        Class<?> clazz = graph.getPropertyClass(pt);
        if (clazz.isInstance(attribute)) {
            MemoryProperty p = new MemoryProperty(graph, pt, this, attribute);
            properties.put(pt, p);//from   w ww  . ja  va 2s.  c o m
            graph.notifyPropertyCreated(this, p);
            return p;
        } else
            throw new IllegalArgumentException("Attribute is not of the correct type.");
    } else
        throw new IllegalArgumentException("Unknown property type.");
}

From source file:org.openrdf.repository.sail.SailRepository.java

private <T> T findSailOf(Sail sail, Class<T> type) {
    if (type.isInstance(sail)) {
        return type.cast(sail);
    } else if (sail instanceof StackableSail) {
        return findSailOf(((StackableSail) sail).getBaseSail(), type);
    } else {/*w  w  w  . ja  va 2s.  c  om*/
        return null;
    }
}

From source file:EventListenerList.java

/**
 * Removes the given listener as a listener of the specified type.
 *//*from   www  . j  a  va 2s  .  c  o  m*/

public void remove(Class listenerType, EventListener listener) {
    if (listener == null)
        return;
    if (!listenerType.isInstance(listener))
        throw new IllegalArgumentException("The listener is not an instance of the listener class.");

    for (int i = 0; i < listenerList.length; i += 2) {
        if ((listenerList[i] == listenerType) && (listenerList[i + 1].equals(listener))) {
            Object[] tempArr = new Object[listenerList.length - 2];
            System.arraycopy(listenerList, 0, tempArr, 0, i);
            System.arraycopy(listenerList, i + 2, tempArr, i, listenerList.length - i - 2);
            listenerList = tempArr;
            return;
        }
    }
}

From source file:guru.qas.martini.jmeter.sampler.SamplerContext.java

protected <T> T get(String key, Class<? extends T> implementation) {
    Map<String, Object> samplerContext = context.getSamplerContext();
    Object o = samplerContext.get(key);
    return implementation.isInstance(o) ? implementation.cast(o) : null;
}

From source file:org.atemsource.atem.impl.pojo.persistence.InMemoryPojoStore.java

public <T> Collection<T> getEntities(Class<T> clazz) {
    List<T> entities = new ArrayList<T>();
    for (Object entity : entities) {
        if (clazz.isInstance(entity)) {
            entities.add((T) entity);//from w w  w .j  ava  2  s . c om
        }
    }
    return entities;
}