Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8233573: Toolkit.getScreenInsets(GraphicsConfiguration) may throw Cla…
…ssCastException

Reviewed-by: prr, jdv
  • Loading branch information
mrserb committed Mar 10, 2020
1 parent bd78bea commit 40f6d697d25293282e3d3ee695ef8cd9a5494799
@@ -102,7 +102,6 @@
import com.apple.laf.AquaMenuBarUI;
import sun.awt.AWTAccessor;
import sun.awt.AppContext;
import sun.awt.CGraphicsConfig;
import sun.awt.CGraphicsDevice;
import sun.awt.LightweightFrame;
import sun.awt.PlatformGraphicsInfo;
@@ -479,7 +478,11 @@ public int getScreenResolution() throws HeadlessException {

@Override
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
@@ -857,11 +857,10 @@ private static Rectangle getWorkArea(long root, int scale)
* _NET_WORKAREA may start at point x1,y1 and end at point x2,y2.
*/
@Override
public Insets getScreenInsets(GraphicsConfiguration gc)
{
XNETProtocol netProto = XWM.getWM().getNETProtocol();
if ((netProto == null) || !netProto.active())
{
public Insets getScreenInsets(final GraphicsConfiguration gc) {
GraphicsDevice gd = gc.getDevice();
XNETProtocol np = XWM.getWM().getNETProtocol();
if (np == null || !(gd instanceof X11GraphicsDevice) || !np.active()) {
return super.getScreenInsets(gc);
}

@@ -663,9 +663,12 @@ public boolean isFrameStateSupported(int state) {
static ColorModel screenmodel;

@Override
public Insets getScreenInsets(GraphicsConfiguration gc)
{
return getScreenInsets(((Win32GraphicsDevice) gc.getDevice()).getScreen());
public Insets getScreenInsets(final GraphicsConfiguration gc) {
GraphicsDevice gd = gc.getDevice();
if (!(gd instanceof Win32GraphicsDevice)) {
return super.getScreenInsets(gc);
}
return getScreenInsets(((Win32GraphicsDevice) gd).getScreen());
}

@Override
@@ -0,0 +1,258 @@
/*
* 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;
}
}
}

0 comments on commit 40f6d69

Please sign in to comment.