Search in sources :

Example 6 with ClassGetSetDTO

use of com.robo4j.socket.http.dto.ClassGetSetDTO in project robo4j by Robo4J.

the class ReflectUtils method createInstanceByClazzAndDescriptorAndJsonDocument.

@SuppressWarnings("unchecked")
public static <T> T createInstanceByClazzAndDescriptorAndJsonDocument(Class<T> clazz, JsonDocument jsonDocument) {
    try {
        Object instance = clazz.getDeclaredConstructor().newInstance();
        getFieldsTypeMap(clazz).entrySet().stream().filter(e -> Objects.nonNull(jsonDocument.getKey(e.getKey()))).forEach(e -> {
            try {
                ClassGetSetDTO value = e.getValue();
                if (value.getValueClass().isEnum()) {
                    if (e.getValue().getCollection() != null) {
                        switch(e.getValue().getCollection()) {
                            case LIST:
                                List<Enum<?>> enumList = ((JsonDocument) jsonDocument.getKey(e.getKey())).getArray().stream().map(el -> extractEnumConstant(el.toString(), (Enum<?>[]) value.getValueClass().getEnumConstants())).collect(Collectors.toCollection(LinkedList::new));
                                value.getSetMethod().invoke(instance, enumList);
                                break;
                            case MAP:
                                throw new IllegalStateException("not implemented");
                            default:
                                throw new IllegalStateException("not allowed");
                        }
                    } else {
                        value.getSetMethod().invoke(instance, extractEnumConstant(jsonDocument.getKey(e.getKey()).toString(), (Enum<?>[]) value.getValueClass().getEnumConstants()));
                    }
                } else {
                    value.getSetMethod().invoke(instance, adjustRoboJsonDocumentCast(value, jsonDocument, e.getKey()));
                }
            } catch (Exception e1) {
                throw new RoboReflectException("create instance field", e1);
            }
        });
        return (T) instance;
    } catch (Exception e) {
        throw new RoboReflectException("create instance with setter", e);
    }
}
Also used : Array(java.lang.reflect.Array) StringConstants(com.robo4j.util.StringConstants) UTF8_COLON(com.robo4j.util.Utf8Constant.UTF8_COLON) HashMap(java.util.HashMap) UTF8_COMMA(com.robo4j.util.Utf8Constant.UTF8_COMMA) LinkedHashMap(java.util.LinkedHashMap) Matcher(java.util.regex.Matcher) Map(java.util.Map) JsonGenericTypeAdapter(com.robo4j.socket.http.json.JsonGenericTypeAdapter) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) Collection(java.util.Collection) WITHOUT_QUOTATION_TYPES(com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES) Set(java.util.Set) Collectors(java.util.stream.Collectors) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO)

Example 7 with ClassGetSetDTO

use of com.robo4j.socket.http.dto.ClassGetSetDTO in project robo4j by Robo4J.

the class ReflectUtils method castObjectByRoboJsonDocument.

@SuppressWarnings("unchecked")
private static <T> T castObjectByRoboJsonDocument(Class<T> clazz, Object value) {
    if (clazz.equals(String.class) || WITHOUT_QUOTATION_TYPES.contains(clazz)) {
        return (T) adjustRoboClassCast(clazz, value);
    } else {
        try {
            Object instance = clazz.getDeclaredConstructor().newInstance();
            Map<String, ClassGetSetDTO> fieldNameMethods = getFieldsTypeMap(clazz);
            JsonDocument document = (JsonDocument) value;
            fieldNameMethods.forEach((k, v) -> {
                Object setValue = adjustRoboClassCast(v.getValueClass(), document.getKey(k));
                try {
                    v.getSetMethod().invoke(instance, setValue);
                } catch (Exception e) {
                    throw new RoboReflectException("set value", e);
                }
            });
            return (T) instance;
        } catch (Exception e) {
            throw new RoboReflectException("casting: new class instance", e);
        }
    }
}
Also used : UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument)

Aggregations

ClassGetSetDTO (com.robo4j.socket.http.dto.ClassGetSetDTO)7 JsonDocument (com.robo4j.socket.http.json.JsonDocument)5 UTF8_CURLY_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT)5 UTF8_CURLY_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT)5 UTF8_SQUARE_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT)5 UTF8_SQUARE_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT)5 List (java.util.List)5 Map (java.util.Map)5 Objects (java.util.Objects)5 Set (java.util.Set)5 JsonGenericTypeAdapter (com.robo4j.socket.http.json.JsonGenericTypeAdapter)4 JsonTypeAdapter (com.robo4j.socket.http.json.JsonTypeAdapter)4 WITHOUT_QUOTATION_TYPES (com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES)4 StringConstants (com.robo4j.util.StringConstants)4 UTF8_COLON (com.robo4j.util.Utf8Constant.UTF8_COLON)4 UTF8_COMMA (com.robo4j.util.Utf8Constant.UTF8_COMMA)4 Array (java.lang.reflect.Array)4 Method (java.lang.reflect.Method)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4