prohibit dialogs and other swing events under write action appcode/171.885 clion/171.886
authorpeter <peter@jetbrains.com>
Fri, 11 Nov 2016 06:52:52 +0000 (07:52 +0100)
committerpeter <peter@jetbrains.com>
Fri, 11 Nov 2016 06:53:29 +0000 (07:53 +0100)
platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java
platform/platform-impl/src/com/intellij/openapi/application/impl/NoSwingUnderWriteAction.java [new file with mode: 0644]

index 82a71f46953b42fb0ac73100cddeee3e9cca772a..0b1b5458df4f5b537f14459af502844d094ac553 100644 (file)
@@ -217,6 +217,8 @@ public class ApplicationImpl extends PlatformComponentManagerImpl implements App
       return Thread.currentThread();
     });
     myLock = new ReadMostlyRWLock(edt);
+
+    NoSwingUnderWriteAction.watchForEvents(this);
   }
 
   /**
diff --git a/platform/platform-impl/src/com/intellij/openapi/application/impl/NoSwingUnderWriteAction.java b/platform/platform-impl/src/com/intellij/openapi/application/impl/NoSwingUnderWriteAction.java
new file mode 100644 (file)
index 0000000..49ea7fe
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.openapi.application.impl;
+
+import com.intellij.ide.IdeEventQueue;
+import com.intellij.openapi.application.Application;
+import com.intellij.openapi.application.ApplicationAdapter;
+import com.intellij.openapi.diagnostic.Logger;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * @author peter
+ */
+class NoSwingUnderWriteAction {
+  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.application.impl.NoSwingUnderWriteAction");
+
+  static void watchForEvents(Application application) {
+    AtomicBoolean reported = new AtomicBoolean();
+    IdeEventQueue.getInstance().addPostprocessor(e -> {
+      if (application.isWriteAccessAllowed() && reported.compareAndSet(false, true)) {
+        LOG.error("AWT events are not allowed inside write action: " + e);
+      }
+      return true;
+    }, application);
+
+    application.addApplicationListener(new ApplicationAdapter() {
+      @Override
+      public void afterWriteActionFinished(@NotNull Object action) {
+        reported.set(false);
+      }
+    });
+  }
+}