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

UIDefaults.getUI swallows original exception

XMLWordPrintable

    • b17
    • x86
    • linux

      ---%<---
      import javax.swing.JComponent;
      import javax.swing.JLabel;
      import javax.swing.UIDefaults;
      import javax.swing.UIManager;
      import javax.swing.plaf.ComponentUI;
      import javax.swing.plaf.basic.BasicLabelUI;
      public class TestUIDefaultsErrorHandling {
          public static void main(String[] args) {
              UIDefaults defaults = UIManager.getDefaults();
              defaults.put("BrokenUI", BrokenUI.class.getName());
              System.err.println(defaults.getUI(new JLabel() {
                  public @Override String getUIClassID() {
                      return "BrokenUI";
                  }
              }));
          }
          public static class BrokenUI extends BasicLabelUI {
              public static ComponentUI createUI(JComponent target) {
                  return new BrokenUI();
              }
              private BrokenUI() {
                  throw new RuntimeException("oops, complex problem with diagnostics");
              }
          }
      }
      ---%<---

      produces

      ---%<---
      java version "1.6.0_11"
      Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
      Java HotSpot(TM) Server VM (build 11.0-b16, mixed mode)

      UIDefaults.getUI() failed: createUI() failed for TestUIDefaultsErrorHandling$1[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER] java.lang.reflect.InvocationTargetException
      java.lang.Error
              at javax.swing.UIDefaults.getUIError(UIDefaults.java:711)
              at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:133)
              at javax.swing.UIDefaults.getUI(UIDefaults.java:757)
              at javax.swing.UIManager.getUI(UIManager.java:1016)
              at javax.swing.JLabel.updateUI(JLabel.java:256)
              at javax.swing.JLabel.<init>(JLabel.java:145)
              at javax.swing.JLabel.<init>(JLabel.java:216)
              at TestUIDefaultsErrorHandling$1.<init>(TestUIDefaultsErrorHandling.java:11)
              at TestUIDefaultsErrorHandling.main(TestUIDefaultsErrorHandling.java:11)
      UIDefaults.getUI() failed: createUI() failed for TestUIDefaultsErrorHandling$1[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER] java.lang.reflect.InvocationTargetException
      java.lang.Error
              at javax.swing.UIDefaults.getUIError(UIDefaults.java:711)
              at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:133)
              at javax.swing.UIDefaults.getUI(UIDefaults.java:757)
              at TestUIDefaultsErrorHandling.main(TestUIDefaultsErrorHandling.java:11)
      null
      ---%<---

      Nowhere is the original exception's message or stack trace even mentioned; there is only a meaningless stack trace from Swing code.

      In practice this means that if any runtime exception is thrown constructing a ComponentUI, if you want to see the details you either need to run in the debugger and set a breakpoint in UIDefaults, or patch UIDefaults and add to -Xbootclasspath/p. Neither is a good option for diagnosing a problem that arises unreproducibly in the field.

      Encountered when trying to diagnose a quite subtle problem in JavaHelp. The original exception - when I found it after a couple hours of work - pointed me quickly to the root problem. The original application log output was useless.

            psadhukhan Prasanta Sadhukhan
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: