do not make VirtualFile Disposable
authorAlexey Kudravtsev <cdr@intellij.com>
Wed, 12 Aug 2020 17:03:07 +0000 (20:03 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Wed, 12 Aug 2020 17:37:39 +0000 (17:37 +0000)
because once disposed, its .dispose() method will never be called again which is most probably unexpected

GitOrigin-RevId: d79c11bbb00d8f34839b72c0c548a47c7a66cf13

platform/core-api/src/com/intellij/openapi/vfs/VirtualFile.java
plugins/editorconfig/src/org/editorconfig/configmanagement/editor/EditorConfigEditorProvider.java
plugins/editorconfig/src/org/editorconfig/configmanagement/editor/EditorConfigPreviewFile.java

index e3b0fbcdfb6f390ad242c200dd5c190f3bb8b917..da3a74ff3653a7037f858d2785a7d36b3349eaf7 100644 (file)
@@ -2,6 +2,7 @@
 package com.intellij.openapi.vfs;
 
 import com.intellij.core.CoreBundle;
+import com.intellij.openapi.Disposable;
 import com.intellij.openapi.application.Application;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
@@ -99,7 +100,12 @@ public abstract class VirtualFile extends UserDataHolderBase implements Modifica
   private static final Key<byte[]> BOM_KEY = Key.create("BOM");
   private static final Key<Charset> CHARSET_KEY = Key.create("CHARSET");
 
-  protected VirtualFile() { }
+  protected VirtualFile() {
+    if (this instanceof Disposable) {
+      throw new IllegalStateException("VirtualFile must not implement Disposable because of life-cycle requirements. " +
+                                      "E.g. VirtualFile should exist throughout the application and may not be disposed half-way.");
+    }
+  }
 
   /**
    * Gets the name of this file.
index eb22398d440d7aef4d493debb91ffd54bebd1507..4ecabf62ea8a55598fef748e4190fd54f84b9d45 100644 (file)
@@ -89,7 +89,7 @@ public class EditorConfigEditorProvider implements AsyncFileEditorProvider, Dumb
         TextEditor ecTextEditor = (TextEditor)TextEditorProvider.getInstance().createEditor(myProject, myFile);
         final EditorConfigEditorWithPreview splitEditor = new EditorConfigEditorWithPreview(
           myFile, myProject, ecTextEditor, previewEditor);
-        Disposer.register(splitEditor, previewFile);
+        Disposer.register(splitEditor, () -> previewFile.unregisterListener());
         Disposer.register(splitEditor, statusListener);
         return splitEditor;
       }
index b3437c6ca4e6091de03378334f36b74a9a480a64..4037222c3ec028a116c74099a877137b67e6e53b 100644 (file)
@@ -3,7 +3,6 @@ package org.editorconfig.configmanagement.editor;
 
 import com.intellij.application.options.CodeStyle;
 import com.intellij.lang.Language;
-import com.intellij.openapi.Disposable;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.command.CommandProcessor;
 import com.intellij.openapi.editor.Document;
@@ -24,7 +23,7 @@ import org.jetbrains.annotations.Nullable;
 
 import java.nio.file.Paths;
 
-public class EditorConfigPreviewFile extends LightVirtualFile implements CodeStyleSettingsListener, Disposable {
+public class EditorConfigPreviewFile extends LightVirtualFile implements CodeStyleSettingsListener {
   private final Project  myProject;
   private final String   myOriginalPath;
   private final Document myDocument;
@@ -85,8 +84,7 @@ public class EditorConfigPreviewFile extends LightVirtualFile implements CodeSty
     return null;
   }
 
-  @Override
-  public void dispose() {
+  public void unregisterListener() {
     CodeStyleSettingsManager.removeListener(myProject, this);
   }
 }