提交 05d10a00 编写于 作者: S serb

8233573: Toolkit.getScreenInsets(GraphicsConfiguration) may throw ClassCastException

Reviewed-by: prr, jdv
上级 3fb775be
...@@ -102,7 +102,6 @@ import javax.swing.UIManager; ...@@ -102,7 +102,6 @@ import javax.swing.UIManager;
import com.apple.laf.AquaMenuBarUI; import com.apple.laf.AquaMenuBarUI;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.AppContext; import sun.awt.AppContext;
import sun.awt.CGraphicsConfig;
import sun.awt.CGraphicsDevice; import sun.awt.CGraphicsDevice;
import sun.awt.LightweightFrame; import sun.awt.LightweightFrame;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
...@@ -470,7 +469,11 @@ public final class LWCToolkit extends LWToolkit { ...@@ -470,7 +469,11 @@ public final class LWCToolkit extends LWToolkit {
@Override @Override
public Insets getScreenInsets(final GraphicsConfiguration gc) { public Insets getScreenInsets(final GraphicsConfiguration gc) {
return ((CGraphicsConfig) gc).getDevice().getScreenInsets(); GraphicsDevice gd = gc.getDevice();
if (!(gd instanceof CGraphicsDevice)) {
return super.getScreenInsets(gc);
}
return ((CGraphicsDevice)gd).getScreenInsets();
} }
@Override @Override
......
...@@ -854,11 +854,10 @@ public final class XToolkit extends UNIXToolkit implements Runnable { ...@@ -854,11 +854,10 @@ public final class XToolkit extends UNIXToolkit implements Runnable {
* _NET_WORKAREA may start at point x1,y1 and end at point x2,y2. * _NET_WORKAREA may start at point x1,y1 and end at point x2,y2.
*/ */
@Override @Override
public Insets getScreenInsets(GraphicsConfiguration gc) public Insets getScreenInsets(final GraphicsConfiguration gc) {
{ GraphicsDevice gd = gc.getDevice();
XNETProtocol netProto = XWM.getWM().getNETProtocol(); XNETProtocol np = XWM.getWM().getNETProtocol();
if ((netProto == null) || !netProto.active()) if (np == null || !(gd instanceof X11GraphicsDevice) || !np.active()) {
{
return super.getScreenInsets(gc); return super.getScreenInsets(gc);
} }
......
...@@ -607,9 +607,12 @@ public final class WToolkit extends SunToolkit implements Runnable { ...@@ -607,9 +607,12 @@ public final class WToolkit extends SunToolkit implements Runnable {
static ColorModel screenmodel; static ColorModel screenmodel;
@Override @Override
public Insets getScreenInsets(GraphicsConfiguration gc) public Insets getScreenInsets(final GraphicsConfiguration gc) {
{ GraphicsDevice gd = gc.getDevice();
return getScreenInsets(((Win32GraphicsDevice) gc.getDevice()).getScreen()); if (!(gd instanceof Win32GraphicsDevice)) {
return super.getScreenInsets(gc);
}
return getScreenInsets(((Win32GraphicsDevice) gd).getScreen());
} }
@Override @Override
......
/*
* Copyright (c) 2020, 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.
*/
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Insets;
import java.awt.PrintJob;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.im.InputMethodHighlight;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
/**
* @test
* @bug 8233573
* @key headful
* @summary Toolkit.getScreenInsets should work for custom GraphicsConfiguration
*/
public final class GetScreenInsetsCustomGC {
public static void main(final String[] args) {
// Default GraphicsConfiguration
GraphicsConfiguration dc =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
// Custom GraphicsConfiguration
GraphicsConfiguration cd = new _GraphicsConfiguration();
// GraphicsConfiguration of BufferedImage
BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
GraphicsConfiguration bic = g.getDeviceConfiguration();
g.dispose();
// Default toolkit
// mey return empty and non-empty insets but never null
if (Toolkit.getDefaultToolkit().getScreenInsets(dc) == null) {
throw new NullPointerException();
}
// return empty insets only for non-default GC
testEmpty(Toolkit.getDefaultToolkit().getScreenInsets(cd));
testEmpty(Toolkit.getDefaultToolkit().getScreenInsets(bic));
try {
Toolkit.getDefaultToolkit().getScreenInsets(null);
throw new RuntimeException("NullPointerException is not thrown");
} catch (NullPointerException ignored) {
// ok
}
// Custom toolkit
Toolkit tk = new _Toolkit();
// mey return empty and non-empty insets but never null
if (tk.getScreenInsets(dc) == null) {
throw new NullPointerException();
}
// return empty insets only for non-default GC
testEmpty(tk.getScreenInsets(cd));
testEmpty(tk.getScreenInsets(bic));
try {
tk.getScreenInsets(null);
throw new RuntimeException("NullPointerException is not thrown");
} catch (NullPointerException ignored) {
// ok
}
}
private static void testEmpty(final Insets insets) {
if ((insets.left | insets.top | insets.bottom | insets.right) != 0) {
System.err.println("Expected empty insets");
System.err.println("Actual: " + insets);
throw new RuntimeException();
}
}
private static class _GraphicsConfiguration extends GraphicsConfiguration {
@Override
public GraphicsDevice getDevice() {
return null;
}
@Override
public ColorModel getColorModel() {
return null;
}
@Override
public ColorModel getColorModel(int transparency) {
return null;
}
@Override
public AffineTransform getDefaultTransform() {
return null;
}
@Override
public AffineTransform getNormalizingTransform() {
return null;
}
@Override
public Rectangle getBounds() {
return null;
}
}
private static class _Toolkit extends Toolkit {
@Override
public Dimension getScreenSize() throws HeadlessException {
return null;
}
@Override
public int getScreenResolution() throws HeadlessException {
return 0;
}
@Override
public ColorModel getColorModel() throws HeadlessException {
return null;
}
@Override
public String[] getFontList() {
return new String[0];
}
@Override
public FontMetrics getFontMetrics(Font font) {
return null;
}
@Override
public void sync() {
}
@Override
public Image getImage(String filename) {
return null;
}
@Override
public Image getImage(URL url) {
return null;
}
@Override
public Image createImage(String filename) {
return null;
}
@Override
public Image createImage(URL url) {
return null;
}
@Override
public boolean prepareImage(Image image, int width, int height, ImageObserver observer) {
return false;
}
@Override
public int checkImage(Image image, int width, int height, ImageObserver observer) {
return 0;
}
@Override
public Image createImage(ImageProducer producer) {
return null;
}
@Override
public Image createImage(byte[] imagedata, int imageoffset, int imagelength) {
return null;
}
@Override
public PrintJob getPrintJob(Frame frame, String jobtitle, Properties props) {
return null;
}
@Override
public void beep() {
}
@Override
public Clipboard getSystemClipboard() throws HeadlessException {
return null;
}
@Override
protected EventQueue getSystemEventQueueImpl() {
return null;
}
@Override
public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
return false;
}
@Override
public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType modalExclusionType) {
return false;
}
@Override
public Map<TextAttribute, ?> mapInputMethodHighlight(InputMethodHighlight highlight) throws HeadlessException {
return null;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册