7146146: Deadlock between subclass of AbstractDocument and UndoManager
Reviewed-by: art
This commit is contained in:
parent
8de79a6398
commit
e630f8d216
@ -31,7 +31,6 @@ import java.text.Bidi;
|
|||||||
|
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
import javax.swing.undo.*;
|
import javax.swing.undo.*;
|
||||||
import javax.swing.event.ChangeListener;
|
|
||||||
import javax.swing.event.*;
|
import javax.swing.event.*;
|
||||||
import javax.swing.tree.TreeNode;
|
import javax.swing.tree.TreeNode;
|
||||||
|
|
||||||
@ -698,28 +697,31 @@ public abstract class AbstractDocument implements Document, Serializable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DocumentFilter filter = getDocumentFilter();
|
DocumentFilter filter = getDocumentFilter();
|
||||||
|
InsertStringResult insertStringResult = null;
|
||||||
|
|
||||||
writeLock();
|
writeLock();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (filter != null) {
|
if (filter != null) {
|
||||||
filter.insertString(getFilterBypass(), offs, str, a);
|
filter.insertString(getFilterBypass(), offs, str, a);
|
||||||
}
|
} else {
|
||||||
else {
|
insertStringResult = handleInsertString(offs, str, a);
|
||||||
handleInsertString(offs, str, a);
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
writeUnlock();
|
writeUnlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
processInsertStringResult(insertStringResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the actual work of inserting the text; it is assumed the
|
* Performs the actual work of inserting the text; it is assumed the
|
||||||
* caller has obtained a write lock before invoking this.
|
* caller has obtained a write lock before invoking this.
|
||||||
*/
|
*/
|
||||||
void handleInsertString(int offs, String str, AttributeSet a)
|
private InsertStringResult handleInsertString(int offs, String str, AttributeSet a)
|
||||||
throws BadLocationException {
|
throws BadLocationException {
|
||||||
if ((str == null) || (str.length() == 0)) {
|
if ((str == null) || (str.length() == 0)) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
UndoableEdit u = data.insertString(offs, str);
|
UndoableEdit u = data.insertString(offs, str);
|
||||||
DefaultDocumentEvent e =
|
DefaultDocumentEvent e =
|
||||||
@ -746,12 +748,29 @@ public abstract class AbstractDocument implements Document, Serializable {
|
|||||||
insertUpdate(e, a);
|
insertUpdate(e, a);
|
||||||
// Mark the edit as done.
|
// Mark the edit as done.
|
||||||
e.end();
|
e.end();
|
||||||
fireInsertUpdate(e);
|
|
||||||
|
InsertStringResult result = new InsertStringResult();
|
||||||
|
|
||||||
|
result.documentEvent = e;
|
||||||
|
|
||||||
// only fire undo if Content implementation supports it
|
// only fire undo if Content implementation supports it
|
||||||
// undo for the composed text is not supported for now
|
// undo for the composed text is not supported for now
|
||||||
if (u != null &&
|
if (u != null && (a == null || !a.isDefined(StyleConstants.ComposedTextAttribute))) {
|
||||||
(a == null || !a.isDefined(StyleConstants.ComposedTextAttribute))) {
|
result.undoableEditEvent = new UndoableEditEvent(this, e);
|
||||||
fireUndoableEditUpdate(new UndoableEditEvent(this, e));
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processInsertStringResult(InsertStringResult insertStringResult) {
|
||||||
|
if (insertStringResult == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fireInsertUpdate(insertStringResult.documentEvent);
|
||||||
|
|
||||||
|
if (insertStringResult.undoableEditEvent != null) {
|
||||||
|
fireUndoableEditUpdate(insertStringResult.undoableEditEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2947,12 +2966,10 @@ public abstract class AbstractDocument implements Document, Serializable {
|
|||||||
*/
|
*/
|
||||||
class UndoRedoDocumentEvent implements DocumentEvent {
|
class UndoRedoDocumentEvent implements DocumentEvent {
|
||||||
private DefaultDocumentEvent src = null;
|
private DefaultDocumentEvent src = null;
|
||||||
private boolean isUndo;
|
|
||||||
private EventType type = null;
|
private EventType type = null;
|
||||||
|
|
||||||
public UndoRedoDocumentEvent(DefaultDocumentEvent src, boolean isUndo) {
|
public UndoRedoDocumentEvent(DefaultDocumentEvent src, boolean isUndo) {
|
||||||
this.src = src;
|
this.src = src;
|
||||||
this.isUndo = isUndo;
|
|
||||||
if(isUndo) {
|
if(isUndo) {
|
||||||
if(src.getType().equals(EventType.INSERT)) {
|
if(src.getType().equals(EventType.INSERT)) {
|
||||||
type = EventType.REMOVE;
|
type = EventType.REMOVE;
|
||||||
@ -3106,13 +3123,23 @@ public abstract class AbstractDocument implements Document, Serializable {
|
|||||||
public void insertString(int offset, String string,
|
public void insertString(int offset, String string,
|
||||||
AttributeSet attr) throws
|
AttributeSet attr) throws
|
||||||
BadLocationException {
|
BadLocationException {
|
||||||
handleInsertString(offset, string, attr);
|
InsertStringResult insertStringResult = handleInsertString(offset, string, attr);
|
||||||
|
|
||||||
|
processInsertStringResult(insertStringResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void replace(int offset, int length, String text,
|
public void replace(int offset, int length, String text,
|
||||||
AttributeSet attrs) throws BadLocationException {
|
AttributeSet attrs) throws BadLocationException {
|
||||||
handleRemove(offset, length);
|
handleRemove(offset, length);
|
||||||
handleInsertString(offset, text, attrs);
|
|
||||||
|
InsertStringResult insertStringResult = handleInsertString(offset, text, attrs);
|
||||||
|
|
||||||
|
processInsertStringResult(insertStringResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class InsertStringResult {
|
||||||
|
DefaultDocumentEvent documentEvent;
|
||||||
|
UndoableEditEvent undoableEditEvent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2012, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @test
|
||||||
|
@bug 7146146
|
||||||
|
@summary Deadlock between subclass of AbstractDocument and UndoManager
|
||||||
|
@author Pavel Porvatov
|
||||||
|
*/
|
||||||
|
|
||||||
|
import javax.swing.text.BadLocationException;
|
||||||
|
import javax.swing.text.PlainDocument;
|
||||||
|
import javax.swing.text.StringContent;
|
||||||
|
import javax.swing.undo.UndoManager;
|
||||||
|
|
||||||
|
public class bug7146146 {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
System.out.print("Iteration " + i);
|
||||||
|
|
||||||
|
test();
|
||||||
|
|
||||||
|
System.out.print(" passed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void test() throws Exception {
|
||||||
|
final PlainDocument doc = new PlainDocument(new StringContent());
|
||||||
|
final UndoManager undoManager = new UndoManager();
|
||||||
|
|
||||||
|
doc.addUndoableEditListener(undoManager);
|
||||||
|
doc.insertString(0, "<Test 1>", null);
|
||||||
|
|
||||||
|
Thread t1 = new Thread("Thread 1") {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
doc.insertString(0, "<Test 2>", null);
|
||||||
|
} catch (BadLocationException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Thread t2 = new Thread("Thread 2") {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
undoManager.undo();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
|
||||||
|
t1.join();
|
||||||
|
t2.join();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user