@Test public void shouldReturnFirstServiceLoaderResultIfAvailable() { final MyService1 expectedService = new MyService1(); ServiceLoaderUtil<BaseRenderer> loader = new ServiceLoaderUtil<>(c -> { ArrayList<BaseRenderer> services = new ArrayList<>(1); services.add(expectedService); return services.iterator(); }); BaseRenderer service = loader.load(BaseRenderer.class, MyService2.class); assertEquals(expectedService, service); }
public R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except) { HashSet<Class<?>> exceptions = new HashSet<>(Arrays.asList(except)); Iterator<R> services = loader.apply(service); while (services.hasNext()) { R candidate = services.next(); if (!exceptions.contains(candidate.getClass())) { return candidate; } } try { return defaultImplementation.getDeclaredConstructor().newInstance(); } catch (Throwable t) { throw new DefaultImplementationInstantiationException(defaultImplementation, t); } }
ServiceLoaderUtil { public R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except) { HashSet<Class<?>> exceptions = new HashSet<>(Arrays.asList(except)); Iterator<R> services = loader.apply(service); while (services.hasNext()) { R candidate = services.next(); if (!exceptions.contains(candidate.getClass())) { return candidate; } } try { return defaultImplementation.getDeclaredConstructor().newInstance(); } catch (Throwable t) { throw new DefaultImplementationInstantiationException(defaultImplementation, t); } } }
ServiceLoaderUtil { public R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except) { HashSet<Class<?>> exceptions = new HashSet<>(Arrays.asList(except)); Iterator<R> services = loader.apply(service); while (services.hasNext()) { R candidate = services.next(); if (!exceptions.contains(candidate.getClass())) { return candidate; } } try { return defaultImplementation.getDeclaredConstructor().newInstance(); } catch (Throwable t) { throw new DefaultImplementationInstantiationException(defaultImplementation, t); } } ServiceLoaderUtil();  ServiceLoaderUtil(Iterable<R> services);  ServiceLoaderUtil(Function<Class<R>, Iterator<R>> loader); }
ServiceLoaderUtil { public R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except) { HashSet<Class<?>> exceptions = new HashSet<>(Arrays.asList(except)); Iterator<R> services = loader.apply(service); while (services.hasNext()) { R candidate = services.next(); if (!exceptions.contains(candidate.getClass())) { return candidate; } } try { return defaultImplementation.getDeclaredConstructor().newInstance(); } catch (Throwable t) { throw new DefaultImplementationInstantiationException(defaultImplementation, t); } } ServiceLoaderUtil();  ServiceLoaderUtil(Iterable<R> services);  ServiceLoaderUtil(Function<Class<R>, Iterator<R>> loader); R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except); R initialise(Class<R> service, Class<? extends R> defaultImplementation, I18nLoader i18n, DocumentAttributes attributes, Class<? extends R>... except); }
ServiceLoaderUtil { public R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except) { HashSet<Class<?>> exceptions = new HashSet<>(Arrays.asList(except)); Iterator<R> services = loader.apply(service); while (services.hasNext()) { R candidate = services.next(); if (!exceptions.contains(candidate.getClass())) { return candidate; } } try { return defaultImplementation.getDeclaredConstructor().newInstance(); } catch (Throwable t) { throw new DefaultImplementationInstantiationException(defaultImplementation, t); } } ServiceLoaderUtil();  ServiceLoaderUtil(Iterable<R> services);  ServiceLoaderUtil(Function<Class<R>, Iterator<R>> loader); R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except); R initialise(Class<R> service, Class<? extends R> defaultImplementation, I18nLoader i18n, DocumentAttributes attributes, Class<? extends R>... except);  }