我有时会看到以下使用 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)
...
根本原因似乎是顽皮的 JDK 代码对 SunGraphics2D 而不是 java.awt.Graphics2D 进行了向下转换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);
我做错了什么,因为我在其他地方找不到任何对此的引用..?
解决方法?我能想到的就是在我的测试中吞下异常,不理想。