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:com.laidians.utils.ClassUtils.java

/**
 * Check if the right-hand side type may be assigned to the left-hand side
 * type, assuming setting by reflection. Considers primitive wrapper
 * classes as assignable to the corresponding primitive types.
 * @param lhsType the target type/*from   ww  w  .ja  va 2s.c om*/
 * @param rhsType the value type that should be assigned to the target type
 * @return if the target type is assignable from the value type
 * @see TypeUtils#isAssignable
 */
public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
    Assert.notNull(lhsType, "Left-hand side type must not be null");
    Assert.notNull(rhsType, "Right-hand side type must not be null");
    if (lhsType.isAssignableFrom(rhsType)) {
        return true;
    }
    if (lhsType.isPrimitive()) {
        Class resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
        if (resolvedPrimitive != null && lhsType.equals(resolvedPrimitive)) {
            return true;
        }
    } else {
        Class resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
        if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
            return true;
        }
    }
    return false;
}

From source file:bear.task.TaskResult.java

public SELF throwIfNot(Class... orExceptions) {
    if (exception.isPresent()) {
        for (Class<? extends Exception> aClass : orExceptions) {
            if (aClass.isAssignableFrom(exception.get().getClass())) {
                return self();
            }/*w  ww  .  j a  v a  2 s. c  om*/
        }

        throw Exceptions.runtime(exception.get());
    }

    return self();
}

From source file:com.agimatec.validation.jsr303.AgimatecValidatorFactory.java

/**
 * Return an object of the specified type to allow access to the
 * provider-specific API.  If the Bean Validation provider
 * implementation does not support the specified class, the
 * ValidationException is thrown.//from   w  ww .  j  av a 2 s.  c  o m
 *
 * @param type the class of the object to be returned.
 * @return an instance of the specified class
 * @throws ValidationException if the provider does not
 *                             support the call.
 */
public <T> T unwrap(Class<T> type) {
    if (type.isAssignableFrom(getClass())) {
        return (T) this;
    } else if (!type.isInterface()) {
        return SecureActions.newInstance(type);
    } else {
        try {
            Class<T> cls = ClassUtils.getClass(type.getName() + "Impl");
            return SecureActions.newInstance(cls);
        } catch (ClassNotFoundException e) {
            throw new ValidationException("Type " + type + " not supported");
        }
    }
}

From source file:com.shigengyu.hyperion.core.WorkflowState.java

public boolean belongsToWorkflow(Class<? extends WorkflowDefinition> workflowDefinitionClass) {
    return stateOwner != null && workflowDefinitionClass.isAssignableFrom(stateOwner.workflow());
}

From source file:edu.northwestern.bioinformatics.studycalendar.restlets.AmendedTemplateHelper.java

@SuppressWarnings({ "unchecked", "RawUseOfParameterizedType" })
private <N extends PlanTreeNode> N drillDown(PlanTreeNode from, Class<N> targetClass) {
    if (targetClass.isAssignableFrom(from.getClass()))
        return (N) from;
    if (!PlanTreeInnerNode.class.isAssignableFrom(from.getClass())) {
        log.debug("Reached tree leaves without finding desired node");
        return null;
    }/*from  w w w  .jav  a  2  s . c o m*/
    PlanTreeInnerNode innerNode = (PlanTreeInnerNode) from;
    UriTemplateParameters param = PARAMS_FOR_NODE_TYPES.get(innerNode.childClass());
    String key = param.extractFrom(getRequest());
    PlanTreeNode next = innerNode.findNaturallyMatchingChild(key);
    if (next == null) {
        throw new NotFound("No %s identified by '%s' in %s",
                StringTools.humanizeClassName(innerNode.childClass().getSimpleName()), key,
                StringTools.humanizeClassName(from.getClass().getSimpleName()));
    } else {
        return drillDown(next, targetClass);
    }
}

From source file:h2o.common.spring.util.ClassUtils.java

/**
 * Check if the right-hand side type may be assigned to the left-hand side
 * type, assuming setting by reflection. Considers primitive wrapper
 * classes as assignable to the corresponding primitive types.
 * @param lhsType the target type//from   w  w w.  ja v a2 s . c o m
 * @param rhsType the value type that should be assigned to the target type
 * @return if the target type is assignable from the value type
 * @see TypeUtils#isAssignable
 */
@SuppressWarnings("rawtypes")
public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
    Assert.notNull(lhsType, "Left-hand side type must not be null");
    Assert.notNull(rhsType, "Right-hand side type must not be null");
    if (lhsType.isAssignableFrom(rhsType)) {
        return true;
    }
    if (lhsType.isPrimitive()) {
        Class resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
        if (resolvedPrimitive != null && lhsType.equals(resolvedPrimitive)) {
            return true;
        }
    } else {
        Class resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
        if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
            return true;
        }
    }
    return false;
}

From source file:com.astamuse.asta4d.util.annotation.AnnotatedPropertyInfo.java

@SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationCls) {
    for (Annotation anno : annotationList) {
        if (annotationCls.isAssignableFrom(anno.getClass())) {
            return (A) anno;
        }/*from  w ww .j a v a  2s. c  o m*/
    }
    return null;
}

From source file:com.bstek.dorado.data.type.AbstractDataType.java

public Object fromObject(Object value) {
    if (value == null) {
        return null;
    }//from  ww  w .j a v a 2s.c  o m

    Class<?> targetType = this.getMatchType();
    if (targetType == null) {
        return value;
    } else if (targetType.isAssignableFrom(value.getClass())) {
        return value;
    }

    throw new DataConvertException(value.getClass(), getMatchType());
}

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

/**
 * {@inheritDoc}// w w w.jav  a 2 s  .com
 */
@Override
public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    final boolean canConvert = value instanceof Number
            && (expectedClass.isAssignableFrom(Number.class) || SIMPLE_NUMBER_CLASSES.contains(expectedClass));
    return canConvert;
}

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

/**
 * {@inheritDoc}//from w w  w.  j a v  a 2s . c o m
 */
@Override
public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    final boolean canConvert = value instanceof Number
            && (expectedClass.isAssignableFrom(Number.class) || SIMPLE_NUMBER_CLASSES.contains(expectedClass));
    return canConvert;
}