Skip to content

Commit

Permalink
8294241: Deprecate URL public constructors
Browse files Browse the repository at this point in the history
Reviewed-by: joehw, prr, alanb, aefimov, michaelm
  • Loading branch information
dfuch committed Nov 3, 2022
1 parent 68209ad commit 4338f52
Show file tree
Hide file tree
Showing 82 changed files with 848 additions and 146 deletions.
4 changes: 3 additions & 1 deletion src/java.base/share/classes/java/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,9 @@ public URL toURL() throws MalformedURLException {
if (isInvalid()) {
throw new MalformedURLException("Invalid file path");
}
return new URL("file", "", slashify(getAbsolutePath(), isDirectory()));
@SuppressWarnings("deprecation")
var result = new URL("file", "", slashify(getAbsolutePath(), isDirectory()));
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -184,6 +184,7 @@ private Socket doTunnel(String urlString, int connectTimeout)
throws IOException
{
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, port));
@SuppressWarnings("deprecation")
URL destURL = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy);
conn.setConnectTimeout(connectTimeout);
Expand Down
9 changes: 6 additions & 3 deletions src/java.base/share/classes/java/net/JarURLConnection.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -172,14 +172,17 @@ private void parseSpecs(URL url) throws MalformedURLException {
throw new MalformedURLException("no !/ found in url spec:" + spec);
}

jarFileURL = new URL(spec.substring(0, separator++));
@SuppressWarnings("deprecation")
var _unused = jarFileURL = new URL(spec.substring(0, separator++));

