Skip to content

Commit 79ed0e7

Browse files
author
Pavel Porvatov
committed
6771184: Some methods in text package don't throw BadLocationException when expected
Reviewed-by: peterz
1 parent 4295a2e commit 79ed0e7

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

jdk/src/share/classes/javax/swing/text/DefaultHighlighter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ public void deinstall(JTextComponent c) {
113113
* @exception BadLocationException if the specified location is invalid
114114
*/
115115
public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p) throws BadLocationException {
116+
if (p0 < 0) {
117+
throw new BadLocationException("Invalid start offset", p0);
118+
}
119+
120+
if (p1 < p0) {
121+
throw new BadLocationException("Invalid end offset", p1);
122+
}
123+
116124
Document doc = component.getDocument();
117125
HighlightInfo i = (getDrawsLayeredHighlights() &&
118126
(p instanceof LayeredHighlighter.LayerPainter)) ?
@@ -217,6 +225,14 @@ else if (mapper != null) {
217225
* @exception BadLocationException if the specified location is invalid
218226
*/
219227
public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException {
228+
if (p0 < 0) {
229+
throw new BadLocationException("Invalid beginning of the range", p0);
230+
}
231+
232+
if (p1 < p0) {
233+
throw new BadLocationException("Invalid end of the range", p1);
234+
}
235+
220236
Document doc = component.getDocument();
221237
if (tag instanceof LayeredHighlightInfo) {
222238
LayeredHighlightInfo lhi = (LayeredHighlightInfo)tag;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2010, 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+
* @test
26+
* @bug 6771184
27+
* @summary Some methods in text package don't throw BadLocationException when expected
28+
* @author Pavel Porvatov
29+
*/
30+
31+
import javax.swing.*;
32+
import javax.swing.text.BadLocationException;
33+
import javax.swing.text.Highlighter;
34+
import javax.swing.text.JTextComponent;
35+
import java.awt.*;
36+
37+
public class bug6771184 {
38+
public static void main(String[] args) {
39+
SwingUtilities.invokeLater(new Runnable() {
40+
public void run() {
41+
JTextArea textArea = new JTextArea("Tested string");
42+
43+
Highlighter highlighter = textArea.getHighlighter();
44+
Highlighter.HighlightPainter myPainter = new Highlighter.HighlightPainter() {
45+
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
46+
}
47+
};
48+
49+
int negativeTestedData[][] = {{50, 0},
50+
{-1, 1},
51+
{-5, -4},
52+
{Integer.MAX_VALUE, Integer.MIN_VALUE},
53+
{Integer.MIN_VALUE, Integer.MAX_VALUE},
54+
{Integer.MIN_VALUE, Integer.MIN_VALUE}};
55+
56+
for (int[] data : negativeTestedData) {
57+
try {
58+
highlighter.addHighlight(data[0], data[1], myPainter);
59+
60+
throw new RuntimeException("Method addHighlight() does not throw BadLocationException for (" +
61+
data[0] + ", " + data[1] + ") ");
62+
} catch (BadLocationException e) {
63+
// Ok
64+
}
65+
66+
Object objRef;
67+
68+
try {
69+
objRef = highlighter.addHighlight(0, 1, myPainter);
70+
} catch (BadLocationException e) {
71+
throw new RuntimeException("highlighter.addHighlight(0, 1, myPainter) throws exception", e);
72+
}
73+
74+
try {
75+
highlighter.changeHighlight(objRef, data[0], data[1]);
76+
77+
throw new RuntimeException("Method changeHighlight() does not throw BadLocationException for (" +
78+
data[0] + ", " + data[1] + ") ");
79+
} catch (BadLocationException e) {
80+
// Ok
81+
}
82+
}
83+
}
84+
});
85+
}
86+
}

0 commit comments

Comments
 (0)