From a3bbe18b336872a1d0a7e6c2685da345adcb0837 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Thu, 8 Jun 2017 17:21:43 +0300 Subject: [PATCH] type migration: don't decompile code --- .../refactoring/typeMigration/TypeEvaluator.java | 16 ++++++++++++++-- .../typeMigration/TypeMigrationLabeler.java | 11 +++++++---- .../typeMigration/TypeMigrationProcessor.java | 2 +- .../JavaFxFieldToPropertyIntention.java | 4 ++-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeEvaluator.java b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeEvaluator.java index 7bbea4a2bca3..d7bccf5807ff 100644 --- a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeEvaluator.java +++ b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeEvaluator.java @@ -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> myTypeMap; private final TypeMigrationRules myRules; private final TypeMigrationLabeler myLabeler; + private final ProjectFileIndex myProjectFileIndex; - public TypeEvaluator(final LinkedList> types, final TypeMigrationLabeler labeler) { + public TypeEvaluator(LinkedList> 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> entry : myTypeMap.entrySet()) { if (Comparing.equal(element, entry.getKey().getElement())) { return entry.getValue().getFirst(); diff --git a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java index cf02b871b77e..3cfb1c5412b7 100644 --- a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java +++ b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java @@ -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, Set> myRootUsagesTree = new HashMap<>(); private final Set 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 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) { diff --git a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationProcessor.java b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationProcessor.java index 80a20cf336de..8021fdd4b50b 100644 --- a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationProcessor.java @@ -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); diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/codeInsight/JavaFxFieldToPropertyIntention.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/codeInsight/JavaFxFieldToPropertyIntention.java index cc7b27e566c3..e92d389de7c9 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/codeInsight/JavaFxFieldToPropertyIntention.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/codeInsight/JavaFxFieldToPropertyIntention.java @@ -122,7 +122,7 @@ public class JavaFxFieldToPropertyIntention extends PsiElementBaseIntentionActio final TypeMigrationRules rules = new TypeMigrationRules(); final Set virtualFiles = ContainerUtil.map2SetNotNull(myFiles, PsiFile::getVirtualFile); rules.setBoundScope(GlobalSearchScope.filesScope(myProject, virtualFiles)); - final TypeMigrationLabeler labeler = new TypeMigrationLabeler(rules, toType); + final TypeMigrationLabeler labeler = new TypeMigrationLabeler(rules, toType, myProject); labeler.getMigratedUsages(false, field); for (PsiReference reference : myReferences) { @@ -132,7 +132,7 @@ public class JavaFxFieldToPropertyIntention extends PsiElementBaseIntentionActio final TypeConversionDescriptor conversion = myProperty.myObservableType.findDirectConversion(expression, toType, fromType); if (conversion != null) { - TypeMigrationReplacementUtil.replaceExpression(expression, myProject, conversion, new TypeEvaluator(null, null)); + TypeMigrationReplacementUtil.replaceExpression(expression, myProject, conversion, new TypeEvaluator(null, null, myProject)); } } }