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.bstek.dorado.data.type.DateDataType.java

/**
 * ????/*from ww  w  .  j a v  a2  s  .  co m*/
 * 
 * @param value
 *            ?
 * @return ?
 * @throws ValueConvertException
 */
protected Date convertObject2Date(Object value) throws DataConvertException {
    Class<?> targetType = this.getMatchType();
    if (targetType != null && targetType.isAssignableFrom(value.getClass())
            || targetType == null && value instanceof Date) {
        return (Date) value;
    } else if (value instanceof String) {
        return convertText2Date((String) value);
    } else if (value instanceof Long) {
        return new Date(((Long) value).longValue());
    } else {
        throw new DataConvertException(value.getClass(), getMatchType());
    }
}

From source file:com.mmnaseri.dragonfly.entity.impl.DefaultEntityHandlerContext.java

@Override
public <E> EntityHandler<E> getHandler(Class<E> entityType) {
    if (entityHandlers.containsKey(entityType)) {
        //noinspection unchecked
        return (EntityHandler<E>) entityHandlers.get(entityType);
    }//from w ww.  j  a v  a2s. c om
    for (Class<?> registeredType : entityHandlers.keySet()) {
        if (registeredType.isAssignableFrom(entityType)) {
            //noinspection unchecked
            return (EntityHandler<E>) entityHandlers.get(registeredType);
        }
    }
    final GenericEntityHandler<E> entityHandler = new GenericEntityHandler<E>(entityType, entityContext,
            tableMetadataRegistry.getTableMetadata(entityType));
    entityHandlers.put(entityType, entityHandler);
    return entityHandler;
}

From source file:org.arrow.data.Neo4JDataConfiguration.java

/**
 * {@inheritDoc}// ww  w  . j  a  va2s.  co m
 */
@Override
public Neo4jTemplate neo4jTemplate() throws Exception {
    return new Neo4jTemplate(mappingInfrastructure().getObject()) {

        @SuppressWarnings("unchecked")
        @Override
        public <S extends PropertyContainer, T> T createEntityFromState(S state, Class<T> type,
                MappingPolicy mappingPolicy) {

            if (type.isInterface() && type.isAssignableFrom(ProcessSpecification.class)) {
                return (T) super.createEntityFromState(state, Process.class, mappingPolicy);
            }
            return super.createEntityFromState(state, type, mappingPolicy);
        }

    };
}

From source file:org.usrz.libs.riak.introspection.RiakIntrospector.java

public boolean getConverter(Class<?> in, Class<?> out) {
    converters.put(Iterable.class, HashSet.class);

    //        if (in.containedTypeCount() != out.containedTypeCount()) return false;

    for (Map.Entry<Class<?>, Class<?>> entry : converters.entrySet()) {
        final Class<?> cin = entry.getKey();
        final Class<?> cout = entry.getValue();

        if ((cin.isAssignableFrom(in)) && (out.isAssignableFrom(cout))) {
            return true;
        }/*from   w  w  w .j a v  a2 s .c o  m*/
    }

    return false;
}

From source file:org.shredzone.cilla.core.event.EventInvoker.java

/**
 * Evaluates a single parameter of the handler method's parameter list.
 *
 * @param type//w w  w  .ja v  a 2  s  .c om
 *            Expected parameter type
 * @param event
 *            Event with further data
 * @return Parameter value
 */
private Object evaluateParameter(Class<?> type, Event<?> event) {
    Object source = event.getSource();
    if (source != null && type.isAssignableFrom(source.getClass())) {
        return type.cast(source);
    }

    if (type.isAssignableFrom(EventType.class)) {
        return event.getType();
    }

    if (type.isAssignableFrom(Event.class)) {
        return event;
    }

    // Alas, we cannot find anything to satisfy this parameter
    throw new IllegalStateException("Unknown parameter type " + type.getName());
}

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

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

    Class<?> targetType = this.getMatchType();
    if (targetType != null && !targetType.isAssignableFrom(value.getClass())) {
        throw new IllegalArgumentException("Type error! " + targetType.getName() + " expected but "
                + value.getClass().getName() + " found.");
    }
    return value;
}

