From 5ee6314268b719f5ee6dabf2b880a6ddc1f4fc82 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Thu, 17 Oct 2024 23:16:55 +0200 Subject: [PATCH] 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 --- .../org.eclipse.jface/META-INF/MANIFEST.MF | 2 +- .../jface/resource/ImageDescriptor.java | 36 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF index 47bd323cd19..c0a6a166d28 100644 --- a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.jface;singleton:=true -Bundle-Version: 3.35.200.qualifier +Bundle-Version: 3.36.0.qualifier Bundle-Vendor: %providerName Bundle-Localization: plugin Export-Package: org.eclipse.jface, diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/ImageDescriptor.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/ImageDescriptor.java index 1a529975466..23071678e1f 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/ImageDescriptor.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/ImageDescriptor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2023 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -205,25 +205,47 @@ public static ImageDescriptor createFromURLSupplier(boolean useMissingImage, Sup } /** - * Convenient method to create an ImageDescriptor from an URI + * Convenient method to create an ImageDescriptor from an URI. * - * Delegates to ImageDescriptor createFromURL + * Delegates to {@link ImageDescriptor#createFromURL(URL)}. Important + * This method should only be used when it's guaranteed that the given + * {@link URI} is also a valid {@link URL}, in order to avoid the + * {@link MalformedURLException} thrown by {@link URI#toURL()}. + * + * If the URI is {@code null} or not a valid {@link URL}, then an image from + * {@link #getMissingImageDescriptor()} will be returned. * * @param uriIconPath The URI of the image file. * @return a new image descriptor * - * @since 3.19 + * @since 3.36 */ - public ImageDescriptor imageDescriptorFromURI(URI uriIconPath) { + public static ImageDescriptor createFromURI(URI uriIconPath) { try { - return ImageDescriptor.createFromURL(new URL(uriIconPath.toString())); - } catch (MalformedURLException | NullPointerException e) { + return ImageDescriptor.createFromURL(uriIconPath != null ? uriIconPath.toURL() : null); + } catch (MalformedURLException e) { // return the missing image placeholder to indicate // the incorrect call without interfering with the user flow return getMissingImageDescriptor(); } } + /** + * Convenient method to create an ImageDescriptor from an URI + * + * Delegates to ImageDescriptor createFromURL + * + * @param uriIconPath The URI of the image file. + * @return a new image descriptor + * + * @since 3.19 + * @deprecated Use {@link #createFromURI(URI)} instead. + */ + @Deprecated(since = "3.36", forRemoval = true) + public ImageDescriptor imageDescriptorFromURI(URI uriIconPath) { + return createFromURI(uriIconPath); + } + @Override public Object createResource(Device device) throws DeviceResourceException { Image result = createImage(false, device);