Example usage for java.lang Class isEnum

List of usage examples for java.lang Class isEnum

Introduction

In this page you can find the example usage for java.lang Class isEnum.

Prototype

public boolean isEnum() 

Source Link

Document

Returns true if and only if this class was declared as an enum in the source code.

Usage

From source file:ca.oson.json.Oson.java

private <T> Object getParameterValue(Map<String, Object> map, Class<T> valueType, String parameterName,
        Class parameterType) {
    Object parameterValue = getMapValue(map, parameterName);

    Class fieldType = null;/*from   w w w .jav  a2s.  co m*/
    if (Map.class.isAssignableFrom(parameterType)) {
        // just give the whole map data to it
        // it can use as much of it as it likes
        parameterValue = map;

    } else {

        if (parameterType.isPrimitive() || Number.class.isAssignableFrom(parameterType)
                || parameterType == String.class || parameterType == Character.class
                || parameterType == Boolean.class || Date.class.isAssignableFrom(parameterType)
                || parameterType.isEnum() || Enum.class.isAssignableFrom(parameterType)) {
            // do nothing

        } else {
            String toGenericString = parameterType.toGenericString();
            fieldType = ObjectUtil.getComponentType(toGenericString);

            if (fieldType == null) {
                Field[] fields = getFields(valueType);
                for (Field field : fields) {
                    if (field.getName().equalsIgnoreCase(parameterName)) {
                        // private java.util.List<ca.oson.json.domain.Address> ca.oson.json.domain.Person.addressList
                        toGenericString = field.toGenericString();

                        // fieldType = field.getType(); // getClass();
                        fieldType = ObjectUtil.getComponentType(toGenericString);
                        break;
                    }
                }
            }
        }

        FieldData objectDTO = null;
        if (fieldType != null) {
            // FieldData(Object valueToProcess, Type type, boolean json2Java)
            ComponentType componentType = new ComponentType(parameterType, fieldType);
            cachedComponentTypes(componentType);

            objectDTO = new FieldData(parameterValue, parameterType, true);
            objectDTO.componentType = componentType;
        } else {
            objectDTO = new FieldData(parameterValue, parameterType, true);
        }
        objectDTO.required = true;
        parameterValue = json2Object(objectDTO);
    }

    return parameterValue;
}

From source file:ca.oson.json.Oson.java

@SuppressWarnings("unchecked")
private <E> E json2Object(FieldData objectDTO) {
    // String value, Class<E> returnType
    Class<E> returnType = objectDTO.returnType;

    Object value = objectDTO.valueToProcess;
    if (StringUtil.isNull(value)) {
        if (objectDTO.required()) {
            return (E) DefaultValue.getSystemDefault(returnType);
        }/*from  ww w . j  av a2s  . com*/

        return null;
    }

    if (returnType == null || returnType == Object.class) {
        if (StringUtil.isEmpty(value)) {
            return null;
        }

        returnType = (Class<E>) value.getClass();
        objectDTO.returnType = returnType;
    }

    // first, get the class mapper
    //if (objectDTO.fieldMapper == null && objectDTO.classMapper == null) {
    objectDTO.classMapper = getGlobalizedClassMapper(returnType);
    //}

    if (returnType == String.class) {
        return (E) json2String(objectDTO);

        //      } else if (returnType == Optional.class) {
        //         return (E) json2Optional(objectDTO);

    } else if (returnType == Character.class || returnType == char.class) {
        return (E) json2Character(objectDTO);

    } else if (returnType == Boolean.class || returnType == boolean.class
            || returnType == AtomicBoolean.class) {
        return (E) json2Boolean(objectDTO);

    } else if (Number.class.isAssignableFrom(returnType) || returnType.isPrimitive()) {

        objectDTO.valueToProcess = StringUtil.unquote(objectDTO.valueToProcess, isEscapeHtml());

        if (returnType == Integer.class || returnType == int.class) {
            return (E) json2Integer(objectDTO);
        } else if (returnType == Long.class || returnType == long.class) {
            return (E) json2Long(objectDTO);

        } else if (returnType == Double.class || returnType == double.class) {
            return (E) json2Double(objectDTO);

        } else if (returnType == Byte.class || returnType == byte.class) {
            return (E) json2Byte(objectDTO);

        } else if (returnType == Short.class || returnType == short.class) {
            return (E) json2Short(objectDTO);

        } else if (returnType == Float.class || returnType == float.class) {
            return (E) json2Float(objectDTO);

        } else if (returnType == BigDecimal.class) {
            return (E) json2BigDecimal(objectDTO);

        } else if (returnType == BigInteger.class) {
            return (E) json2BigInteger(objectDTO);

        } else if (returnType == AtomicInteger.class) {
            return (E) json2AtomicInteger(objectDTO);

        } else if (returnType == AtomicLong.class) {
            return (E) json2AtomicLong(objectDTO);

        } else { // default to Double, in case no specific type is specified
            return (E) json2Double(objectDTO);
        }

    } else if (returnType.isEnum() || Enum.class.isAssignableFrom(returnType)) {
        return (E) json2Enum(objectDTO);

    } else if (returnType == Date.class || Date.class.isAssignableFrom(returnType)) {
        return (E) json2Date(objectDTO);

    } else if (returnType.isArray() || Collection.class.isAssignableFrom(returnType)) {

        Collection<E> values = null;
        Class<?> valueType = value.getClass();
        if (Collection.class.isAssignableFrom(valueType)) {
            values = (Collection<E>) value;
        } else if (valueType.isArray()) {
            values = Arrays.asList((E[]) value);
        }

        if (values == null) {
            //return null;
        } else {
            objectDTO.valueToProcess = values;
        }

        Class<E> componentType = (Class<E>) returnType.getComponentType();
        if (componentType == null) {
            objectDTO.incrLevel();
            componentType = guessComponentType(objectDTO);
            objectDTO.descLevel();
        }
        if (componentType == null) {
            componentType = CollectionArrayTypeGuesser.guessElementType(values, returnType, getJsonClassType());
        }
        if (componentType == null)
            componentType = (Class<E>) Object.class;

        if (componentType.isPrimitive()) {
            E arr = null;
            if (componentType == int.class) {
                arr = (E) json2ArrayInt(objectDTO);

            } else if (componentType == byte.class) {
                arr = (E) json2ArrayByte(objectDTO);
            } else if (componentType == char.class) {
                arr = (E) json2ArrayChar(objectDTO);
            } else if (componentType == float.class) {
                arr = (E) json2ArrayFloat(objectDTO);
            } else if (componentType == double.class) {
                arr = (E) json2ArrayDouble(objectDTO);
            } else if (componentType == long.class) {
                arr = (E) json2ArrayLong(objectDTO);
            } else if (componentType == short.class) {
                arr = (E) json2ArrayShort(objectDTO);
            } else if (componentType == boolean.class) {
                arr = (E) json2ArrayBoolean(objectDTO);
            } else {
                // return null;
            }

            if (arr != null && objectDTO.classMapper.orderArrayAndList) {
                Arrays.sort((E[]) arr);
            }

            return arr;

        } else if (Collection.class.isAssignableFrom(returnType)) {
            return (E) json2Collection(objectDTO);

        } else {
            return (E) json2Array(objectDTO);
        }

    } else if (returnType != Optional.class && Map.class.isAssignableFrom(returnType)) {
        return (E) json2Map(objectDTO);

    } else {
        if (objectDTO.returnObj == null) {
            return (E) deserialize2Object(value, returnType, objectDTO);

        } else {
            objectDTO.returnType = returnType;
            return (E) deserialize2Object(objectDTO);
        }
    }
}