提交 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -49,7 +49,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -49,7 +49,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
/** /**
* ReferenceQueue of unreferenced WeakPCLs. * ReferenceQueue of unreferenced WeakPCLs.
*/ */
private static ReferenceQueue<DesktopProperty> queue; private static final ReferenceQueue<DesktopProperty> queue = new ReferenceQueue<DesktopProperty>();
/** /**
* PropertyChangeListener attached to the Toolkit. * PropertyChangeListener attached to the Toolkit.
...@@ -58,7 +58,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -58,7 +58,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
/** /**
* Key used to lookup value from desktop. * Key used to lookup value from desktop.
*/ */
private String key; private final String key;
/** /**
* Value to return. * Value to return.
*/ */
...@@ -66,17 +66,8 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -66,17 +66,8 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
/** /**
* Fallback value in case we get null from desktop. * 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 * Cleans up any lingering state held by unrefeernced
...@@ -138,13 +129,10 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -138,13 +129,10 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
* *
* @param key Key used in looking up desktop value. * @param key Key used in looking up desktop value.
* @param fallback Value used if desktop property is null. * @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.key = key;
this.fallback = fallback; this.fallback = fallback;
this.toolkit = toolkit;
// The only sure fire way to clear our references is to create a // 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. // Thread and wait for a reference to be added to the queue.
// Because it is so rare that you will actually change the look // Because it is so rare that you will actually change the look
...@@ -175,13 +163,14 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -175,13 +163,14 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
* Returns the value from the desktop. * Returns the value from the desktop.
*/ */
protected Object getValueFromDesktop() { protected Object getValueFromDesktop() {
if (this.toolkit == null) { Toolkit toolkit = Toolkit.getDefaultToolkit();
this.toolkit = Toolkit.getDefaultToolkit();
} if (pcl == null) {
Object value = toolkit.getDesktopProperty(getKey()); pcl = new WeakPCL(this, getKey(), UIManager.getLookAndFeel());
pcl = new WeakPCL(this, toolkit, getKey(), UIManager.getLookAndFeel());
toolkit.addPropertyChangeListener(getKey(), pcl); toolkit.addPropertyChangeListener(getKey(), pcl);
return value; }
return toolkit.getDesktopProperty(getKey());
} }
/** /**
...@@ -205,13 +194,8 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -205,13 +194,8 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
* <code>createValue</code> will ask for the property again. * <code>createValue</code> will ask for the property again.
*/ */
public void invalidate() { public void invalidate() {
if (pcl != null) {
toolkit.removePropertyChangeListener(getKey(), pcl);
toolkit = null;
pcl = null;
value = null; value = null;
} }
}
/** /**
* Requests that all components in the GUI hierarchy be updated * Requests that all components in the GUI hierarchy be updated
...@@ -271,13 +255,11 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -271,13 +255,11 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
*/ */
private static class WeakPCL extends WeakReference<DesktopProperty> private static class WeakPCL extends WeakReference<DesktopProperty>
implements PropertyChangeListener { implements PropertyChangeListener {
private Toolkit kit;
private String key; private String key;
private LookAndFeel laf; private LookAndFeel laf;
WeakPCL(DesktopProperty target, Toolkit kit, String key, LookAndFeel laf) { WeakPCL(DesktopProperty target, String key, LookAndFeel laf) {
super(target, queue); super(target, queue);
this.kit = kit;
this.key = key; this.key = key;
this.laf = laf; this.laf = laf;
} }
...@@ -297,7 +279,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue { ...@@ -297,7 +279,7 @@ public class DesktopProperty implements UIDefaults.ActiveValue {
} }
void dispose() { 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,8 +25,6 @@ ...@@ -25,8 +25,6 @@
package javax.swing.plaf.metal; package javax.swing.plaf.metal;
import java.awt.*; import java.awt.*;
import java.beans.*;
import javax.swing.*;
/** /**
* DesktopProperty that only uses font height in configuring font. This * DesktopProperty that only uses font height in configuring font. This
...@@ -60,7 +58,7 @@ class MetalFontDesktopProperty extends com.sun.java.swing.plaf.windows.DesktopPr ...@@ -60,7 +58,7 @@ class MetalFontDesktopProperty extends com.sun.java.swing.plaf.windows.DesktopPr
* @param type MetalTheme font type. * @param type MetalTheme font type.
*/ */
MetalFontDesktopProperty(int 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 ...@@ -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 * @param type Type of font being used, corresponds to MetalTheme font
* type. * type.
*/ */
MetalFontDesktopProperty(String key, Toolkit kit, int type) { MetalFontDesktopProperty(String key, int type) {
super(key, null, kit); super(key, null);
this.type = type; 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -1541,10 +1541,8 @@ public class MetalLookAndFeel extends BasicLookAndFeel ...@@ -1541,10 +1541,8 @@ public class MetalLookAndFeel extends BasicLookAndFeel
table.putDefaults(defaults); table.putDefaults(defaults);
if (isWindows() && useSystemFonts() && theme.isSystemTheme()) { if (isWindows() && useSystemFonts() && theme.isSystemTheme()) {
Toolkit kit = Toolkit.getDefaultToolkit();
Object messageFont = new MetalFontDesktopProperty( Object messageFont = new MetalFontDesktopProperty(
"win.messagebox.font.height", kit, MetalTheme. "win.messagebox.font.height", MetalTheme.CONTROL_TEXT_FONT);
CONTROL_TEXT_FONT);
defaults = new Object[] { defaults = new Object[] {
"OptionPane.messageFont", messageFont, "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.
先完成此消息的编辑!
想要评论请 注册