Skip to content

Commit

Permalink
8291792: DefaultStyledDocument.setCharacterAttributes accepts negativ…
Browse files Browse the repository at this point in the history
…e length

Reviewed-by: psadhukhan, prr
  • Loading branch information
Tejesh R authored and prsadhuk committed Aug 30, 2022
1 parent f766d92 commit 4a28f37
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,32 @@ public Style getLogicalStyle(int p) {
* A write lock is held by this operation while changes
* are being made, and a DocumentEvent is sent to the listeners
* after the change has been successfully completed.
*
* <p>
* {@code offset} and {@code length} define the range of the text
* over which the attributes are set.
* If the length is &lt;= 0, then no action is taken and the method
* just returns.
* If the offset is &lt;=0 or &gt; the length of the text then no
* action is taken, and the method just returns.
* Otherwise if {@code offset + length} will exceed the length of
* the text then the affected range is truncated.
* </p>
*
* <p>
* This method is thread safe, although most Swing methods
* are not. Please see
* <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
* in Swing</A> for more information.
*
* @param offset the offset in the document &gt;= 0
* @param length the length &gt;= 0
* @param length the length &gt; 0
* @param s the attributes
* @param replace true if the previous attributes should be replaced
* before setting the new attributes
*/
public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) {
if (length == 0) {
if (length <= 0) {
return;
}
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2022, 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.BorderLayout;
import java.awt.Dimension;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/*
* @test
* @bug 8291792
* @key headful
* @summary Test to check if negative length check is implemented in
* setCharacterAttributes(). Test should not throw any exception on
* negative length.
* @run main DocNegLenCharAttrTest
*/
public class DocNegLenCharAttrTest {
private static JFrame frame;
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
test();
}
});
} finally {
frame.dispose();
}
System.out.println("Test Pass!");
}

public static void test() {
DefaultStyledDocument doc;
frame = new JFrame();
doc = new DefaultStyledDocument();
JTextPane text = new JTextPane();
text.setDocument(doc);
text.setText("hello world");
doc.setCharacterAttributes(6, -5,
createLabelAttribute("world"), true);

frame.setPreferredSize(new Dimension(100,70));
frame.add(text);
frame.setLayout(new BorderLayout());
frame.add(text,BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}

private static AttributeSet createLabelAttribute(String text){
JLabel lbl = new JLabel(text.toUpperCase());
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setComponent(attr,lbl);
return attr;
}
}

1 comment on commit 4a28f37

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.