Java Reflection Assignable isAssignableValue(Class type, Object value)

Here you can find the source of isAssignableValue(Class type, Object value)

Description

Determine if the given type is assignable from the given value, assuming setting by reflection.

License

Open Source License

Parameter

Parameter Description
type the target type
value the value that should be assigned to the type

Return

if the type is assignable from the value

Declaration

public static boolean isAssignableValue(Class<?> type, Object value) 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**/*www  .  ja  v a  2  s .c om*/
     * Map with primitive wrapper type as key and corresponding primitive
     * type as value, for example: Integer.class -> int.class.
     */
    private static final Map<Class<?>, Class<?>> primitiveWrapperTypeMap = new HashMap<Class<?>, Class<?>>(8);
    /**
     * Map with primitive type as key and corresponding wrapper
     * type as value, for example: int.class -> Integer.class.
     */
    private static final Map<Class<?>, Class<?>> primitiveTypeToWrapperMap = new HashMap<Class<?>, Class<?>>(8);

    /**
     * Determine if the given type is assignable from the given value,
     * assuming setting by reflection. Considers primitive wrapper classes
     * as assignable to the corresponding primitive types.
     * @param type the target type
     * @param value the value that should be assigned to the type
     * @return if the type is assignable from the value
     */
    public static boolean isAssignableValue(Class<?> type, Object value) {
        if (type == null) {
            throw new IllegalArgumentException("Type must not be null");
        }
        return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive());
    }

    /**
     * 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
     * @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) {
        if (lhsType == null) {
            throw new IllegalArgumentException("Left-hand side type must not be null");
        }
        if (rhsType == null) {
            throw new IllegalArgumentException("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;
    }
}

Related

  1. isAssignableFrom(Class class1, Class class2)
  2. isAssignableFrom(Class to, Class from)
  3. isAssignableFrom(Class to, Class from)
  4. isAssignableFrom(Class toClass, Class fromClass)
  5. isAssignableValue(Class type, Object value)
  6. isClassAssignableFrom(Class formal, Class actual)
  7. isCompatible(Class actualType, Class parameterType)
  8. isCompatible(Class parameterType, Class parameterClass)
  9. isCompatible(Class fromClass, Class toClass)