Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid use of java.net.URL constructors #29481

Closed
sbrannen opened this issue Nov 14, 2022 · 3 comments
Closed

Avoid use of java.net.URL constructors #29481

sbrannen opened this issue Nov 14, 2022 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Nov 14, 2022

Overview

Since all URL constructors are deprecated starting with Java 20, we should stop using them in order to allow Spring Framework to be built using Java 20.

In addition, the rationale for deprecating those constructors (which is more important than the actual deprecation) is applicable today.

For full details, see the Constructing instances of URL section of the URL Javadoc. The API Note also provides valuable advice regarding correct usage of File, Path, URI, and URL.

Additional Resources

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Nov 14, 2022
@sbrannen sbrannen modified the milestones: 6.0.x, 6.0.0 Nov 14, 2022
@jhoeller jhoeller modified the milestones: 6.0.0, 6.1.x Nov 14, 2022
@jhoeller
Copy link
Contributor

jhoeller commented Nov 14, 2022

This turns out to be rather involved due to the differences between URI and URL rules and the restricted behavior of the java.net.URI#toURL() method. As a first step, #29486 prepares the codebase for alternative URL construction arrangements in 6.0; a full replacement/reimplementation of URL construction rules from user-supplied Strings is likely to come in 6.1 then.

@jhoeller
Copy link
Contributor

jhoeller commented Nov 14, 2022

With a combination of URI construction from a cleaned path and subsequent defensive URL conversion, all our tests pass. This is still rather involved but seems to be equivalent for our common purposes now.

Corresponding code is available in UrlResource(String), ResourceUtils.toURL(String) and ResourceUtils.toRelativeURL in comment blocks now, for an easy switch in 6.1 (or whenever we find it appropriate).

@sbrannen
Copy link
Member Author

Update: added a section with additional resources regarding changes in behavior in URL constructors beginning with Java 20.

@jhoeller jhoeller modified the milestones: 6.1.x, 6.1.0-M1 Jan 10, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
Explicit path computation also leads to consistent relative path semantics for resource URLs.

Closes spring-projectsgh-29481
Closes spring-projectsgh-28522
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants