mbien commented on code in PR #8114:
URL: https://github.com/apache/netbeans/pull/8114#discussion_r1903668162


##########
platform/openide.util.ui/src/org/openide/util/ImageUtilities.java:
##########
@@ -210,6 +210,59 @@ public static final Image loadImage(String resource, 
boolean localized) {
         return loadImageInternal(resource, localized);
     }
 
+    /**
+     * Load an image from an URL. If the URL uses the {@code nbresloc} 
protocol, it is loaded using
+     * the resource loading mechanism provided by {@link 
#loadImage(java.lang.String)}. An SVG
+     * image may be substituted when available.
+     *
+     * <p>This method is intended for use only when a URL must be used instead 
of a resource path,
+     * e.g. in the implementation of pre-existing NetBeans APIs.
+     *
+     * @param url the URL of the image, possibly with the nbresloc protocol
+     * @return the loaded image, or either null or an uninitialized image if 
the image was not
+     *         available
+     * @since 7.34
+     */
+    public static final Image loadImage(URL url) {
+        Parameters.notNull("icon", url);
+        if (url.getProtocol().equals("nbresloc")) { // NOI18N
+            // Omit the initial slash of the path.
+            return loadImage(url.getPath().substring(1));
+        } else {
+            /* Observed to return an image with size (-1, -1) if URL points to 
a non-existent file
+            (after ensureLoaded(Image) is called). */
+            return Toolkit.getDefaultToolkit().createImage(url);

Review Comment:
   loading UI resources via URL is a bit scary to me tbh. IMO this should 
filter for local resources only (protocol allowlist?), but I am curious what 
others think about this. Also Java  has no security manager anymore - so the 
training wheels are off.
   
   
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/java/awt/Toolkit.html#createImage(java.net.URL)
   
   There are also numerous other problems with java.net.URL which can make 
performance unpredictable when used as keys in sets/maps etc. Having a utility 
like this might encourage more usage out of convenience even when it could be a 
String. ([URL.equals can make DNS 
requests](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/net/URL.html#equals(java.lang.Object))).
   
   (edit: I have a jackpot rule active which warns about URLs as collection 
keys and generally try to avoid using them unless it actually is a web resource 
- catch-all classes often end up being too powerful)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to