mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
varargs: prefer Object... (IDEA-55412)
This commit is contained in:
+32
@@ -16,11 +16,13 @@
|
||||
package com.intellij.psi.scope.conflictResolvers;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.infos.CandidateInfo;
|
||||
import com.intellij.psi.infos.MethodCandidateInfo;
|
||||
import com.intellij.psi.scope.PsiConflictResolver;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.*;
|
||||
import gnu.trove.THashSet;
|
||||
import gnu.trove.TIntArrayList;
|
||||
@@ -57,6 +59,9 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
|
||||
if (conflicts.isEmpty()) return null;
|
||||
if (conflicts.size() == 1) return conflicts.get(0);
|
||||
|
||||
checkVarargMethods(conflicts, myActualParameterTypes.length);
|
||||
if (conflicts.size() == 1) return conflicts.get(0);
|
||||
|
||||
boolean atLeastOneMatch = checkParametersNumber(conflicts, myActualParameterTypes.length, true);
|
||||
if (conflicts.size() == 1) return conflicts.get(0);
|
||||
|
||||
@@ -457,4 +462,31 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
|
||||
}
|
||||
return substitutor;
|
||||
}
|
||||
|
||||
public void checkVarargMethods(final List<CandidateInfo> conflicts,
|
||||
final int argumentsCount) {
|
||||
PsiMethod objectVararg = null;
|
||||
for (CandidateInfo conflict : conflicts) {
|
||||
final PsiMethod method = (PsiMethod)conflict.getElement();
|
||||
final int parametersCount = method.getParameterList().getParametersCount();
|
||||
if (method.isVarArgs() && parametersCount - 1 == argumentsCount) {
|
||||
final PsiType type = method.getParameterList().getParameters()[parametersCount - 1].getType();
|
||||
final PsiType componentType = ((PsiArrayType)type).getComponentType();
|
||||
final PsiClassType classType = PsiType.getJavaLangObject(method.getManager(), GlobalSearchScope.allScope(method.getProject()));
|
||||
if (Comparing.equal(componentType, classType)) {
|
||||
objectVararg = method;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (objectVararg != null) {
|
||||
for (Iterator<CandidateInfo> iterator = conflicts.iterator(); iterator.hasNext(); ) {
|
||||
CandidateInfo conflict = iterator.next();
|
||||
PsiMethod method = (PsiMethod)conflict.getElement();
|
||||
if (method != objectVararg && method.isVarArgs() && method.getParameterList().getParametersCount() - 1 == argumentsCount) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class XY {
|
||||
private void f(Object... bs) {}
|
||||
|
||||
private void f(int... is) {}
|
||||
|
||||
public static void main(String[] args) {
|
||||
<ref>f();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class XY {
|
||||
private void f(String... bs) {}
|
||||
|
||||
private void f(int... is) {}
|
||||
|
||||
public static void main(String[] args) {
|
||||
<ref>f();
|
||||
}
|
||||
}
|
||||
@@ -249,6 +249,32 @@ public class ResolveMethod15Test extends Resolve15TestCase {
|
||||
assertEquals(2, candidates.length);
|
||||
}
|
||||
|
||||
public void testFilterVarargsVsVarargs3() throws Exception {
|
||||
final PsiReference ref = configureByFile();
|
||||
assertThat(ref, instanceOf(PsiReferenceExpression.class));
|
||||
final PsiReferenceExpression refExpr = (PsiReferenceExpression)ref;
|
||||
PsiCallExpression call = (PsiCallExpression) refExpr.getParent();
|
||||
JavaResolveResult resolveResult = call.resolveMethodGenerics();
|
||||
assertNotNull(resolveResult.getElement());
|
||||
assertFalse(resolveResult.isValidResult());
|
||||
|
||||
final JavaResolveResult[] candidates = refExpr.multiResolve(false);
|
||||
assertEquals(1, candidates.length);
|
||||
}
|
||||
|
||||
public void testFilterVarargsVsVarargs4() throws Exception {
|
||||
final PsiReference ref = configureByFile();
|
||||
assertThat(ref, instanceOf(PsiReferenceExpression.class));
|
||||
final PsiReferenceExpression refExpr = (PsiReferenceExpression)ref;
|
||||
PsiCallExpression call = (PsiCallExpression) refExpr.getParent();
|
||||
JavaResolveResult resolveResult = call.resolveMethodGenerics();
|
||||
assertNull(resolveResult.getElement());
|
||||
assertFalse(resolveResult.isValidResult());
|
||||
|
||||
final JavaResolveResult[] candidates = refExpr.multiResolve(false);
|
||||
assertEquals(2, candidates.length);
|
||||
}
|
||||
|
||||
//IDEADEV-3313
|
||||
public void testCovariantReturnTypes() throws Exception {
|
||||
final PsiReference ref = configureByFile();
|
||||
|
||||
Reference in New Issue
Block a user