author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 14191 | 01f4a54604a6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14191
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
// |
|
25 |
||
26 |
import java.io.*; |
|
27 |
import java.util.*; |
|
28 |
||
29 |
||
30 |
public class Basic { |
|
31 |
||
32 |
private static PrintStream out = System.err; |
|
33 |
||
34 |
private static <T> Set<T> setOf(Iterable<T> it) { |
|
35 |
Set<T> s = new HashSet<T>(); |
|
36 |
for (T t : it) |
|
37 |
s.add(t); |
|
38 |
return s; |
|
39 |
} |
|
40 |
||
41 |
private static <T> void checkEquals(Set<T> s1, Set<T> s2, boolean eq) { |
|
42 |
if (s1.equals(s2) != eq) |
|
43 |
throw new RuntimeException(String.format("%b %s : %s", |
|
44 |
eq, s1, s2)); |
|
45 |
} |
|
46 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
14191
diff
changeset
|
47 |
abstract static class TestLoader { |
14191
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
48 |
String name; |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
49 |
|
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
50 |
TestLoader(String name) { this.name = name; } |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
51 |
|
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
52 |
abstract ServiceLoader<FooService> load(); |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
53 |
} |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
54 |
|
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
55 |
static TestLoader tcclLoader = new TestLoader("Thread context class loader") { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
56 |
ServiceLoader<FooService> load() { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
57 |
return ServiceLoader.load(FooService.class); |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
58 |
} |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
59 |
}; |
2 | 60 |
|
14191
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
61 |
static TestLoader systemClLoader = new TestLoader("System class loader") { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
62 |
ServiceLoader<FooService> load() { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
63 |
return ServiceLoader.load(FooService.class, ClassLoader.getSystemClassLoader()); |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
64 |
} |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
65 |
}; |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
66 |
|
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
67 |
static TestLoader nullClLoader = new TestLoader("null (defer to system class loader)") { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
68 |
ServiceLoader<FooService> load() { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
69 |
return ServiceLoader.load(FooService.class, null); |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
70 |
} |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
71 |
}; |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
72 |
|
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
73 |
public static void main(String[] args) { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
74 |
for (TestLoader tl : Arrays.asList(tcclLoader, systemClLoader, nullClLoader)) { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
75 |
test(tl); |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
76 |
} |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
77 |
} |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
78 |
|
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
79 |
static void test(TestLoader tl) { |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
80 |
ServiceLoader<FooService> sl = tl.load(); |
01f4a54604a6
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents:
5506
diff
changeset
|
81 |
out.format("%s: %s%n", tl.name, sl); |
2 | 82 |
|
83 |
// Providers are cached |
|
84 |
Set<FooService> ps = setOf(sl); |
|
85 |
checkEquals(ps, setOf(sl), true); |
|
86 |
||
87 |
// The cache can be flushed and reloaded |
|
88 |
sl.reload(); |
|
89 |
checkEquals(ps, setOf(sl), false); |
|
90 |
||
91 |
} |
|
92 |
} |