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 2 commits 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

github-actions bot commented Apr 24, 2025

Test Results

 1 824 files  ±0   1 824 suites  ±0   1h 39m 22s ⏱️ +2s
 7 918 tests ±0   7 690 ✅ ±0  228 💤 ±0  0 ❌ ±0 
23 841 runs  ±0  23 093 ✅ ±0  748 💤 ±0  0 ❌ ±0 

Results for commit 20d1219. ± Comparison against base commit 70589ac.

♻️ This comment has been updated with latest results.

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.

@@ -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.

Copy link
Member Author

Choose a reason for hiding this comment

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

@HannesWell

Is this the implementation you were referring to?

try {
			// make sure the directory exists
			URL url = new URL(baseUrl, PERS_FOLDER);
			File dir = new File(url.getFile());
			if (!dir.exists() && (!create || !dir.mkdir())) {
				return null;
			}

			// make sure the file exists
			Path p = Path.of(URIUtil.toURI(baseUrl.getPath() + File.pathSeparator + PERS_FILENAME));
			if (!Files.exists(p) && (!create || !p.toFile().createNewFile())) {
				return null;
			}

			return p.toUri().toURL();
		} catch (IOException e) {
			// cannot log because instance area has not been set
			return null;

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's the caller I was referring to.

@eclipse-platform-bot
Copy link
Contributor

eclipse-platform-bot commented Apr 27, 2025

This pull request changes some projects for the first time in this development cycle.
Therefore the following files need a version increment:

bundles/org.eclipse.e4.ui.css.core/META-INF/MANIFEST.MF

An additional commit containing all the necessary changes was pushed to the top of this PR's branch. To obtain these changes (for example if you want to push more changes) either fetch from your fork or apply the git patch.

Git patch
From ff02de510f6048124e4233631559d25af42a4a52 Mon Sep 17 00:00:00 2001
From: Eclipse Platform Bot <[email protected]>
Date: Wed, 30 Apr 2025 07:05:56 +0000
Subject: [PATCH] Version bump(s) for 4.36 stream


diff --git a/bundles/org.eclipse.e4.ui.css.core/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.ui.css.core/META-INF/MANIFEST.MF
index 164a33caaa..459c82913a 100644
--- a/bundles/org.eclipse.e4.ui.css.core/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.e4.ui.css.core/META-INF/MANIFEST.MF
@@ -4,7 +4,7 @@ Bundle-SymbolicName: org.eclipse.e4.ui.css.core;singleton:=true
 Bundle-Name: %pluginName
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
-Bundle-Version: 0.14.500.qualifier
+Bundle-Version: 0.14.600.qualifier
 Export-Package: org.eclipse.e4.ui.css.core;x-internal:=true,
  org.eclipse.e4.ui.css.core.css2;x-friends:="org.eclipse.e4.ui.css.swt.theme,org.eclipse.e4.ui.css.swt,org.eclipse.e4.ui.css.jface",
  org.eclipse.e4.ui.css.core.dom;x-friends:="org.eclipse.e4.ui.css.swt,org.eclipse.ui.views.properties.tabbed,org.eclipse.ui.forms",
-- 
2.49.0

Further information are available in Common Build Issues - Missing version increments.

@elsazac elsazac force-pushed the toURL_PlatformUI branch 3 times, most recently from a0cfbc8 to fa3c80c Compare April 30, 2025 07:01
@HannesWell
Copy link
Member

Thank you @elsazac for the update.
Unfortunately I misunderstood your previous comment. I meant that the method getPersistenceUrl() should just return a Path.
To do this the Path representation of the baseUrl should be first extracted by using the method I'm introducing with

but sadly that will probably not land anymore in this release cycle. But I plan to have this ready early next cycle and I suggest to put this on hold for that time. I'll keep you posted once that is ready, but also invite you to monitor that PR.
That being said, once you got the Path of the baseUrl, then all checks and concatenations can just be done on the Path object and the same applies for the caller.

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.

3 participants