diff --git a/src/java.base/share/classes/java/io/File.java b/src/java.base/share/classes/java/io/File.java index 8f1e88219fecd..2e3b8b0a9e480 100644 --- a/src/java.base/share/classes/java/io/File.java +++ b/src/java.base/share/classes/java/io/File.java @@ -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; } /** diff --git a/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java b/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java index c0988050ef07a..c959816dadfbb 100644 --- a/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java +++ b/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java @@ -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 @@ -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); diff --git a/src/java.base/share/classes/java/net/JarURLConnection.java b/src/java.base/share/classes/java/net/JarURLConnection.java index 232a548e0a756..2c2734b08d7ba 100644 --- a/src/java.base/share/classes/java/net/JarURLConnection.java +++ b/src/java.base/share/classes/java/net/JarURLConnection.java @@ -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 @@ -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; diff --git a/src/java.base/share/classes/java/net/URI.java b/src/java.base/share/classes/java/net/URI.java index 9b42c0347ac3b..b532d41059e1d 100644 --- a/src/java.base/share/classes/java/net/URI.java +++ b/src/java.base/share/classes/java/net/URI.java @@ -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 -- diff --git a/src/java.base/share/classes/java/net/URL.java b/src/java.base/share/classes/java/net/URL.java index f247bfebb63d2..56793be1e974f 100644 --- a/src/java.base/share/classes/java/net/URL.java +++ b/src/java.base/share/classes/java/net/URL.java @@ -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. + * + *

Constructing instances of {@code URL}

+ * + * 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. + *

+ * 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. *

* The URL class does not itself encode or decode any URL components * according to the escaping mechanism defined in RFC2396. It is the @@ -152,6 +172,7 @@ * * @apiNote * + * * 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)} @@ -164,6 +185,11 @@ * from the direct string representation of a {@code File} or {@code Path} * instance. *

+ * 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}. + *

* Some components of a URL or URI, such as userinfo, may * be abused to construct misleading URLs or URIs. Applications * that deal with URLs or URIs should take into account @@ -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 + * constructor deprecation for more + * details. */ + @Deprecated(since = "20") public URL(String protocol, String host, int port, String file) throws MalformedURLException { @@ -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 + * constructor deprecation for more + * details. */ + @Deprecated(since = "20") public URL(String protocol, String host, String file) throws MalformedURLException { this(protocol, host, -1, file); @@ -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 constructor deprecation + * for more details. */ + @Deprecated(since = "20") public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException { if (handler != null) { @@ -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 + * constructor deprecation for more + * details. */ + @Deprecated(since = "20") public URL(String spec) throws MalformedURLException { this(null, spec); } @@ -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 + * constructor deprecation for more + * details. */ + @Deprecated(since = "20") public URL(URL context, String spec) throws MalformedURLException { this(context, spec, null); } @@ -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 constructor deprecation + * for more details. */ + @Deprecated(since = "20") public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException { @@ -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 API note 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(); @@ -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); } /* diff --git a/src/java.base/share/classes/java/security/Security.java b/src/java.base/share/classes/java/security/Security.java index dcfe3b4d6bbe4..257dc172ee2af 100644 --- a/src/java.base/share/classes/java/security/Security.java +++ b/src/java.base/share/classes/java/security/Security.java @@ -25,6 +25,7 @@ package java.security; +import java.net.MalformedURLException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.io.*; @@ -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(); @@ -993,4 +994,9 @@ public static Set getAlgorithms(String serviceName) { } return Collections.unmodifiableSet(result); } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/src/java.base/share/classes/javax/crypto/JceSecurity.java.template b/src/java.base/share/classes/javax/crypto/JceSecurity.java.template index 69ca07dbc6d92..220dd6f1f5c64 100644 --- a/src/java.base/share/classes/javax/crypto/JceSecurity.java.template +++ b/src/java.base/share/classes/javax/crypto/JceSecurity.java.template @@ -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 @@ -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); } diff --git a/src/java.base/share/classes/javax/crypto/ProviderVerifier.java b/src/java.base/share/classes/javax/crypto/ProviderVerifier.java index c7344b852dee7..cbfd02c32f4f7 100644 --- a/src/java.base/share/classes/javax/crypto/ProviderVerifier.java +++ b/src/java.base/share/classes/javax/crypto/ProviderVerifier.java @@ -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 + "!/"); diff --git a/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java b/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java index 71c09fc7b6a6f..7b49189418dba 100644 --- a/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java +++ b/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java @@ -493,6 +493,7 @@ public Loader run() throws IOException { isDefaultJarHandler(url) && file.endsWith("!/")) { // extract the nested URL + @SuppressWarnings("deprecation") URL nestedUrl = new URL(file.substring(0, file.length() - 2)); return new JarLoader(nestedUrl, jarHandler, lmap, acc); } else { @@ -605,7 +606,8 @@ final URL getBaseURL() { URL findResource(final String name, boolean check) { URL url; try { - url = new URL(base, ParseUtil.encodePath(name, false)); + @SuppressWarnings("deprecation") + var _unused = url = new URL(base, ParseUtil.encodePath(name, false)); } catch (MalformedURLException e) { return null; } @@ -641,7 +643,8 @@ URL findResource(final String name, boolean check) { Resource getResource(final String name, boolean check) { final URL url; try { - url = new URL(base, ParseUtil.encodePath(name, false)); + @SuppressWarnings("deprecation") + var _unused = url = new URL(base, ParseUtil.encodePath(name, false)); } catch (MalformedURLException e) { return null; } @@ -729,7 +732,7 @@ private JarLoader(URL url, URLStreamHandler jarHandler, @SuppressWarnings("removal") AccessControlContext acc) throws IOException { - super(new URL("jar", "", -1, url + "!/", jarHandler)); + super(newURL("jar", "", -1, url + "!/", jarHandler)); csu = url; handler = jarHandler; lmap = loaderMap; @@ -738,6 +741,13 @@ private JarLoader(URL url, URLStreamHandler jarHandler, ensureOpen(); } + @SuppressWarnings("deprecation") + private static URL newURL(String protocol, String host, int port, String file, URLStreamHandler handler) + throws MalformedURLException + { + return new URL(protocol, host, port, file, handler); + } + @Override public void close () throws IOException { // closing is synchronized at higher level @@ -782,6 +792,7 @@ public Void run() throws IOException { // URL until we actually need to try to load something from them. for (int i = 0; i < jarfiles.length; i++) { try { + @SuppressWarnings("deprecation") URL jarURL = new URL(csu, jarfiles[i]); // If a non-null loader already exists, leave it alone. String urlNoFragString = URLUtil.urlNoFragString(jarURL); @@ -829,6 +840,7 @@ private JarFile getJarFile(URL url) throws IOException { return checkJar(new JarFile(new File(p.getPath()), true, ZipFile.OPEN_READ, JarFile.runtimeVersion())); } + @SuppressWarnings("deprecation") URLConnection uc = (new URL(getBaseURL(), "#runtime")).openConnection(); uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION); JarFile jarFile = ((JarURLConnection)uc).getJarFile(); @@ -862,7 +874,8 @@ Resource checkResource(final String name, boolean check, } else { nm = name; } - url = new URL(getBaseURL(), ParseUtil.encodePath(nm, false)); + @SuppressWarnings("deprecation") + var _unused = url = new URL(getBaseURL(), ParseUtil.encodePath(nm, false)); if (check) { URLClassPath.check(url); } @@ -993,7 +1006,8 @@ Resource getResource(final String name, boolean check, final URL url; try{ - url = new URL(csu, jarName); + @SuppressWarnings("deprecation") + var _unused = url = new URL(csu, jarName); String urlNoFragString = URLUtil.urlNoFragString(url); if ((newLoader = (JarLoader)lmap.get(urlNoFragString)) == null) { /* no loader has been set up for this jar file @@ -1116,6 +1130,7 @@ private static URL[] parseClassPath(URL base, String value) int i = 0; while (st.hasMoreTokens()) { String path = st.nextToken(); + @SuppressWarnings("deprecation") URL url = DISABLE_CP_URL_CHECK ? new URL(base, path) : tryResolve(base, path); if (url != null) { urls[i] = url; @@ -1152,6 +1167,7 @@ static URL tryResolve(URL base, String input) throws MalformedURLException { * @throws MalformedURLException */ static URL tryResolveFile(URL base, String input) throws MalformedURLException { + @SuppressWarnings("deprecation") URL retVal = new URL(base, input); if (input.indexOf(':') >= 0 && !"file".equalsIgnoreCase(retVal.getProtocol())) { @@ -1173,6 +1189,7 @@ static URL tryResolveFile(URL base, String input) throws MalformedURLException { static URL tryResolveNonFile(URL base, String input) throws MalformedURLException { String child = input.replace(File.separatorChar, '/'); if (isRelative(child)) { + @SuppressWarnings("deprecation") URL url = new URL(base, child); String bp = base.getPath(); String urlp = url.getPath(); @@ -1217,7 +1234,8 @@ private FileLoader(URL url) throws IOException { String path = url.getFile().replace('/', File.separatorChar); path = ParseUtil.decode(path); dir = (new File(path)).getCanonicalFile(); - normalizedBase = new URL(getBaseURL(), "."); + @SuppressWarnings("deprecation") + var _unused = normalizedBase = new URL(getBaseURL(), "."); } /* @@ -1236,7 +1254,8 @@ URL findResource(final String name, boolean check) { Resource getResource(final String name, boolean check) { final URL url; try { - url = new URL(getBaseURL(), ParseUtil.encodePath(name, false)); + @SuppressWarnings("deprecation") + var _unused = url = new URL(getBaseURL(), ParseUtil.encodePath(name, false)); if (url.getFile().startsWith(normalizedBase.getFile()) == false) { // requested resource had ../..'s in path diff --git a/src/java.base/share/classes/jdk/internal/module/ModulePatcher.java b/src/java.base/share/classes/jdk/internal/module/ModulePatcher.java index 607357352f914..ce837027faa26 100644 --- a/src/java.base/share/classes/jdk/internal/module/ModulePatcher.java +++ b/src/java.base/share/classes/jdk/internal/module/ModulePatcher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 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 @@ -454,7 +454,9 @@ public String getName() { public URL getURL() { String encodedPath = ParseUtil.encodePath(name, false); try { - return new URL("jar:" + csURL + "!/" + encodedPath); + @SuppressWarnings("deprecation") + var result = new URL("jar:" + csURL + "!/" + encodedPath); + return result; } catch (MalformedURLException e) { return null; } diff --git a/src/java.base/share/classes/sun/net/util/URLUtil.java b/src/java.base/share/classes/sun/net/util/URLUtil.java index a3cf4d7fb16a4..6d80825e901c7 100644 --- a/src/java.base/share/classes/sun/net/util/URLUtil.java +++ b/src/java.base/share/classes/sun/net/util/URLUtil.java @@ -95,6 +95,7 @@ public static Permission getConnectPermission(URL url) throws IOException { String urlString = url.toString(); int bangPos = urlString.indexOf("!/"); urlString = urlString.substring(4, bangPos > -1 ? bangPos : urlString.length()); + @SuppressWarnings("deprecation") URL u = new URL(urlString); return getURLConnectPermission(u); // If protocol is HTTP or HTTPS than use URLPermission object diff --git a/src/java.base/share/classes/sun/net/www/ParseUtil.java b/src/java.base/share/classes/sun/net/www/ParseUtil.java index d84fa82b19e42..def688ad96a3a 100644 --- a/src/java.base/share/classes/sun/net/www/ParseUtil.java +++ b/src/java.base/share/classes/sun/net/www/ParseUtil.java @@ -239,7 +239,9 @@ public static URL fileToEncodedURL(File file) if (!path.endsWith("/") && file.isDirectory()) { path = path + "/"; } - return new URL("file", "", path); + @SuppressWarnings("deprecation") + var result = new URL("file", "", path); + return result; } public static java.net.URI toURI(URL url) { diff --git a/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java b/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java index 0e8584eece5c8..36f5e3cb37fcd 100644 --- a/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java +++ b/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java @@ -987,7 +987,7 @@ public static InputStream openConnectionCheckRedirects(URLConnection c) String loc = http.getHeaderField("Location"); URL target = null; if (loc != null) { - target = new URL(base, loc); + target = newURL(base, loc); } http.disconnect(); if (target == null @@ -1885,7 +1885,7 @@ private InputStream getInputStream0() throws IOException { String path = tok.nextToken(); try { /* path could be an abs_path or a complete URI */ - URL u = new URL (url, path); + URL u = newURL (url, path); DigestAuthentication d = new DigestAuthentication ( false, u, realm, "Digest", pw, digestparams, srv.authenticatorKey); @@ -2502,6 +2502,7 @@ public InetAddress run() if (ret == null && defaultAuth != null && defaultAuth.schemeSupported(scheme)) { try { + @SuppressWarnings("deprecation") URL u = new URL("http", host, port, "/"); String a = defaultAuth.authString(u, scheme, realm); if (a != null) { @@ -2616,7 +2617,7 @@ private AuthenticationInfo getServerAuthentication(AuthenticationHeader authhdr) if (NTLMAuthenticationProxy.supported) { URL url1; try { - url1 = new URL (url, "/"); /* truncate the path */ + url1 = newURL (url, "/"); /* truncate the path */ } catch (Exception e) { url1 = url; } @@ -2774,14 +2775,14 @@ private boolean followRedirect() throws IOException { URL locUrl; try { - locUrl = new URL(loc); + locUrl = newURL(loc); if (!url.getProtocol().equalsIgnoreCase(locUrl.getProtocol())) { return false; } } catch (MalformedURLException mue) { - // treat loc as a relative URI to conform to popular browsers - locUrl = new URL(url, loc); + // treat loc as a relative URI to conform to popular browsers + locUrl = newURL(url, loc); } final URL locUrl0 = locUrl; @@ -3994,6 +3995,16 @@ public void close() throws IOException { } } } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } + + @SuppressWarnings("deprecation") + private static URL newURL(URL context, String spec) throws MalformedURLException { + return new URL(context, spec); + } } /** An input stream that just returns EOF. This is for diff --git a/src/java.base/share/classes/sun/net/www/protocol/jar/Handler.java b/src/java.base/share/classes/sun/net/www/protocol/jar/Handler.java index 48381c8a276c3..2884f825b8439 100644 --- a/src/java.base/share/classes/sun/net/www/protocol/jar/Handler.java +++ b/src/java.base/share/classes/sun/net/www/protocol/jar/Handler.java @@ -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 @@ -78,8 +78,8 @@ protected boolean sameFile(URL u1, URL u2) { URL enclosedURL1 = null, enclosedURL2 = null; try { - enclosedURL1 = new URL(file1.substring(0, sep1)); - enclosedURL2 = new URL(file2.substring(0, sep2)); + enclosedURL1 = newURL(file1.substring(0, sep1)); + enclosedURL2 = newURL(file2.substring(0, sep2)); } catch (MalformedURLException unused) { return super.sameFile(u1, u2); } @@ -108,7 +108,7 @@ protected int hashCode(URL u) { URL enclosedURL = null; String fileWithoutEntry = file.substring(0, sep); try { - enclosedURL = new URL(fileWithoutEntry); + enclosedURL = newURL(fileWithoutEntry); h += enclosedURL.hashCode(); } catch (MalformedURLException unused) { h += fileWithoutEntry.hashCode(); @@ -179,7 +179,7 @@ private String parseAbsoluteSpec(String spec) { // test the inner URL try { String innerSpec = spec.substring(0, index - 1); - new URL(innerSpec); + newURL(innerSpec); } catch (MalformedURLException e) { throw new NullPointerException("invalid url: " + spec + " (" + e + ")"); @@ -259,4 +259,9 @@ private static String doCanonicalize(String file) { return file; } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/src/java.base/share/classes/sun/net/www/protocol/jrt/JavaRuntimeURLConnection.java b/src/java.base/share/classes/sun/net/www/protocol/jrt/JavaRuntimeURLConnection.java index f8b10485f5778..b4cd98870ca23 100644 --- a/src/java.base/share/classes/sun/net/www/protocol/jrt/JavaRuntimeURLConnection.java +++ b/src/java.base/share/classes/sun/net/www/protocol/jrt/JavaRuntimeURLConnection.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 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 @@ -166,6 +166,7 @@ public Permission getPermission() { /** * Returns a jrt URL for the given module and resource name. */ + @SuppressWarnings("deprecation") private static URL toJrtURL(String module, String name) { try { return new URL("jrt:/" + module + "/" + name); @@ -177,6 +178,7 @@ private static URL toJrtURL(String module, String name) { /** * Returns a jrt URL for the given module. */ + @SuppressWarnings("deprecation") private static URL toJrtURL(String module) { try { return new URL("jrt:/" + module); diff --git a/src/java.base/share/classes/sun/security/provider/ConfigFile.java b/src/java.base/share/classes/sun/security/provider/ConfigFile.java index ae72c9ad6ad32..775e36c61baa6 100644 --- a/src/java.base/share/classes/sun/security/provider/ConfigFile.java +++ b/src/java.base/share/classes/sun/security/provider/ConfigFile.java @@ -256,7 +256,7 @@ private void init() throws IOException { URL configURL; try { - configURL = new URL(extra_config); + configURL = newURL(extra_config); } catch (MalformedURLException mue) { File configFile = new File(extra_config); if (configFile.exists()) { @@ -293,7 +293,7 @@ private void init() throws IOException { if (debugConfig != null) { debugConfig.println("\tReading config: " + config_url); } - init(new URL(config_url), newConfig); + init(newURL(config_url), newConfig); initialized = true; } catch (PropertyExpander.ExpandException peee) { throw ioException("Unable.to.properly.expand.config", @@ -669,4 +669,9 @@ private IOException ioException(String resourceKey, Object... args) { return new IOException(form.format(args)); } } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/src/java.base/share/classes/sun/security/provider/PolicyFile.java b/src/java.base/share/classes/sun/security/provider/PolicyFile.java index 640fb63dd259e..f69f513dc9fcf 100644 --- a/src/java.base/share/classes/sun/security/provider/PolicyFile.java +++ b/src/java.base/share/classes/sun/security/provider/PolicyFile.java @@ -414,7 +414,7 @@ public Boolean run() { policyURL = ParseUtil.fileToEncodedURL (new File(policyFile.getCanonicalPath())); } else { - policyURL = new URL(extra_policy); + policyURL = newURL(extra_policy); } if (debug != null) { debug.println("reading "+policyURL); @@ -672,7 +672,7 @@ private CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore, URL location; if (ge.codeBase != null) - location = new URL(ge.codeBase); + location = newURL(ge.codeBase); else location = null; @@ -1607,7 +1607,7 @@ private CodeSource canonicalizeCodebase(CodeSource cs, int separator = spec.indexOf("!/"); if (separator != -1) { try { - u = new URL(spec.substring(0, separator)); + u = newURL(spec.substring(0, separator)); } catch (MalformedURLException e) { // Fail silently. In this case, url stays what // it was above @@ -2223,4 +2223,9 @@ ProtectionDomainCache getPdMapping() { } } } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/src/java.base/share/classes/sun/security/provider/SeedGenerator.java b/src/java.base/share/classes/sun/security/provider/SeedGenerator.java index d9eb83ff60dd2..94ffc26ab3fbb 100644 --- a/src/java.base/share/classes/sun/security/provider/SeedGenerator.java +++ b/src/java.base/share/classes/sun/security/provider/SeedGenerator.java @@ -504,6 +504,7 @@ static class URLSeedGenerator extends SeedGenerator { @SuppressWarnings("removal") private void init() throws IOException { + @SuppressWarnings("deprecation") final URL device = new URL(deviceName); try { seedStream = java.security.AccessController.doPrivileged diff --git a/src/java.base/share/classes/sun/security/provider/certpath/OCSP.java b/src/java.base/share/classes/sun/security/provider/certpath/OCSP.java index 4ad35683aa1b1..eaef53598b7f1 100644 --- a/src/java.base/share/classes/sun/security/provider/certpath/OCSP.java +++ b/src/java.base/share/classes/sun/security/provider/certpath/OCSP.java @@ -183,7 +183,8 @@ public static byte[] getOCSPBytes(List certIds, URI responderURI, Base64.getEncoder().encodeToString(bytes), UTF_8)); if (encodedGetReq.length() <= 255) { - url = new URL(encodedGetReq.toString()); + @SuppressWarnings("deprecation") + var _unused = url = new URL(encodedGetReq.toString()); con = (HttpURLConnection)url.openConnection(); con.setDoOutput(true); con.setDoInput(true); diff --git a/src/java.base/share/classes/sun/security/tools/KeyStoreUtil.java b/src/java.base/share/classes/sun/security/tools/KeyStoreUtil.java index 7492c4cda93eb..cb025e7431585 100644 --- a/src/java.base/share/classes/sun/security/tools/KeyStoreUtil.java +++ b/src/java.base/share/classes/sun/security/tools/KeyStoreUtil.java @@ -155,7 +155,8 @@ public static char[] getPassWithModifier(String modifier, String arg, try { URL url; try { - url = new URL(arg); + @SuppressWarnings("deprecation") + var _unused = url = new URL(arg); } catch (java.net.MalformedURLException mue) { File f = new File(arg); if (f.exists()) { diff --git a/src/java.base/share/classes/sun/security/tools/PathList.java b/src/java.base/share/classes/sun/security/tools/PathList.java index ef524c4582192..79ff0e8055329 100644 --- a/src/java.base/share/classes/sun/security/tools/PathList.java +++ b/src/java.base/share/classes/sun/security/tools/PathList.java @@ -99,7 +99,9 @@ private static URL fileToURL(File file) { name = name + "/"; } try { - return new URL("file", "", name); + @SuppressWarnings("deprecation") + var result = new URL("file", "", name); + return result; } catch (MalformedURLException e) { throw new IllegalArgumentException("file"); } diff --git a/src/java.base/share/classes/sun/security/util/PolicyUtil.java b/src/java.base/share/classes/sun/security/util/PolicyUtil.java index 150d2cf98efbb..dc29f97ca044e 100644 --- a/src/java.base/share/classes/sun/security/util/PolicyUtil.java +++ b/src/java.base/share/classes/sun/security/util/PolicyUtil.java @@ -110,14 +110,16 @@ public static InputStream getInputStream(URL url) throws IOException { if (storePassURL != null) { URL passURL; try { - passURL = new URL(storePassURL); + @SuppressWarnings("deprecation") + var _unused = passURL = new URL(storePassURL); // absolute URL } catch (MalformedURLException e) { // relative URL if (policyUrl == null) { throw e; } - passURL = new URL(policyUrl, storePassURL); + @SuppressWarnings("deprecation") + var _unused = passURL = new URL(policyUrl, storePassURL); } if (debug != null) { @@ -138,14 +140,16 @@ public static InputStream getInputStream(URL url) throws IOException { */ URL keyStoreUrl; try { - keyStoreUrl = new URL(keyStoreName); + @SuppressWarnings("deprecation") + var _unused = keyStoreUrl = new URL(keyStoreName); // absolute URL } catch (MalformedURLException e) { // relative URL if (policyUrl == null) { throw e; } - keyStoreUrl = new URL(policyUrl, keyStoreName); + @SuppressWarnings("deprecation") + var _unused = keyStoreUrl = new URL(policyUrl, keyStoreName); } if (debug != null) { diff --git a/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java b/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java index 207e95d27f8fd..4b2c110b3e1df 100644 --- a/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java +++ b/src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 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 @@ -77,7 +77,8 @@ public URLConnection openConnection(URL u, Proxy p) URL ru; try { - ru = new URL("ftp", host, u.getFile() + + @SuppressWarnings("deprecation") + var _unused = ru = new URL("ftp", host, u.getFile() + (u.getRef() == null ? "": "#" + u.getRef())); if (p != null) { uc = ru.openConnection(p); diff --git a/src/java.base/unix/classes/sun/security/provider/NativePRNG.java b/src/java.base/unix/classes/sun/security/provider/NativePRNG.java index 5bdd85c4f898c..25548bfeb8096 100644 --- a/src/java.base/unix/classes/sun/security/provider/NativePRNG.java +++ b/src/java.base/unix/classes/sun/security/provider/NativePRNG.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 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 @@ -108,7 +108,8 @@ private static URL getEgdUrl() { debug.println("NativePRNG egdUrl: " + egdSource); } try { - egdUrl = new URL(egdSource); + @SuppressWarnings("deprecation") + var _unused = egdUrl = new URL(egdSource); if (!egdUrl.getProtocol().equalsIgnoreCase("file")) { return null; } diff --git a/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java b/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java index 18ccda6e7c1d9..2865aa32a95ff 100644 --- a/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java +++ b/src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 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 @@ -94,7 +94,8 @@ public URLConnection openConnection(URL url, Proxy p) URL newurl; try { - newurl = new URL("ftp", host, file + + @SuppressWarnings("deprecation") + var _unused = newurl = new URL("ftp", host, file + (url.getRef() == null ? "": "#" + url.getRef())); if (p != null) { diff --git a/src/java.base/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java b/src/java.base/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java index 8f8808bd5eb99..178ffe84a0643 100644 --- a/src/java.base/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java +++ b/src/java.base/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 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 @@ -155,7 +155,8 @@ private URL urlFor(URL url) throws IOException { if (host != null && !host.isEmpty() && !host.equalsIgnoreCase("localhost")) { - url = new URL("file", "", "//" + host + url.getPath()); + @SuppressWarnings("deprecation") + var _unused = url = new URL("file", "", "//" + host + url.getPath()); } } return url; diff --git a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CDataTransferer.java b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CDataTransferer.java index 0a7591bd064f4..32cd2efa79564 100644 --- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CDataTransferer.java +++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CDataTransferer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 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 @@ -145,7 +145,9 @@ public Object translateBytes(byte[] bytes, DataFlavor flavor, String xml = new String(bytes, charset); // macosx pasteboard returns a property list that consists of one URL // let's extract it. - return new URL(extractURL(xml)); + @SuppressWarnings("deprecation") + var result = new URL(extractURL(xml)); + return result; } if(isUriListFlavor(flavor) && format == CF_FILE) { diff --git a/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java b/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java index 075e4d54f598a..d430825bb8565 100644 --- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java +++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java @@ -999,8 +999,10 @@ public boolean enableInputMethodsForTextComponent() { private static URL getScaledImageURL(URL url) { try { String scaledImagePath = getScaledImageName(url.getPath()); - return scaledImagePath == null ? null : new URL(url.getProtocol(), + @SuppressWarnings("deprecation") + var result = scaledImagePath == null ? null : new URL(url.getProtocol(), url.getHost(), url.getPort(), scaledImagePath); + return result; } catch (MalformedURLException e) { return null; } diff --git a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java index dc9838154415b..295e474579bd7 100644 --- a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java +++ b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java @@ -157,6 +157,7 @@ protected Metacity(String themeName) throws IOException, ParserConfigurationExce this.themeName = themeName; themeDir = getThemeDir(themeName); if (themeDir != null) { + @SuppressWarnings("deprecation") URL themeURL = new URL(themeDir, "metacity-theme-1.xml"); xmlDoc = getXMLDoc(themeURL); if (xmlDoc == null) { @@ -558,7 +559,8 @@ public Object run() { if (url != null) { String str = url.toString(); try { - themeDir = new URL(str.substring(0, str.lastIndexOf('/'))+"/"); + @SuppressWarnings("deprecation") + var _unused = themeDir = new URL(str.substring(0, str.lastIndexOf('/'))+"/"); } catch (MalformedURLException ex) { themeDir = null; } @@ -576,6 +578,7 @@ public Object run() { } // Note: this is a small file (< 1024 bytes) so it's not worth // starting an XML parser or even to use a buffered reader. + @SuppressWarnings("deprecation") URL url = new URL(new File(userHome).toURI().toURL(), ".gconf/apps/metacity/general/%25gconf.xml"); // Pending: verify character encoding spec for gconf @@ -668,6 +671,7 @@ protected Image getImage(String key) { if (image == null) { if (themeDir != null) { try { + @SuppressWarnings("deprecation") URL url = new URL(themeDir, key); image = (Image)new Privileged().doPrivileged(Privileged.GET_IMAGE, url); } catch (MalformedURLException ex) { diff --git a/src/java.desktop/share/classes/java/applet/Applet.java b/src/java.desktop/share/classes/java/applet/Applet.java index 9f0373205d876..1d0e25e117767 100644 --- a/src/java.desktop/share/classes/java/applet/Applet.java +++ b/src/java.desktop/share/classes/java/applet/Applet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 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 @@ -313,7 +313,9 @@ public Image getImage(URL url) { */ public Image getImage(URL url, String name) { try { - return getImage(new URL(url, name)); + @SuppressWarnings("deprecation") + var u = new URL(url, name); + return getImage(u); } catch (MalformedURLException e) { return null; } @@ -363,7 +365,9 @@ public AudioClip getAudioClip(URL url) { */ public AudioClip getAudioClip(URL url, String name) { try { - return getAudioClip(new URL(url, name)); + @SuppressWarnings("deprecation") + var u = new URL(url, name); + return getAudioClip(u); } catch (MalformedURLException e) { return null; } diff --git a/src/java.desktop/share/classes/java/awt/SplashScreen.java b/src/java.desktop/share/classes/java/awt/SplashScreen.java index b7318bd04a65f..a7939f4a385c5 100644 --- a/src/java.desktop/share/classes/java/awt/SplashScreen.java +++ b/src/java.desktop/share/classes/java/awt/SplashScreen.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 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 @@ -224,7 +224,9 @@ public URL getImageURL() throws IllegalStateException { String jarName = _getImageJarName(splashPtr); if (fileName != null) { if (jarName != null) { - imageURL = new URL("jar:"+(new File(jarName).toURL().toString())+"!/"+fileName); + @SuppressWarnings("deprecation") + var _unused = imageURL = + new URL("jar:"+(new File(jarName).toURL().toString())+"!/"+fileName); } else { imageURL = new File(fileName).toURL(); } diff --git a/src/java.desktop/share/classes/java/beans/Beans.java b/src/java.desktop/share/classes/java/beans/Beans.java index 131473f83bf94..6dd1aa9f39c1a 100644 --- a/src/java.desktop/share/classes/java/beans/Beans.java +++ b/src/java.desktop/share/classes/java/beans/Beans.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 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 @@ -44,6 +44,7 @@ import java.lang.reflect.Modifier; +import java.net.MalformedURLException; import java.net.URL; import java.util.Enumeration; @@ -304,13 +305,13 @@ public static Object instantiate(ClassLoader cls, String beanName, if (s.endsWith(resourceName)) { int ix = s.length() - resourceName.length(); - codeBase = new URL(s.substring(0,ix)); + codeBase = newURL(s.substring(0,ix)); docBase = codeBase; ix = s.lastIndexOf('/'); if (ix >= 0) { - docBase = new URL(s.substring(0,ix+1)); + docBase = newURL(s.substring(0,ix+1)); } } } @@ -356,6 +357,11 @@ private static void unsafeBeanContextAdd(BeanContext beanContext, Object res) { beanContext.add(res); } + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } + /** * From a given bean, obtain an object representing a specified * type view of that source object. diff --git a/src/java.desktop/share/classes/javax/swing/JEditorPane.java b/src/java.desktop/share/classes/javax/swing/JEditorPane.java index 236e187fde81c..ee2f174206c0a 100644 --- a/src/java.desktop/share/classes/javax/swing/JEditorPane.java +++ b/src/java.desktop/share/classes/javax/swing/JEditorPane.java @@ -795,9 +795,11 @@ protected InputStream getStream(URL page) throws IOException { if (redirect) { String loc = conn.getHeaderField("Location"); if (loc.startsWith("http", 0)) { - page = new URL(loc); + @SuppressWarnings("deprecation") + var _unused = page = new URL(loc); } else { - page = new URL(page, loc); + @SuppressWarnings("deprecation") + var _unused = page = new URL(page, loc); } return getStream(page); } @@ -926,6 +928,7 @@ public void setPage(String url) throws IOException { if (url == null) { throw new IOException("invalid url"); } + @SuppressWarnings("deprecation") URL page = new URL(url); setPage(page); } @@ -1948,7 +1951,8 @@ public Object getAccessibleActionObject(int i) { if (href != null) { URL u; try { - u = new URL(JEditorPane.this.getPage(), href); + @SuppressWarnings("deprecation") + var _unused = u = new URL(JEditorPane.this.getPage(), href); } catch (MalformedURLException m) { u = null; } diff --git a/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthParser.java b/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthParser.java index 71dda727219b8..2da1f958718b6 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthParser.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthParser.java @@ -260,7 +260,9 @@ private URL getResource(String path) { return _classResourceBase.getResource(path); } else { try { - return new URL(_urlResourceBase, path); + @SuppressWarnings("deprecation") + var result = new URL(_urlResourceBase, path); + return result; } catch (MalformedURLException mue) { return null; } diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 73087bd20225f..37a0c9306172b 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 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 @@ -1293,6 +1293,7 @@ static URL getURL(URL base, String cssString) { } // Absolute first try { + @SuppressWarnings("deprecation") URL url = new URL(cssString); if (url != null) { return url; @@ -1303,6 +1304,7 @@ static URL getURL(URL base, String cssString) { if (base != null) { // Relative URL, try from base try { + @SuppressWarnings("deprecation") URL url = new URL(base, cssString); return url; } diff --git a/src/java.desktop/share/classes/javax/swing/text/html/FormView.java b/src/java.desktop/share/classes/javax/swing/text/html/FormView.java index 62ebaa5fdbe72..8af4f9e2f1e19 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/FormView.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/FormView.java @@ -276,6 +276,7 @@ private JComponent createInputComponent(AttributeSet attr, Object model) { JButton button; try { URL base = ((HTMLDocument)getElement().getDocument()).getBase(); + @SuppressWarnings("deprecation") URL srcURL = new URL(base, srcAtt); Icon icon = new ImageIcon(srcURL); button = new JButton(icon); @@ -515,13 +516,15 @@ protected void submitData(String data) { String action = (String) attrs.getAttribute(HTML.Attribute.ACTION); URL actionURL; try { - actionURL = (action == null) + @SuppressWarnings("deprecation") + var _unused = actionURL = (action == null) ? new URL(base.getProtocol(), base.getHost(), base.getPort(), base.getFile()) : new URL(base, action); if (!isPostMethod) { String query = data.toString(); - actionURL = new URL(actionURL + "?" + query); + @SuppressWarnings("deprecation") + var _unused2 = actionURL = new URL(actionURL + "?" + query); } } catch (MalformedURLException e) { actionURL = null; diff --git a/src/java.desktop/share/classes/javax/swing/text/html/FrameView.java b/src/java.desktop/share/classes/javax/swing/text/html/FrameView.java index 5ecbaefa70961..345f947a9cce1 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/FrameView.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/FrameView.java @@ -71,7 +71,8 @@ protected Component createComponent() { if (srcAtt != null && !srcAtt.isEmpty()) { try { URL base = ((HTMLDocument)elem.getDocument()).getBase(); - src = new URL(base, srcAtt); + @SuppressWarnings("deprecation") + var _unused = src = new URL(base, srcAtt); htmlPane = new FrameEditorPane(); htmlPane.addHyperlinkListener(this); JEditorPane host = getHostPane(); @@ -373,7 +374,8 @@ public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) { } Object postData = movePostData(htmlPane, null); - src = new URL(base, srcAtt); + @SuppressWarnings("deprecation") + var _unused = src = new URL(base, srcAtt); if (oldPage.equals(src) && (src.getRef() == null) && (postData == null)) { return; } diff --git a/src/java.desktop/share/classes/javax/swing/text/html/HTMLDocument.java b/src/java.desktop/share/classes/javax/swing/text/html/HTMLDocument.java index c75425fd2877c..61463eb431fb3 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/HTMLDocument.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/HTMLDocument.java @@ -3528,6 +3528,7 @@ public void start(HTML.Tag t, MutableAttributeSet attr) { String href = (String) attr.getAttribute(HTML.Attribute.HREF); if (href != null) { try { + @SuppressWarnings("deprecation") URL newBase = new URL(base, href); setBase(newBase); hasBaseTag = true; @@ -4122,10 +4123,12 @@ void addCSSRules(String rules) { void linkCSSStyleSheet(String href) { URL url; try { - url = new URL(base, href); + @SuppressWarnings("deprecation") + var _unused = url = new URL(base, href); } catch (MalformedURLException mfe) { try { - url = new URL(href); + @SuppressWarnings("deprecation") + var _unused = url = new URL(href); } catch (MalformedURLException mfe2) { url = null; } diff --git a/src/java.desktop/share/classes/javax/swing/text/html/HTMLEditorKit.java b/src/java.desktop/share/classes/javax/swing/text/html/HTMLEditorKit.java index 4347e96d675a3..24e8c255547c7 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/HTMLEditorKit.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/HTMLEditorKit.java @@ -969,7 +969,8 @@ HyperlinkEvent createHyperlinkEvent(JEditorPane html, URL u; try { URL base = hdoc.getBase(); - u = new URL(base, href); + @SuppressWarnings("deprecation") + var _unused = u = new URL(base, href); // Following is a workaround for 1.2, in which // new URL("file://...", "#...") causes the filename to // be lost. @@ -979,7 +980,8 @@ HyperlinkEvent createHyperlinkEvent(JEditorPane html, String newFile = u.getFile(); if (baseFile != null && newFile != null && !newFile.startsWith(baseFile)) { - u = new URL(base, baseFile + href); + @SuppressWarnings("deprecation") + var _unused2 = u = new URL(base, baseFile + href); } } } catch (MalformedURLException m) { @@ -1013,7 +1015,8 @@ void fireEvents(JEditorPane editor, HTMLDocument doc, String href, // fire an exited event on the old link URL u; try { - u = new URL(doc.getBase(), this.href); + @SuppressWarnings("deprecation") + var _unused = u = new URL(doc.getBase(), this.href); } catch (MalformedURLException m) { u = null; } @@ -1026,7 +1029,8 @@ void fireEvents(JEditorPane editor, HTMLDocument doc, String href, // fire an entered event on the new link URL u; try { - u = new URL(doc.getBase(), href); + @SuppressWarnings("deprecation") + var _unused = u = new URL(doc.getBase(), href); } catch (MalformedURLException m) { u = null; } @@ -2392,6 +2396,7 @@ private void activateLink(String href, HTMLDocument doc, try { URL page = (URL)doc.getProperty(Document.StreamDescriptionProperty); + @SuppressWarnings("deprecation") URL url = new URL(page, href); HyperlinkEvent linkEvent = new HyperlinkEvent (editor, HyperlinkEvent.EventType. diff --git a/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java b/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java index c48b00e56a3d4..9f7b334edbebd 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java @@ -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 @@ -196,6 +196,7 @@ public URL getImageURL() { URL reference = ((HTMLDocument)getDocument()).getBase(); try { + @SuppressWarnings("deprecation") URL u = new URL(reference,src); return u; } catch (MalformedURLException e) { diff --git a/src/java.desktop/share/classes/javax/swing/text/html/IsindexView.java b/src/java.desktop/share/classes/javax/swing/text/html/IsindexView.java index 4b2fb1e1c7369..22c345027ae9f 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/IsindexView.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/IsindexView.java @@ -104,6 +104,7 @@ public void actionPerformed(ActionEvent evt) { action = hdoc.getBase().toString(); } try { + @SuppressWarnings("deprecation") URL url = new URL(action+"?"+data); JEditorPane pane = (JEditorPane)getContainer(); pane.setPage(url); diff --git a/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java b/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java index a10988c19b3d2..fd3c75829e861 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java @@ -2105,11 +2105,13 @@ public static final class ListPainter implements Serializable { tmpstr = st.nextToken(); if (st.hasMoreTokens()) tmpstr = st.nextToken(); + @SuppressWarnings("deprecation") URL u = new URL(tmpstr); img = new ImageIcon(u); } catch (MalformedURLException e) { if (tmpstr != null && ss != null && ss.getBase() != null) { try { + @SuppressWarnings("deprecation") URL u = new URL(ss.getBase(), tmpstr); img = new ImageIcon(u); } catch (MalformedURLException murle) { diff --git a/src/java.desktop/share/classes/sun/awt/image/URLImageSource.java b/src/java.desktop/share/classes/sun/awt/image/URLImageSource.java index 379b3e46cf592..0cb44ae64b0bc 100644 --- a/src/java.desktop/share/classes/sun/awt/image/URLImageSource.java +++ b/src/java.desktop/share/classes/sun/awt/image/URLImageSource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 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 @@ -58,7 +58,7 @@ public URLImageSource(URL u) { } public URLImageSource(String href) throws MalformedURLException { - this(new URL(null, href)); + this(newURL(null, href)); } public URLImageSource(URL u, URLConnection uc) { @@ -159,4 +159,9 @@ protected ImageDecoder getDecoder() { } return id; } + + @SuppressWarnings("deprecation") + private static URL newURL(URL context, String spec) throws MalformedURLException { + return new URL(context, spec); + } } diff --git a/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java b/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java index 0e398edef8575..e2a69c79c3e11 100644 --- a/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java +++ b/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 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 @@ -291,6 +291,7 @@ static String[] getDefaultPrinter() { return printerInfo.clone(); } try { + @SuppressWarnings("deprecation") URL url = new URL("http", getServer(), getPort(), ""); final HttpURLConnection urlConnection = IPPPrintService.getIPPConnection(url); @@ -405,6 +406,7 @@ static String[] getAllPrinters() { } try { + @SuppressWarnings("deprecation") URL url = new URL("http", getServer(), getPort(), ""); final HttpURLConnection urlConnection = diff --git a/src/java.desktop/unix/classes/sun/print/IPPPrintService.java b/src/java.desktop/unix/classes/sun/print/IPPPrintService.java index 82cb84a14550e..91be3e06a5a7a 100644 --- a/src/java.desktop/unix/classes/sun/print/IPPPrintService.java +++ b/src/java.desktop/unix/classes/sun/print/IPPPrintService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 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 @@ -36,6 +36,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -406,7 +407,7 @@ protected static void debug_println(String str) { defaultMediaIndex = -1; try { myURL = - new URL(uriStr.replaceFirst("ipp", "http")); + newURL(uriStr.replaceFirst("ipp", "http")); } catch (Exception e) { IPPPrintService.debug_println(debugPrefix+ " IPPPrintService, myURL="+ @@ -1762,7 +1763,7 @@ public synchronized boolean isPostscript() { if (isCupsPrinter) { try { urlConnection = getIPPConnection( - new URL(myURL+".ppd")); + newURL(myURL+".ppd")); InputStream is = urlConnection.getInputStream(); if (is != null) { @@ -2076,4 +2077,9 @@ public boolean equals(Object obj) { public int hashCode() { return this.getClass().hashCode()+getName().hashCode(); } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/src/java.desktop/unix/classes/sun/print/PrintServiceLookupProvider.java b/src/java.desktop/unix/classes/sun/print/PrintServiceLookupProvider.java index 7fa1b201340c5..75e77e23a121e 100644 --- a/src/java.desktop/unix/classes/sun/print/PrintServiceLookupProvider.java +++ b/src/java.desktop/unix/classes/sun/print/PrintServiceLookupProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 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 @@ -27,6 +27,7 @@ import java.io.BufferedReader; import java.io.IOException; +import java.net.MalformedURLException; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Vector; @@ -475,7 +476,7 @@ private PrintService getServiceByName(PrinterName nameAttr) { if (CUPSPrinter.isCupsRunning()) { try { return new IPPPrintService(name, - new URL("http://"+ + newURL("http://"+ CUPSPrinter.getServer()+":"+ CUPSPrinter.getPort()+"/"+ name)); @@ -687,7 +688,7 @@ public synchronized PrintService getDefaultPrintService() { psuri, true); } else { defaultPS = new IPPPrintService(defaultPrinter, - new URL("http://"+ + newURL("http://"+ CUPSPrinter.getServer()+":"+ CUPSPrinter.getPort()+"/"+ defaultPrinter)); @@ -969,4 +970,9 @@ public void run() { } } } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/src/java.desktop/windows/classes/sun/awt/windows/WDataTransferer.java b/src/java.desktop/windows/classes/sun/awt/windows/WDataTransferer.java index 456300c6197c5..d665047c795db 100644 --- a/src/java.desktop/windows/classes/sun/awt/windows/WDataTransferer.java +++ b/src/java.desktop/windows/classes/sun/awt/windows/WDataTransferer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 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 @@ -271,7 +271,9 @@ public Object translateBytes(byte[] bytes, DataFlavor flavor, long format, } catch (UnsupportedFlavorException cannotHappen) { } } - return new URL(new String(bytes, charset)); + @SuppressWarnings("deprecation") + var result = new URL(new String(bytes, charset)); + return result; } return super.translateBytes(bytes , flavor, format, diff --git a/src/java.management/share/classes/javax/management/loading/MLet.java b/src/java.management/share/classes/javax/management/loading/MLet.java index ec6ab22e09634..250e5945645ce 100644 --- a/src/java.management/share/classes/javax/management/loading/MLet.java +++ b/src/java.management/share/classes/javax/management/loading/MLet.java @@ -406,6 +406,7 @@ public void addURL(URL url) { */ public void addURL(String url) throws ServiceNotFoundException { try { + @SuppressWarnings("deprecation") URL ur = new URL(url); if (!Arrays.asList(getURLs()).contains(ur)) super.addURL(ur); @@ -571,8 +572,10 @@ public Set getMBeansFromURL(String url) // Appends the specified JAR file URL to the list of // URLs to search for classes and resources. try { + @SuppressWarnings("deprecation") + var u = new URL(codebase.toString() + tok); if (!Arrays.asList(getURLs()) - .contains(new URL(codebase.toString() + tok))) { + .contains(u)) { addURL(codebase + tok); } } catch (MalformedURLException me) { diff --git a/src/java.management/share/classes/javax/management/loading/MLetContent.java b/src/java.management/share/classes/javax/management/loading/MLetContent.java index 3001c06eb2ace..32b099a3615f7 100644 --- a/src/java.management/share/classes/javax/management/loading/MLetContent.java +++ b/src/java.management/share/classes/javax/management/loading/MLetContent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 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 @@ -102,7 +102,8 @@ public MLetContent(URL url, Map attributes, att += "/"; } try { - baseURL = new URL(documentURL, att); + @SuppressWarnings("deprecation") + var _unused = baseURL = new URL(documentURL, att); } catch (MalformedURLException e) { // OK : Move to next block as baseURL could not be initialized. } @@ -112,7 +113,8 @@ public MLetContent(URL url, Map attributes, int i = file.lastIndexOf('/'); if (i >= 0 && i < file.length() - 1) { try { - baseURL = new URL(documentURL, file.substring(0, i + 1)); + @SuppressWarnings("deprecation") + var _unused = baseURL = new URL(documentURL, file.substring(0, i + 1)); } catch (MalformedURLException e) { // OK : Move to next block as baseURL could not be initialized. } diff --git a/src/java.management/share/classes/javax/management/loading/MLetParser.java b/src/java.management/share/classes/javax/management/loading/MLetParser.java index ef8df7e91baae..0e53b1ddcc603 100644 --- a/src/java.management/share/classes/javax/management/loading/MLetParser.java +++ b/src/java.management/share/classes/javax/management/loading/MLetParser.java @@ -249,6 +249,7 @@ public List parse(URL url) throws IOException { /** * Parse the document pointed by the URL urlname */ + @SuppressWarnings("deprecation") public List parseURL(String urlname) throws IOException { // Parse the document // diff --git a/src/java.naming/share/classes/com/sun/jndi/ldap/VersionHelper.java b/src/java.naming/share/classes/com/sun/jndi/ldap/VersionHelper.java index 7d11ead19643c..1d7565e00e3ad 100644 --- a/src/java.naming/share/classes/com/sun/jndi/ldap/VersionHelper.java +++ b/src/java.naming/share/classes/com/sun/jndi/ldap/VersionHelper.java @@ -126,6 +126,7 @@ private ClassLoader getContextClassLoader() { return AccessController.doPrivileged(act); } + @SuppressWarnings("deprecation") private static URL[] getUrlArray(String[] url) throws MalformedURLException { URL[] urlArray = new URL[url.length]; for (int i = 0; i < urlArray.length; i++) { diff --git a/src/java.naming/share/classes/com/sun/naming/internal/VersionHelper.java b/src/java.naming/share/classes/com/sun/naming/internal/VersionHelper.java index 21f1b19921d81..6dbdb6f420b10 100644 --- a/src/java.naming/share/classes/com/sun/naming/internal/VersionHelper.java +++ b/src/java.naming/share/classes/com/sun/naming/internal/VersionHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 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 @@ -285,7 +285,9 @@ private static URL[] getUrlArray(String codebase) StringTokenizer parser = new StringTokenizer(codebase); List list = new ArrayList<>(); while (parser.hasMoreTokens()) { - list.add(new URL(parser.nextToken())); + @SuppressWarnings("deprecation") + var u = new URL(parser.nextToken()); + list.add(u); } return list.toArray(new URL[0]); } diff --git a/src/java.rmi/share/classes/sun/rmi/server/LoaderHandler.java b/src/java.rmi/share/classes/sun/rmi/server/LoaderHandler.java index 8b3586b40a0fd..7e840a8690612 100644 --- a/src/java.rmi/share/classes/sun/rmi/server/LoaderHandler.java +++ b/src/java.rmi/share/classes/sun/rmi/server/LoaderHandler.java @@ -773,7 +773,9 @@ private static URL[] pathToURLs(String path) StringTokenizer st = new StringTokenizer(path); // divide by spaces URL[] urls = new URL[st.countTokens()]; for (int i = 0; st.hasMoreTokens(); i++) { - urls[i] = new URL(st.nextToken()); + @SuppressWarnings("deprecation") + var url = new URL(st.nextToken()); + urls[i] = url; } synchronized (pathToURLsCache) { pathToURLsCache.put(path, diff --git a/src/java.scripting/share/classes/com/sun/tools/script/shell/Main.java b/src/java.scripting/share/classes/com/sun/tools/script/shell/Main.java index b928aa6e5f94a..55b0daa1a61e1 100644 --- a/src/java.scripting/share/classes/com/sun/tools/script/shell/Main.java +++ b/src/java.scripting/share/classes/com/sun/tools/script/shell/Main.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 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 @@ -540,7 +540,9 @@ private static URL fileToURL(File file) { name = name + "/"; } try { - return new URL("file", "", name); + @SuppressWarnings("deprecation") + var result = new URL("file", "", name); + return result; } catch (MalformedURLException e) { throw new IllegalArgumentException("file"); } diff --git a/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialDatalink.java b/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialDatalink.java index 4deea4c6b5a7f..477dd030e5c9b 100644 --- a/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialDatalink.java +++ b/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialDatalink.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 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 @@ -97,6 +97,7 @@ public SerialDatalink(URL url) throws SerialException { * URL object in the Java programming language. * @throws SerialException if the URL object cannot be de-serialized */ + @SuppressWarnings("deprecation") public URL getDatalink() throws SerialException { URL aURL = null; diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/DocumentCache.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/DocumentCache.java index c6722c98465ce..e50acc829f3b9 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/DocumentCache.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/DocumentCache.java @@ -194,6 +194,7 @@ public DocumentCache(int size, XSLTCDTMManager dtmManager) throws SAXException { */ private final long getLastModified(String uri) { try { + @SuppressWarnings("deprecation") URL url = new URL(uri); URLConnection connection = url.openConnection(); long timestamp = connection.getLastModified(); diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java index 03fef860517cd..a93c76b41f470 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java @@ -1605,7 +1605,8 @@ private String getStylesheetFileName(Source source) else { URL url; try { - url = new URL(systemId); + @SuppressWarnings("deprecation") + URL _unused = url = new URL(systemId); } catch (MalformedURLException e) { return null; diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerImpl.java index a7a122d07aa43..3772ecf789cee 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerImpl.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerImpl.java @@ -521,7 +521,8 @@ else if (result instanceof StreamResult) { } } else if (systemId.startsWith("http:")) { - url = new URL(systemId); + @SuppressWarnings("deprecation") + URL _unused = url = new URL(systemId); final URLConnection connection = url.openConnection(); _tohFactory.setOutputStream(_ostream = connection.getOutputStream()); return _tohFactory.getSerializationHandler(); diff --git a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java index 8a01f74e79032..6cee549dbe276 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -647,6 +647,7 @@ public String setupCurrentEntity(boolean reference, String name, XMLInputSource if (reader == null) { stream = xmlInputSource.getByteStream(); if (stream == null) { + @SuppressWarnings("deprecation") URL location = new URL(expandedSystemId); URLConnection connect = location.openConnection(); if (!(connect instanceof HttpURLConnection)) { @@ -2037,6 +2038,7 @@ else if (gNeedEscaping[b]) { public static OutputStream createOutputStream(String uri) throws IOException { // URI was specified. Handle relative URIs. final String expanded = XMLEntityManager.expandSystemId(uri, null, true); + @SuppressWarnings("deprecation") final URL url = new URL(expanded != null ? expanded : uri); OutputStream out = null; String protocol = url.getProtocol(); diff --git a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/xinclude/XIncludeTextReader.java b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/xinclude/XIncludeTextReader.java index a2b27f771e8ad..8e2f8abcb139f 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/xinclude/XIncludeTextReader.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/xinclude/XIncludeTextReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -125,6 +125,7 @@ protected Reader getReader(XMLInputSource source) throws IOException { else { String expandedSystemId = XMLEntityManager.expandSystemId(source.getSystemId(), source.getBaseSystemId(), false); + @SuppressWarnings("deprecation") URL url = new URL(expandedSystemId); URLConnection urlCon = url.openConnection(); diff --git a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/CharInfo.java b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/CharInfo.java index ba93113c8804c..30249a08e3a72 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/CharInfo.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/CharInfo.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -220,6 +220,7 @@ private CharInfo(String entitiesResource, String method, boolean internal) if (is == null) { try { + @SuppressWarnings("deprecation") URL url = new URL(entitiesResource); is = url.openStream(); } catch (Exception e) { diff --git a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/Encodings.java b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/Encodings.java index 79b4673420211..a88606d5085e8 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/Encodings.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/Encodings.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -335,6 +335,7 @@ private InputStream openEncodingsFileStream() throws MalformedURLException, IOEx } if (urlString != null && urlString.length() > 0) { + @SuppressWarnings("deprecation") URL url = new URL(urlString); is = url.openStream(); } diff --git a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/dom3/LSSerializerImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/dom3/LSSerializerImpl.java index a740550b796e5..6863df3afed47 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/dom3/LSSerializerImpl.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/dom3/LSSerializerImpl.java @@ -956,6 +956,7 @@ public boolean write(Node nodeArg, LSOutput destination) throws LSException { // Expand the System Id and obtain an absolute URI for it. String absoluteURI = SystemIDResolver.getAbsoluteURI(uri); + @SuppressWarnings("deprecation") URL url = new URL(absoluteURI); OutputStream urlOutStream = null; String protocol = url.getProtocol(); @@ -1234,6 +1235,7 @@ public boolean writeToURI(Node nodeArg, String uri) throws LSException { // REVISIT: Can this be used to get an absolute expanded URI String absoluteURI = SystemIDResolver.getAbsoluteURI(uri); + @SuppressWarnings("deprecation") URL url = new URL(absoluteURI); OutputStream urlOutStream = null; String protocol = url.getProtocol(); diff --git a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/ListingErrorHandler.java b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/ListingErrorHandler.java index 21fe4995377f3..75aef58130aa1 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/ListingErrorHandler.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/ListingErrorHandler.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -413,7 +414,7 @@ protected static String getSourceLine(String sourceUrl, int lineNum) try { // Try to get a URL from it as-is - url = new URL(sourceUrl); + url = newURL(sourceUrl); } catch (java.net.MalformedURLException mue) { @@ -431,7 +432,7 @@ protected static String getSourceLine(String sourceUrl, int lineNum) else { // The url is relative, so attempt to get absolute - url = new URL(SystemIDResolver.getAbsoluteURI(sourceUrl)); + url = newURL(SystemIDResolver.getAbsoluteURI(sourceUrl)); // If this fails, allow the exception to propagate } } @@ -562,4 +563,8 @@ public boolean getThrowOnFatalError() /** If we should throw exception on fatalErrors; default:true. */ protected boolean throwOnFatalError = true; + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/src/java.xml/share/classes/javax/xml/catalog/BaseEntry.java b/src/java.xml/share/classes/javax/xml/catalog/BaseEntry.java index 3d5718d661db4..e6d804859cf90 100644 --- a/src/java.xml/share/classes/javax/xml/catalog/BaseEntry.java +++ b/src/java.xml/share/classes/javax/xml/catalog/BaseEntry.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 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 @@ -223,9 +223,11 @@ URL verifyURI(String arg, URL base, String uri) { try { if (base != null) { - url = new URL(base, uri); + @SuppressWarnings("deprecation") + URL _unused = url = new URL(base, uri); } else { - url = new URL(uri); + @SuppressWarnings("deprecation") + URL _unused = url = new URL(uri); } } catch (MalformedURLException e) { CatalogMessages.reportIAE(ERR_INVALID_ARGUMENT, diff --git a/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java b/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java index 6bbbdc77b7dd4..72cd64fca3d03 100644 --- a/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java +++ b/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java @@ -140,7 +140,8 @@ public CatalogImpl(CatalogImpl parent, CatalogFeatures f, URI... uris) throws Ca if (verifyCatalogFile(null, uri)) { systemId = temp; try { - baseURI = new URL(systemId); + @SuppressWarnings("deprecation") + URL _unused = baseURI = new URL(systemId); } catch (MalformedURLException e) { CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH, new Object[]{temp}, e); diff --git a/src/java.xml/share/classes/javax/xml/catalog/CatalogResolverImpl.java b/src/java.xml/share/classes/javax/xml/catalog/CatalogResolverImpl.java index 0977479ca9d02..062148e406a62 100644 --- a/src/java.xml/share/classes/javax/xml/catalog/CatalogResolverImpl.java +++ b/src/java.xml/share/classes/javax/xml/catalog/CatalogResolverImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 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 @@ -154,14 +154,15 @@ public Source resolve(String href, String base) { new Object[]{href, base}); } try { - URL url = null; - if (base == null) { - url = new URL(uri); + @SuppressWarnings("deprecation") + URL url = new URL(uri); result = url.toString(); } else { + @SuppressWarnings("deprecation") URL baseURL = new URL(base); - url = (href.length() == 0 ? baseURL : new URL(baseURL, uri)); + @SuppressWarnings("deprecation") + URL url = (href.length() == 0 ? baseURL : new URL(baseURL, uri)); result = url.toString(); } } catch (java.net.MalformedURLException mue) { @@ -219,7 +220,9 @@ public InputStream resolveEntity(String publicId, String systemId, String baseUr if (is != null && !is.isEmpty()) { try { - return new URL(is.getSystemId()).openStream(); + @SuppressWarnings("deprecation") + InputStream result = new URL(is.getSystemId()).openStream(); + return result; } catch (IOException ex) { //considered as no mapping. } diff --git a/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java b/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java index adf557bf2c726..78962261de12c 100644 --- a/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java +++ b/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java @@ -340,6 +340,7 @@ public static String checkAccess(String systemId, String allowedProtocols, if (!systemId.contains(":")) { protocol = "file"; } else { + @SuppressWarnings("deprecation") URL url = new URL(systemId); protocol = url.getProtocol(); if (protocol.equalsIgnoreCase("jar")) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/FSInfo.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/FSInfo.java index ab0d3d8e3fd32..9a0402aae342b 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/FSInfo.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/FSInfo.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 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 @@ -137,6 +137,7 @@ public List getJarClassPath(Path file) throws IOException { * @throws MalformedURLException */ static URL tryResolveFile(URL base, String input) throws MalformedURLException { + @SuppressWarnings("deprecation") URL retVal = new URL(base, input); if (input.indexOf(':') >= 0 && !"file".equalsIgnoreCase(retVal.getProtocol())) { // 'input' contains a ':', which might be a scheme, or might be diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java index 7727928bdbe12..8d907dba293d2 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -690,7 +690,9 @@ public URL findResource(String name) { } try { - return new URL(PROTOCOL, null, -1, name, handler); + @SuppressWarnings("deprecation") + var result = new URL(PROTOCOL, null, -1, name, handler); + return result; } catch (MalformedURLException e) { return null; } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/ServiceProxy.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/ServiceProxy.java index c932bb2d766ee..20324629ef48a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/ServiceProxy.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/ServiceProxy.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 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 @@ -138,6 +138,7 @@ public static boolean hasService(Class service, URL[] urls) for (URL url: urls) { try { String fullName = prefix + service.getName(); + @SuppressWarnings("deprecation") URL u = new URL(url, fullName); boolean found = parse(service, u); if (found) diff --git a/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java b/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java index a78ee38508806..8aec39aab2fb0 100644 --- a/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java +++ b/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java @@ -2253,7 +2253,8 @@ void loadKeyStore(String keyStoreName, boolean prompt) { keyStoreName = keyStoreName.replace(File.separatorChar, '/'); URL url = null; try { - url = new URL(keyStoreName); + @SuppressWarnings("deprecation") + var _unused = url = new URL(keyStoreName); } catch (java.net.MalformedURLException e) { // try as file url = new File(keyStoreName).toURI().toURL(); diff --git a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlOptions.java b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlOptions.java index ca1602a45e722..e7f0c934c158d 100644 --- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlOptions.java +++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlOptions.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 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 @@ -458,7 +458,8 @@ public boolean process(String opt, List args) { public boolean process(String opt, List args) { docrootParent = args.get(0); try { - new URL(docrootParent); + @SuppressWarnings("deprecation") + var _unused = new URL(docrootParent); } catch (MalformedURLException e) { messages.error("doclet.MalformedURL", docrootParent); return false; diff --git a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java index 4079675307439..4fbfc2dac77a5 100644 --- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java +++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java @@ -578,7 +578,8 @@ private String checkLinkCompatibility(String packageName, String moduleName, Str public boolean isUrl (String urlCandidate) { try { - new URL(urlCandidate); + @SuppressWarnings("deprecation") + var _unused = new URL(urlCandidate); //No exception was thrown, so this must really be a URL. return true; } catch (MalformedURLException e) { @@ -587,6 +588,7 @@ public boolean isUrl (String urlCandidate) { } } + @SuppressWarnings("deprecation") private URL toURL(String url) throws Fault { try { return new URL(url); @@ -632,7 +634,8 @@ private InputStream open(URL url) throws IOException { String loc = http.getHeaderField("Location"); URL target = null; if (loc != null) { - target = new URL(base, loc); + @SuppressWarnings("deprecation") + var _unused = target = new URL(base, loc); } http.disconnect(); if (target == null || redirects >= 5) { diff --git a/src/jdk.jconsole/share/classes/sun/tools/jconsole/JConsole.java b/src/jdk.jconsole/share/classes/sun/tools/jconsole/JConsole.java index 908f1e5056c91..ea9526b2b73d7 100644 --- a/src/jdk.jconsole/share/classes/sun/tools/jconsole/JConsole.java +++ b/src/jdk.jconsole/share/classes/sun/tools/jconsole/JConsole.java @@ -1047,7 +1047,9 @@ private static URL fileToURL(File file) throws MalformedURLException { if (!file.isFile()) { name = name + "/"; } - return new URL("file", "", name); + @SuppressWarnings("deprecation") + var result = new URL("file", "", name); + return result; } diff --git a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java index 14e9f49ff230f..7a7b6239ffca9 100644 --- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java +++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 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 @@ -3077,7 +3077,8 @@ private boolean runFile(String filename, String context) { path = toPathResolvingUserHome(filename); } catch (InvalidPathException ipe) { try { - url = new URL(filename); + @SuppressWarnings("deprecation") + var _unused = url = new URL(filename); if (url.getProtocol().equalsIgnoreCase("file")) { path = Paths.get(url.toURI()); } @@ -3092,7 +3093,8 @@ private boolean runFile(String filename, String context) { } else { if (url == null) { try { - url = new URL(filename); + @SuppressWarnings("deprecation") + var _unused = url = new URL(filename); } catch (MalformedURLException mue) { throw new FileNotFoundException(filename); } diff --git a/src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java b/src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java index 5a266b384cfc2..27bf0b95ebec4 100644 --- a/src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java +++ b/src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 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 @@ -185,9 +185,8 @@ public Enumeration findResources(String name) throws IOException { private URL doFindResource(String name) { if (classFiles.containsKey(name)) { try { - return new URL(null, - new URI("jshell", null, "/" + name, null).toString(), - new ResourceURLStreamHandler(name)); + return URL.of(new URI("jshell", null, "/" + name, null), + new ResourceURLStreamHandler(name)); } catch (MalformedURLException | URISyntaxException ex) { throw new InternalError(ex); } diff --git a/src/jdk.security.auth/share/classes/com/sun/security/auth/module/KeyStoreLoginModule.java b/src/jdk.security.auth/share/classes/com/sun/security/auth/module/KeyStoreLoginModule.java index 19610eeac260e..7e3c597a131ce 100644 --- a/src/jdk.security.auth/share/classes/com/sun/security/auth/module/KeyStoreLoginModule.java +++ b/src/jdk.security.auth/share/classes/com/sun/security/auth/module/KeyStoreLoginModule.java @@ -393,7 +393,7 @@ private void checkStorePass() throws LoginException { } InputStream in = null; try { - in = new URL(keyStorePasswordURL).openStream(); + in = newURL(keyStorePasswordURL).openStream(); keyStorePassword = Password.readPassword(in); } catch (IOException e) { LoginException le = new LoginException @@ -421,7 +421,7 @@ private void checkKeyPass() throws LoginException { } else { InputStream in = null; try { - in = new URL(privateKeyPasswordURL).openStream(); + in = newURL(privateKeyPasswordURL).openStream(); privateKeyPassword = Password.readPassword(in); } catch (IOException e) { LoginException le = new LoginException @@ -599,7 +599,7 @@ private void getKeyStoreInfo() throws LoginException { // if using protected auth path, keyStorePassword will be null keyStore.load(null, keyStorePassword); } else { - in = new URL(keyStoreURL).openStream(); + in = newURL(keyStoreURL).openStream(); keyStore.load(in, keyStorePassword); } } catch (MalformedURLException e) { @@ -911,4 +911,9 @@ private void debugPrint(String message) { System.err.println("Debug KeyStoreLoginModule: " + message); } } + + @SuppressWarnings("deprecation") + private static URL newURL(String spec) throws MalformedURLException { + return new URL(spec); + } } diff --git a/test/jdk/java/net/URL/URIToURLTest.java b/test/jdk/java/net/URL/URIToURLTest.java index 81fc0ee5b0aa3..d70f53505b1c7 100644 --- a/test/jdk/java/net/URL/URIToURLTest.java +++ b/test/jdk/java/net/URL/URIToURLTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 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 @@ -83,6 +83,11 @@ public static void main(String args[]) throws Exception { boolean userInfoCheck = userInfo == null? url.getUserInfo() == null : userInfo.equals(url.getUserInfo()); + if (!userInfoCheck) { + throw new RuntimeException("uri.userInfo is " + userInfo + + " url.userInfo is " + + url.getUserInfo()); + } if (uri.getPort() != url.getPort()) throw new RuntimeException("uri.port is " + uri.getPort() + " url's is " + diff --git a/test/jdk/java/net/URL/URLFromURITest.java b/test/jdk/java/net/URL/URLFromURITest.java new file mode 100644 index 0000000000000..874120f9abfd3 --- /dev/null +++ b/test/jdk/java/net/URL/URLFromURITest.java @@ -0,0 +1,377 @@ +/* + * Copyright (c) 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @bug 8294241 + * @library /test/lib + * @modules java.base/java.net:+open + * @summary Test URL::fromURI(URI, URLStreamHandler) + * @run junit/othervm URLFromURITest + */ + +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodHandles.Lookup; +import java.lang.invoke.VarHandle; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.util.Locale; +import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; +import jdk.test.lib.RandomFactory; + +import org.junit.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.*; + +public class URLFromURITest { + + static final Random RAND = RandomFactory.getRandom(); + + record TestInput(String uri, URLStreamHandler handler) { + static TestInput withNoHandler(String uri) { + return new TestInput(uri, null); + } + TestInput withCustomHandler() { + return new TestInput(uri(), new CustomStreamHandler()); + } + TestInput withUrlPrefix() {return inputWithUrlPrefix(this);} + } + + static URI uriWithUrlPrefix(URI uri) { + return URI.create(stringWithUrlPrefix(uri.toString())); + } + + static String stringWithUrlPrefix(String uriStr) { + if (uriStr.regionMatches(true, 0, "url:", 0, 4)) return uriStr; + return RAND.nextBoolean() ? "url:" + uriStr : "Url:" + uriStr; + } + + static TestInput inputWithUrlPrefix(TestInput input) { + String uriStr = input.uri(); + var handler = input.handler(); + + var urlUriStr = stringWithUrlPrefix(uriStr); + if (uriStr.equals(urlUriStr)) return null; + + var urlURI = URI.create(urlUriStr); + try { + new URL(null, urlURI.toString(), handler); + } catch (Throwable t) { + System.err.println("skipping new URL(null, \"" + urlURI + "\", handler): " + t); + return null; + } + return new TestInput(urlUriStr, handler); + } + + static Stream uris() { + var uris = Stream.of( + "http://jag:cafebabe@java.sun.com:94/b/c/d?q#g", + "http://[1080:0:0:0:8:800:200C:417A]/index.html", + "http://a/b/c/d;p?q", + "mailto:mduerst@ifi.unizh.ch", + "http:comp.infosystems.www.servers.unix", + "http://j%41g:cafeb%41be@java.sun.com:94/%41/b/c/d?q#g", + "jar:file:///x.jar!/", + "jmod:/java.base", + "jmod:///java.base"); + + if (hasFtp()) { + uris = Stream.concat(uris, + Stream.of("ftp://ftp.is.co.za/rfc/rfc1808.txt")); + } + + return uris; + } + + static Stream nonOverridableUris() { + return Stream.of("file:///nohost/%41/", + "file://with.host/%41/", + "file:/x/y/z", + "jrt:/java.base/java/lang/Integer.class", + "jrt:///java.base/java/lang/Integer.class"); + } + static Stream withNoHandler() { + return Stream.concat(uris(), nonOverridableUris()) + .map(TestInput::withNoHandler); + } + + static Stream withCustomHandler() { + var withHandlers = uris() + .map(TestInput::withNoHandler) + .map(TestInput::withCustomHandler); + return Stream.concat(withHandlers, Stream.of( + new TestInput("foo:bar:baz", new CustomStreamHandler()), + new TestInput("jar:file:///x.jar!/", new CustomStreamHandler()), + new TestInput("jar:jar:file///x.jar!/bing", new CustomStreamHandler()), + new TestInput("blah://localhost:80/x/y/z", new CustomStreamHandler()) + )); + } + + static Stream overridingNonOverridable() { + return nonOverridableUris().map(TestInput::withNoHandler) + .map(TestInput::withCustomHandler); + } + + @Test + public void checkExceptions() { + var noscheme = URI.create("http"); + var unknown = URI.create("unknown:///foo/bar"); + var opaque = URI.create("opaque:opaque-path"); + var jrt = URI.create("jrt:/java.base/java.lang.Integer.class"); + var file = URI.create("file:/"); + var unoscheme = uriWithUrlPrefix(noscheme); + var uunknown = uriWithUrlPrefix(unknown); + var uopaque = uriWithUrlPrefix(opaque); + var ujrt = uriWithUrlPrefix(jrt); + var ufile = uriWithUrlPrefix(file); + var handler = new CustomStreamHandler(); + assertThrows(NullPointerException.class, () -> URL.of(null, null)); + assertThrows(NullPointerException.class, () -> URL.of(null, handler)); + assertThrows(IllegalArgumentException.class, () -> URL.of(noscheme, null)); + assertThrows(IllegalArgumentException.class, () -> URL.of(noscheme, handler)); + assertThrows(IllegalArgumentException.class, () -> URL.of(jrt, handler)); + assertThrows(IllegalArgumentException.class, () -> URL.of(file, handler)); + assertThrows(IllegalArgumentException.class, () -> URL.of(ujrt, handler)); + assertThrows(IllegalArgumentException.class, () -> URL.of(ufile, handler)); + assertThrows(MalformedURLException.class, () -> URL.of(unknown, null)); + assertThrows(MalformedURLException.class, () -> URL.of(opaque, null)); + assertThrows(MalformedURLException.class, () -> URL.of(uunknown, null)); + assertThrows(MalformedURLException.class, () -> URL.of(uopaque, null)); + assertThrows(MalformedURLException.class, () -> URL.of(unoscheme, null)); + assertThrows(MalformedURLException.class, () -> URL.of(unoscheme, handler)); + } + + @ParameterizedTest + @MethodSource(value = "withNoHandler") + public void testWithNoHandler(TestInput input) throws Exception { + String uriStr = input.uri(); + URLStreamHandler handler = input.handler(); + System.err.println("testWithNoHandler: " + uriStr); + assertNull(handler, input + ": input handler"); + URI uri = new URI(uriStr); + URL url = URL.of(uri, handler); + checkNoHandler(input, uri, url); + var urlInput = input.withUrlPrefix(); + if (urlInput != null) { + try { + var urlURI = URI.create(input.uri()); + checkNoHandler(urlInput, uri, URL.of(urlURI, null)); + } catch (Throwable x) { + throw new AssertionError("Failed: " + urlInput.uri() + " with: " + x, x); + } + } + } + + private void checkNoHandler(TestInput input, URI uri, URL url) throws Exception { + System.err.println("Testing: " + uri); + checkURL(input, uri, url); + URLStreamHandler urlHandler = URLAccess.getHandler(url); + assertNotNull(urlHandler, input + ": URL.handler"); + assertNull(urlHandler.getClass().getClassLoader(), + input + ": URL.handler class loader"); + } + + @ParameterizedTest + @MethodSource(value = "withCustomHandler") + public void checkCustomHandler(TestInput input) throws Exception { + String uriStr = input.uri(); + URLStreamHandler handler = input.handler(); + System.err.println("testWithCustomHandler: " + input); + assertNotNull(handler, input + ": input handler"); + URI uri = new URI(uriStr); + URL url = URL.of(uri, handler); + checkCustomHandler(input, uri, url, handler); + var urlInput = input.withUrlPrefix(); + if (urlInput != null) { + urlInput = urlInput.withCustomHandler(); + handler = urlInput.handler(); + try { + var urlURI = URI.create(urlInput.uri()); + checkCustomHandler(urlInput, uri, URL.of(urlURI, handler), handler); + } catch (Throwable x) { + throw new AssertionError("Failed with handler: " + urlInput.uri() + " with: " + x, x); + } + } + } + + private void checkCustomHandler(TestInput input, URI uri, URL url, + URLStreamHandler handler) throws Exception { + System.err.println("Testing: " + uri); + checkURL(input, uri, url); + URLStreamHandler urlHandler = URLAccess.getHandler(url); + assertSame(handler, urlHandler, input + ": URL.handler"); + URLConnection c = url.openConnection(); + assertNotNull(c, input + ": opened connection"); + assertEquals(CustomURLConnection.class, c.getClass(), + input + ": connection class"); + assertEquals(CustomStreamHandler.class, urlHandler.getClass(), + input + ": handler class"); + assertEquals(((CustomURLConnection)c).handler, handler, input + ": handler"); + assertEquals(c.getURL(), url, input + ": connection url"); + var customHandler = (CustomStreamHandler)urlHandler; + assertEquals(customHandler.parseURLCalled(), 1, "parseURL calls"); + } + + @ParameterizedTest + @MethodSource(value = "overridingNonOverridable") + public void testOverridingNonOverridable(TestInput input) throws Exception { + String uriStr = input.uri(); + URLStreamHandler handler = input.handler(); + System.err.println("testOverridingNonOverridable: " + input); + assertNotNull(handler, input + ": input handler"); + URI uri = new URI(uriStr); + try { + URL url = URL.of(uri, handler); + throw new AssertionError("Should not be able to specify handler for: " + uriStr); + } catch (IllegalArgumentException x) { + System.err.println("Got expected exception: " + x); + } + } + + private static boolean isFileBased(URI uri) { + String scheme = uri.getScheme(); + boolean isJrt = "jrt".equals(scheme.toLowerCase(Locale.ROOT)); + boolean isJmod = "jmod".equals(scheme.toLowerCase(Locale.ROOT)); + boolean isFile = "file".equals(scheme.toLowerCase(Locale.ROOT)); + return isJmod || isJrt || isFile; + } + + private static void checkURL(TestInput input, URI uri, URL url) throws MalformedURLException { + String scheme = uri.getScheme(); + assertEquals(scheme, url.getProtocol(), input + ": scheme"); + + if (uri.isOpaque()) { + String ssp = uri.getSchemeSpecificPart(); + assertEquals(ssp, url.getPath(), input + ": ssp"); + } else { + String authority = uri.getRawAuthority(); + boolean isHierarchical = uri.toString().startsWith(scheme + "://"); + boolean isFileBased = isFileBased(uri); + + // Network based URLs usually follow URI, but file based + // protocol handlers have a few discrepancies in how they + // treat an absent authority: + // - URI authority is null if there is no authority, always + // - URL authority is null or empty depending on the protocol + // and on whether the URL is hierarchical or not. + if (isFileBased && authority == null) { + // jrt: takes a fastpath - so that jrt:/ is equivalent to jrt:/// + if (scheme.equals("jrt")) { + authority = ""; + } + if (isHierarchical) { + authority = ""; + } + } + assertEquals(authority, url.getAuthority(), input + ": authority"); + + // Network based URLs usually follow URI, but file based + // protocol handlers have a few discrepancies in how they + // treat an absent host: + String host = uri.getHost(); + if (isFileBased && host == null) { + host = ""; + } + + assertEquals(host, url.getHost(), input + ": host"); + if (host != null) { + String userInfo = uri.getRawUserInfo(); + assertEquals(userInfo, url.getUserInfo(), input + ": userInfo"); + assertEquals(uri.getPort(), url.getPort(), input + ": port"); + } + + String path = uri.getRawPath(); + assertEquals(path, url.getPath(), input + ": path"); + + String query = uri.getQuery(); + assertEquals(query, url.getQuery(), input + ": query"); + } + String frag = uri.getRawFragment(); + assertEquals(frag, url.getRef(), input + ": fragment"); + } + + @SuppressWarnings("deprecation") + private static boolean hasFtp() { + try { + return new java.net.URL("ftp://localhost/") != null; + } catch (java.net.MalformedURLException x) { + System.err.println("FTP not supported by this runtime."); + return false; + } + } + + static class CustomURLConnection extends URLConnection { + + public final CustomStreamHandler handler; + CustomURLConnection(CustomStreamHandler handler, URL url) { + super(url); + this.handler = handler; + } + + @Override + public void connect() throws IOException { + + } + } + static class CustomStreamHandler extends URLStreamHandler { + + final AtomicInteger parseURLCalled = new AtomicInteger(); + + @Override + protected void parseURL(URL u, String spec, int start, int limit) { + parseURLCalled.incrementAndGet(); + super.parseURL(u, spec, start, limit); + } + + @Override + protected URLConnection openConnection(URL u) throws IOException { + return new CustomURLConnection(this, u); + } + + public int parseURLCalled() { + return parseURLCalled.get(); + } + } + + static final class URLAccess { + static final VarHandle HANDLER; + static { + try { + Lookup lookup = MethodHandles.privateLookupIn(URL.class, MethodHandles.lookup()); + HANDLER = lookup.findVarHandle(URL.class, "handler", URLStreamHandler.class); + } catch (Exception x) { + throw new ExceptionInInitializerError(x); + } + } + static URLStreamHandler getHandler(URL url) { + return (URLStreamHandler)HANDLER.get(url); + } + } +} diff --git a/test/micro/org/openjdk/bench/java/net/ThreadLocalParseUtil.java b/test/micro/org/openjdk/bench/java/net/ThreadLocalParseUtil.java index 37d03d7f35343..93af26379a7cc 100644 --- a/test/micro/org/openjdk/bench/java/net/ThreadLocalParseUtil.java +++ b/test/micro/org/openjdk/bench/java/net/ThreadLocalParseUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 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 @@ -72,6 +72,7 @@ public String decodeTest() throws Throwable { @Benchmark public URI appendEncodedTest() throws Throwable { + @SuppressWarnings("deprecation") URL url = new URL("https://example.com/xyz/abc/def?query=#30"); return (URI) MH_TO_URI.invokeExact(url); } diff --git a/test/micro/org/openjdk/bench/java/security/ProtectionDomainBench.java b/test/micro/org/openjdk/bench/java/security/ProtectionDomainBench.java index 83765805f77c6..ec85e09634f04 100644 --- a/test/micro/org/openjdk/bench/java/security/ProtectionDomainBench.java +++ b/test/micro/org/openjdk/bench/java/security/ProtectionDomainBench.java @@ -79,6 +79,7 @@ public void setupClasses() throws Exception { cs = new CodeSource[numberOfCodeSources]; for (int i = 0; i < numberOfCodeSources; i++) { + @SuppressWarnings("deprecation") URL u = new URL("file:/tmp/duke" + i); cs[i] = new CodeSource(u, (java.security.cert.Certificate[]) null); }