I sometimes see the following stacktrace running tests built with UISpec4j
java.lang.ClassCastException: org.uispec4j.interception.toolkit.Empty$DummyGraphics2D cannot be cast to sun.java2d.SunGraphics2D
at java.awt.Component$BltBufferStrategy.getDrawGraphics(Component.java:4348)
at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:522)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:276)
at javax.swing.RepaintManager.paint(RepaintManager.java:1265)
at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
at javax.swing.JComponent.paintImmediately(JComponent.java:4990)
at javax.swing.AbstractButton.doClick(AbstractButton.java:371)
...
The root cause appears to be naughty JDK code doing a downcast to SunGraphics2D rather than java.awt.Graphics2D in java.awt.Component.BltBufferStrategy.getDrawGraphics()
/**
* @return the draw graphics
*/
public Graphics getDrawGraphics() {
revalidate();
Image backBuffer = getBackBuffer();
if (backBuffer == null) {
return getGraphics();
}
SunGraphics2D g = (SunGraphics2D)backBuffer.getGraphics();
g.constrain(-insets.left, -insets.top,
backBuffer.getWidth(null) + insets.left,
backBuffer.getHeight(null) + insets.top);
Am I doing something wrong as I cannot find any references to this elsewhere..?
Workarounds? All I can think to do is to swallow the exception in my test, not ideal.