From source file:net.kuujo.vertigo.util.serialization.impl.InclusiveAnnotationIntrospector.java

/**
 * Indicates whether the given type is a "serializable" type.
 *///from ww w  .  ja  v  a  2  s  .c o  m
private boolean isSerializableType(Class<?> type) {
    Boolean serializable = cache.get(type);
    if (serializable != null) {
        return serializable;
    }

    if (type == Object.class) {
        return true;
    }

    if (primitiveTypes.contains(type)) {
        cache.put(type, true);
        return true;
    }

    if (JsonSerializable.class.isAssignableFrom(type)) {
        cache.put(type, true);
        return true;
    }

    for (Class<?> clazz : serializableTypes) {
        if (clazz.isAssignableFrom(type)) {
            cache.put(type, true);
            return true;
        }
    }
    cache.put(type, false);
    return false;
}

From source file:com.bstek.dorado.data.entity.EntityUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T toEntity(Object object, DataType dataType) throws Exception {
    if (isSimpleValue(object)) {
        return (T) object;
    }//w  ww .j  av a2s.co m

    // Expression
    if (object instanceof Expression) {
        object = ((Expression) object).evaluate();
    }

    Class<?> cl = object.getClass();
    if (object instanceof Collection) {
        Collection<?> entities = (Collection<?>) object;
        if (entities instanceof EntityCollection<?>) {
            DataType currentDataType = getDataType(entities);
            if (currentDataType != dataType && dataType != null) {
                ((EntityCollection<?>) entities).setDataType((AggregationDataType) dataType);
            }
            return (T) object;
        }
    } else {
        EntityEnhancer entityEnhancer = getEntityEnhancer(object);
        if (entityEnhancer != null) {
            DataType currentDataType = entityEnhancer.getDataType();
            if (currentDataType != dataType && dataType != null) {
                entityEnhancer.setDataType((EntityDataType) dataType);
            }
            return (T) object;
        }
    }

    boolean useProxy = true;
    if (dataType == null) {
        dataType = getDataType(cl);
    }

    if (dataType != null) {
        Class<?> matchType = dataType.getMatchType();
        if (matchType != null) {
            boolean matching = false;
            if (matchType.isPrimitive()) {
                matching = ClassUtils.primitiveToWrapper(matchType).equals(cl);
            } else {
                matching = matchType.isAssignableFrom(cl);
            }
            if (!matching) {
                if (dataType instanceof EntityDataType) {
                    DataType realDataType = getDataType(cl);
                    if (realDataType instanceof EntityDataType) {
                        matching = true;
                        useProxy = false;
                    }
                } else if (dataType instanceof AggregationDataType) {
                    DataType realDataType = getDataType(cl);
                    if (realDataType instanceof AggregationDataType) {
                        matching = true;
                    }
                }

                if (!matching) {
                    throw new IllegalArgumentException(
                            "Result type mismatch. expect [" + matchType + "] but [" + cl + "].");
                }
            }
        }
    }

    if (object instanceof Collection) {
        // Collection???
        AggregationDataType AggregationDataType = (AggregationDataType) dataType;
        if (object instanceof List) {
            object = new EntityList((List) object, AggregationDataType);
        } else if (object instanceof Set) {
            object = new EntitySet((Set) object, AggregationDataType);
        } else {
            throw new IllegalArgumentException("Unsupported result type [" + cl.getName() + "].");
        }
    } else if (object.getClass().isArray()) {
        Class type = object.getClass();
        if (isSimpleType(type.getComponentType())) {
            return (T) object;
        } else {
            // ??java.util.List
            logger.warn("Dorado converted a " + object.getClass() + " to " + List.class + " automatically.");

            List list = CollectionUtils.arrayToList(object);
            object = new EntityList(list, (AggregationDataType) dataType);
        }
    } else {
        // TODO Entity????
        // Entity???
        EntityDataType entityDataType = (EntityDataType) dataType;
        if (!(entityDataType instanceof CustomEntityDataType)) {
            if (useProxy) {
                // ????
                if (object instanceof EnhanceableEntity) {
                    EnhanceableEntity enhanceableEntity = (EnhanceableEntity) object;
                    if (enhanceableEntity.getEntityEnhancer() == null) {
                        EntityEnhancer entityEnhancer;
                        if (object instanceof Map) {
                            entityEnhancer = new EnhanceableMapEntityEnhancer(entityDataType);
                        } else {
                            entityEnhancer = new EnhanceableBeanEntityEnhancer(entityDataType,
                                    object.getClass());
                        }
                        enhanceableEntity.setEntityEnhancer(entityEnhancer);
                    }
                    return (T) object;
                } else {
                    MethodInterceptor[] mis = getMethodInterceptorFactory().createInterceptors(entityDataType,
                            object.getClass(), object);
                    object = ProxyBeanUtils.proxyBean(object, mis);
                }
            } else {
                // ????
                Class<?> creationType = entityDataType.getCreationType();
                if (creationType == null) {
                    creationType = entityDataType.getMatchType();
                }

                Map map;
                if (object instanceof Map) {
                    map = (Map) object;
                } else {
                    map = BeanMap.create(object);
                }

                if (creationType == null) {
                    Record record = new Record(map);
                    record.setEntityEnhancer(new EnhanceableMapEntityEnhancer(entityDataType));
                    object = record;
                } else {
                    MethodInterceptor[] mis = getMethodInterceptorFactory().createInterceptors(entityDataType,
                            creationType, null);
                    object = ProxyBeanUtils.createBean(creationType, mis);
                    setValues(object, map);
                }
            }
        } else {
            object = ((CustomEntityDataType) entityDataType).toMap(object);
        }
    }
    return (T) object;
}

