7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
authorpsandoz
Wed, 17 Oct 2012 20:34:04 +0100
changeset 14191 01f4a54604a6
parent 14190 8aec2eef7cdb
child 14192 e218f76e78db
7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec Reviewed-by: dholmes, alanb
jdk/src/share/classes/java/util/ServiceLoader.java
jdk/test/java/util/ServiceLoader/Basic.java
jdk/test/java/util/ServiceLoader/basic.sh
--- a/jdk/src/share/classes/java/util/ServiceLoader.java	Wed Oct 17 12:03:20 2012 -0700
+++ b/jdk/src/share/classes/java/util/ServiceLoader.java	Wed Oct 17 20:34:04 2012 +0100
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -214,7 +214,7 @@
 
     private ServiceLoader(Class<S> svc, ClassLoader cl) {
         service = Objects.requireNonNull(svc, "Service interface cannot be null");
-        loader = cl;
+        loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
         reload();
     }
 
--- a/jdk/test/java/util/ServiceLoader/Basic.java	Wed Oct 17 12:03:20 2012 -0700
+++ b/jdk/test/java/util/ServiceLoader/Basic.java	Wed Oct 17 20:34:04 2012 +0100
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,10 +44,41 @@
                                                      eq, s1, s2));
     }
 
-    public static void main(String[] args) {
+    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);
+        }
+    };
 
-        ServiceLoader<FooService> sl = ServiceLoader.load(FooService.class);
-        out.format("%s%n", sl);
+    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) {
+        for (TestLoader tl : Arrays.asList(tcclLoader, systemClLoader, nullClLoader)) {
+            test(tl);
+        }
+    }
+
+    static void test(TestLoader tl) {
+        ServiceLoader<FooService> sl = tl.load();
+        out.format("%s: %s%n", tl.name, sl);
 
         // Providers are cached
         Set<FooService> ps = setOf(sl);
@@ -58,5 +89,4 @@
         checkEquals(ps, setOf(sl), false);
 
     }
-
 }
--- a/jdk/test/java/util/ServiceLoader/basic.sh	Wed Oct 17 12:03:20 2012 -0700
+++ b/jdk/test/java/util/ServiceLoader/basic.sh	Wed Oct 17 20:34:04 2012 +0100
@@ -22,7 +22,7 @@
 #
 
 # @test
-# @bug 4640520 6354623
+# @bug 4640520 6354623 7198496
 # @summary Unit test for java.util.ServiceLoader
 #
 # @build Basic Load FooService FooProvider1 FooProvider2 FooProvider3