changeset 6349:586028bbf885

7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec Reviewed-by: dholmes, alanb
author psandoz
date Wed, 17 Oct 2012 20:34:04 +0100
parents 6156b9235758
children b265ead7f331
files src/share/classes/java/util/ServiceLoader.java test/java/util/ServiceLoader/Basic.java test/java/util/ServiceLoader/basic.sh
diffstat 3 files changed, 38 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/ServiceLoader.java	Wed Oct 17 12:03:20 2012 -0700
+++ b/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/test/java/util/ServiceLoader/Basic.java	Wed Oct 17 12:03:20 2012 -0700
+++ b/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/test/java/util/ServiceLoader/basic.sh	Wed Oct 17 12:03:20 2012 -0700
+++ b/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