type migration: don't decompile code

This commit is contained in:
Dmitry Batkovich
2017-06-08 17:46:51 +03:00
parent 67e0ef0d39
commit a3bbe18b33
4 changed files with 24 additions and 9 deletions
@@ -16,9 +16,13 @@
package com.intellij.refactoring.typeMigration;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.tree.IElementType;
@@ -27,7 +31,6 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo;
import com.intellij.util.Function;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -46,8 +49,11 @@ public class TypeEvaluator {
private final HashMap<TypeMigrationUsageInfo, LinkedList<PsiType>> myTypeMap;
private final TypeMigrationRules myRules;
private final TypeMigrationLabeler myLabeler;
private final ProjectFileIndex myProjectFileIndex;
public TypeEvaluator(final LinkedList<Pair<TypeMigrationUsageInfo, PsiType>> types, final TypeMigrationLabeler labeler) {
public TypeEvaluator(LinkedList<Pair<TypeMigrationUsageInfo, PsiType>> types,
TypeMigrationLabeler labeler,
Project project) {
myLabeler = labeler;
myRules = labeler == null ? new TypeMigrationRules() : labeler.getRules();
myTypeMap = new HashMap<>();
@@ -62,6 +68,7 @@ public class TypeEvaluator {
}
}
myProjectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
}
public boolean setType(final TypeMigrationUsageInfo usageInfo, @NotNull PsiType type) {
@@ -99,6 +106,11 @@ public class TypeEvaluator {
@Nullable
public PsiType getType(PsiElement element) {
VirtualFile file = element.getContainingFile().getVirtualFile();
if (file == null || !myProjectFileIndex.isInContent(file)) {
return TypeMigrationLabeler.getElementType(element);
}
for (Map.Entry<TypeMigrationUsageInfo, LinkedList<PsiType>> entry : myTypeMap.entrySet()) {
if (Comparing.equal(element, entry.getKey().getElement())) {
return entry.getValue().getFirst();
@@ -67,6 +67,7 @@ public class TypeMigrationLabeler {
private boolean myShowWarning = true;
private volatile MigrateException myException;
private final Semaphore myDialogSemaphore = new Semaphore();
private final Project myProject;
public TypeMigrationRules getRules() {
return myRules;
@@ -90,13 +91,14 @@ public class TypeMigrationLabeler {
private final Map<Pair<TypeMigrationUsageInfo, TypeMigrationUsageInfo>, Set<PsiElement>> myRootUsagesTree = new HashMap<>();
private final Set<TypeMigrationUsageInfo> myProcessedRoots = new HashSet<>();
public TypeMigrationLabeler(TypeMigrationRules rules, PsiType rootType) {
this(rules, Functions.constant(rootType), null);
public TypeMigrationLabeler(TypeMigrationRules rules, PsiType rootType, Project project) {
this(rules, Functions.constant(rootType), null, project);
}
public TypeMigrationLabeler(TypeMigrationRules rules,
Function<PsiElement, PsiType> migrationRootTypeFunction,
@Nullable("any root accepted if null") PsiElement[] allowedRoots) {
@Nullable("any root accepted if null") PsiElement[] allowedRoots,
Project project) {
myRules = rules;
myMigrationRootTypeFunction = migrationRootTypeFunction;
myAllowedRoots = allowedRoots == null ? null : ContainerUtil.set(allowedRoots);
@@ -105,6 +107,7 @@ public class TypeMigrationLabeler {
myFailedConversions = new LinkedHashMap<>();
myNewExpressionTypeChange = new LinkedHashMap<>();
myClassTypeArgumentsChange = new LinkedHashMap<>();
myProject = project;
}
public boolean hasFailedConversions() {
@@ -1068,7 +1071,7 @@ public class TypeMigrationLabeler {
private void migrate(boolean autoMigrate, final PsiElement... victims) {
myMigrationRoots = new LinkedList<>();
myTypeEvaluator = new TypeEvaluator(myMigrationRoots, this);
myTypeEvaluator = new TypeEvaluator(myMigrationRoots, this, myProject);
for (PsiElement victim : victims) {
@@ -226,7 +226,7 @@ public class TypeMigrationProcessor extends BaseRefactoringProcessor {
@NotNull
@Override
public UsageInfo[] findUsages() {
myLabeler = new TypeMigrationLabeler(myRules, myRootTypes, myAllowDependentRoots ? null : myRoots);
myLabeler = new TypeMigrationLabeler(myRules, myRootTypes, myAllowDependentRoots ? null : myRoots, myProject);
try {
return myLabeler.getMigratedUsages(!isPreviewUsages(), myRoots);