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.redhat.rcm.maven.plugin.buildmetadata.data.MetaDataProviderBuilder.java

private void setNonNullProperty(final MetaDataProvider instance, final String propertyName,
        final Object propertyValue, final Class<?> propertyType) throws MojoExecutionException {
    if (propertyValue != null) {
        final Class<? extends MetaDataProvider> metaDataProviderClass = instance.getClass();
        try {/*ww  w  .j a  v a 2s .c o  m*/
            final Field field = findField(metaDataProviderClass, propertyName);
            final Class<?> type = field.getType();
            if (type.isAssignableFrom(propertyType)) {
                field.setAccessible(true);
                field.set(instance, propertyValue);
            }
        } catch (final NoSuchFieldException e) {
            // OK, no such field, so we do not set it.
        } catch (final Exception e) {
            throw new MojoExecutionException("Cannot set property '" + propertyName
                    + "' for the instance of class '" + metaDataProviderClass.getName() + "'.", e);
        }
    }
}

From source file:gobblin.data.management.retention.policy.CombineRetentionPolicy.java

@VisibleForTesting
@SuppressWarnings("unchecked")
public Class<T> commonSuperclass(Class<T> classA, Class<T> classB) {

    if (classA.isAssignableFrom(classB)) {
        // a is superclass of b, so return class of a
        return classA;
    }// w  w  w  .j a va 2 s.  com
    // a is not superclass of b. Either b is superclass of a, or they are not in same branch
    // find closest superclass of a that is also a superclass of b
    Class<?> klazz = classA;
    while (!klazz.isAssignableFrom(classB)) {
        klazz = klazz.getSuperclass();
    }
    if (DatasetVersion.class.isAssignableFrom(klazz)) {
        return (Class<T>) klazz;
    }
    // this should never happen, but there for safety
    return (Class<T>) DatasetVersion.class;
}

From source file:ch.rasc.wampspring.method.MethodParameterConverter.java

public Object convert(MethodParameter parameter, Object argument) {
    if (argument == null) {
        if (parameter.getParameterType().getName().equals("java.util.Optional")) {
            return OptionalUnwrapper.empty();
        }//  w ww.j av a 2s.c  o m

        return null;
    }

    Class<?> sourceClass = argument.getClass();
    Class<?> targetClass = parameter.getParameterType();

    TypeDescriptor td = new TypeDescriptor(parameter);

    if (targetClass.isAssignableFrom(sourceClass)) {
        return convertListElements(td, argument);
    }

    if (this.conversionService.canConvert(sourceClass, targetClass)) {
        try {
            return convertListElements(td, this.conversionService.convert(argument, targetClass));
        } catch (Exception e) {

            TypeFactory typeFactory = this.objectMapper.getTypeFactory();
            if (td.isCollection()) {
                JavaType type = CollectionType.construct(td.getType(),
                        typeFactory.constructType(td.getElementTypeDescriptor().getType()));
                return this.objectMapper.convertValue(argument, type);
            } else if (td.isArray()) {
                JavaType type = typeFactory.constructArrayType(td.getElementTypeDescriptor().getType());
                return this.objectMapper.convertValue(argument, type);
            }

            throw e;
        }
    }
    return this.objectMapper.convertValue(argument, targetClass);
}

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

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

    if (canConvert) {
        try {
            canConvert = globalDelegate.canConvertValueForScript(Double.valueOf((String) value), expectedClass);
        } catch (final NumberFormatException ex) {
            canConvert = false;
        }
    }

    return canConvert;
}

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

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

    if (canConvert) {
        try {
            canConvert = globalDelegate.canConvertValueForJava(Double.valueOf((String) value), expectedClass);
        } catch (final NumberFormatException ex) {
            canConvert = false;
        }
    }

    return canConvert;
}

From source file:bear.task.TaskResult.java

protected SELF throwIfExceptionIs(Class<? extends Exception>... exceptions) {
    if (exception.isPresent()) {
        for (Class<? extends Exception> aClass : exceptions) {
            if (aClass.isAssignableFrom(exception.get().getClass())) {
                throw Exceptions.runtime(exception.get());
            }/*www .j  a v a  2s .c om*/
        }
    }

    return self();
}

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

/**
 * {@inheritDoc}/*from w w w  .  ja  v  a 2 s. c om*/
 */
@Override
public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    final boolean canConvert = value instanceof Date
            && expectedClass.isAssignableFrom(IdScriptableObject.class);
    return canConvert;
}

From source file:com.cloudmine.api.rest.response.CMObjectResponse.java

private <CMO extends CMObject> boolean isReturnableFor(Class<CMO> klass, CMObject object) {
    return object != null && klass != null && klass.isAssignableFrom(object.getClass());
}

From source file:com.jdon.strutsutil.util.EditeViewPageUtil.java

/**
 * ?key  /admin/productAction.do?action=edit&productId=1721
 * ?:productIdproductmodelmapping.xmlkey
 * /*from w  ww  . j  a  v  a2 s.  c om*/
 *  /admin/productAction.do?action=edit&userId=16
 * userId?modelmapping.xmlkey?override
 * 
 * 
 * @param actionMapping
 * @param request
 * @return ?key
 * @throws java.lang.Exception
 */
public Object getParamKeyValue(HttpServletRequest request, ModelHandler modelHandler) {

    Object keyValue = null;
    try {
        ModelMapping modelMapping = modelHandler.getModelMapping();
        String keyName = modelMapping.getKeyName();
        Debug.logVerbose("[JdonFramework] the keyName is  " + keyName, module);
        String keyValueS = request.getParameter(keyName);
        Debug.logVerbose("[JdonFramework] got the keyValue is  " + keyValueS, module);
        if (keyValueS == null) {
            Debug.logVerbose("[JdonFramework]the keyValue is null", module);
        }
        Class keyClassType = modelMapping.getKeyClassType();
        if (keyClassType.isAssignableFrom(String.class)) {
            keyValue = keyValueS;
        } else {
            Debug.logVerbose("[JdonFramework] convert String keyValue to" + keyClassType.getName(), module);
            keyValue = ConvertUtils.convert(keyValueS, keyClassType);
        }
    } catch (Exception e) {
        Debug.logError("[JdonFramework] getParamKeyValue error: " + e);
    }
    return keyValue;
}

From source file:com.sworddance.util.CUtilities.java

private static <T> Class<T> guessComponentType(Object... objects) {
    Class<T> type = null;
    for (Object object : newNotNullIterator(objects)) {
        Class<T> prevType = type;
        if (object.getClass().isArray()) {
            type = (Class<T>) object.getClass().getComponentType();
        } else {//from  w  ww. j av a2s .c o  m
            type = (Class<T>) object.getClass();
        }
        if (prevType != null && !type.isAssignableFrom(prevType)) {
            type = (Class<T>) Object.class;
            break;
        }
    }
    return type;
}