try to infer types before check for applicability (IDEA-68986)

(cherry picked from commit d8bbd5c1b70135cd1eee5215dd2bffaf6d16e905)
This commit is contained in:
anna
2011-12-05 21:15:36 +01:00
parent dbd9aa0bda
commit 99f4b3a821
3 changed files with 67 additions and 1 deletions

View File

@@ -32,6 +32,7 @@ import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.resolve.DefaultParameterTypeInferencePolicy;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.util.PsiFormatUtil;
@@ -104,6 +105,7 @@ public class StaticImportMethodFix implements IntentionAction {
GlobalSearchScope scope = element.getResolveScope();
PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20);
List<PsiMethod> applicableList = new ArrayList<PsiMethod>();
final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(element.getProject()).getResolveHelper();
for (PsiMethod method : methods) {
ProgressManager.checkCanceled();
if (JavaCompletionUtil.isInExcludedPackage(method)) continue;
@@ -114,7 +116,10 @@ public class StaticImportMethodFix implements IntentionAction {
&& ((PsiJavaFile)file).getPackageName().length() != 0
&& PsiUtil.isAccessible(method, element, method.getContainingClass())) {
list.add(method);
if (PsiUtil.isApplicable(method, PsiSubstitutor.EMPTY, argumentList)) {
PsiSubstitutor substitutorForMethod = resolveHelper
.inferTypeArguments(method.getTypeParameters(), method.getParameterList().getParameters(), argumentList.getExpressions(),
PsiSubstitutor.EMPTY, element.getParent(), DefaultParameterTypeInferencePolicy.INSTANCE);
if (PsiUtil.isApplicable(method, substitutorForMethod, argumentList)) {
applicableList.add(method);
}
}
@@ -192,6 +197,10 @@ public class StaticImportMethodFix implements IntentionAction {
}
private void chooseAndImport(Editor editor, final Project project) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
doImport(candidates.get(0));
return;
}
final BaseListPopupStep<PsiMethod> step =
new BaseListPopupStep<PsiMethod>(QuickFixBundle.message("class.to.import.chooser.title"), candidates) {

View File

@@ -0,0 +1,30 @@
// "Static Import Method..." "true"
package p;
import static p.FFF.myEqualTo;
public class X {
public void test() throws Exception {
assertMe("", myEqualTo(""));
}
<V> void assertMe(V v, M<V> m) {
}
}
class M<T> {
}
class FFF {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}
class LLL {
public static M<String> myEqualTo(String string) {
return null;
}
}

View File

@@ -0,0 +1,27 @@
// "Static Import Method..." "true"
package p;
public class X {
public void test() throws Exception {
assertMe("", my<caret>EqualTo(""));
}
<V> void assertMe(V v, M<V> m) {
}
}
class M<T> {
}
class FFF {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}
class LLL {
public static M<String> myEqualTo(String string) {
return null;
}
}