JDK-8233573 : Toolkit.getScreenInsets(GraphicsConfiguration) may throw ClassCastException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 8,11,14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: x86_64
  • Submitted: 2019-10-22
  • Updated: 2020-06-09
  • Resolved: 2020-03-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 15
11.0.8-oracleFixed 13.0.4Fixed 15 b16Fixed
Description
ADDITIONAL SYSTEM INFORMATION :
Java 11.0.4

A DESCRIPTION OF THE PROBLEM :
We have crash reports from a user indicating that Toolkit.getScreenInsets(GraphicsConfiguration) can throw ClassCastException on Windows in some cases. The code is running in an IntelliJ IDEA plugin. I see in the source for WToolkit.getScreenInsets(GraphicsConfiguration) that the result of GraphicsContext.getDevice() is cast to Win32GraphicsDevice without any type validation.

We have not seen this bug in our Java application, which uses the same code and has a few hundred thousand users, but have seen in in the IntelliJ plugin, which has a few hundred, so most likely IntelliJ is implementing a buffered image paint for icons.

Relevant parts of the stack dump:
java.lang.ClassCastException: class sun.awt.image.BufferedImageDevice cannot be cast to class sun.awt.Win32GraphicsDevice (sun.awt.image.BufferedImageDevice and sun.awt.Win32GraphicsDevice are in module java.desktop of loader 'bootstrap')
	at java.desktop/sun.awt.windows.WToolkit.getScreenInsets(WToolkit.java:612)
	at hu.c(hu.java:602)
...
	at org.jgrasp.ijplugin.IJIcon.paintIcon(IJIcon.java:1)
	at com.intellij.openapi.actionSystem.ex.ActionButtonLook.paintIconAt(ActionButtonLook.java:141)
	at com.intellij.openapi.actionSystem.ex.ActionButtonLook.paintIcon(ActionButtonLook.java:137)
	at com.intellij.openapi.actionSystem.impl.ActionButton.paintButtonLook(ActionButton.java:400)
	at com.intellij.openapi.actionSystem.impl.ActionButton.paintComponent(ActionButton.java:364)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)



CUSTOMER SUBMITTED WORKAROUND :
Catch the ClassCastException and assume zero insets.

FREQUENCY : rarely



Comments
Fix request (13u) Requesting backport to 13u for parity with 11u, applies cleanly.
09-06-2020

Fix request (11u) -- will label after testing completed. I would like to downport this for parity with 11.0.8-oracle. Applies clean.
21-04-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/392d5e420c73 User: psadhukhan Date: 2020-03-22 11:49:29 +0000
22-03-2020

URL: https://hg.openjdk.java.net/jdk/client/rev/392d5e420c73 User: serb Date: 2020-03-10 20:38:54 +0000
10-03-2020

I can confirm the issue by the source code check. It does not windows specific.
05-11-2019

Reported with JDK 11.0.4, java.lang.ClassCastException: class sun.awt.image.BufferedImageDevice cannot be cast to class sun.awt.Win32GraphicsDevice We require detailed information including a simple test case to verify this locally. Also it would help to know status with current JDK versions 11.0.5 and 13.0.1. Written back to the submitter requesting additional information.
05-11-2019