From source file:org.springmodules.cache.config.CachingListenerValidatorImpl.java

/**
 * @see CachingListenerValidator#validate(Object, int, ParserContext)
 *//* w  w  w  . ja  va 2 s  .  c  o  m*/
public void validate(Object cachingListener, int index, ParserContext parserContext)
        throws IllegalStateException {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    BeanDefinition beanDefinition = null;

    if (cachingListener instanceof RuntimeBeanReference) {
        String beanName = ((RuntimeBeanReference) cachingListener).getBeanName();
        beanDefinition = registry.getBeanDefinition(beanName);

    } else if (cachingListener instanceof BeanDefinitionHolder) {
        beanDefinition = ((BeanDefinitionHolder) cachingListener).getBeanDefinition();
    } else {
        throw new IllegalStateException("The caching listener reference/holder [" + index
                + "] should be an instance of <" + RuntimeBeanReference.class.getName() + "> or <"
                + BeanDefinitionHolder.class.getName() + ">");
    }

    Class expectedClass = CachingListener.class;
    Class actualClass = resolveBeanClass(beanDefinition);

    if (!expectedClass.isAssignableFrom(actualClass)) {
        throw new IllegalStateException("The caching listener [" + index + "] should be an instance of <"
                + expectedClass.getName() + ">");
    }
}

From source file:com.clicktravel.cheddar.application.retry.RetryableAspect.java

private boolean shouldRetryMethod(final Class<? extends Throwable> thrownClass, final Retryable retryable,
        final int attempts) {
    if (attempts >= retryable.maxAttempts()) {
        return false;
    }//from w  w  w.j a  v a  2 s.c o  m
    final HashSet<Class<? extends Throwable>> failClasses = new HashSet<>(
            Arrays.asList(retryable.failImmediatelyOn()));
    failClasses.addAll(alwaysImmediateFailureExceptionClasses);
    for (final Class<? extends Throwable> failClass : failClasses) {
        if (failClass.isAssignableFrom(thrownClass)) {
            return false;
        }
    }
    return true;
}