Here you can find the source of load(Class
private static <T> Set<Class<? extends T>> load(Class<T> serviceClass, ClassLoader loader)
//package com.java2s; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.Set; public class Main { private static final String SERVICES = "META-INF/services"; private static <T> Set<Class<? extends T>> load(Class<T> serviceClass, ClassLoader loader) { String serviceFile = SERVICES + "/" + serviceClass.getName(); LinkedHashSet<Class<? extends T>> providers = new LinkedHashSet<Class<? extends T>>(); LinkedHashSet<Class<? extends T>> vetoedProviders = new LinkedHashSet<Class<? extends T>>(); try {/*from ww w .j a v a 2 s .c o m*/ Enumeration<URL> enumeration = loader.getResources(serviceFile); while (enumeration.hasMoreElements()) { final URL url = enumeration.nextElement(); final InputStream is = url.openStream(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String line = reader.readLine(); while (null != line) { line = skipCommentAndTrim(line); if (line.length() > 0) { try { boolean mustBeVetoed = line.startsWith("!"); if (mustBeVetoed) { line = line.substring(1); } Class<? extends T> provider = loader .loadClass(line).asSubclass( serviceClass); if (mustBeVetoed) { vetoedProviders.add(provider); } if (vetoedProviders.contains(provider)) { providers.remove(provider); } else { providers.add(provider); } } catch (ClassCastException e) { throw new IllegalStateException( "Service " + line + " does not implement expected type " + serviceClass.getName()); } } line = reader.readLine(); } } finally { if (reader != null) { reader.close(); } } } } catch (Exception e) { throw new RuntimeException("Could not load services for " + serviceClass.getName(), e); } return providers; } private static String skipCommentAndTrim(String line) { final int comment = line.indexOf('#'); if (comment > -1) { line = line.substring(0, comment); } line = line.trim(); return line; } }