Converts "FROM" object to "TO" format / class.

@param <TO>
target class to convert the source object
@param <FROM>
source class
@param to
target type
@param from
source object

@return converted value in "TO" format / class
435597
@SuppressWarnings({ "unchecked", "rawtypes" })
	public static final <TO, FROM> TO convert(Class<TO> to, FROM from) {

		// Convert null value
		if (from == null || to == null) {
			return null;
		}

		// Convert "null" String to null
		if ("null".equals(from)) {
			return null;
		}

		// Class is same
		if (to == Object.class || from.getClass() == to) {
			return (TO) from;
		}

		// BSON conversion
		boolean isFromMap = from != null && from instanceof Map;
		if (isFromMap) {
			Object subValue = getSubValue((Map) from);
			if (subValue != null) {
				return convert(to, subValue);
			}
		}

		// Convert FROM -> TO
		DataConverterKey<TO, FROM> key = new DataConverterKey<TO, FROM>(to, (Class<FROM>) from.getClass());
		DataConverter<TO, FROM> converter = (DataConverter<TO, FROM>) converters.get(key);
		if (converter != null) {
			return converter.convert(from);
		}

		// Convert any -> TO
		converter = (DataConverter<TO, FROM>) defaultConverters.get(to);
		if (converter != null) {
			return converter.convert(from);
		}

		// Converter not found
		throw new IllegalArgumentException("Unable to convert " + from.getClass() + " to " + to
				+ ", converter not found in DataConverterRegistry!");
	}
1370925
public void convert(TypeDesc fromType, TypeDesc toType) {
        if (toType == TypeDesc.OBJECT) {
            if (fromType.isPrimitive()) {
                toType = fromType.toObjectType();
            }
            else {
                return;
            }
        }

        if (fromType == toType) {
            return;
        }

        TypeDesc fromPrimitiveType = fromType.toPrimitiveType();
        if (fromPrimitiveType == null) {
            throw invalidConversion(fromType, toType);
        }
        int fromTypeCode = fromPrimitiveType.getTypeCode();

        if (!fromType.isPrimitive()) {
            unbox(fromType, fromPrimitiveType);
        }

        TypeDesc toPrimitiveType = toType.toPrimitiveType();
        if (toPrimitiveType == null) {
            throw invalidConversion(fromType, toType);
        }
        int toTypeCode = toPrimitiveType.getTypeCode();

        int stackAdjust = 0;
        byte op;

        switch (fromTypeCode) {
        case TypeDesc.INT_CODE:
        case TypeDesc.BYTE_CODE:
        case TypeDesc.SHORT_CODE:
        case TypeDesc.CHAR_CODE:
        case TypeDesc.BOOLEAN_CODE:
            switch (toTypeCode) {
            case TypeDesc.BYTE_CODE:
                op = (fromTypeCode == TypeDesc.BYTE_CODE) ?
                    Opcode.NOP : Opcode.I2B;
                break;
            case TypeDesc.SHORT_CODE:
                op = (fromTypeCode == TypeDesc.SHORT_CODE) ?
                    Opcode.NOP : Opcode.I2S;
                break;
            case TypeDesc.CHAR_CODE:
                op = (fromTypeCode == TypeDesc.CHAR_CODE) ?
                    Opcode.NOP : Opcode.I2C;
                break;
            case TypeDesc.FLOAT_CODE:
                op = Opcode.I2F;
                break;
            case TypeDesc.LONG_CODE:
                stackAdjust = 1;
                op = Opcode.I2L;
                break;
            case TypeDesc.DOUBLE_CODE:
                stackAdjust = 1;
                op = Opcode.I2D;
                break;
            case TypeDesc.INT_CODE:
                op = Opcode.NOP;
                break;
            case TypeDesc.BOOLEAN_CODE:
                toBoolean(!toType.isPrimitive());
                return;
            default:
                throw invalidConversion(fromType, toType);
            }
            break;

        case TypeDesc.LONG_CODE:
            switch (toTypeCode) {
            case TypeDesc.INT_CODE:
                stackAdjust = -1;
                op = Opcode.L2I;
                break;
            case TypeDesc.FLOAT_CODE:
                stackAdjust = -1;
                op = Opcode.L2F;
                break;
            case TypeDesc.DOUBLE_CODE:
                op = Opcode.L2D;
                break;
            case TypeDesc.BYTE_CODE:
            case TypeDesc.CHAR_CODE:
            case TypeDesc.SHORT_CODE:
                addCode(-1, Opcode.L2I);
                convert(TypeDesc.INT, toPrimitiveType);
                // fall through
            case TypeDesc.LONG_CODE:
                op = Opcode.NOP;
                break;
            case TypeDesc.BOOLEAN_CODE:
                loadConstant(0L);
                math(Opcode.LCMP);
                toBoolean(!toType.isPrimitive());
                return;
            default:
                throw invalidConversion(fromType, toType);
            }
            break;

        case TypeDesc.FLOAT_CODE:
            switch (toTypeCode) {
            case TypeDesc.INT_CODE:
                op = Opcode.F2I;
                break;
            case TypeDesc.LONG_CODE:
                stackAdjust = 1;
                op = Opcode.F2L;
                break;
            case TypeDesc.DOUBLE_CODE:
                stackAdjust = 1;
                op = Opcode.F2D;
                break;
            case TypeDesc.BYTE_CODE:
            case TypeDesc.CHAR_CODE:
            case TypeDesc.SHORT_CODE:
                addCode(0, Opcode.F2I);
                convert(TypeDesc.INT, toPrimitiveType);
                // fall through
            case TypeDesc.FLOAT_CODE:
                op = Opcode.NOP;
                break;
            case TypeDesc.BOOLEAN_CODE:
                loadConstant(0.0f);
                math(Opcode.FCMPG);
                toBoolean(!toType.isPrimitive());
                return;
            default:
                throw invalidConversion(fromType, toType);
            }
            break;

        case TypeDesc.DOUBLE_CODE:
            switch (toTypeCode) {
            case TypeDesc.INT_CODE:
                stackAdjust = -1;
                op = Opcode.D2I;
                break;
            case TypeDesc.FLOAT_CODE:
                stackAdjust = -1;
                op = Opcode.D2F;
                break;
            case TypeDesc.LONG_CODE:
                op = Opcode.D2L;
                break;
            case TypeDesc.BYTE_CODE:
            case TypeDesc.CHAR_CODE:
            case TypeDesc.SHORT_CODE:
                addCode(-1, Opcode.D2I);
                convert(TypeDesc.INT, toPrimitiveType);
                // fall through
            case TypeDesc.DOUBLE_CODE:
                op = Opcode.NOP;
                break;
            case TypeDesc.BOOLEAN_CODE:
                loadConstant(0.0d);
                math(Opcode.DCMPG);
                toBoolean(!toType.isPrimitive());
                return;
            default:
                throw invalidConversion(fromType, toType);
            }
            break;

        default:
            throw invalidConversion(fromType, toType);
        }

        if (toType.isPrimitive()) {
            if (op != Opcode.NOP) {
                addCode(stackAdjust, op);
            }
        }
        else {
            if (op == Opcode.NOP) {
                prebox(toPrimitiveType, toType);
            }
            // Slight optimization here. Perform prebox on single word value,
            // depending on what conversion is being applied.
            else if (!fromPrimitiveType.isDoubleWord() &&
                     toPrimitiveType.isDoubleWord()) {
                prebox(fromPrimitiveType, toType);
                addCode(stackAdjust, op);
            }
            else {
                addCode(stackAdjust, op);
                prebox(toPrimitiveType, toType);
            }
            box(toPrimitiveType, toType);
        }
    }
