8215990: Avoid using reflection to create common default URLStreamHandlers

Reviewed-by: alanb
This commit is contained in:
Claes Redestad 2019-01-02 19:06:16 +01:00
parent df05de6ea7
commit 53c47fe98e

View file

@ -1220,16 +1220,22 @@ public final class URL implements java.io.Serializable {
private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory(); private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory();
private static class DefaultFactory implements URLStreamHandlerFactory { 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) { 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 { try {
@SuppressWarnings("deprecation") Object o = Class.forName(name).getDeclaredConstructor().newInstance();
Object o = Class.forName(name).newInstance();
return (URLStreamHandler)o; return (URLStreamHandler)o;
} catch (ClassNotFoundException x) {
// ignore
} catch (Exception e) { } catch (Exception e) {
// For compatibility, all Exceptions are ignored. // For compatibility, all Exceptions are ignored.
// any number of exceptions can get thrown here // any number of exceptions can get thrown here