private static T getPOJOFromMapParams()

in lagerta-core/src/main/java/com/epam/lagerta/util/JDBCKeyValueMapper.java [136:162]


    private static <T> T getPOJOFromMapParams(Map<String, Object> columnValues, Class<T> targetClass) {
        Constructor<T> constructor = null;
        try {
            constructor = targetClass.getConstructor();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(
                    "No found default constructor(without parameters) for class " + targetClass.getName() + " It should be public", e);
        }
        T targetObject;
        try {
            targetObject = constructor.newInstance();
            for (Map.Entry<String, Object> columnNameAndValue : columnValues.entrySet()) {
                String fieldName = columnNameAndValue.getKey();
                if (EntityDescriptor.KEY_FIELD_NAME.equalsIgnoreCase(fieldName)
                        || EntityDescriptor.VAL_FIELD_NAME.equalsIgnoreCase(fieldName)) {
                    continue;
                }
                Object value = columnNameAndValue.getValue();
                Field declaredField = targetClass.getDeclaredField(fieldName);
                declaredField.setAccessible(true);
                declaredField.set(targetObject, value);
            }
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
            throw new RuntimeException("Can not create new instance of " + targetClass.getSimpleName(), e);
        }
        return targetObject;
    }