Skip to content

Commit 2866dd0

Browse files
ptzieglerHannesWell
authored andcommitted
Create static variant of ImageDescriptor.imageDescriptorFromURI()
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
1 parent e153ca3 commit 2866dd0

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

bundles/org.eclipse.jface/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.jface;singleton:=true
5-
Bundle-Version: 3.35.200.qualifier
5+
Bundle-Version: 3.36.0.qualifier
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin
88
Export-Package: org.eclipse.jface,

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/ImageDescriptor.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2023 IBM Corporation and others.
2+
* Copyright (c) 2000, 2024 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -205,25 +205,47 @@ public static ImageDescriptor createFromURLSupplier(boolean useMissingImage, Sup
205205
}
206206

207207
/**
208-
* Convenient method to create an ImageDescriptor from an URI
208+
* Convenient method to create an ImageDescriptor from an URI.
209209
*
210-
* Delegates to ImageDescriptor createFromURL
210+
* Delegates to {@link ImageDescriptor#createFromURL(URL)}. <em>Important</em>
211+
* This method should only be used when it's guaranteed that the given
212+
* {@link URI} is also a valid {@link URL}, in order to avoid the
213+
* {@link MalformedURLException} thrown by {@link URI#toURL()}.
214+
*
215+
* If the URI is {@code null} or not a valid {@link URL}, then an image from
216+
* {@link #getMissingImageDescriptor()} will be returned.
211217
*
212218
* @param uriIconPath The URI of the image file.
213219
* @return a new image descriptor
214220
*
215-
* @since 3.19
221+
* @since 3.36
216222
*/
217-
public ImageDescriptor imageDescriptorFromURI(URI uriIconPath) {
223+
public static ImageDescriptor createFromURI(URI uriIconPath) {
218224
try {
219-
return ImageDescriptor.createFromURL(new URL(uriIconPath.toString()));
220-
} catch (MalformedURLException | NullPointerException e) {
225+
return ImageDescriptor.createFromURL(uriIconPath != null ? uriIconPath.toURL() : null);
226+
} catch (MalformedURLException e) {
221227
// return the missing image placeholder to indicate
222228
// the incorrect call without interfering with the user flow
223229
return getMissingImageDescriptor();
224230
}
225231
}
226232

233+
/**
234+
* Convenient method to create an ImageDescriptor from an URI
235+
*
236+
* Delegates to ImageDescriptor createFromURL
237+
*
238+
* @param uriIconPath The URI of the image file.
239+
* @return a new image descriptor
240+
*
241+
* @since 3.19
242+
* @deprecated Use {@link #createFromURI(URI)} instead.
243+
*/
244+
@Deprecated(since = "3.36", forRemoval = true)
245+
public ImageDescriptor imageDescriptorFromURI(URI uriIconPath) {
246+
return createFromURI(uriIconPath);
247+
}
248+
227249
@Override
228250
public Object createResource(Device device) throws DeviceResourceException {
229251
Image result = createImage(false, device);

0 commit comments

Comments
 (0)