8134721: NPE in SwingUtilities2.drawChars after JDK-6302464
authoralexsch
Tue, 01 Sep 2015 09:40:16 +0400
changeset 32496 4177c977d9e6
parent 32495 2895fff4cabc
child 32497 64edba808ec4
8134721: NPE in SwingUtilities2.drawChars after JDK-6302464 Reviewed-by: serb, azvegint
jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java
jdk/test/javax/swing/text/Utilities/8134721/bug8134721.java
--- a/jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java	Mon Aug 31 11:29:55 2015 -0700
+++ b/jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java	Tue Sep 01 09:40:16 2015 +0400
@@ -483,7 +483,9 @@
                 }
             }
 
-            Object aaHint = c.getClientProperty(KEY_TEXT_ANTIALIASING);
+            Object aaHint = (c == null)
+                                ? null
+                                : c.getClientProperty(KEY_TEXT_ANTIALIASING);
             if (aaHint != null) {
                 Object oldContrast = null;
                 Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
@@ -765,7 +767,9 @@
         }
         // Assume we're not printing if we get here, or that we are invoked
         // via Swing text printing which is laid out for the printer.
-        Object aaHint = c.getClientProperty(KEY_TEXT_ANTIALIASING);
+        Object aaHint = (c == null)
+                            ? null
+                            : c.getClientProperty(KEY_TEXT_ANTIALIASING);
         if (aaHint != null && (g instanceof Graphics2D)) {
             Graphics2D g2 = (Graphics2D)g;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/Utilities/8134721/bug8134721.java	Tue Sep 01 09:40:16 2015 +0400
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2015, 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.Graphics;
+import java.awt.image.BufferedImage;
+import javax.swing.SwingUtilities;
+import javax.swing.text.Segment;
+import javax.swing.text.Utilities;
+
+/**
+ * @test
+ * @bug 8134721
+ * @author Alexandr Scherbatiy
+ * @summary NPE in SwingUtilities2.drawChars after JDK-6302464
+ */
+public class bug8134721 {
+
+    public static void main(String[] args) throws Exception {
+        SwingUtilities.invokeAndWait(bug8134721::testNPE);
+    }
+
+    private static void testNPE() {
+
+        Graphics g = null;
+        try {
+            String test = "\ttest\ttest2";
+            BufferedImage buffImage = new BufferedImage(
+                    100, 100, BufferedImage.TYPE_INT_RGB);
+            g = buffImage.createGraphics();
+            Segment segment = new Segment(test.toCharArray(), 0, test.length());
+            Utilities.drawTabbedText(segment, 0, 0, g, null, 0);
+        } finally {
+            if (g != null) {
+                g.dispose();
+            }
+        }
+    }
+}
\ No newline at end of file