mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
guava type migration: quick fix is shown for arrays (IDEA-147662)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import java.util.Optional;
|
||||
|
||||
public class SubOrder {
|
||||
public void useArray(Optional<String>[] pa) {
|
||||
Optional<String> v = pa[0];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user