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

Create static variant of ImageDescriptor.imageDescriptorFromURI() #2416

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

Conversation

ptziegler
Copy link
Contributor

This method is intended to be used as a wrapper for createFromURL() to avoid having to deal with the checked MalformedURLException. However, this method is effectively unusable, as it requires the user to already have an instance of the ImageDescriptor they want to created.

Instead, create a new, static createFromURI() method, mark the old method as deprecated and internally delegate to the new method.

The old method was created as a response to Bug 559656 [1], in order to match the signature of IResourceUtilities. But because the latter is accessed via an OSGi service, it doesn't need to be static. Which is something that doesn't really work for image descriptors.

[1] https://bugs.eclipse.org/bugs/show_bug.cgi?id=559656

@ptziegler
Copy link
Contributor Author

@vogella You contributed the original method. wdyt?

Copy link
Contributor

github-actions bot commented Oct 17, 2024

Test Results

 1 821 files  ±0   1 821 suites  ±0   1h 53m 16s ⏱️ + 7m 51s
 7 724 tests ±0   7 496 ✅ +1  228 💤 ±0  0 ❌  - 1 
24 333 runs  ±0  23 586 ✅ +1  747 💤 ±0  0 ❌  - 1 

Results for commit 7307874. ± Comparison against base commit 136afb4.

♻️ This comment has been updated with latest results.

@vogella
Copy link
Contributor

vogella commented Oct 18, 2024

LGTM, thanks @ptziegler

*/
public ImageDescriptor imageDescriptorFromURI(URI uriIconPath) {
public static ImageDescriptor createFromURI(URI uriIconPath) {
try {
return ImageDescriptor.createFromURL(new URL(uriIconPath.toString()));
} catch (MalformedURLException | NullPointerException e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As we are currently changing the method, catching NPE is really really BAD!
We should check for null like in createFromURL

Suggested change
} catch (MalformedURLException | NullPointerException e) {
} catch (MalformedURLException e) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In e.g. createFromURL(), we have an explicit null check where getMissingImageDescriptor() is returned. I think it makes more sense to do the same here, rather than forwarding the exception.

@ptziegler
Copy link
Contributor Author

I've also noticed that new URL(String) will be deprecated in Java 20, so I switched to the equivalent URI.toURL().

@merks
Copy link
Contributor

merks commented Oct 18, 2024

Be very careful. It looks so innocent and obvious, bu there are cases where URI creation will thrown an exception where URL creation does not. E.g., if there is a space in the string

@vogella
Copy link
Contributor

vogella commented Nov 5, 2024

@laeubi do you still have open concerns here?

Copy link
Contributor

@laeubi laeubi left a comment

Choose a reason for hiding this comment

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

Looks fine for me, using this method somewhere in the code (even in a testcase) might help to demonstrate its usefulness.

Maybe we should mention in the javadoc that this only works for URIs that can be converted to URLs?

This method is intended to be used as a wrapper for createFromURL() to
avoid having to deal with the checked MalformedURLException. However,
this method is effectively unusable, as it requires the user to already
have an instance of the ImageDescriptor they want to created.

Instead, create a new, static createFromURI() method, mark the old
method as deprecated and internally delegate to the new method.

The old method was created as a response to Bug 559656 [1], in order to
match the signature of IResourceUtilities. But because the latter is
accessed via an OSGi service, it doesn't need to be static. Which is
something that doesn't really work for image descriptors.

[1] https://bugs.eclipse.org/bugs/show_bug.cgi?id=559656
@ptziegler
Copy link
Contributor Author

Maybe we should mention in the javadoc that this only works for URIs that can be converted to URLs?

Adding a disclaimer sounds like a good idea.

@HannesWell
Copy link
Member

Be very careful. It looks so innocent and obvious, bu there are cases where URI creation will thrown an exception where URL creation does not. E.g., if there is a space in the string

That's right.
But here an URI is converted into a URL and as far as I know URIs are strict regarding their content and therefore the conversion URI -> URL should be (relatively) save? But maybe I'm wrong. It's definitively not true for the opposite: URL -> URL can have many problems since URLs are not strictly validating their content.

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.

5 participants