guava type migration: quick fix is shown for arrays (IDEA-147662)

This commit is contained in:
Dmitry Batkovich
2015-11-12 19:03:48 +03:00
parent 151339e77a
commit 884bfbaa6b
4 changed files with 28 additions and 8 deletions

View File

@@ -118,7 +118,7 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
public void visitVariable(PsiVariable variable) {
if (!checkVariables) return;
final PsiType type = variable.getType();
final PsiClassType targetType = getConversionClassType(type);
PsiType targetType = getConversionClassType(type);
if (targetType != null) {
holder.registerProblem(variable,
PROBLEM_DESCRIPTION_FOR_VARIABLE,
@@ -130,7 +130,7 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
public void visitMethod(PsiMethod method) {
super.visitMethod(method);
if (!checkReturnTypes) return;
final PsiClassType targetType = getConversionClassType(method.getReturnType());
final PsiType targetType = getConversionClassType(method.getReturnType());
if (targetType != null) {
final PsiTypeElement typeElement = method.getReturnTypeElement();
if (typeElement != null) {
@@ -172,15 +172,17 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
holder.registerProblem(chain, PROBLEM_DESCRIPTION_FOR_METHOD_CHAIN, new MigrateFluentIterableChainQuickFix(chain, initialType, targetType));
}
private PsiClassType getConversionClassType(PsiType initialType) {
if (initialType instanceof PsiClassType) {
final PsiClassType.ClassResolveResult resolveResult = ((PsiClassType)initialType).resolveGenerics();
private PsiType getConversionClassType(PsiType initialType) {
final PsiType type = initialType.getDeepComponentType();
if (type instanceof PsiClassType) {
final PsiClassType.ClassResolveResult resolveResult = ((PsiClassType)type).resolveGenerics();
final PsiClass psiClass = resolveResult.getElement();
if (psiClass != null) {
final String qName = psiClass.getQualifiedName();
final PsiClass targetClass = myGuavaClassConversions.getValue().get(qName);
if (targetClass != null) {
return addTypeParameters(initialType, resolveResult, targetClass);
final PsiClassType createdType = addTypeParameters(type, resolveResult, targetClass);
return initialType instanceof PsiArrayType ? new PsiArrayType(createdType) : createdType;
}
}
}
@@ -220,6 +222,7 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
}
}
@NotNull
private PsiClassType addTypeParameters(PsiType currentType, PsiClassType.ClassResolveResult currentTypeResolveResult, PsiClass targetClass) {
final Map<PsiTypeParameter, PsiType> substitutionMap = currentTypeResolveResult.getSubstitutor().getSubstitutionMap();
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(holder.getProject());
@@ -330,9 +333,9 @@ public class GuavaInspection extends BaseJavaLocalInspectionTool {
public static class MigrateMethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement {
private final PsiClassType myTargetType;
private final PsiType myTargetType;
private MigrateMethodReturnTypeFix(@NotNull PsiMethod method, PsiClassType targetType) {
private MigrateMethodReturnTypeFix(@NotNull PsiMethod method, PsiType targetType) {
super(method);
myTargetType = targetType;
}

View File

@@ -202,6 +202,10 @@ public class GuavaInspectionTest extends JavaCodeInsightFixtureTestCase {
doTest();
}
public void testMigrateArrays() {
doTest();
}
private void doTestNoQuickFixes(final Class<? extends IntentionAction>... quickFixesClasses) {
myFixture.configureByFile(getTestName(true) + ".java");
myFixture.enableInspections(new GuavaInspection());

View File

@@ -0,0 +1,6 @@
import com.google.common.base.Optional;
public class SubOrder {
public void useArray(Opti<caret>onal<String>[] pa) {
Optional<String> v = pa[0];
}
}

View File

@@ -0,0 +1,7 @@
import java.util.Optional;
public class SubOrder {
public void useArray(Optional<String>[] pa) {
Optional<String> v = pa[0];
}
}