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.castlemock.web.basis.model.ServiceFacadeImpl.java

/**
 * The initialize method is responsible for for locating all the service instances for a specific module
 * and organizing them depending on the type.
 * @param clazz The class of the {@link ServiceAdapter} that the facade is managing
 * @see Service//from  w w  w  .j a  va 2  s .c  o m
 * @see TypeIdentifier
 * @see TypeIdentifiable
 */
protected void initiate(final Class<?> clazz) {
    final Map<String, Object> foundServices = applicationContext
            .getBeansWithAnnotation(org.springframework.stereotype.Service.class);

    for (Map.Entry<String, Object> entry : foundServices.entrySet()) {
        final Object value = entry.getValue();
        if (clazz.isInstance(value)) {
            final SA serviceAdapter = (SA) value;
            final String type = serviceAdapter.getTypeIdentifier().getType().toUpperCase();
            services.put(type, serviceAdapter);
        }
    }
}

From source file:com.facebook.presto.jdbc.PrestoConnection.java

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
    return iface.isInstance(this);
}

From source file:ch.sdi.report.SdiReporter.java

/**
 * @param aCollector//from  ww  w .  j  a v a 2  s.  co  m
 * @param aString
 * @return
 */
private <T> T findSimpleEntry(ReportType aReportType, String aKey, Class<T> aClass) {
    Collection<ReportMsg> messages = findOfTypes(aReportType);
    for (ReportMsg msg : messages) {
        if (msg.getKey().equals(aKey)) {
            Object o = msg.getValue();

            if (aClass.isInstance(o)) {
                return aClass.cast(o);

            } // if aClass.isInstance( obj )

            myLog.warn("Entry found for report type " + aReportType + " and key " + aKey
                    + ", but not of desired type! Entry: " + o.getClass().getName() + "; expected: "
                    + aClass.getName());
            return null;

        } // if key.equals( aKey )
    }

    myLog.warn("No entry found for report type " + aReportType + " and key " + aKey);
    return null;
}

From source file:com.linkedin.pinot.core.plan.maker.MetadataAndDictionaryAggregationPlanMakerTest.java

@Test(dataProvider = "testPlanNodeMakerDataProvider")
public void testInstancePlanMakerForMetadataAndDictionaryPlan(String query,
        Class<? extends PlanNode> planNodeClass, Class<? extends PlanNode> starTreePlanNodeClass) {

    BrokerRequest brokerRequest = COMPILER.compileToBrokerRequest(query);
    PlanNode plan = PLAN_MAKER.makeInnerSegmentPlan(_indexSegment, brokerRequest);
    Assert.assertTrue(planNodeClass.isInstance(plan));

    plan = PLAN_MAKER.makeInnerSegmentPlan(_starTreeIndexSegment, brokerRequest);
    Assert.assertTrue(starTreePlanNodeClass.isInstance(plan));
}

From source file:com.lrs.enviroment.Metadata.java

protected <T> T convertToType(Object value, Class<T> requiredType) {
    if (value instanceof Map && ((Map) value).isEmpty()) {
        return null;
    } else if (requiredType.isInstance(value)) {
        return (T) value;
    }//from w w w.ja  v  a2 s.  c om
    if (requiredType == String.class) {
        return (T) String.valueOf(value);
    } else if (requiredType == Boolean.class) {
        Boolean booleanObject = toBooleanObject(String.valueOf(value));
        return (T) (booleanObject != null ? booleanObject : Boolean.FALSE);
    } else if (requiredType == Integer.class) {
        if (value instanceof Number) {
            return (T) Integer.valueOf(((Number) value).intValue());
        } else {
            return (T) Integer.valueOf(String.valueOf(value));
        }
    } else if (requiredType == Long.class) {
        if (value instanceof Number) {
            return (T) Long.valueOf(((Number) value).longValue());
        } else {
            return (T) Long.valueOf(String.valueOf(value));
        }
    } else if (requiredType == Double.class) {
        if (value instanceof Number) {
            return (T) Double.valueOf(((Number) value).doubleValue());
        } else {
            return (T) Double.valueOf(String.valueOf(value));
        }
    } else if (requiredType == BigDecimal.class) {
        return (T) new BigDecimal(String.valueOf(value));
    } else {
        return convertToOtherTypes(value, requiredType);
    }
}

From source file:hudson.PluginManager.java

/**
 * Get the plugin instance that implements a specific class, use to find your plugin singleton.
 * Note: beware the classloader fun./*from  w ww.j  a va2 s  . c om*/
 * @param pluginClazz The class that your plugin implements.
 * @return The plugin singleton or <code>null</code> if for some reason the plugin is not loaded.
 */
public PluginWrapper getPlugin(Class<? extends Plugin> pluginClazz) {
    for (PluginWrapper p : plugins) {
        if (pluginClazz.isInstance(p.getPlugin()))
            return p;
    }
    return null;
}

From source file:de.Keyle.MyPet.api.util.hooks.PluginHookManager.java

/**
 * searches for an instance of a plugin//from   w  w  w .  j a v  a2 s . c  o m
 *
 * @param clazz class of the plugin
 * @return instance of the plugin
 */
public <T extends JavaPlugin> Optional<T> getPluginInstance(Class<T> clazz) {
    try {
        T plugin = JavaPlugin.getPlugin(clazz);
        if (plugin != null) {
            return Optional.of(plugin);
        }
    } catch (NoSuchMethodError e) {
        for (Plugin p : Bukkit.getPluginManager().getPlugins()) {
            if (clazz.isInstance(p)) {
                T plugin = clazz.cast(p);
                return Optional.of(plugin);
            }
        }
    }
    return Optional.absent();
}

From source file:com.sun.faces.demotest.HtmlUnitTestCase.java

/**
 * Depth first search from root to find all children that are
 * instances of HtmlInput.  Add them to the list.
 *//*from ww  w  .j a  v a2s. c  o m*/
protected List getAllElementsOfGivenClass(HtmlElement root, List list, Class matchClass) {
    Iterator iter = null;
    if (null == root) {
        return list;
    }
    if (null == list) {
        list = new ArrayList();
    }
    iter = root.getAllHtmlChildElements();
    while (iter.hasNext()) {
        getAllElementsOfGivenClass((HtmlElement) iter.next(), list, matchClass);
    }
    if (matchClass.isInstance(root)) {
        if (!list.contains(root)) {
            list.add(root);
        }
    }
    return list;
}

From source file:eionet.cr.util.Util.java

/**
 *
 * @param pageContext/*from w ww  . ja  va2  s.  c om*/
 * @param objectClass
 * @return
 */
public static Object findInAnyScope(PageContext pageContext, Class objectClass) {

    if (pageContext == null || objectClass == null) {
        return null;
    }

    int[] scopes = { PageContext.APPLICATION_SCOPE, PageContext.PAGE_SCOPE, PageContext.REQUEST_SCOPE,
            PageContext.SESSION_SCOPE };
    for (int i = 0; i < scopes.length; i++) {
        Enumeration attrs = pageContext.getAttributeNamesInScope(scopes[i]);
        while (attrs != null && attrs.hasMoreElements()) {
            String name = (String) attrs.nextElement();
            Object o = pageContext.getAttribute(name, scopes[i]);
            if (o != null && objectClass.isInstance(o)) {
                return o;
            }
        }
    }

    return null;
}