< prev index next >
src/java.base/share/classes/java/util/ServiceLoader.java
Print this page
*** 1005,1024 ****
private final class LazyClassPathLookupIterator<T>
implements Iterator<Provider<T>>
{
static final String PREFIX = "META-INF/services/";
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.
*/
private int parseLine(URL u, BufferedReader r, int lc, Set<String> names)
throws IOException
{
String ln = r.readLine();
--- 1005,1025 ----
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 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,1050 ****
--- 1040,1053 ----
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,1080 ****
private boolean hasNextService() {
if (nextClass != null || nextErrorMessage != null) {
return true;
}
! Class<?> clazz = null;
do {
if (configs == null) {
try {
String fullName = PREFIX + service.getName();
if (loader == null) {
--- 1073,1083 ----
private boolean hasNextService() {
if (nextClass != null || nextErrorMessage != null) {
return true;
}
! Class<?> clazz;
do {
if (configs == null) {
try {
String fullName = PREFIX + service.getName();
if (loader == null) {
< prev index next >