< prev index next >

src/java.base/share/classes/java/util/ServiceLoader.java

Print this page

        

@@ -1005,20 +1005,21 @@
     private final class LazyClassPathLookupIterator<T>
         implements Iterator<Provider<T>>
     {
         static final String PREFIX = "META-INF/services/";
 
+        Set<String> providerNames = new HashSet<>();  // to avoid duplicates
         Enumeration<URL> configs;
         Iterator<String> pending;
         Class<?> nextClass;
         String nextErrorMessage;  // when hasNext fails with CNFE
 
         LazyClassPathLookupIterator() { }
 
         /**
          * Parse a single line from the given configuration file, adding the
-         * name on the line to the names list.
+         * name on the line to set of names if not already seen.
          */
         private int parseLine(URL u, BufferedReader r, int lc, Set<String> names)
             throws IOException
         {
             String ln = r.readLine();

@@ -1039,12 +1040,14 @@
                 for (int i = start; i < n; i += Character.charCount(cp)) {
                     cp = ln.codePointAt(i);
                     if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
                         fail(service, u, lc, "Illegal provider-class name: " + ln);
                 }
+                if (providerNames.add(ln)) {
                 names.add(ln);
             }
+            }
             return lc + 1;
         }
 
         /**
          * Parse the content of the given URL as a provider-configuration file.

@@ -1070,11 +1073,11 @@
         private boolean hasNextService() {
             if (nextClass != null || nextErrorMessage != null) {
                 return true;
             }
 
-            Class<?> clazz = null;
+            Class<?> clazz;
             do {
                 if (configs == null) {
                     try {
                         String fullName = PREFIX + service.getName();
                         if (loader == null) {
< prev index next >