@Test public void loadOnce() throws Exception { ClassLoader loader = loaderForJarWithEntries( CharSequence.class.getName(), String.class.getName(), StringBuilder.class.getName()); ImmutableList<CharSequence> providers = SimpleServiceLoader.load(CharSequence.class, loader); assertThat(providers).contains(""); List<Class<?>> classes = providers.stream().map(Object::getClass).collect(toList()); assertThat(classes).containsExactly(String.class, StringBuilder.class).inOrder(); }
public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } private  SimpleServiceLoader(); }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } private  SimpleServiceLoader(); static ImmutableList<T> load(Class<? extends T> service, ClassLoader loader); }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } private  SimpleServiceLoader(); static ImmutableList<T> load(Class<? extends T> service, ClassLoader loader);  }