extract current migration tracking out of JavaPsiFacade to a separate PsiMigrationMan...
authorDmitry Jemerov <yole@jetbrains.com>
Mon, 3 Oct 2011 17:34:19 +0000 (19:34 +0200)
committerDmitry Jemerov <yole@jetbrains.com>
Tue, 4 Oct 2011 14:03:46 +0000 (16:03 +0200)
java/java-impl/src/com/intellij/psi/impl/JavaPsiFacadeImpl.java
java/java-impl/src/com/intellij/psi/impl/file/PsiPackageImpl.java
java/java-impl/src/com/intellij/psi/impl/migration/PsiMigrationImpl.java
java/java-impl/src/com/intellij/psi/impl/migration/PsiMigrationManager.java [new file with mode: 0644]
java/java-impl/src/com/intellij/refactoring/migration/MigrationProcessor.java
java/java-impl/src/com/intellij/refactoring/migration/MigrationUtil.java
java/java-psi-api/src/com/intellij/psi/JavaPsiFacade.java
resources/src/META-INF/IdeaPlugin.xml

index 7f5dab51635eb47febaa69485d37790aa1edffe0..03897c4d328892d3e4fc0c355aa27154b51fecf0 100644 (file)
@@ -33,7 +33,7 @@ import com.intellij.psi.*;
 import com.intellij.psi.impl.file.PsiPackageImpl;
 import com.intellij.psi.impl.file.impl.JavaFileManager;
 import com.intellij.psi.impl.file.impl.JavaFileManagerImpl;
-import com.intellij.psi.impl.migration.PsiMigrationImpl;
+import com.intellij.psi.impl.migration.PsiMigrationManager;
 import com.intellij.psi.impl.source.DummyHolderFactory;
 import com.intellij.psi.impl.source.JavaDummyHolder;
 import com.intellij.psi.impl.source.JavaDummyHolderFactory;
