BeanToStringConverter.java

package sidplay.ini.converter;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.beans.Introspector.getBeanInfo;
import static java.util.Objects.nonNull;

public final class BeanToStringConverter {

    private BeanToStringConverter() {
    }

    public static String toString(Object bean) {
        return defaultIfThrows(b -> Arrays.stream(getBeanInfo(b.getClass(), Object.class).getPropertyDescriptors())
                        .filter(descriptor -> nonNull(descriptor.getReadMethod()))
                        .map(defaultIfThrows(
                                descriptor -> new SimpleImmutableEntry<>(descriptor.getName(),
                                        descriptor.getReadMethod().invoke(b)),
                                descriptor -> new SimpleImmutableEntry<>(descriptor.getName(),
                                        "<bean value could not be determined>")))
                        .map(SimpleImmutableEntry::toString).collect(Collectors.joining("\n")),
                b -> "<bean introspection failed>").apply(bean);
    }

    private static <T, R> Function<T, R> defaultIfThrows(FunctionThatThrows<T, R> delegate,
                                                         Function<T, R> defaultValueSupplier) {
        return x -> {
            try {
                return delegate.apply(x);
            } catch (Throwable throwable) {
                return defaultValueSupplier.apply(x);
            }
        };
    }

    @FunctionalInterface
    private interface FunctionThatThrows<T, R> {
        R apply(T t) throws Throwable;
    }
}