6828982: UIDefaults.getUI swallows original exception
authorpsadhukhan
Thu, 11 Oct 2018 14:19:36 +0530
changeset 52254 ac510fd737eb
parent 52253 c2d3b6d84115
child 52255 c7afaa79cbb3
6828982: UIDefaults.getUI swallows original exception Reviewed-by: kaddepalli, prr
src/java.desktop/share/classes/javax/swing/UIDefaults.java
test/jdk/javax/swing/UIDefaults/TestUIDefaultsErrorHandling.java
--- a/src/java.desktop/share/classes/javax/swing/UIDefaults.java	Wed Oct 10 16:20:52 2018 +0530
+++ b/src/java.desktop/share/classes/javax/swing/UIDefaults.java	Thu Oct 11 14:19:36 2018 +0530
@@ -32,6 +32,8 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.io.UncheckedIOException;
 import java.lang.reflect.*;
 import java.util.HashMap;
@@ -808,7 +810,11 @@
                 getUIError("static createUI() method not found in " + uiClass);
             }
             catch (Exception e) {
-                getUIError("createUI() failed for " + target + " " + e);
+                StringWriter w = new StringWriter();
+                PrintWriter pw = new PrintWriter(w);
+                e.printStackTrace(pw);
+                pw.flush();
+                getUIError("createUI() failed for " + target + "\n" + w);
             }
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/UIDefaults/TestUIDefaultsErrorHandling.java	Thu Oct 11 14:19:36 2018 +0530
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+   @bug 6828982
+   @summary Verifies UIDefaults.getUI retains original exception
+   @run main TestUIDefaultsErrorHandling
+ */
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+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 {
+
+    private final static String erroutput = "oops, complex problem with diagnostics";
+
+    public static void main(String[] args) {
+        final PrintStream err = System.err;
+        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+        System.setErr(new PrintStream(bytes));
+
+        try {
+            UIDefaults defaults = UIManager.getDefaults();
+            defaults.put("BrokenUI", BrokenUI.class.getName());
+            defaults.getUI(new JLabel() {
+                public @Override String getUIClassID() {
+                    return "BrokenUI";
+                }
+            });
+            if (!(bytes.toString().contains(erroutput))) {
+                throw new RuntimeException("UIDefauls swallows exception trace");
+            }
+        } finally {
+            System.setErr(err);
+        }
+    }
+    public static class BrokenUI extends BasicLabelUI {
+        public static ComponentUI createUI(JComponent target) {
+            return new BrokenUI();
+        }
+        private BrokenUI() {
+            throw new RuntimeException(erroutput);
+        }
+    }
+}