@@ -63,7 +63,6 @@ import java.util.concurrent.ConcurrentMap;
 public class JavaPsiFacadeImpl extends JavaPsiFacadeEx implements Disposable {
   private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.JavaPsiFacadeImpl");
 
-  private PsiMigrationImpl myCurrentMigration;
   private final PsiElementFinder[] myElementFinders;
   private final PsiResolveHelper myResolveHelper;
   private final PsiNameHelper myNameHelper;
@@ -227,11 +226,6 @@ public class JavaPsiFacadeImpl extends JavaPsiFacadeEx implements Disposable {
     return null;
   }
 
-
-  public PsiMigrationImpl getCurrentMigration() {
-    return myCurrentMigration;
-  }
-
   @NotNull
   public PsiJavaParserFacade getParserFacade() {
     return getElementFactory(); // TODO: lighter implementation which doesn't mark all the elements as generated.
@@ -242,13 +236,6 @@ public class JavaPsiFacadeImpl extends JavaPsiFacadeEx implements Disposable {
     return myResolveHelper;
   }
 
-  @NotNull
-  public PsiMigration startMigration() {
-    LOG.assertTrue(myCurrentMigration == null);
-    myCurrentMigration = new PsiMigrationImpl(this, (PsiManagerImpl)PsiManager.getInstance(myProject));
-    return myCurrentMigration;
-  }
-
   @NotNull
   public PsiNameHelper getNameHelper() {
     return myNameHelper;
@@ -299,8 +286,8 @@ public class JavaPsiFacadeImpl extends JavaPsiFacadeEx implements Disposable {
   @Nullable
   private PsiPackage findPackageDefault(String qualifiedName) {
     final PsiPackage aPackage = myFileManager.findPackage(qualifiedName);
-    if (aPackage == null && myCurrentMigration != null) {
-      final PsiPackage migrationPackage = myCurrentMigration.getMigrationPackage(qualifiedName);
+    if (aPackage == null && PsiMigrationManager.getInstance(myProject).getCurrentMigration() != null) {
+      final PsiPackage migrationPackage = PsiMigrationManager.getInstance(myProject).getCurrentMigration().getMigrationPackage(qualifiedName);
       if (migrationPackage != null) return migrationPackage;
     }
 
@@ -311,8 +298,8 @@ public class JavaPsiFacadeImpl extends JavaPsiFacadeEx implements Disposable {
     public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
       PsiClass psiClass = myFileManager.findClass(qualifiedName, scope);
 
-      if (psiClass == null && myCurrentMigration != null) {
-        psiClass = myCurrentMigration.getMigrationClass(qualifiedName);
+      if (psiClass == null && PsiMigrationManager.getInstance(myProject).getCurrentMigration() != null) {
+        psiClass = PsiMigrationManager.getInstance(myProject).getCurrentMigration().getMigrationClass(qualifiedName);
       }
 
       return psiClass;
@@ -321,8 +308,8 @@ public class JavaPsiFacadeImpl extends JavaPsiFacadeEx implements Disposable {
     @NotNull
     public PsiClass[] findClasses(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
       final PsiClass[] classes = myFileManager.findClasses(qualifiedName, scope);
-      if (classes.length == 0 && myCurrentMigration != null) {
-        final PsiClass migrationClass = myCurrentMigration.getMigrationClass(qualifiedName);
+      if (classes.length == 0 && PsiMigrationManager.getInstance(myProject).getCurrentMigration() != null) {
+        final PsiClass migrationClass = PsiMigrationManager.getInstance(myProject).getCurrentMigration().getMigrationClass(qualifiedName);
         if (migrationClass != null) {
           return new PsiClass[]{migrationClass};
         }
@@ -410,14 +397,6 @@ public class JavaPsiFacadeImpl extends JavaPsiFacadeEx implements Disposable {
     }
   }
 
-  public void migrationModified(boolean terminated) {
-    if (terminated) {
-      myCurrentMigration = null;
-    }
-
-    ((PsiManagerEx)PsiManager.getInstance(myProject)).beforeChange(true);
-  }
-
 
   public boolean isPartOfPackagePrefix(String packageName) {
     final Collection<String> packagePrefixes = myFileManager.getNonTrivialPackagePrefixes();
index 03c26ea2c04d6faddfe6fbba6dadc9da39bad525..1e65ea0ff00af2d081e3c175c54c8830146603a4 100644 (file)
@@ -40,6 +40,7 @@ import com.intellij.psi.impl.JavaPsiFacadeImpl;
 import com.intellij.psi.impl.PackagePrefixElementFinder;
 import com.intellij.psi.impl.PsiManagerEx;
 import com.intellij.psi.impl.migration.PsiMigrationImpl;
+import com.intellij.psi.impl.migration.PsiMigrationManager;
 import com.intellij.psi.impl.source.tree.java.PsiCompositeModifierList;
 import com.intellij.psi.scope.ElementClassHint;
 import com.intellij.psi.scope.NameHint;
@@ -275,7 +276,7 @@ public class PsiPackageImpl extends PsiPackageBase implements PsiPackage, Querya
     ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
 
     final JavaPsiFacadeImpl facade = getFacade();
-    final PsiMigrationImpl migration = facade.getCurrentMigration();
+    final PsiMigrationImpl migration = PsiMigrationManager.getInstance(getProject()).getCurrentMigration();
 
     final Condition<String> prefixMatcher = processor.getHint(JavaCompletionHints.NAME_FILTER);
 
index 52353c149abba9cbb483765c497ffc18a719df05..7a09bb3f7f61eddcb82f30d0f963bc63ef76515d 100644 (file)
@@ -17,10 +17,10 @@ package com.intellij.psi.impl.migration;
 
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.psi.JavaPsiFacade;
 import com.intellij.psi.PsiClass;
 import com.intellij.psi.PsiMigration;
 import com.intellij.psi.PsiPackage;
-import com.intellij.psi.impl.JavaPsiFacadeImpl;
 import com.intellij.psi.impl.PsiManagerImpl;
 import com.intellij.util.containers.HashMap;
 
@@ -33,7 +33,8 @@ import java.util.Map;
  */
 public class PsiMigrationImpl implements PsiMigration {
   private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.migration.PsiMigrationImpl");
-  private final JavaPsiFacadeImpl myFacade;
+  private final PsiMigrationManager myMigrationManager;
+  private final JavaPsiFacade myFacade;
   private final PsiManagerImpl myManager;
   private final Map<String, MigrationClassImpl> myQNameToClassMap = new HashMap<String, MigrationClassImpl>();
   private final Map<String, List<PsiClass>>  myPackageToClassesMap = new HashMap<String, List<PsiClass>>();
@@ -41,7 +42,8 @@ public class PsiMigrationImpl implements PsiMigration {
   private final Map<String, List<PsiPackage>>  myPackageToSubpackagesMap = new HashMap<String, List<PsiPackage>>();
   private boolean myIsValid = true;
 
-  public PsiMigrationImpl(JavaPsiFacadeImpl facade, PsiManagerImpl manager) {
+  public PsiMigrationImpl(PsiMigrationManager migrationManager, JavaPsiFacade facade, PsiManagerImpl manager) {
+    myMigrationManager = migrationManager;
     myFacade = facade;
     myManager = manager;
   }
@@ -59,7 +61,7 @@ public class PsiMigrationImpl implements PsiMigration {
     }
     List<PsiClass> psiClasses = getClassesList(packageName);
     psiClasses.add(migrationClass);
-    myFacade.migrationModified(false);
+    myMigrationManager.migrationModified(false);
     return migrationClass;
   }
 
@@ -87,7 +89,7 @@ public class PsiMigrationImpl implements PsiMigration {
     }
     List<PsiPackage> psiPackages = getSubpackagesList(parentName);
     psiPackages.add(migrationPackage);
-    myFacade.migrationModified(false);
+    myMigrationManager.migrationModified(false);
     return migrationPackage;
   }
 
@@ -98,7 +100,7 @@ public class PsiMigrationImpl implements PsiMigration {
     myPackageToClassesMap.clear();
     myPackageToSubpackagesMap.clear();
     myIsValid = false;
-    myFacade.migrationModified(true);
+    myMigrationManager.migrationModified(true);
   }
 
   private void assertValid() {
diff --git a/java/java-impl/src/com/intellij/psi/impl/migration/PsiMigrationManager.java b/java/java-impl/src/com/intellij/psi/impl/migration/PsiMigrationManager.java
new file mode 100644 (file)
index 0000000..5f24dca
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2000-2011 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.psi.impl.migration;
+
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.JavaPsiFacade;
+import com.intellij.psi.PsiManager;
+import com.intellij.psi.PsiMigration;
+import com.intellij.psi.impl.PsiManagerEx;
+import com.intellij.psi.impl.PsiManagerImpl;
+import org.jetbrains.annotations.NotNull;
+
+public class PsiMigrationManager {
+  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.migration.PsiMigrationManager");
+
+  public static PsiMigrationManager getInstance(Project project) {
+    return ServiceManager.getService(project, PsiMigrationManager.class);
+  }
+  
+  private final Project myProject;
+  private PsiMigrationImpl myCurrentMigration;
+
+  public PsiMigrationManager(Project project) {
+    myProject = project;
+  }
+
+  public PsiMigrationImpl getCurrentMigration() {
+    return myCurrentMigration;
+  }
+
+  /**
+   * Initiates a migrate refactoring. The refactoring is finished when
+   * {@link com.intellij.psi.PsiMigration#finish()} is called.
+   *
+   * @return the migrate operation object.
+   */
+  @NotNull
+  public PsiMigration startMigration() {
+    LOG.assertTrue(myCurrentMigration == null);
+    myCurrentMigration = new PsiMigrationImpl(this, JavaPsiFacade.getInstance(myProject),
+                                              (PsiManagerImpl)PsiManager.getInstance(myProject));
+    return myCurrentMigration;
+  }
+
+  public void migrationModified(boolean terminated) {
+    if (terminated) {
+      myCurrentMigration = null;
+    }
+
+    ((PsiManagerEx)PsiManager.getInstance(myProject)).beforeChange(true);
+  }
+}
index b24df105fa510c2068e59ace94887b7bc0030311..49a17a44ff45bcc8c325474a2a012a77305e500d 100644 (file)
@@ -22,10 +22,8 @@ import com.intellij.openapi.extensions.Extensions;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.Messages;
 import com.intellij.openapi.util.Ref;
-import com.intellij.psi.JavaPsiFacade;
-import com.intellij.psi.PsiElement;
-import com.intellij.psi.PsiManager;
 import com.intellij.psi.PsiMigration;
+import com.intellij.psi.impl.migration.PsiMigrationManager;
 import com.intellij.refactoring.BaseRefactoringProcessor;
 import com.intellij.refactoring.RefactoringBundle;
 import com.intellij.refactoring.RefactoringHelper;
@@ -47,27 +45,27 @@ class MigrationProcessor extends BaseRefactoringProcessor {
   public MigrationProcessor(Project project, MigrationMap migrationMap) {
     super(project);
     myMigrationMap = migrationMap;
-    myPsiMigration = startMigration(PsiManager.getInstance(project));
+    myPsiMigration = startMigration(project);
   }
 
   protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) {
     return new MigrationUsagesViewDescriptor(myMigrationMap, false);
   }
 
-  private PsiMigration startMigration(final PsiManager psiManager) {
-    final PsiMigration migration = JavaPsiFacade.getInstance(psiManager.getProject()).startMigration();
-    findOrCreateEntries(psiManager, migration);
+  private PsiMigration startMigration(Project project) {
+    final PsiMigration migration = PsiMigrationManager.getInstance(project).startMigration();
+    findOrCreateEntries(project, migration);
     return migration;
   }
 
-  private void findOrCreateEntries(final PsiManager psiManager, final PsiMigration migration) {
+  private void findOrCreateEntries(Project project, final PsiMigration migration) {
     for (int i = 0; i < myMigrationMap.getEntryCount(); i++) {
       MigrationMapEntry entry = myMigrationMap.getEntryAt(i);
       if (entry.getType() == MigrationMapEntry.PACKAGE) {
-        MigrationUtil.findOrCreatePackage(psiManager, migration, entry.getOldName());
+        MigrationUtil.findOrCreatePackage(project, migration, entry.getOldName());
       }
       else {
-        MigrationUtil.findOrCreateClass(psiManager, migration, entry.getOldName());
+        MigrationUtil.findOrCreateClass(project, migration, entry.getOldName());
       }
     }
   }
@@ -75,7 +73,6 @@ class MigrationProcessor extends BaseRefactoringProcessor {
   @NotNull
   protected UsageInfo[] findUsages() {
     ArrayList<UsageInfo> usagesVector = new ArrayList<UsageInfo>();
-    PsiManager psiManager = PsiManager.getInstance(myProject);
     try {
       if (myMigrationMap == null) {
         return UsageInfo.EMPTY_ARRAY;
@@ -84,10 +81,10 @@ class MigrationProcessor extends BaseRefactoringProcessor {
         MigrationMapEntry entry = myMigrationMap.getEntryAt(i);
         UsageInfo[] usages;
         if (entry.getType() == MigrationMapEntry.PACKAGE) {
-          usages = MigrationUtil.findPackageUsages(psiManager, myPsiMigration, entry.getOldName());
+          usages = MigrationUtil.findPackageUsages(myProject, myPsiMigration, entry.getOldName());
         }
         else {
-          usages = MigrationUtil.findClassUsages(psiManager, myPsiMigration, entry.getOldName());
+          usages = MigrationUtil.findClassUsages(myProject, myPsiMigration, entry.getOldName());
         }
 
         for (UsageInfo usage : usages) {
@@ -112,18 +109,17 @@ class MigrationProcessor extends BaseRefactoringProcessor {
   }
 
   protected void performRefactoring(UsageInfo[] usages) {
-    PsiManager psiManager = PsiManager.getInstance(myProject);
-    final PsiMigration psiMigration = JavaPsiFacade.getInstance(psiManager.getProject()).startMigration();
+    final PsiMigration psiMigration = PsiMigrationManager.getInstance(myProject).startMigration();
     LocalHistoryAction a = LocalHistory.getInstance().startAction(getCommandName());
 
     try {
       for (int i = 0; i < myMigrationMap.getEntryCount(); i++) {
         MigrationMapEntry entry = myMigrationMap.getEntryAt(i);
         if (entry.getType() == MigrationMapEntry.PACKAGE) {
-          MigrationUtil.doPackageMigration(psiManager, psiMigration, entry.getNewName(), usages);
+          MigrationUtil.doPackageMigration(myProject, psiMigration, entry.getNewName(), usages);
         }
         if (entry.getType() == MigrationMapEntry.CLASS) {
-          MigrationUtil.doClassMigration(psiManager, psiMigration, entry.getNewName(), usages);
+          MigrationUtil.doClassMigration(myProject, psiMigration, entry.getNewName(), usages);
         }
       }
 
index 89d3cb420954b891820b59046d5f69031b8f0a4a..89ec41278336a5285caebd174924344072dd4279 100644 (file)
@@ -17,6 +17,7 @@ package com.intellij.refactoring.migration;
 
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Computable;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.psi.*;
@@ -34,15 +35,15 @@ public class MigrationUtil {
   private MigrationUtil() {
   }
 
-  public static UsageInfo[] findPackageUsages(PsiManager manager, PsiMigration migration, String qName) {
-    PsiPackage aPackage = findOrCreatePackage(manager, migration, qName);
+  public static UsageInfo[] findPackageUsages(Project project, PsiMigration migration, String qName) {
+    PsiPackage aPackage = findOrCreatePackage(project, migration, qName);
 
-    return findRefs(manager, aPackage);
+    return findRefs(project, aPackage);
   }
 
-  public static void doPackageMigration(PsiManager manager, PsiMigration migration, String newQName, UsageInfo[] usages) {
+  public static void doPackageMigration(Project project, PsiMigration migration, String newQName, UsageInfo[] usages) {
     try {
-      PsiPackage aPackage = findOrCreatePackage(manager, migration, newQName);
+      PsiPackage aPackage = findOrCreatePackage(project, migration, newQName);
 
       // rename all references
       for (UsageInfo usage : usages) {
@@ -75,15 +76,15 @@ public class MigrationUtil {
     }
   }
 
-  public static UsageInfo[] findClassUsages(PsiManager manager, PsiMigration migration, String qName) {
-    PsiClass aClass = findOrCreateClass(manager, migration, qName);
+  public static UsageInfo[] findClassUsages(Project project, PsiMigration migration, String qName) {
+    PsiClass aClass = findOrCreateClass(project, migration, qName);
 
-    return findRefs(manager, aClass);
+    return findRefs(project, aClass);
   }
 
-  private static UsageInfo[] findRefs(final PsiManager manager, final PsiElement aClass) {
+  private static UsageInfo[] findRefs(final Project project, final PsiElement aClass) {
     final ArrayList<UsageInfo> results = new ArrayList<UsageInfo>();
-    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject());
+    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(project);
     for (PsiReference usage : ReferencesSearch.search(aClass, projectScope, false)) {
       results.add(new UsageInfo(usage));
     }
@@ -91,9 +92,9 @@ public class MigrationUtil {
     return results.toArray(new UsageInfo[results.size()]);
   }
 
-  public static void doClassMigration(PsiManager manager, PsiMigration migration, String newQName, UsageInfo[] usages) {
+  public static void doClassMigration(Project project, PsiMigration migration, String newQName, UsageInfo[] usages) {
     try {
-      PsiClass aClass = findOrCreateClass(manager, migration, newQName);
+      PsiClass aClass = findOrCreateClass(project, migration, newQName);
 
       // rename all references
       for (UsageInfo usage : usages) {
@@ -114,8 +115,8 @@ public class MigrationUtil {
     }
   }
 
-  static PsiPackage findOrCreatePackage(PsiManager manager, final PsiMigration migration, final String qName) {
-    PsiPackage aPackage = JavaPsiFacade.getInstance(manager.getProject()).findPackage(qName);
+  static PsiPackage findOrCreatePackage(Project project, final PsiMigration migration, final String qName) {
+    PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(qName);
     if (aPackage != null) {
       return aPackage;
     }
@@ -128,8 +129,8 @@ public class MigrationUtil {
     }
   }
 
-  static PsiClass findOrCreateClass(PsiManager manager, final PsiMigration migration, final String qName) {
-    PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(qName, GlobalSearchScope.allScope(manager.getProject()));
+  static PsiClass findOrCreateClass(Project project, final PsiMigration migration, final String qName) {
+    PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(qName, GlobalSearchScope.allScope(project));
     if (aClass == null) {
       aClass = ApplicationManager.getApplication().runWriteAction(new Computable<PsiClass>() {
         public PsiClass compute() {
index ad43bff13031bfa21f6a53357567cb1f9f92c40a..b4313bebf5668776fbfab089bf3ae0c6b11794d5 100644 (file)
@@ -111,15 +111,6 @@ public abstract class JavaPsiFacade {
   @NotNull
   public abstract PsiResolveHelper getResolveHelper();
 
-  /**
-   * Initiates a migrate refactoring. The refactoring is finished when
-   * {@link com.intellij.psi.PsiMigration#finish()} is called.
-   *
-   * @return the migrate operation object.
-   */
-  @NotNull
-  public abstract PsiMigration startMigration();
-
   /**
    * Returns the name helper for the project, which can be used to validate
    * and parse Java identifiers.
index f6a53787d719160f3b8cd48f272bebf72b4acf99..ce512b1cff345b857ffddb120ce0e8971366ddcf 100644 (file)
                     serviceImplementation="com.intellij.psi.impl.JavaPsiImplementationHelperImpl"/>
     <projectService serviceInterface="com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade"
                     serviceImplementation="com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacadeImpl"/>
+    <projectService serviceInterface="com.intellij.psi.impl.migration.PsiMigrationManager"
+                    serviceImplementation="com.intellij.psi.impl.migration.PsiMigrationManager"/>
 
     <applicationService serviceInterface="com.intellij.psi.JavaDirectoryService"
                         serviceImplementation="com.intellij.psi.impl.file.JavaDirectoryServiceImpl"/>