8215990: Avoid using reflection to create common default URLStreamHandlers
authorredestad
Wed, 02 Jan 2019 19:06:16 +0100
changeset 53125 dee9426ef417
parent 53124 a22e41395bfa
child 53126 3d0f6ef91216
8215990: Avoid using reflection to create common default URLStreamHandlers Reviewed-by: alanb
src/java.base/share/classes/java/net/URL.java
--- 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