7195919: (sl) ServiceLoader can throw CCE without needing to create instance
authorsmarks
Mon, 10 Sep 2012 16:05:53 -0700
changeset 14218 c6f2434dd88d
parent 14217 eee71820f0a0
child 14219 e99355081c6d
7195919: (sl) ServiceLoader can throw CCE without needing to create instance Reviewed-by: ahgross, alanb, dmeetry
jdk/src/share/classes/java/util/ServiceLoader.java
jdk/src/share/classes/sun/misc/Service.java
--- a/jdk/src/share/classes/java/util/ServiceLoader.java	Sat Sep 08 22:23:42 2012 -0700
+++ b/jdk/src/share/classes/java/util/ServiceLoader.java	Mon Sep 10 16:05:53 2012 -0700
@@ -358,14 +358,21 @@
             }
             String cn = nextName;
             nextName = null;
+            Class<?> c = null;
             try {
-                S p = service.cast(Class.forName(cn, true, loader)
-                                   .newInstance());
-                providers.put(cn, p);
-                return p;
+                c = Class.forName(cn, false, loader);
             } catch (ClassNotFoundException x) {
                 fail(service,
                      "Provider " + cn + " not found");
+            }
+            if (!service.isAssignableFrom(c)) {
+                fail(service,
+                     "Provider " + cn  + " not a subtype");
+            }
+            try {
+                S p = service.cast(c.newInstance());
+                providers.put(cn, p);
+                return p;
             } catch (Throwable x) {
                 fail(service,
                      "Provider " + cn + " could not be instantiated: " + x,
--- a/jdk/src/share/classes/sun/misc/Service.java	Sat Sep 08 22:23:42 2012 -0700
+++ b/jdk/src/share/classes/sun/misc/Service.java	Mon Sep 10 16:05:53 2012 -0700
@@ -284,12 +284,20 @@
             }
             String cn = nextName;
             nextName = null;
+            Class<?> c = null;
             try {
-                return service.cast(Class.forName(cn, true, loader).newInstance());
+                c = Class.forName(cn, false, loader);
             } catch (ClassNotFoundException x) {
                 fail(service,
                      "Provider " + cn + " not found");
-            } catch (Exception x) {
+            }
+            if (!service.isAssignableFrom(c)) {
+                fail(service,
+                     "Provider " + cn  + " not a subtype");
+            }
+            try {
+                return service.cast(c.newInstance());
+            } catch (Throwable x) {
                 fail(service,
                      "Provider " + cn + " could not be instantiated: " + x,
                      x);