Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8361610

Avoid wasted work in ImageIcon(Image) for setting description

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8, 11, 17, 21, 24, 25, 26
    • client-libs
    • None

      The ImageIcon(Image) [1] constructor sets the description field to the value of "comment" property if it's a string. The ImageIcon(Image, String) [2] constructor calls ImageIcon(Image). Since the two-parameter constructor explicitly provides the value for the description field, the implicitly set value for description is immediately overwritten, which is wasted work.

      This issue was found in the code review [3] for JDK-8159055:

      “This is wasted work if the app calls ImageIcon(Image, String) because that promptly over-writes whatever was obtained via this code.”

      There's a suggested fix for it [4]:

          public ImageIcon (Image image) {
              String description = null;
              if (image != null) {
                  Object o = image.getProperty("comment", null);
                  if (o instanceof String) {
                      description = (String) o;
                  }
              }
              this(image, description);

      And the ImageIcon(Image, String) constructor will do the actual work of setting the fields and loading the image.

      [1] https://github.com/openjdk/jdk/blob/853319439e7887ddd54f8c4a3d79aa62ec51fd64/src/java.desktop/share/classes/javax/swing/ImageIcon.java#L226-L229
      [2] https://github.com/openjdk/jdk/blob/853319439e7887ddd54f8c4a3d79aa62ec51fd64/src/java.desktop/share/classes/javax/swing/ImageIcon.java#L211-L212
      [3] https://github.com/openjdk/jdk/pull/25767#discussion_r2155610976
      [4] https://github.com/openjdk/jdk/pull/25767#discussion_r2159249820

            psadhukhan Prasanta Sadhukhan
            aivanov Alexey Ivanov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: