8215990: Avoid using reflection to create common default URLStreamHandlers
Reviewed-by: alanb
--- a/src/java.base/share/classes/java/net/URL.java Wed Jan 02 13:06:04 2019 -0500
+++ b/src/java.base/share/classes/java/net/URL.java Wed Jan 02 19:06:16 2019 +0100
@@ -1220,16 +1220,22 @@
private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory();
private static class DefaultFactory implements URLStreamHandlerFactory {
- private static String PREFIX = "sun.net.www.protocol";
+ private static String PREFIX = "sun.net.www.protocol.";
public URLStreamHandler createURLStreamHandler(String protocol) {
- String name = PREFIX + "." + protocol + ".Handler";
+ // Avoid using reflection during bootstrap
+ switch (protocol) {
+ case "file":
+ return new sun.net.www.protocol.file.Handler();
+ case "jar":
+ return new sun.net.www.protocol.jar.Handler();
+ case "jrt":
+ return new sun.net.www.protocol.jrt.Handler();
+ }
+ String name = PREFIX + protocol + ".Handler";
try {
- @SuppressWarnings("deprecation")
- Object o = Class.forName(name).newInstance();
+ Object o = Class.forName(name).getDeclaredConstructor().newInstance();
return (URLStreamHandler)o;
- } catch (ClassNotFoundException x) {
- // ignore
} catch (Exception e) {
// For compatibility, all Exceptions are ignored.
// any number of exceptions can get thrown here