1294760
@Override
    public Long fromString(Class targetClass, String s)
    {
        try
        {
            if (s == null)
            {
                if (log.isWarnEnabled())
                {
                    log.warn("String s is null.");
                }
                return null;
            }
            Long l = new Long(s);
            return l;
        }
        catch (NumberFormatException e)
        {
            log.error("Number format exception, Caused by {}.", e);
            throw new PropertyAccessException(e);
        }
    }
60461
@SuppressWarnings("unchecked")
    public static <T> T convertValue(Object target, Class<T> clazz) {
        if (clazz == null) {
            throw new NullPointerException("Class parameter is null!");
        }
        if (target == null) {
            return null;
        }
        if (target instanceof JsonNode) {
            return convertValue((JsonNode) target, clazz);
        }
        if (Number.class.isAssignableFrom(clazz) || byte.class == clazz || short.class == clazz
                || int.class == clazz || long.class == clazz || float.class == clazz
                || double.class == clazz) {
            return convertNumber(target, clazz);
        }
        if (clazz == Boolean.class || clazz == boolean.class) {
            return (T) convertBoolean(target);
        }
        if (clazz == Character.class || clazz == char.class) {
            return (T) convertChar(target);
        }
        if (Date.class.isAssignableFrom(clazz)) {
            return (T) convertDate(target);
        }
        if (Object[].class.isAssignableFrom(clazz) || List.class.isAssignableFrom(clazz)) {
            return (T) convertArrayOrList(target);
        }
        if (clazz.isAssignableFrom(target.getClass())) {
            return (T) target;
        }
        if (clazz == String.class) {
            return (T) target.toString();
        }
        throw new IllegalArgumentException(
                "Cannot convert an object of type [" + target.getClass() + "] to [" + clazz + "]!");
    }
