varargs: prefer object for primitive varargs only

This commit is contained in:
anna
2011-02-18 10:53:17 +01:00
parent 9a0f90591a
commit 90e0e831ee
9 changed files with 162 additions and 7 deletions
@@ -59,9 +59,6 @@ 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);
@@ -84,6 +81,9 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
checkSpecifics(conflicts, applicabilityLevel);
if (conflicts.size() == 1) return conflicts.get(0);
checkPrimitiveVarargs(conflicts, myActualParameterTypes.length);
if (conflicts.size() == 1) return conflicts.get(0);
THashSet<CandidateInfo> uniques = new THashSet<CandidateInfo>(conflicts);
if (uniques.size() == 1) return uniques.iterator().next();
return null;
@@ -463,8 +463,8 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
return substitutor;
}
public void checkVarargMethods(final List<CandidateInfo> conflicts,
final int argumentsCount) {
public static void checkPrimitiveVarargs(final List<CandidateInfo> conflicts,
final int argumentsCount) {
PsiMethod objectVararg = null;
for (CandidateInfo conflict : conflicts) {
final PsiMethod method = (PsiMethod)conflict.getElement();
@@ -483,8 +483,13 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
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();
if (method != objectVararg && method != null && method.isVarArgs()) {
final int paramsCount = method.getParameterList().getParametersCount();
final PsiType type = method.getParameterList().getParameters()[paramsCount - 1].getType();
final PsiType componentType = ((PsiArrayType)type).getComponentType();
if (argumentsCount == paramsCount - 1 && componentType instanceof PsiPrimitiveType) {
iterator.remove();
}
}
}
}
@@ -0,0 +1,8 @@
class XY {
public static void foo(Object... x) { }
public static void foo(Object a, Object... x) { }
public static void main(String[] args) {
<ref>foo("a");
}
}
@@ -0,0 +1,8 @@
class XY {
public static void foo(Object... x) { }
public static void foo(Object a, Object o, Object... x) { }
public static void main(String[] args) {
<ref>foo("a", "b");
}
}
@@ -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();
}
}
@@ -0,0 +1,9 @@
class XY {
public static void foo(String... x) { }
public static void foo(Object a, Object... x) { }
public static void main(String[] args) {
<ref>foo("a");
}
}
@@ -0,0 +1,8 @@
class XY {
public static void foo(String... x) { }
public static void foo(Object a, String... x) { }
public static void main(String[] args) {
<ref>foo("a");
}
}
@@ -0,0 +1,8 @@
class XY {
public static void foo(String... x) { }
public static void foo(String a, String... x) { }
public static void main(String[] args) {
<ref>foo("a");
}
}
@@ -0,0 +1,91 @@
/*
* Copyright 2000-2011 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.psi.resolve;
import com.intellij.psi.*;
import org.jetbrains.annotations.NonNls;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
public class ResolveVarargsMethodTest extends Resolve15TestCase {
public void testPrimitiveObject() throws Exception {
doTest(1);
}
public void testPrimitiveString() throws Exception {
doTest(2);
}
public void testObjectObject() throws Exception {
doTest(2);
}
//bug in javac; @see jdk7
public void testObjectObjectObject() throws Exception {
doTest(2);
}
public void testStringObject() throws Exception {
doTest(1);
}
public void testStringObjectString() throws Exception {
doTest(1);
}
public void testStringStringString() throws Exception {
doTest(2);
}
private void doTest(int resolved) 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();
PsiElement element = resolveResult.getElement();
if (resolved == 1) {
assertNotNull(element);
} else {
assertNull(element);
}
final JavaResolveResult[] candidates = refExpr.multiResolve(false);
assertEquals(resolved, candidates.length);
}
private PsiReference configureByFile() throws Exception {
return configureByFile("method/varargs/" + getTestName(false) + ".java");
}
private static PsiMethod checkResolvesUnique(final PsiReference ref) {
assertThat(ref, instanceOf(PsiReferenceExpression.class));
final PsiReferenceExpression refExpr = (PsiReferenceExpression)ref;
final PsiElement parent = refExpr.getParent();
assertThat(parent, instanceOf(PsiMethodCallExpression.class));
final PsiMethodCallExpression expression = (PsiMethodCallExpression)parent;
final PsiMethod method = expression.resolveMethod();
assertNotNull(method);
return method;
}
private static void assertResolvesToMethodInClass(JavaResolveResult result, @NonNls String name) {
PsiMethod method = (PsiMethod)result.getElement();
assertNotNull(method);
assertTrue(result.isValidResult());
assertEquals(name, method.getContainingClass().getName());
}
}