提交 d58597e0 编写于 作者: R rupashka

6824600: OOM occurs when setLookAndFeel() is executed in Windows L&F(XP style)

Reviewed-by: alexp
上级 6471c8ff
/*
* Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
* Copyright 2001-2009 Sun Microsystems, Inc. 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
......@@ -49,7 +49,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
/**
* ReferenceQueue of unreferenced WeakPCLs.
*/
private static ReferenceQueue<DesktopProperty> queue;
private static final ReferenceQueue<DesktopProperty> queue = new ReferenceQueue<DesktopProperty>();
/**
* PropertyChangeListener attached to the Toolkit.
......@@ -58,7 +58,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
/**
* Key used to lookup value from desktop.
*/
private String key;
private final String key;
/**
* Value to return.
*/
......@@ -66,17 +66,8 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
/**
* Fallback value in case we get null from desktop.
*/
private Object fallback;
private final Object fallback;
/**
* Toolkit.
*/
private Toolkit toolkit;
static {
queue = new ReferenceQueue<DesktopProperty>();
}
/**
* Cleans up any lingering state held by unrefeernced
......@@ -138,13 +129,10 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
*
* @param key Key used in looking up desktop value.
* @param fallback Value used if desktop property is null.
* @param toolkit Toolkit used to fetch property from, can be null
* in which default will be used.
*/
public DesktopProperty(String key, Object fallback, Toolkit toolkit) {
public DesktopProperty(String key, Object fallback) {
this.key = key;
this.fallback = fallback;
this.toolkit = toolkit;
// The only sure fire way to clear our references is to create a
// Thread and wait for a reference to be added to the queue.
// Because it is so rare that you will actually change the look
......@@ -175,13 +163,14 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
* Returns the value from the desktop.
*/
protected Object getValueFromDesktop() {
if (this.toolkit == null) {
this.toolkit = Toolkit.getDefaultToolkit();
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (pcl == null) {
pcl = new WeakPCL(this, getKey(), UIManager.getLookAndFeel());
toolkit.addPropertyChangeListener(getKey(), pcl);
}
Object value = toolkit.getDesktopProperty(getKey());
pcl = new WeakPCL(this, toolkit, getKey(), UIManager.getLookAndFeel());
toolkit.addPropertyChangeListener(getKey(), pcl);
return value;
return toolkit.getDesktopProperty(getKey());
}
/**
......@@ -205,12 +194,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
* <code>createValue</code> will ask for the property again.
*/
public void invalidate() {
if (pcl != null) {
toolkit.removePropertyChangeListener(getKey(), pcl);
toolkit = null;
pcl = null;
value = null;
}
value = null;
}
/**
......@@ -271,13 +255,11 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
*/
private static class WeakPCL extends WeakReference<DesktopProperty>
implements PropertyChangeListener {
private Toolkit kit;
private String key;
private LookAndFeel laf;
WeakPCL(DesktopProperty target, Toolkit kit, String key, LookAndFeel laf) {
WeakPCL(DesktopProperty target, String key, LookAndFeel laf) {
super(target, queue);
this.kit = kit;
this.key = key;
this.laf = laf;
}
......@@ -297,7 +279,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
}
void dispose() {
kit.removePropertyChangeListener(key, this);
Toolkit.getDefaultToolkit().removePropertyChangeListener(key, this);
}
}
}
/*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
* Copyright 2001-2009 Sun Microsystems, Inc. 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
......@@ -25,8 +25,6 @@
package javax.swing.plaf.metal;
import java.awt.*;
import java.beans.*;
import javax.swing.*;
/**
* DesktopProperty that only uses font height in configuring font. This
......@@ -60,7 +58,7 @@ class MetalFontDesktopProperty extends com.sun.java.swing.plaf.windows.DesktopPr
* @param type MetalTheme font type.
*/
MetalFontDesktopProperty(int type) {
this(propertyMapping[type], Toolkit.getDefaultToolkit(), type);
this(propertyMapping[type], type);
}
/**
......@@ -72,8 +70,8 @@ class MetalFontDesktopProperty extends com.sun.java.swing.plaf.windows.DesktopPr
* @param type Type of font being used, corresponds to MetalTheme font
* type.
*/
MetalFontDesktopProperty(String key, Toolkit kit, int type) {
super(key, null, kit);
MetalFontDesktopProperty(String key, int type) {
super(key, null);
this.type = type;
}
......
/*
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
* Copyright 1998-2009 Sun Microsystems, Inc. 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
......@@ -1541,10 +1541,8 @@ public class MetalLookAndFeel extends BasicLookAndFeel
table.putDefaults(defaults);
if (isWindows() && useSystemFonts() && theme.isSystemTheme()) {
Toolkit kit = Toolkit.getDefaultToolkit();
Object messageFont = new MetalFontDesktopProperty(
"win.messagebox.font.height", kit, MetalTheme.
CONTROL_TEXT_FONT);
"win.messagebox.font.height", MetalTheme.CONTROL_TEXT_FONT);
defaults = new Object[] {
"OptionPane.messageFont", messageFont,
......
/*
* Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 6824600
@summary OOM occurs when setLookAndFeel() is executed in Windows L&F(XP style)
@author Pavel Porvatov
@run main Test6824600
*/
import com.sun.java.swing.plaf.windows.DesktopProperty;
import java.awt.*;
public class Test6824600 {
public static void main(String[] args) throws Exception {
Toolkit toolkit = Toolkit.getDefaultToolkit();
HackedDesktopProperty desktopProperty = new HackedDesktopProperty("Button.background", null);
// Register listener in toolkit
desktopProperty.getValueFromDesktop();
int length = toolkit.getPropertyChangeListeners().length;
// Make several invocations
desktopProperty.getValueFromDesktop();
desktopProperty.getValueFromDesktop();
desktopProperty.invalidate();
desktopProperty.getValueFromDesktop();
desktopProperty.getValueFromDesktop();
if (length != toolkit.getPropertyChangeListeners().length) {
throw new RuntimeException("New listeners were added into Toolkit");
}
}
public static class HackedDesktopProperty extends DesktopProperty {
public HackedDesktopProperty(String key, Object fallback) {
super(key, fallback);
}
// Publish the method
public Object getValueFromDesktop() {
return super.getValueFromDesktop();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册