474687
@Override
    public Byte fromBytes(Class targetClass, byte[] b)
    {
        try
        {
            if (b == null)
            {
                return null;
            }
            // return new Byte(new String(b, Constants.ENCODING));
            return (ByteBuffer.wrap(b).get());
        }
        catch (NumberFormatException e)
        {
            log.warn("Number format exception caught!,returning null!");
            return null;
        }
    }
31195
@Override
    public Short fromString(Class targetClass, String s)
    {
        try
        {

            if (s == null)
            {
                return null;
            }
            Short sh = new Short(s);
            return sh;
        }
        catch (NumberFormatException e)
        {
            log.error("Number format exception, Caused by {}.", e);
            throw new PropertyAccessException(e);
        }
    }
962280
@Override
    public Object fromString(Class targetClass, String s)
    {
        try
        {
            if (s == null)
            {
                return null;
            }
            Object o = (Object) s;
            return o;
        }
        catch (NumberFormatException e)
        {
            log.error("Number format exception, Caused by {}.", e);
            throw new PropertyAccessException(e);
        }
    }
1068058
@Override
    public Object convert(Class<?> targetType, Object value) {
        // are we dealing with a number to number conversion?
        if ((value instanceof Number) && Number.class.isAssignableFrom(targetType)) {
            if (Integer.class.equals(targetType)) {
                //intValue is the odd man out
                return ((Number) value).intValue();
            } else {
                //everything else is short/long/float/byte/doubleValue().
                try {
                    Method m = value.getClass().getMethod(targetType.getSimpleName().toLowerCase() + "Value");
                    return m.invoke(value);
                } catch (NoSuchMethodException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (SecurityException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                }
            }
        } else if ((value instanceof Number) && String.class.equals(targetType)) {
            //widening conversions only
            if ((value instanceof Double) || (value instanceof Float)) {
                return format.format(((Number) value).doubleValue());
            } else {
                return format.format(((Number) value).longValue());
            }
        } else if ((value instanceof String) && Number.class.isAssignableFrom(targetType)) {
            //convert fron string
            if (Integer.class.equals(targetType)) {
                //parseInt is the odd man out
                return Integer.parseInt(value.toString());
            } else {
                try {
                    //everything else is parseShort/Long/Float/etc.
                    Method m = targetType.getMethod("parse" + targetType.getSimpleName(), String.class);
                    return m.invoke(null, value.toString());
                } catch (NoSuchMethodException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (SecurityException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(NumberConverters.class.getName()).log(Level.SEVERE, null, ex);
                    throw new IllegalArgumentException("this code must not be reached");
                }
            }
        }
        throw new IllegalArgumentException("no conversion supported");
    }
202694
public static Object getTranslatedObject(Object value, Class<?> sourceClass, Class<?> targetClass)
    {
        if (sourceClass.isAssignableFrom(Date.class))
        {
            value = PropertyAccessorHelper.fromDate(targetClass, sourceClass, value);
        }
        else
        {
            value = PropertyAccessorHelper.fromSourceToTargetClass(targetClass, sourceClass, value);
        }
        return value;
    }
478549
@Override
    public void addConverter(Class<?> from, Class<?> to, Converter converter) {
        Map toMap = fromMap.get(from);
        if (toMap == null) {
            toMap = new HashMap<Class<?>, Map<Class<?>, Converter>>();
            fromMap.put(from, toMap);
        }
        toMap.put(to, converter);
    }
396539
public static boolean isAssignableFrom(TypeReference<?> from, TypeReference<?> to) {
		if (from == null) {
			return false;
		}

		if (to.equals(from)) {
			return true;
		}

		if (to.getType() instanceof Class) {
			return to.getRawType().isAssignableFrom(from.getRawType());
		} else if (to.getType() instanceof ParameterizedType) {
			return isAssignableFrom(from.getType(), (ParameterizedType) to
					.getType(), new HashMap<String, Type>());
		} else if (to.getType() instanceof GenericArrayType) {
			return to.getRawType().isAssignableFrom(from.getRawType())
					&& isAssignableFrom(from.getType(), (GenericArrayType) to
							.getType());
		} else {
			throw new AssertionError("Unexpected Type : " + to);
		}
	}
790061
public Object convert(Class<?> type, String origin) throws ConversionException {
        if (origin == null) {
            return convertNullValue(type);
        }

        Converter converter = find(type);
        try {
            return converter.convert(origin);
        } catch (Exception e) {
            throw new ConversionException(String.format("Could not convert string <%s> to type <%s>", origin, type), e);
        }
    }
107646
protected <T> TypeConverter findTypeConverter(Class<?> sourceType, Class<T> targetType, Class<? extends Annotation> formattingAnnotation) {
        TypeConverter typeConverter = null;
        List<Class> sourceHierarchy = ClassUtils.resolveHierarchy(sourceType);
        List<Class> targetHierarchy = ClassUtils.resolveHierarchy(targetType);
        boolean hasFormatting = formattingAnnotation != null;
        for (Class sourceSuperType : sourceHierarchy) {
            for (Class targetSuperType : targetHierarchy) {
                ConvertiblePair pair = new ConvertiblePair(sourceSuperType, targetSuperType, formattingAnnotation);
                typeConverter = typeConverters.get(pair);
                if (typeConverter != null) {
                    converterCache.put(pair, typeConverter);
                    return typeConverter;
                }
            }
        }
        if (hasFormatting) {
            for (Class sourceSuperType : sourceHierarchy) {
                for (Class targetSuperType : targetHierarchy) {
                    ConvertiblePair pair = new ConvertiblePair(sourceSuperType, targetSuperType);
                    typeConverter = typeConverters.get(pair);
                    if (typeConverter != null) {
                        converterCache.put(pair, typeConverter);
                        return typeConverter;
                    }
                }
            }
        }
        return typeConverter;
    }
1203182
public <T> T toObject(Class<T> toClass, Class<? extends NumConverter> converterClass) {
		return (T) toObject(toClass, null, converterClass);
	}
68413
public static boolean isAssignableFrom(Type from, Class<?> to) {
    return TypeVisitor.of(from, b -> b
        .onClass(to::isAssignableFrom)
        .onParameterizedType(pt -> TypeVisitor.of(pt.getRawType(), bb -> bb
            .onClass(to::isAssignableFrom)
            .result()))
        .onTypeVariable(tv -> isAssignableFrom(tv.getBounds(), to))
        .onWildcardType(tv -> isAssignableFrom(tv.getUpperBounds(), to))
        .result());
  }
473729
public static void copyFromTo(Object from, Object to, String fieldName) {

        if (from == null || to == null) {
            log.info("object deep copy : from or to is null ");
            return;
        }

        try {
            Method getter = getGetterMethod(from.getClass(), fieldName);

            if (getter == null) {
                //log.info("getter method not found : " + fieldName);
                return;
            }

            Method setter = getSetterMethod(to.getClass(), fieldName, getter.getReturnType());
            if (setter == null) {
                //log.info("setter method not found : " + fieldName);
                return;
            }

            setter.invoke(to, getter.invoke(from, EMPTY_CLASS));

        } catch (IllegalAccessException | InvocationTargetException e) {
            log.info("set method invoke error : " + fieldName);
        }
    }
1017938
public static Object convert(final Object value, Class<?> targetType, final String name) {
        if (value == null) {
            if (targetType.equals(Boolean.TYPE)) {
                return false;
            }
            return value;
        }

        final Class<? extends Object> actualType = value.getClass();

        if (targetType.isPrimitive()) {
            targetType =  PrimitiveTypes.valueOf(targetType.toString().toUpperCase()).getWraper();
        }

        if (targetType.isAssignableFrom(actualType)) {
            return value;
        }

        if (Number.class.isAssignableFrom(actualType) && Number.class.isAssignableFrom(targetType)) {
            return value;
        }

        if (!(value instanceof String)) {
            final String message = String.format("Expected type '%s' for '%s'. Found '%s'", targetType.getName(), name, actualType.getName());
            throw new IllegalArgumentException(message);
        }

        final String stringValue = (String) value;

        if (Enum.class.isAssignableFrom(targetType)) {
            final Class<? extends Enum> enumType = (Class<? extends Enum>) targetType;
            try {
                return Enum.valueOf(enumType, stringValue);
            } catch (final IllegalArgumentException e) {
                try {
                    return Enum.valueOf(enumType, stringValue.toUpperCase());
                } catch (final IllegalArgumentException e1) {
                    return Enum.valueOf(enumType, stringValue.toLowerCase());
                }
            }
        }

        try {
            // Force static initializers to run
            Class.forName(targetType.getName(), true, targetType.getClassLoader());
        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
        }

        final PropertyEditor editor = Editors.get(targetType);

        if (editor == null) {
            final Object result = create(targetType, stringValue);
            if (result != null) {
                return result;
            }
        }

        if (editor == null) {
            final String message = String.format("Cannot convert to '%s' for '%s'. No PropertyEditor", targetType.getName(), name);
            throw new IllegalArgumentException(message);
        }

        editor.setAsText(stringValue);
        return editor.getValue();
    }