Skip to content

Replace deprecated File.toURL() #2937

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

elsazac
Copy link
Member

@elsazac elsazac commented Apr 24, 2025

java.io.File.toURL() is deprecated and marked for removal. Replace it with file.toURI().toURL(), where toURL() is called on a java.net.URI instance, which is not deprecated and handles encoding correctly.

Copy link
Contributor

Test Results

0 files   -  1 824  0 suites   - 1 824   0s ⏱️ - 1h 37m 15s
0 tests  -  7 918  0 ✅  -  7 690  0 💤  - 228  0 ❌ ±0 
0 runs   - 23 841  0 ✅  - 23 093  0 💤  - 748  0 ❌ ±0 

Results for commit 05512b9. ± Comparison against base commit 766f285.

Copy link
Member

@HannesWell HannesWell left a comment

Choose a reason for hiding this comment

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

Thank you for working on this.
This contributes to the large effort of eclipse-platform/eclipse.platform#35.

@@ -33,7 +33,7 @@ public String resolve(String uri) {

@Override
public InputStream getInputStream(String uri) throws Exception {
URL url = new java.net.URL((new File("./")).toURL(), uri);
URL url = new java.net.URL((new File("./")).toURI().toURL(), uri);
Copy link
Member

Choose a reason for hiding this comment

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

This could have a few superfluous elements removed:

Suggested change
URL url = new java.net.URL((new File("./")).toURI().toURL(), uri);
URL url = new URL(new File("./").toURI().toURL(), uri);

Additionally I wonder if the context URL is even necessary at all?
The JavaDoc of the called URL(URL context, String spec) constructor says:

If the scheme component is defined in the given spec and does not match the scheme of the context,
then the new URL is created as an absolute URL based on the spec alone.

And since the passed string is definitively an http-URL and therefore has a scheme that is different from the file-scheme, I would say that the context is always discarded anyways.
So it could probably be just:

Suggested change
URL url = new java.net.URL((new File("./")).toURI().toURL(), uri);
URL url = new URL(uri);

@@ -540,13 +540,13 @@ private static URL getPersistenceUrl(URL baseUrl, boolean create) {
}

// make sure the file exists
url = new URL(dir.toURL(), PERS_FILENAME);
url = new URL(dir.toURI().toURL(), PERS_FILENAME);
Copy link
Member

Choose a reason for hiding this comment

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

Looking at the callers, the method getPersistenceUrl() should directly return a File or even better a Path.
In general the URL to File/Path conversion done here and in the caller are all actually not robust and only work by chance, i.e. in case the URL does not contain encoded characters.
Instead you should extract the Path (File would also work, but Path is more powerful) using Path.of(URIUtil.toURI(baseUrl)) and then perform all resolution and existance checks directly on the Path/File API and never convert back to URL or URI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants