private static TypeConverter findValueOfTypeConverter()

in archaius2-core/src/main/java/com/netflix/archaius/AbstractRegistryDecoder.java [92:128]


    private static <T> TypeConverter<T> findValueOfTypeConverter(Type type) {
        if (!(type instanceof Class)) {
            return null;
        }

        @SuppressWarnings("unchecked")
        Class<T> cls = (Class<T>) type;

        // Look for a valueOf(String) static method. The code *assumes* that such a method will return a T
        Method method;
        try {
            method = cls.getMethod("valueOf", String.class);
            return value -> {
                try {
                    //noinspection unchecked
                    return (T) method.invoke(null, value);
                } catch (Exception e) {
                    throw new ParseException("Error converting value '" + value + "' to '" + type.getTypeName() + "'", e);
                }
            };
        } catch (NoSuchMethodException e1) {
            // Next, look for a T(String) constructor
            Constructor<T> c;
            try {
                c = cls.getConstructor(String.class);
                return value -> {
                    try {
                        return (T) c.newInstance(value);
                    } catch (Exception e) {
                        throw new ParseException("Error converting value", e);
                    }
                };
            } catch (NoSuchMethodException e) {
                return null;
            }
        }
    }