6828982: UIDefaults.getUI swallows original exception

Reviewed-by: kaddepalli, prr
This commit is contained in:
Prasanta Sadhukhan 2018-10-11 14:19:36 +05:30
parent aa4584af89
commit 2491c32fcd
2 changed files with 78 additions and 1 deletions

View file

@ -32,6 +32,8 @@ import javax.swing.event.SwingPropertyChangeSupport;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.HashMap; import java.util.HashMap;
@ -808,7 +810,11 @@ public class UIDefaults extends Hashtable<Object,Object>
getUIError("static createUI() method not found in " + uiClass); getUIError("static createUI() method not found in " + uiClass);
} }
catch (Exception e) { 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);
} }
} }

View file

@ -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);
}
}
}