Example usage for java.lang Class isAssignableFrom

List of usage examples for java.lang Class isAssignableFrom

Introduction

In this page you can find the example usage for java.lang Class isAssignableFrom.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isAssignableFrom(Class<?> cls);

Source Link

Document

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Usage

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

/**
 * {@inheritDoc}// w w w  . ja va 2 s  .c  o m
 */
@Override
public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    boolean canConvert = value instanceof IdScriptableObject && expectedClass.isAssignableFrom(Map.class);

    if (canConvert) {
        final IdScriptableObject object = (IdScriptableObject) value;
        final Object[] propIds = object.getIds();
        for (int i = 0; i < propIds.length; i++) {
            // work on each key in turn
            final Object propId = propIds[i];

            // we are only interested in keys that indicate a list of values
            if (propId instanceof String) {
                // get the value out for the specified key
                final Object val = object.get((String) propId, object);
                canConvert = canConvert && globalDelegate.canConvertValueForJava(val);
            }
        }
    }

    return canConvert;
}

From source file:grails.config.GrailsConfig.java

/**
 * Utility method for retrieving a configuration value and performs type checking
 * (i.e. logs a verbose WARN message on type mismatch).
 *
 * @param key the flattened key//from w w w .  j av a2 s .c  o  m
 * @param type the required type
 * @param <T> the type parameter
 * @return the value retrieved by ConfigurationHolder.flatConfig
 */
public <T> T get(String key, Class<T> type) {
    Object o = get(key);
    if (o != null) {
        if (!type.isAssignableFrom(o.getClass())) {
            LOG.warn(String.format("Configuration type mismatch for configuration value %s (%s)", key,
                    type.getName()));
            return null;
        }
        return type.cast(o);
    }
    return null;
}

From source file:com.wavemaker.tools.project.upgrade.five_dot_zero.AddServiceWireUpgradeTask.java

/**
 * Actually do the upgrade.//ww  w  . j  a va  2 s  . c o m
 */
private void upgradeServices(DesignServiceManager dsm, Project project, UpgradeInfo upgradeInfo) {

    List<String> touchedServices = new ArrayList<String>();
    ClassLoader cl = ProjectUtils.getClassLoaderForProject(project);

    for (Service service : dsm.getServices()) {
        // ignore runtimeService
        if (DesignServiceManager.RUNTIME_SERVICE_ID.equals(service.getId())) {
            continue;
        }

        if (service.getSpringFile() == null) {
            throw new WMRuntimeException(MessageResource.ADD_SRV_UPGRADE_NO_SPRING_FILE,
                    project.getProjectName());
        }

        try {
            File springFile = dsm.getServiceRuntimeFolder(service.getId()).getFile(service.getSpringFile());
            if (!springFile.exists()) {
                DesignServiceManager.generateSpringServiceConfig(service.getId(), service.getClazz(),
                        dsm.getDesignServiceType(service.getType()), springFile, project);
                continue;
            }

            Beans beans = SpringConfigSupport.readBeans(springFile);

            boolean foundServiceWire = false;
            for (Bean bean : beans.getBeanList()) {
                if (bean.getClazz() != null) {
                    Class<?> klass = cl.loadClass(bean.getClazz());
                    Class<?> serviceWireClass = cl.loadClass(ServiceWire.class.getName());
                    if (serviceWireClass.isAssignableFrom(klass)) {
                        foundServiceWire = true;
                        break;
                    }
                }
            }

            if (!foundServiceWire) {
                Bean serviceWireBean = DesignServiceManager
                        .generateServiceWireBean(dsm.getDesignServiceType(service.getType()), service.getId());
                beans.addBean(serviceWireBean);
                SpringConfigSupport.writeBeans(beans, springFile);
                touchedServices.add(service.getId());
            }
        } catch (JAXBException e) {
            throw new WMRuntimeException(e);
        } catch (IOException e) {
            throw new WMRuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new WMRuntimeException(e);
        }
    }

    if (!touchedServices.isEmpty()) {
        upgradeInfo.addMessage("New ServiceWire added to services: " + StringUtils.join(touchedServices, ", "));
    }
}

From source file:ca.uhn.fhir.rest.method.MethodUtil.java

public static Integer findParamAnnotationIndex(Method theMethod, Class<?> toFind) {
    int paramIndex = 0;
    for (Annotation[] annotations : theMethod.getParameterAnnotations()) {
        for (int annotationIndex = 0; annotationIndex < annotations.length; annotationIndex++) {
            Annotation nextAnnotation = annotations[annotationIndex];
            Class<? extends Annotation> class1 = nextAnnotation.getClass();
            if (toFind.isAssignableFrom(class1)) {
                return paramIndex;
            }/*from  w  w  w. j ava2  s.com*/
        }
        paramIndex++;
    }
    return null;
}

From source file:com.jaspersoft.jasperserver.api.common.service.impl.BeanForInterfaceFactoryImpl.java

public boolean hasMapping(Map classToBeanMappings, Class _class) {
    if (classToBeanMappings == null) {
        return false;
    }/*from w w  w .jav a2  s  .  c  o  m*/

    //TODO cache
    try {
        boolean found = false;
        for (Iterator it = classToBeanMappings.keySet().iterator(); it.hasNext();) {
            String itfName = (String) it.next();
            Class itf = Class.forName(itfName, true, Thread.currentThread().getContextClassLoader());
            if (itf.isAssignableFrom(_class)) {
                found = true;
                break;
            }
        }
        return found;
    } catch (ClassNotFoundException e) {
        log.error(e, e);
        throw new JSExceptionWrapper(e);
    }
}

From source file:ca.uhn.fhir.rest.server.interceptor.ExceptionHandlingInterceptor.java

private void populateDetails(FhirContext theCtx, Throwable theException, IBaseOperationOutcome theOo) {
    if (myReturnStackTracesForExceptionTypes != null) {
        for (Class<?> next : myReturnStackTracesForExceptionTypes) {
            if (next.isAssignableFrom(theException.getClass())) {
                String detailsValue = theException.getMessage() + "\n\n"
                        + ExceptionUtils.getStackTrace(theException);
                OperationOutcomeUtil.addIssue(theCtx, theOo, "error", detailsValue, null, PROCESSING);
                return;
            }/* w w  w  .j a v  a 2  s .c  o  m*/
        }
    }

    OperationOutcomeUtil.addIssue(theCtx, theOo, "error", theException.getMessage(), null, PROCESSING);
}

From source file:de.metas.ui.web.config.WebuiExceptionHandler.java

private final boolean isErrorMatching(final Class<?> baseClass, final Class<?> clazz) {
    return baseClass.isAssignableFrom(clazz);
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.StringToNumberConverter.java

/**
 *
 * {@inheritDoc}//from   ww w .jav  a  2  s. co  m
 */
@Override
public int getForScriptConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) {
    final int confidence;
    if (String.class.isAssignableFrom(valueInstanceClass) && (expectedClass.isAssignableFrom(Number.class)
            || SIMPLE_NUMBER_CLASSES.contains(expectedClass))) {
        confidence = MEDIUM_CONFIDENCE;
    } else {
        confidence = LOWEST_CONFIDENCE;
    }
    return confidence;
}

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

/**
 * {@inheritDoc}/*w  w w  .  j a v a 2  s .  com*/
 */
@Override
public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    final boolean canConvert = Context.getCurrentContext() != null
            && expectedClass.isAssignableFrom(Scriptable.class)
            && !this.currentConversions.get().contains(value);
    return canConvert;
}