public static <T> T newInstance(Class klass, ServiceLoader loader) {
        try {
            try {
                Constructor<T> constructor = klass.getDeclaredConstructor(ServiceLoader.class);
                return constructor.newInstance(loader);
            } catch (NoSuchMethodException e) {
                return (T)klass.getDeclaredConstructor().newInstance();
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
Loads a class and instantiates it.  If the class can be initialized
with a ServiceLoader, the ServiceLoader constructor is used.
Otherwise, a zero arg newInstance() is called.

@param klass     class to build
@param loader    service loader
@param <T>       service type
@return instance of service
tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java