Skip to content

Commit 49f09b5

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 816a056 commit 49f09b5

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-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.100.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: 32 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,50 @@ 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) {
224+
if (uriIconPath == null) {
225+
return getMissingImageDescriptor();
226+
}
218227
try {
219-
return ImageDescriptor.createFromURL(new URL(uriIconPath.toString()));
220-
} catch (MalformedURLException | NullPointerException e) {
228+
return ImageDescriptor.createFromURL(uriIconPath.toURL());
229+
} catch (MalformedURLException e) {
221230
// return the missing image placeholder to indicate
222231
// the incorrect call without interfering with the user flow
223232
return getMissingImageDescriptor();
224233
}
225234
}
226235

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

0 commit comments

Comments
 (0)