# HG changeset patch # User smarks # Date 1347318353 25200 # Node ID c6f2434dd88d8d53d2ff6e0109928635ee71dfd5 # Parent eee71820f0a0b0377fdfebaf88350c7f3fa01b99 7195919: (sl) ServiceLoader can throw CCE without needing to create instance Reviewed-by: ahgross, alanb, dmeetry diff -r eee71820f0a0 -r c6f2434dd88d jdk/src/share/classes/java/util/ServiceLoader.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, diff -r eee71820f0a0 -r c6f2434dd88d jdk/src/share/classes/sun/misc/Service.java --- 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);