type migration: recreate type if invalid in variables migration as it was before

This commit is contained in:
Dmitry Batkovich
2016-07-18 10:22:05 +03:00
parent ea513be421
commit 6fb4fc6b7c
2 changed files with 18 additions and 5 deletions
@@ -366,8 +366,15 @@ public class TypeMigrationLabeler {
LOG.assertTrue(migrationTypeAndVariables != null);
final PsiVariable[] variables = PsiTreeUtil.getChildrenOfType(typeElement.getParent().getParent(), PsiVariable.class);
if (variables != null && variables.length == migrationTypeAndVariables.getValue().size()) {
typeElement
.replace(JavaPsiFacade.getElementFactory(variables[0].getProject()).createTypeElement(migrationTypeAndVariables.getKey()));
try {
PsiType migrationType = migrationTypeAndVariables.getKey();
final Project project = variables[0].getProject();
migrationType = TypeMigrationReplacementUtil.revalidateType(migrationType, project);
typeElement.replace(JavaPsiFacade.getElementFactory(project).createTypeElement(migrationType));
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
continue;
}
}
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.source.tree.CompositeElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
@@ -114,11 +115,16 @@ public class TypeMigrationReplacementUtil {
return expression;
}
static PsiType revalidateType(@NotNull PsiType migrationType, @NotNull Project project) {
if (!migrationType.isValid()) {
migrationType = JavaPsiFacade.getElementFactory(project).createTypeByFQClassName(migrationType.getCanonicalText());
}
return migrationType;
}
static void migrateMemberOrVariableType(final PsiElement element, final Project project, PsiType migratedType) {
try {
if (!migratedType.isValid()) {
migratedType = JavaPsiFacade.getElementFactory(project).createTypeByFQClassName(migratedType.getCanonicalText());
}
migratedType = revalidateType(migratedType, project);
final PsiTypeElement typeElement = JavaPsiFacade.getInstance(project).getElementFactory().createTypeElement(migratedType);
if (element instanceof PsiMethod) {
final PsiTypeElement returnTypeElement = ((PsiMethod)element).getReturnTypeElement();