move transaction write action assertions to a single place, add diagnostics for flaky...
authorpeter <peter@jetbrains.com>
Mon, 2 May 2016 17:05:03 +0000 (19:05 +0200)
committerpeter <peter@jetbrains.com>
Mon, 2 May 2016 17:05:03 +0000 (19:05 +0200)
platform/core-impl/src/com/intellij/openapi/application/TransactionGuardImpl.java
platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java
platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java
platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileDocumentManagerImpl.java

index 6950a95a49c8920c87df14ef128355676565b447..bf67f58d8a874719764b356e22987873c264853c 100644 (file)
@@ -233,8 +233,15 @@ public class TransactionGuardImpl extends TransactionGuard {
     };
   }
 
-  public boolean isWriteActionAllowed() {
-    return !Registry.is("ide.require.transaction.for.model.changes", false) || myWritingAllowed;
+  public void assertWriteActionAllowed() {
+    if (Registry.is("ide.require.transaction.for.model.changes", false) && !myWritingAllowed) {
+      String message = "Write access is allowed from model transactions only, see TransactionGuard documentation for details";
+      if (ApplicationManager.getApplication().isUnitTestMode()) {
+        message += "; current modality=" + ModalityState.current() + "; unsafe modality=" + myUnsafeModality;
+      }
+      // please assign exceptions here to Peter
+      LOG.error(message);
+    }
   }
 
   @Override
index 3e1b9486e717b987d1304829a1b283877e089b00..8c81772651695b4e97fa5f95ea33d1b23fc3730f 100644 (file)
@@ -214,10 +214,7 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen
   @Override
   public void commitAllDocuments() {
     ApplicationManager.getApplication().assertIsDispatchThread();
-    if (!((TransactionGuardImpl)TransactionGuard.getInstance()).isWriteActionAllowed()) {
-      // please assign exceptions here to Peter
-      LOG.error("Write access is not allowed in this context, see TransactionGuard documentation for details");
-    }
+    ((TransactionGuardImpl)TransactionGuard.getInstance()).assertWriteActionAllowed();
 
     if (myUncommittedDocuments.isEmpty()) return;
 
@@ -291,10 +288,7 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen
 
   @Override
   public void commitDocument(@NotNull final Document doc) {
-    if (!((TransactionGuardImpl)TransactionGuard.getInstance()).isWriteActionAllowed()) {
-      // please assign exceptions here to Peter
-      LOG.error("Write access is not allowed in this context, see TransactionGuard documentation for details");
-    }
+    ((TransactionGuardImpl)TransactionGuard.getInstance()).assertWriteActionAllowed();
 
     final Document document = doc instanceof DocumentWindow ? ((DocumentWindow)doc).getDelegate() : doc;
     if (!isCommitted(document)) {
index 4a213e254ab9f69ed3e774d864e725120f2a1245..67202dabc577fbd8f544785a97f97be23885b32a 100644 (file)
@@ -1104,9 +1104,8 @@ public class ApplicationImpl extends PlatformComponentManagerImpl implements App
   private void startWrite(@NotNull Class clazz) {
     assertIsDispatchThread("Write access is allowed from event dispatch thread only");
     HeavyProcessLatch.INSTANCE.stopThreadPrioritizing(); // let non-cancellable read actions complete faster, if present
-    if (!isDisposed() && !isDisposeInProgress() && !((TransactionGuardImpl)TransactionGuard.getInstance()).isWriteActionAllowed()) {
-      // please assign exceptions here to Peter
-      LOG.error("Write access is allowed from model transactions only, see TransactionGuard documentation for details");
+    if (!isDisposed() && !isDisposeInProgress()) {
+      ((TransactionGuardImpl)TransactionGuard.getInstance()).assertWriteActionAllowed();
     }
     boolean writeActionPending = myWriteActionPending;
     if (gatherWriteActionStatistics && myWriteActionsStack.isEmpty() && !writeActionPending) {
index ec7ee0e5cbbbf47d0887bbafb8f36b8ad56e59b1..a926abf28bee27b66f83fb214b58c6cb99c25678 100644 (file)
@@ -300,10 +300,7 @@ public class FileDocumentManagerImpl extends FileDocumentManager implements Virt
    */
   public void saveAllDocuments(boolean isExplicit) {
     ApplicationManager.getApplication().assertIsDispatchThread();
-    if (!((TransactionGuardImpl)TransactionGuard.getInstance()).isWriteActionAllowed()) {
-      // please assign exceptions here to Peter
-      LOG.error("Write access is not allowed in this context, see TransactionGuard documentation for details");
-    }
+    ((TransactionGuardImpl)TransactionGuard.getInstance()).assertWriteActionAllowed();
 
     myMultiCaster.beforeAllDocumentsSaving();
     if (myUnsavedDocuments.isEmpty()) return;
@@ -344,10 +341,7 @@ public class FileDocumentManagerImpl extends FileDocumentManager implements Virt
 
   public void saveDocument(@NotNull final Document document, final boolean explicit) {
     ApplicationManager.getApplication().assertIsDispatchThread();
-    if (!((TransactionGuardImpl)TransactionGuard.getInstance()).isWriteActionAllowed()) {
-      // please assign exceptions here to Peter
-      LOG.error("Write access is not allowed in this context, see TransactionGuard documentation for details");
-    }
+    ((TransactionGuardImpl)TransactionGuard.getInstance()).assertWriteActionAllowed();
 
     if (!myUnsavedDocuments.contains(document)) return;