/*
* The url argument may have had a runtime fragment appended, so
* we need to add a runtime fragment to the jarFileURL to enable
* runtime versioning when the underlying jar file is opened.
*/
if ("runtime".equals(url.getRef())) {
jarFileURL = new URL(jarFileURL, "#runtime");
@SuppressWarnings("deprecation")
var _unused2 = jarFileURL = new URL(jarFileURL, "#runtime");
}
entryName = null;

Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/URI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ public URI relativize(URI uri) {
* or if some other error occurred while constructing the URL
*/
public URL toURL() throws MalformedURLException {
return URL.fromURI(this);
return URL.of(this, null);
}

// -- Component access methods --
Expand Down
130 changes: 125 additions & 5 deletions src/java.base/share/classes/java/net/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@
* the protocol, host name, or port number is missing, the value is
* inherited from the fully specified URL. The file component must be
* specified. The optional fragment is not inherited.
*
* <h2><a id="constructor-deprecation"></a>Constructing instances of {@code URL}</h2>
*
* The {@code java.net.URL} constructors are deprecated.
* Developers are encouraged to use {@link URI java.net.URI} to parse
* or construct a {@code URL}. In cases where an instance of {@code
* java.net.URL} is needed to open a connection, {@link URI} can be used
* to construct or parse the URL string, possibly calling {@link
* URI#parseServerAuthority()} to validate that the authority component
* can be parsed as a server-based authority, and then calling
* {@link URI#toURL()} to create the {@code URL} instance.
* <p>
* The URL constructors are specified to throw
* {@link MalformedURLException} but the actual parsing/validation
* that is performed is implementation dependent. Some parsing/validation
* may be delayed until later, when the underlying {@linkplain
* URLStreamHandler stream handler's implementation} is called.
* Being able to construct an instance of {@code URL} doesn't
* provide any guarantee about its conformance to the URL
* syntax specification.
* <p>
* The URL class does not itself encode or decode any URL components
* according to the escaping mechanism defined in RFC2396. It is the
Expand All @@ -152,6 +172,7 @@
*
* @apiNote
*
* <a id="integrity"></a>
* Applications working with file paths and file URIs should take great
* care to use the appropriate methods to convert between the two.
* The {@link Path#of(URI)} factory method and the {@link File#File(URI)}
Expand All @@ -164,6 +185,11 @@
* from the direct string representation of a {@code File} or {@code Path}
* instance.
* <p>
* Before constructing a {@code URL} from a {@code URI}, and depending
* on the protocol involved, applications should consider validating
* whether the URI authority {@linkplain URI#parseServerAuthority()
* can be parsed as server-based}.
* <p>
* Some components of a URL or URI, such as <i>userinfo</i>, may
* be abused to construct misleading URLs or URIs. Applications
* that deal with URLs or URIs should take into account
Expand Down Expand Up @@ -373,7 +399,11 @@ public final class URL implements java.io.Serializable {
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
* java.lang.String)
* @deprecated Use {@link URI#toURL()} to construct an instance of URL. See the note on
* <a href="#constructor-deprecation">constructor deprecation</a> for more
* details.
*/
@Deprecated(since = "20")
public URL(String protocol, String host, int port, String file)
throws MalformedURLException
{
Expand All @@ -399,7 +429,11 @@ public URL(String protocol, String host, int port, String file)
* rejects, or is known to reject, the {@code URL}
* @see java.net.URL#URL(java.lang.String, java.lang.String,
* int, java.lang.String)
* @deprecated Use {@link URI#toURL()} to construct an instance of URL. See the note on
* <a href="#constructor-deprecation">constructor deprecation</a> for more
* details.
*/
@Deprecated(since = "20")
public URL(String protocol, String host, String file)
throws MalformedURLException {
this(protocol, host, -1, file);
Expand Down Expand Up @@ -446,7 +480,13 @@ public URL(String protocol, String host, String file)
* java.lang.String)
* @see SecurityManager#checkPermission
* @see java.net.NetPermission
* @deprecated
* Use {@link #of(URI, URLStreamHandler)} to construct an instance of URL
* associated with a custom protocol handler.
* See the note on <a href="#constructor-deprecation">constructor deprecation</a>
* for more details.
*/
@Deprecated(since = "20")
public URL(String protocol, String host, int port, String file,
URLStreamHandler handler) throws MalformedURLException {
if (handler != null) {
Expand Down Expand Up @@ -533,7 +573,11 @@ public URL(String protocol, String host, int port, String file,
* URLStreamHandler#parseURL parseURL method} throws
* {@code IllegalArgumentException}
* @see java.net.URL#URL(java.net.URL, java.lang.String)
* @deprecated Use {@link URI#toURL()} to construct an instance of URL. See the note on
* <a href="#constructor-deprecation">constructor deprecation</a> for more
* details.
*/
@Deprecated(since = "20")
public URL(String spec) throws MalformedURLException {
this(null, spec);
}
Expand Down Expand Up @@ -593,7 +637,11 @@ public URL(String spec) throws MalformedURLException {
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandler#parseURL(java.net.URL,
* java.lang.String, int, int)
* @deprecated Use {@link URI#toURL()} to construct an instance of URL. See the note on
* <a href="#constructor-deprecation">constructor deprecation</a> for more
* details.
*/
@Deprecated(since = "20")
public URL(URL context, String spec) throws MalformedURLException {
this(context, spec, null);
}
Expand Down Expand Up @@ -626,7 +674,13 @@ public URL(URL context, String spec) throws MalformedURLException {
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandler#parseURL(java.net.URL,
* java.lang.String, int, int)
* @deprecated
* Use {@link #of(URI, URLStreamHandler)} to construct an instance of URL
* associated with a custom protocol handler.
* See the note on <a href="#constructor-deprecation">constructor deprecation</a>
* for more details.
*/
@Deprecated(since = "20")
public URL(URL context, String spec, URLStreamHandler handler)
throws MalformedURLException
{
Expand Down Expand Up @@ -748,23 +802,70 @@ public URL(URL context, String spec, URLStreamHandler handler)
}

/**
* Creates a URL from a URI, as if by invoking {@code uri.toURL()}.
* Creates a URL from a URI, as if by invoking {@code uri.toURL()}, but
* associating it with the given {@code URLStreamHandler}, if allowed.
*
* @apiNote
* Applications should consider performing additional integrity
* checks before constructing a {@code URL} and opening a connection.
* See the <a href=#integrity>API note</a> in the class level API
* documentation.
*
* @implSpec The implementation of this method includes calling the {@link
* URLStreamHandler#parseURL(URL, String, int, int) parseURL} method on the
* selected handler.
*
* @param uri the {@code URI} from which the returned {@code URL} should
* be built
* @param handler a custom protocol stream handler for
* the returned {@code URL}. Can be {@code null},
* in which case the default stream handler for
* the protocol if any, will be used.
*
* @return a new {@code URL} instance created from the given {@code URI}
* and associated with the given {@code URLStreamHandler}, if any
*
* @throws NullPointerException if {@code uri} is {@code null}
*
* @throws IllegalArgumentException if no protocol is specified
* (the {@linkplain URI#getScheme() uri scheme} is {@code null}), or
* if the {@code URLStreamHandler} is not {@code null} and can not be
* set for the given protocol
*
* @throws MalformedURLException if an unknown protocol is found,
* or the given URI fails to comply with the specific
* syntax of the associated protocol, or the
* underlying stream handler's {@linkplain
* URLStreamHandler#parseURL(URL, String, int, int)
* parseURL method} throws {@code IllegalArgumentException}
*
* @throws SecurityException
* if a security manager exists and its
* {@code checkPermission} method doesn't allow
* specifying a stream handler
*
* @see java.net.URI#toURL()
*
* @since 20
*/
static URL fromURI(URI uri) throws MalformedURLException {
public static URL of(URI uri, URLStreamHandler handler)
throws MalformedURLException {
if (!uri.isAbsolute()) {
throw new IllegalArgumentException("URI is not absolute");
}

String protocol = uri.getScheme();

// fast path for canonical jrt:/... URLs
//
// In general we need to go via Handler.parseURL, but for the jrt
// protocol we enforce that the Handler is not overridable and can
// optimize URI to URL conversion.
//
// Case-sensitive comparison for performance; malformed protocols will
// be handled correctly by the slow path.
if (protocol.equals("jrt") && !uri.isOpaque()
if (handler == null && protocol.equals("jrt") && !uri.isOpaque()
&& uri.getRawAuthority() == null
&& uri.getRawFragment() == null) {

String query = uri.getRawQuery();
Expand All @@ -780,9 +881,28 @@ static URL fromURI(URI uri) throws MalformedURLException {
int port = uri.getPort();

return new URL("jrt", host, port, file, null);
} else {
return new URL((URL)null, uri.toString(), null);
}

// slow path (will work for non-canonical forms of jrt: too)

if ("url".equalsIgnoreCase(protocol)) {;
String uristr = uri.toString();
try {
URI inner = new URI(uristr.substring(4));
if (inner.isAbsolute()) {
protocol = inner.getScheme();
}
} catch (URISyntaxException use) {
throw new MalformedURLException(use.getMessage());
}
}

if (handler != null && !isOverrideable(protocol)) {
throw new IllegalArgumentException("Can't override URLStreamHandler for protocol "
+ protocol);
}

return new URL((URL)null, uri.toString(), handler);
}

/*
Expand Down
10 changes: 8 additions & 2 deletions src/java.base/share/classes/java/security/Security.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package java.security;

import java.net.MalformedURLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.io.*;
Expand Down Expand Up @@ -135,10 +136,10 @@ private static boolean loadProps(File masterFile, String extraPropFile, boolean
File propFile = new File(extraPropFile);
URL propURL;
if (propFile.exists()) {
propURL = new URL
propURL = newURL
("file:" + propFile.getCanonicalPath());
} else {
propURL = new URL(extraPropFile);
propURL = newURL(extraPropFile);
}

is = propURL.openStream();
Expand Down Expand Up @@ -993,4 +994,9 @@ public static Set<String> getAlgorithms(String serviceName) {
}
return Collections.unmodifiableSet(result);
}

@SuppressWarnings("deprecation")
private static URL newURL(String spec) throws MalformedURLException {
return new URL(spec);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -243,7 +243,8 @@ final class JceSecurity {

static {
try {
NULL_URL = new URL("http://null.oracle.com/");
@SuppressWarnings("deprecation")
var _unused = NULL_URL = new URL("http://null.oracle.com/");
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void verify() throws IOException {
// If the protocol of jarURL isn't "jar", we should
// construct a JAR URL so we can open a JarURLConnection
// for verifying this provider.
@SuppressWarnings("deprecation")
final URL url = jarURL.getProtocol().equalsIgnoreCase("jar")?
jarURL : new URL("jar:" + jarURL + "!/");

Expand Down
Loading

1 comment on commit 4338f52

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.