changeset 6520:e869a8513cb7

7195919: (sl) ServiceLoader can throw CCE without needing to create instance Reviewed-by: ahgross, alanb, dmeetry
author smarks
date Mon, 10 Sep 2012 16:05:53 -0700
parents 4488ea026532
children 9a7e2fa3c9c5
files src/share/classes/java/util/ServiceLoader.java src/share/classes/sun/misc/Service.java
diffstat 2 files changed, 21 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/ServiceLoader.java	Sat Sep 08 22:23:42 2012 -0700
+++ b/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/src/share/classes/sun/misc/Service.java	Sat Sep 08 22:23:42 2012 -0700
+++ b/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);