Example usage for java.lang Class toGenericString

List of usage examples for java.lang Class toGenericString

Introduction

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

Prototype

public String toGenericString() 

Source Link

Document

Returns a string describing this Class , including information about modifiers and type parameters.

Usage

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

private <E, R> Class guessMapKeyType(FieldData newFieldData) {
    Class returnType = newFieldData.returnType;
    String toGenericString = null;
    Class fieldType = null;/*from w  w w . j av a 2 s. co m*/
    if (newFieldData.field != null) {
        toGenericString = newFieldData.field.toGenericString();
        fieldType = ObjectUtil.getComponentType(toGenericString);
    }
    if (fieldType == null && returnType != null) {
        toGenericString = returnType.toGenericString();
        fieldType = ObjectUtil.getComponentType(toGenericString);
    }
    if (fieldType == null && newFieldData.setter != null) {
        toGenericString = newFieldData.setter.toGenericString();
        fieldType = ObjectUtil.getComponentType(toGenericString);
    }

    ComponentType type = getComponentType();
    if (type == null) {
        Class enclosingtype = newFieldData.getEnclosingtype();
        if (enclosingtype != null) {
            type = cachedComponentTypes(enclosingtype);
        }
    }

    int level = newFieldData.level;

    if (fieldType != null) {
        if (type == null) {
            type = new ComponentType(newFieldData.returnType, fieldType);
            type = cachedComponentTypes(type);

        } else {
            type.add(fieldType);
        }

        if (newFieldData.returnType == Object.class) {
            newFieldData.returnType = fieldType;
        }

        return fieldType;
    }

    if (type != null) {
        ComponentType componentType = type.getComponentType();
        while (componentType != null && componentType.getComponentType() != null && level > 1) {
            componentType = componentType.getComponentType();
            level--;
        }

        if (level == 1 && componentType != null && componentType.getKeyType() != null) {
            return componentType.getKeyType();
        }

        return type.getKeyType();
    }

    return guessComponentType(newFieldData);
}

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;/* w  w  w  .j a  v  a2  s . c o 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

private <E, R> Class guessComponentType(FieldData newFieldData) {
    boolean json2Java = newFieldData.json2Java;

    if (!json2Java) {
        return newFieldData.returnType;
    } else {//  w ww  . ja  v a2s.c  om

        E obj = (E) newFieldData.valueToProcess;

        Class returnType = newFieldData.returnType;
        Class itemType = null;
        if (obj != null) {
            itemType = obj.getClass();
        }

        if (newFieldData.componentType != null) {
            Class classType = newFieldData.componentType.getClassType();
            Class cls = newFieldData.componentType.getMainComponentType();
            if (cls != null
                    && (ObjectUtil.isBasicDataType(cls) || ObjectUtil.isSameDataType(classType, returnType))) {
                return cls;
            }
        }

        String returnTypeName = null;
        if (returnType != null) {
            returnTypeName = returnType.getName();
        }
        String itemTypeName = null;
        if (itemType != null) {
            itemTypeName = itemType.getName();
        }

        int returnTypeCount = 0;
        int itemTypeCount = 0;
        String toGenericString = null;
        Class fieldType = null;
        if (newFieldData.field != null) {
            toGenericString = newFieldData.field.toGenericString();
            Class[] fieldTypes = ObjectUtil.getComponentTypes(toGenericString);

            if (fieldTypes != null && fieldTypes.length > 0) {
                if (fieldTypes.length == 1) {
                    fieldType = fieldTypes[0];
                } else {
                    fieldType = fieldTypes[0];

                    if (newFieldData.isMapValue) {
                        fieldType = fieldTypes[1];
                    } else {
                        fieldType = fieldTypes[0];
                    }
                }
            }

            if (returnTypeName != null && toGenericString.indexOf(returnTypeName) > -1) {
                returnTypeCount++;
            }
            if (itemTypeName != null && toGenericString.indexOf(itemTypeName) > -1) {
                itemTypeCount++;
            }
        }
        if (fieldType == null && returnType != null) {
            toGenericString = returnType.toGenericString();
            fieldType = ObjectUtil.getComponentType(toGenericString);
            if (returnTypeName != null && toGenericString.indexOf(returnTypeName) > -1) {
                returnTypeCount++;
            }
            if (itemTypeName != null && toGenericString.indexOf(itemTypeName) > -1) {
                itemTypeCount++;
            }
        }
        if (fieldType == null && newFieldData.setter != null) {
            toGenericString = newFieldData.setter.toGenericString();
            fieldType = ObjectUtil.getComponentType(toGenericString);
            if (returnTypeName != null && toGenericString.indexOf(returnTypeName) > -1) {
                returnTypeCount++;
            }
            if (itemTypeName != null && toGenericString.indexOf(itemTypeName) > -1) {
                itemTypeCount++;
            }

            if (fieldType == null) {
                Class[] types = newFieldData.setter.getParameterTypes();
                if (types != null && types.length > 0) {
                    toGenericString = types[0].toGenericString();
                    fieldType = ObjectUtil.getComponentType(toGenericString);
                    if (returnTypeName != null && toGenericString.indexOf(returnTypeName) > -1) {
                        returnTypeCount++;
                    }
                    if (itemTypeName != null && toGenericString.indexOf(itemTypeName) > -1) {
                        itemTypeCount++;
                    }
                }
            }
        }

        ComponentType type = getComponentType();
        if (type == null) {
            Class enclosingtype = newFieldData.getEnclosingtype();
            if (enclosingtype != null) {
                type = cachedComponentTypes(enclosingtype);
            }
        }

        int level = newFieldData.level;

        if (fieldType != null) {
            if (returnTypeCount < itemTypeCount) {
                newFieldData.returnType = itemType;
            }

            if (type == null) {
                type = new ComponentType(newFieldData.returnType, fieldType);
                type = cachedComponentTypes(type);

            } else {
                type.add(fieldType);
            }

            if (newFieldData.returnType == Object.class) {
                newFieldData.returnType = fieldType;
            }

            return fieldType;
        }

        if (returnType != null) {
            Class comptype = returnType.getComponentType();
            if (comptype != null && !comptype.isInterface() && !Modifier.isAbstract(comptype.getModifiers())) {
                return comptype;
            }
        }

        if ((returnType != null && Map.class.isAssignableFrom(returnType))
                || (itemType != null && Map.class.isAssignableFrom(itemType))) {
            String className = ((Map<String, String>) obj).get(getJsonClassType());
            if (className != null && className.length() > 0) {
                try {
                    // figure out obj's class
                    fieldType = Class.forName(className);

                    if (type == null) {
                        if (newFieldData.returnType != fieldType) {
                            type = new ComponentType(newFieldData.returnType, fieldType);
                            type = cachedComponentTypes(type);
                        }

                    } else {
                        type.add(fieldType);
                    }
                    newFieldData.returnType = fieldType;

                    return fieldType;

                } catch (ClassNotFoundException e) {
                    // e.printStackTrace();
                }
            }

            if (returnTypeCount > 0 && !Map.class.isAssignableFrom(returnType)
                    && !Collection.class.isAssignableFrom(returnType) && returnType != Optional.class
                    && returnType != Object.class && !returnType.isArray()) {
                if (type != null) {
                    type.add(returnType);
                }

                return returnType;
            }

            if (type != null) {
                ComponentType componentType = type.getComponentType();
                while (componentType != null && componentType.getComponentType() != null && level > 1) {
                    componentType = componentType.getComponentType();
                    level--;
                }

                if (level == 1 && componentType != null && componentType.getClassType() != null) {
                    return componentType.getClassType();
                }

                Class[] ctypes = type.getComponentClassType();
                float MIN_MAX_COUNT = 20.0f;
                if (ctypes != null && ctypes.length > 0) {
                    int length = ctypes.length;

                    Map<String, R> map = (Map) obj;

                    Map<String, Class> lnames = new HashMap<>(); //names.stream().map(name -> name.toLowerCase()).collect(Collectors.toSet());
                    for (String name : map.keySet()) {
                        Object value = map.get(name);
                        if (value != null) {
                            lnames.put(name.toLowerCase(), map.get(name).getClass());
                        }
                    }
                    int maxIdx = -1;
                    float maxCount = 0.0f;
                    for (int i = 0; i < length; i++) {
                        Class ctype = ctypes[i];

                        Field[] fields = getFields(ctype);
                        if (fields != null && fields.length > 0) {
                            int count = 0;

                            for (Field field : fields) {
                                String name = field.getName().toLowerCase();
                                if (lnames.containsKey(name)) {
                                    count++;

                                    Class ftype = field.getType();
                                    Class mtype = lnames.get(name);
                                    if (ObjectUtil.isSameDataType(ftype, mtype)) {
                                        count++;
                                    }
                                }
                            }
                            float currentCount = count * 100.0f / fields.length;
                            if (currentCount > maxCount) {
                                maxCount = currentCount;
                                maxIdx = i;
                            } else if (maxIdx == -1 && ctype.isAssignableFrom(itemType)) {
                                maxIdx = i;
                            }
                        }
                    }

                    if (maxIdx > -1) {
                        newFieldData.returnType = ctypes[maxIdx];

                        return ctypes[maxIdx];
                    }

                    Set<Class> processed = new HashSet(Arrays.asList(ctypes));

                    // now try to locate it in all cachedComponentTypes
                    for (Entry<Class, ComponentType> entry : cachedComponentTypes.entrySet()) {
                        Class myMasterClass = entry.getKey();

                        if (myMasterClass != this.masterClass && entry.getValue() != null) {
                            ctypes = entry.getValue().getComponentClassType();
                            if (ctypes != null) {
                                length = ctypes.length;

                                maxCount = 0.0f;
                                for (int i = 0; i < length; i++) {
                                    Class ctype = ctypes[i];

                                    if (!processed.contains(ctype)) {
                                        Field[] fields = getFields(ctype);
                                        if (fields != null && fields.length > 0) {
                                            int count = 0;

                                            for (Field field : fields) {
                                                String name = field.getName();
                                                if (name != null) {
                                                    name = name.toLowerCase();
                                                    if (lnames.containsKey(name)) {
                                                        count++;

                                                        Class ftype = field.getType();
                                                        Class mtype = lnames.get(name);
                                                        if (ObjectUtil.isSameType(ftype, mtype)) {
                                                            count++;
                                                        }
                                                    }
                                                }
                                            }
                                            float currentCount = count * 100.0f / fields.length;
                                            if (currentCount > maxCount) {
                                                maxCount = currentCount;
                                                maxIdx = i;
                                            }
                                            //                                 else if (maxIdx == -1 && ctype.isAssignableFrom(itemType)) {
                                            //                                    maxIdx = i;
                                            //                                 }
                                        }
                                    }
                                }

                                if (maxIdx > -1 && maxCount > MIN_MAX_COUNT) {
                                    newFieldData.returnType = ctypes[maxIdx];

                                    type.add(ctypes[maxIdx]);

                                    return ctypes[maxIdx];
                                }

                                processed.addAll(Arrays.asList(ctypes));
                            }
                        }
                    }

                    if (ctypes != null && ctypes.length == 1) {
                        return ctypes[0];
                    }
                }

            }

        } else if ((returnType != null
                && (Collection.class.isAssignableFrom(returnType) || returnType.isArray()))
                || (itemType != null && (Collection.class.isAssignableFrom(itemType) || itemType.isArray()))) {

            //  && ComponentType.class.isAssignableFrom(erasedType.getClass())
            if (type != null) {

                ComponentType componentType = type.getComponentType();
                while (componentType != null && componentType.getComponentType() != null && level > 1) {
                    componentType = componentType.getComponentType();
                    level--;
                }

                if (level == 1 && componentType != null && componentType.getClassType() != null) {
                    return componentType.getClassType();
                }

                Class[] ctypes = type.getComponentClassType();

                if (ctypes != null && ctypes.length > 0) {
                    if (ctypes.length == 1) {
                        Class cmptype = ctypes[0].getComponentType();
                        if (cmptype != null
                                && (cmptype.isArray() || Collection.class.isAssignableFrom(cmptype))) {
                            type.add(cmptype);
                        }
                        //if (!ObjectUtil.isBasicDataType(ctypes[0])) {
                        return ctypes[0];
                        //}
                    }

                    int length = ctypes.length;
                    int depth = CollectionArrayTypeGuesser.getDepth(obj);
                    Class baseType = CollectionArrayTypeGuesser.getBaseType(obj);
                    Class possible = null;
                    for (int i = 0; i < length; i++) {
                        Class ctype = ctypes[i];

                        if (ctype.isArray() || Collection.class.isAssignableFrom(ctype)) {
                            //Class compType = CollectionArrayTypeGuesser.guessElementType(collection, ctype, getJsonClassType());
                            int typedepth = CollectionArrayTypeGuesser.getDepth(ctype);
                            Class cbaseType = CollectionArrayTypeGuesser.getBaseType(ctype);
                            if (depth == typedepth) {
                                if (ObjectUtil.isSameType(baseType, cbaseType)) {
                                    Class cmptype = ctype.getComponentType();
                                    if (cmptype.isArray() || Collection.class.isAssignableFrom(cmptype)) {
                                        type.add(cmptype);
                                    }

                                    return ctype;

                                } else if (itemType.isAssignableFrom(ctype)
                                        || ctype.isAssignableFrom(itemType)) {
                                    possible = ctype;
                                }
                            }
                        }

                    }

                    if (possible != null) {
                        return possible;
                    }

                }

            }

        }
        //         else if (StringUtil.isNumeric(obj.toString())) {
        //            if (obj.toString().contains(".")) {
        //               return Double.class;
        //            } else {
        //               return Integer.class;
        //            }
        //         }

        if (type != null && type.getComponentType() != null) {
            Class classType = type.getComponentType().getClassType();
            if (classType != null && ObjectUtil.isSameDataType(classType, itemType)) {
                return classType;
            }
        }

        return itemType;
    }
}