public static void resolveFields()

in amazon-kinesis-client-multilang/src/main/java/software/amazon/kinesis/multilang/config/ConfigurationSettableUtils.java [43:108]


    public static void resolveFields(Object source, Map<Class<?>, Object> configObjects, Set<Class<?>> restrictTo,
            Set<Class<?>> skipIf) {
        for (Field field : source.getClass().getDeclaredFields()) {
            for (ConfigurationSettable b : field.getAnnotationsByType(ConfigurationSettable.class)) {
                if (restrictTo != null && !restrictTo.contains(b.configurationClass())) {
                    continue;
                }
                if (skipIf != null && skipIf.contains(b.configurationClass())) {
                    continue;
                }
                field.setAccessible(true);
                Object configObject = configObjects.get(b.configurationClass());
                if (configObject != null) {
                    String setterName = field.getName();
                    if (!StringUtils.isEmpty(b.methodName())) {
                        setterName = b.methodName();
                    }
                    Object value;
                    try {
                        value = field.get(source);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }

                    if (value != null && !value.equals(Defaults.defaultValue(field.getType()))) {
                        Method setter = null;
                        if (b.convertToOptional()) {
                            value = Optional.of(value);
                        }
                        if (ClassUtils.isPrimitiveOrWrapper(value.getClass())) {
                            Class<?> primitiveType = field.getType().isPrimitive() ? field.getType()
                                    : ClassUtils.wrapperToPrimitive(field.getType());
                            Class<?> wrapperType = !field.getType().isPrimitive() ? field.getType()
                                    : ClassUtils.primitiveToWrapper(field.getType());

                            try {
                                setter = b.configurationClass().getMethod(setterName, primitiveType);
                            } catch (NoSuchMethodException e) {
                                //
                                // Ignore this
                                //
                            }
                            if (setter == null) {
                                try {
                                    setter = b.configurationClass().getMethod(setterName, wrapperType);
                                } catch (NoSuchMethodException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        } else {
                            try {
                                setter = b.configurationClass().getMethod(setterName, value.getClass());
                            } catch (NoSuchMethodException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        try {
                            setter.invoke(configObject, value);
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }