From 9235e3996fa24fbfd5996a0ca51959a0b13c2a5c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 15 Nov 2022 00:33:48 +0100 Subject: [PATCH] Equivalent code without java.net.URL constructor in comment blocks See gh-29486 See gh-29481 --- .../org/springframework/core/io/UrlResource.java | 15 +++++++++++++++ .../org/springframework/util/ResourceUtils.java | 10 ++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/UrlResource.java b/spring-core/src/main/java/org/springframework/core/io/UrlResource.java index 8c78fb2382be..f5973a5ba231 100644 --- a/spring-core/src/main/java/org/springframework/core/io/UrlResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/UrlResource.java @@ -97,6 +97,21 @@ public UrlResource(URI uri) throws MalformedURLException { */ public UrlResource(String path) throws MalformedURLException { Assert.notNull(path, "Path must not be null"); + + // Equivalent without java.net.URL constructor - for building on JDK 20+ + /* + try { + this.uri = ResourceUtils.toURI(StringUtils.cleanPath(path)); + this.url = this.uri.toURL(); + this.cleanedUrl = StringUtils.cleanPath(path); + } + catch (URISyntaxException | IllegalArgumentException ex) { + MalformedURLException exToThrow = new MalformedURLException(ex.getMessage()); + exToThrow.initCause(ex); + throw exToThrow; + } + */ + this.uri = null; this.url = ResourceUtils.toURL(path); this.cleanedUrl = StringUtils.cleanPath(path); diff --git a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java index 2d7a77879af9..110c9342105f 100644 --- a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java @@ -390,13 +390,12 @@ public static URI toURI(String location) throws URISyntaxException { * @since 6.0 */ public static URL toURL(String location) throws MalformedURLException { - // Not fully equivalent - but to be moved in the given direction - // since JDK 20 deprecates all direct java.net.URL constructors. + // Equivalent without java.net.URL constructor - for building on JDK 20+ /* try { - return toURI(location).toURL(); + return toURI(StringUtils.cleanPath(location)).toURL(); } - catch (URISyntaxException ex) { + catch (URISyntaxException | IllegalArgumentException ex) { MalformedURLException exToThrow = new MalformedURLException(ex.getMessage()); exToThrow.initCause(ex); throw exToThrow; @@ -419,8 +418,7 @@ public static URL toRelativeURL(URL root, String relativePath) throws MalformedU // # can appear in filenames, java.net.URL should not treat it as a fragment relativePath = StringUtils.replace(relativePath, "#", "%23"); - // Not fully equivalent - but to be moved in the given direction - // since JDK 20 deprecates all direct java.net.URL constructors. + // Equivalent without java.net.URL constructor - for building on JDK 20+ /* return toURL(StringUtils.applyRelativePath(root.toString(), relativePath)); */