Skip to content

Commit 0c21dd0

Browse files
committed
6206189: Graphics2D.clip specifies incorrectly that a 'null' is a valid value for this method
Reviewed-by: aivanov, kizune, azvegint
1 parent 2b55501 commit 0c21dd0

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

src/java.desktop/share/classes/java/awt/Graphics.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -354,7 +354,8 @@ public FontMetrics getFontMetrics() {
354354
* {@code Rectangle} objects. This method sets the
355355
* user clip, which is independent of the clipping associated
356356
* with device bounds and window visibility.
357-
* @param clip the {@code Shape} to use to set the clip
357+
* @param clip the {@code Shape} to use to set the clip.
358+
* Passing {@code null} clears the current {@code clip}.
358359
* @see java.awt.Graphics#getClip()
359360
* @see java.awt.Graphics#clipRect
360361
* @see java.awt.Graphics#setClip(int, int, int, int)

src/java.desktop/share/classes/java/awt/Graphics2D.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1195,15 +1195,20 @@ public abstract boolean hit(Rectangle rect,
11951195
* {@code Clip}. This method is used to make the current
11961196
* {@code Clip} smaller.
11971197
* To make the {@code Clip} larger, use {@code setClip}.
1198-
* The <i>user clip</i> modified by this method is independent of the
1198+
* <p>The <i>user clip</i> modified by this method is independent of the
11991199
* clipping associated with device bounds and visibility. If no clip has
12001200
* previously been set, or if the clip has been cleared using
12011201
* {@link Graphics#setClip(Shape) setClip} with a {@code null}
12021202
* argument, the specified {@code Shape} becomes the new
12031203
* user clip.
1204+
* <p>Since this method intersects the specified shape
1205+
* with the current clip, it will throw {@code NullPointerException}
1206+
* for a {@code null} shape unless the user clip is also {@code null}.
1207+
* So calling this method with a {@code null} argument is not recommended.
12041208
* @param s the {@code Shape} to be intersected with the current
1205-
* {@code Clip}. If {@code s} is {@code null},
1206-
* this method clears the current {@code Clip}.
1209+
* {@code Clip}. This method updates the current {@code Clip}.
1210+
* @throws NullPointerException if {@code s} is {@code null}
1211+
* and a user clip is currently set.
12071212
*/
12081213
public abstract void clip(Shape s);
12091214

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
25+
import java.awt.Graphics2D;
26+
import java.awt.image.BufferedImage;
27+
import java.awt.Shape;
28+
29+
/**
30+
* @test
31+
* @bug 6206189
32+
* @summary Verifies passing null to Graphics2D.clip(Shape) throws NPE.
33+
*/
34+
public class TestNullClip {
35+
36+
public static void main(String[] argv) {
37+
BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
38+
Graphics2D g2d = (Graphics2D)bi.getGraphics();
39+
40+
g2d.clip(null); // silently return, no NPE should be thrown
41+
42+
g2d.setClip(0, 0, 100, 100);
43+
g2d.setClip(null);
44+
Shape clip1 = g2d.getClip();
45+
if (clip1 != null) {
46+
throw new RuntimeException("Clip is not cleared");
47+
}
48+
g2d.setClip(0, 0, 100, 100);
49+
try {
50+
g2d.clip(null);
51+
throw new RuntimeException("NPE is expected");
52+
} catch (NullPointerException e) {
53+
//expected
54+
System.out.println("NPE is thrown");
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)