提交 3f152b10 编写于 作者: P psandoz

7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec

Reviewed-by: dholmes, alanb
上级 68316525
/* /*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -214,7 +214,7 @@ public final class ServiceLoader<S> ...@@ -214,7 +214,7 @@ public final class ServiceLoader<S>
private ServiceLoader(Class<S> svc, ClassLoader cl) { private ServiceLoader(Class<S> svc, ClassLoader cl) {
service = Objects.requireNonNull(svc, "Service interface cannot be null"); service = Objects.requireNonNull(svc, "Service interface cannot be null");
loader = cl; loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
reload(); reload();
} }
......
/* /*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -44,10 +44,41 @@ public class Basic { ...@@ -44,10 +44,41 @@ public class Basic {
eq, s1, s2)); eq, s1, s2));
} }
static abstract class TestLoader {
String name;
TestLoader(String name) { this.name = name; }
abstract ServiceLoader<FooService> load();
}
static TestLoader tcclLoader = new TestLoader("Thread context class loader") {
ServiceLoader<FooService> load() {
return ServiceLoader.load(FooService.class);
}
};
static TestLoader systemClLoader = new TestLoader("System class loader") {
ServiceLoader<FooService> load() {
return ServiceLoader.load(FooService.class, ClassLoader.getSystemClassLoader());
}
};
static TestLoader nullClLoader = new TestLoader("null (defer to system class loader)") {
ServiceLoader<FooService> load() {
return ServiceLoader.load(FooService.class, null);
}
};
public static void main(String[] args) { public static void main(String[] args) {
for (TestLoader tl : Arrays.asList(tcclLoader, systemClLoader, nullClLoader)) {
test(tl);
}
}
ServiceLoader<FooService> sl = ServiceLoader.load(FooService.class); static void test(TestLoader tl) {
out.format("%s%n", sl); ServiceLoader<FooService> sl = tl.load();
out.format("%s: %s%n", tl.name, sl);
// Providers are cached // Providers are cached
Set<FooService> ps = setOf(sl); Set<FooService> ps = setOf(sl);
...@@ -58,5 +89,4 @@ public class Basic { ...@@ -58,5 +89,4 @@ public class Basic {
checkEquals(ps, setOf(sl), false); checkEquals(ps, setOf(sl), false);
} }
} }
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
# #
# @test # @test
# @bug 4640520 6354623 # @bug 4640520 6354623 7198496
# @summary Unit test for java.util.ServiceLoader # @summary Unit test for java.util.ServiceLoader
# #
# @build Basic Load FooService FooProvider1 FooProvider2 FooProvider3 # @build Basic Load FooService FooProvider1 FooProvider2 FooProvider3
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册