-
Notifications
You must be signed in to change notification settings - Fork 209
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
base: master
Are you sure you want to change the base?
Conversation
38b504b
to
05512b9
Compare
There was a problem hiding this 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); |
There was a problem hiding this comment.
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:
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:
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); |
There was a problem hiding this comment.
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.
java.io.File.toURL()
is deprecated and marked for removal. Replace it withfile.toURI().toURL()
, wheretoURL()
is called on ajava.net.URI
instance, which is not deprecated and handles encoding correctly.