raw type to parameterized: warn about conflicts (IDEA-173770)

This commit is contained in:
Anna Kozlova
2017-06-06 14:54:07 +03:00
parent 6487bb6913
commit a19a52bf49
4 changed files with 52 additions and 13 deletions
@@ -0,0 +1,128 @@
/*
* Copyright 2000-2017 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.codeInspection.miscGenerics;
import com.intellij.codeInspection.*;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.PsiSearchHelper;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.typeMigration.TypeMigrationProcessor;
import com.intellij.refactoring.typeMigration.TypeMigrationRules;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author dsl
*/
public class RawTypeCanBeGenericInspection extends BaseJavaBatchLocalInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder,
boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitVariable(PsiVariable variable) {
super.visitVariable(variable);
final PsiTypeElement variableTypeElement = variable.getTypeElement();
if (variableTypeElement != null) {
final PsiType type = getSuggestedType(variable);
if (type != null) {
final String typeText = type.getPresentableText();
final String message =
InspectionsBundle.message("inspection.raw.variable.type.can.be.generic.quickfix", variable.getName(), typeText);
final PsiElement beforeInitializer =
PsiTreeUtil.skipSiblingsBackward(variable.getInitializer(), PsiWhiteSpace.class, PsiComment.class);
final ProblemDescriptor descriptor =
holder.getManager().createProblemDescriptor(variableTypeElement,
beforeInitializer != null ? beforeInitializer : variableTypeElement,
message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOnTheFly, new MyLocalQuickFix(message));
holder.registerProblem(descriptor);
}
}
}
};
}
@Nullable
private static PsiType getSuggestedType(@NotNull PsiVariable variable) {
final PsiExpression initializer = variable.getInitializer();
if (initializer == null) return null;
final PsiType variableType = variable.getType();
final PsiType initializerType = initializer.getType();
if (!(variableType instanceof PsiClassType)) return null;
final PsiClassType variableClassType = (PsiClassType) variableType;
if (!variableClassType.isRaw()) return null;
if (!(initializerType instanceof PsiClassType)) return null;
final PsiClassType initializerClassType = (PsiClassType) initializerType;
if (initializerClassType.isRaw()) return null;
final PsiClassType.ClassResolveResult variableResolveResult = variableClassType.resolveGenerics();
final PsiClassType.ClassResolveResult initializerResolveResult = initializerClassType.resolveGenerics();
if (initializerResolveResult.getElement() == null) return null;
PsiClass variableResolved = variableResolveResult.getElement();
if (variableResolved == null) return null;
PsiSubstitutor targetSubstitutor = TypeConversionUtil.getClassSubstitutor(variableResolved, initializerResolveResult.getElement(), initializerResolveResult.getSubstitutor());
if (targetSubstitutor == null) return null;
PsiType type = JavaPsiFacade.getInstance(variable.getProject()).getElementFactory().createType(variableResolved, targetSubstitutor);
if (variableType.equals(type)) return null;
return type;
}
private static class MyLocalQuickFix implements LocalQuickFix {
private String myName;
public MyLocalQuickFix(@NotNull String name) {
myName = name;
}
@Nls
@NotNull
@Override
public String getName() {
return myName;
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return InspectionsBundle.message("inspection.raw.variable.type.can.be.generic.family.quickfix");
}
@Override
public boolean startInWriteAction() {
return false;
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getStartElement().getParent();
if (element instanceof PsiVariable) {
final PsiVariable variable = (PsiVariable)element;
final PsiType type = getSuggestedType(variable);
if (type != null) {
final TypeMigrationRules rules = new TypeMigrationRules();
rules.setBoundScope(PsiSearchHelper.SERVICE.getInstance(project).getUseScope(variable));
TypeMigrationProcessor.runHighlightingTypeMigration(project, null, rules, variable, type, false);
}
}
}
}
}
@@ -16,7 +16,6 @@
package com.intellij.refactoring.typeMigration;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
@@ -35,12 +34,18 @@ import com.intellij.ui.content.Content;
import com.intellij.usageView.UsageInfo;
import com.intellij.usageView.UsageViewDescriptor;
import com.intellij.usageView.UsageViewManager;
import com.intellij.util.*;
import com.intellij.util.containers.*;
import com.intellij.util.Function;
import com.intellij.util.Functions;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import static com.intellij.util.ObjectUtils.assertNotNull;
@@ -137,7 +142,7 @@ public class TypeMigrationProcessor extends BaseRefactoringProcessor {
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
if (hasFailedConversions()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new RuntimeException(StringUtil.join(myLabeler.getFailedConversionsReport(), "\n"));
throw new BaseRefactoringProcessor.ConflictsInTestsException(Arrays.asList(myLabeler.getFailedConversionsReport()));
}
FailedConversionsDialog dialog = new FailedConversionsDialog(myLabeler.getFailedConversionsReport(), myProject);
if (!dialog.showAndGet()) {