ambiguous method calls: conflict resolver, tests

(IDEA-67919; IDEA-67863; IDEA-67920;
 IDEA-67831; IDEA-67833; IDEA-67674;
 IDEA-67587; IDEA-57646; IDEA-57407;
 IDEA-57279; IDEA-57296; IDEA-57297;
 IDEA-57316; IDEA-57331)
This commit is contained in:
anna
2012-04-06 11:55:43 +02:00
parent 4aaf383d47
commit dc5c191afc
16 changed files with 414 additions and 4 deletions
@@ -16,6 +16,8 @@
package com.intellij.psi.scope.conflictResolvers;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.JavaVersionService;
import com.intellij.openapi.util.Comparing;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
@@ -27,6 +29,7 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.*;
import gnu.trove.THashSet;
import gnu.trove.TIntArrayList;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Iterator;
@@ -412,8 +415,14 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
PsiType type1 = classSubstitutor1.substitute(methodSubstitutor1.substitute(types1[i]));
PsiType type2 = classSubstitutor2.substitute(methodSubstitutor2.substitute(types2[i]));
final Specifics specifics = type1 == null || type2 == null ? null : checkSubtyping(type1, type2, method1, method2);
if (specifics == null) continue;
Specifics specifics = type1 == null || type2 == null ? null : checkSubtyping(type1, type2, method1, method2);
if (specifics == null) {
if (Comparing.equal(type1, type2)) {
specifics = checkSubstitutorSpecific(method1, method2, classSubstitutor1, classSubstitutor2, types1[i], types2[i]);
}
if (specifics == null) continue;
}
switch (specifics) {
case FIRST:
if (isMoreSpecific == Specifics.SECOND) return Specifics.NEITHER;
@@ -447,14 +456,55 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
}
}
if (isMoreSpecific == null) {
if (typeParameters1.length < typeParameters2.length) return Specifics.FIRST;
if (typeParameters1.length > typeParameters2.length) return Specifics.SECOND;
if (!JavaVersionService.getInstance().isAtLeast(myArgumentsList, JavaSdkVersion.JDK_1_7) ||
!MethodSignatureUtil.areParametersErasureEqual(method1, method2)) {
if (typeParameters1.length < typeParameters2.length) return Specifics.FIRST;
if (typeParameters1.length > typeParameters2.length) return Specifics.SECOND;
}
return Specifics.NEITHER;
}
return isMoreSpecific;
}
@Nullable
private static Specifics checkSubstitutorSpecific(PsiMethod method1,
PsiMethod method2,
PsiSubstitutor classSubstitutor1,
PsiSubstitutor classSubstitutor2,
PsiType type1,
PsiType type2) {
final Map<PsiTypeParameter, PsiType> map1 = classSubstitutor1.getSubstitutionMap();
final Map<PsiTypeParameter, PsiType> map2 = classSubstitutor2.getSubstitutionMap();
if (map1.size() == 1 && map2.size() == 1) {
final PsiType t1 = map1.values().iterator().next();
final PsiType t2 = map2.values().iterator().next();
int d1 = t1 != null ? t1.getArrayDimensions() : 0;
int d2 = t2 != null ? t2.getArrayDimensions() : 0;
if (d1 > d2) {
return Specifics.SECOND;
}
else if (d2 > d1) {
return Specifics.FIRST;
}
else {
final PsiTypeParameter p1 = map1.keySet().iterator().next();
final PsiTypeParameter p2 = map2.keySet().iterator().next();
Specifics specifics = checkSubtyping(TypeConversionUtil.erasure(PsiSubstitutor.EMPTY.substitute(p1)),
TypeConversionUtil.erasure(PsiSubstitutor.EMPTY.substitute(p2)), method1, method2);
if (specifics != null) {
return specifics;
} else {
final PsiType ctype1 = classSubstitutor1.substitute(type1);
final PsiType ctype2 = classSubstitutor2.substitute(type2);
return checkSubtyping(ctype1, ctype2, method1, method2);
}
}
}
return null;
}
private PsiSubstitutor calculateMethodSubstitutor(final PsiTypeParameter[] typeParameters,
final PsiType[] types1,
final PsiType[] types2,
@@ -0,0 +1,21 @@
package pck;
import java.util.List;
import static pck.C.foo;
import static pck.D.foo;
class C {
static <T> void foo(List<? extends List<T>> x) { }
}
class D {
static <T> String foo(List<List<T>> x) { return null; }
}
public class Main {
public static void main(String[] args){
List<List<String>> x = null;
foo(x).toLowerCase();
}
}
@@ -0,0 +1,21 @@
package pck;
import java.util.List;
import static pck.C.foo;
import static pck.D.foo;
class C {
static <T> void foo(List<T> x) { }
}
class D {
static <T> String foo(List<T[]> x) { return null; }
}
public class Main {
public static void main(String[] args){
List<String[]> x = null;
foo(x).toLowerCase();
}
}
@@ -0,0 +1,16 @@
package pck;
interface A<T>
{
T foo();
}
interface B<T> extends A<T[]> { }
class C<T extends A<Object[]> & B<?>>
{
void foo(T x)
{
Object[] foo = x.foo();
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2012 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 pck;
interface A<T>
{
T foo();
}
interface B<T extends Cloneable> extends A<T> { }
class C<T extends A<?> & B<?>>
{
void bar(T x)
{
x.foo();
}
}
@@ -0,0 +1,28 @@
package pck;
interface IA<T> {
T a();
}
interface IB {
String a();
}
abstract class C implements IA<String>, IB {
{
a();
}
}
interface IAO {
Object a();
}
abstract class CO implements IAO, IB {
{
a();
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2012 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 pck;
interface IA<T> {
T a();
}
interface IB {
String a();
}
abstract class C implements IA<String>, IB {
{
a();
}
}
interface IAO {
Object a();
}
abstract class CO implements IAO, IB {
{
a();
}
}
@@ -0,0 +1,15 @@
package pck;
class A<T> {}
interface IA{
void foo(A<?> x);
}
interface IB{
<T> void foo(A<T> x);
}
class C {
<T extends IA & IB> void bar(T x, A<String> y){
x.foo<error descr="Ambiguous method call: both 'IA.foo(A<?>)' and 'IB.foo(A<String>)' match">(y)</error>;
}
}
@@ -0,0 +1,15 @@
package pck;
class A<T> {}
interface IA{
<T> void foo(A<A<T>> x);
}
interface IB{
<T> void foo(A<? super A<T>> x);
}
class C {
<T extends IB & IA> void bar(T x, A<A<String>> y){
x.foo(y);
}
}
@@ -0,0 +1,11 @@
package pck;
class B<K> {}
class A<K> extends B<K> {
void foo(A<A<String>> b){
bar(b);
}
<T> void bar(B<? extends A<?>> a){}
<T> void bar(A<? extends A<T>> a){}
}
@@ -0,0 +1,21 @@
package pck;
import java.util.List;
import static pck.C.foo;
import static pck.D.foo;
class C {
static <T> void foo(List<T> x) { }
}
class D {
static <T extends List<?>> String foo(List<T> x) { return null; }
}
public class Main {
public static void main(String[] args){
List<List<String>> x = null;
foo(x).toCharArray();
}
}
@@ -0,0 +1,16 @@
package pck;
import static pck.C.foo;
import static pck.D.foo;
public class C {
public static void foo(){}
}
class D {
public static <T> void foo(){}
}
class B {
{
foo<error descr="Ambiguous method call: both 'C.foo()' and 'D.foo()' match">()</error>;
}
}
@@ -0,0 +1,17 @@
package pck;
import static pck.D.foo;
import static pck.C.foo;
public class C {
public static <T> void foo(Comparable<T> x){}
}
class D {
public static void foo(Comparable<?> x){}
}
class B{
public static void bar(Comparable<?> x){
foo<error descr="Ambiguous method call: both 'D.foo(Comparable<?>)' and 'C.foo(Comparable<?>)' match">(x)</error>;
}
}
@@ -0,0 +1,24 @@
/*
* Copyright 2000-2012 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 pck;
abstract class A {
abstract void foo(Object... x);
abstract void foo(int... x);
{
foo<error descr="Ambiguous method call: both 'A.foo(Object...)' and 'A.foo(int...)' match">(1)</error>;
}
}
@@ -0,0 +1,9 @@
package pck;
abstract class A {
abstract <T> void foo(T... y);
abstract <T> void foo(T[]... y);
void bar(){
foo();
}
}
@@ -17,6 +17,9 @@ package com.intellij.codeInsight.daemon;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.defUse.DefUseInspection;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.JavaVersionService;
import com.intellij.openapi.projectRoots.JavaVersionServiceImpl;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
@@ -60,4 +63,73 @@ public class AdvHighlightingJdk7Test extends DaemonAnalyzerTestCase {
public void testStaticAndSingleImportConflict() throws Exception {
doTest(BASE_PATH + "staticImportConflict/UsageMixed.java", BASE_PATH + "/staticImportConflict", false, false);
}
//ambiguous method calls
private void doTestAmbiguous() throws Exception {
final String name = getTestName(true);
final JavaVersionServiceImpl versionService = (JavaVersionServiceImpl)JavaVersionService.getInstance();
try {
versionService.setTestVersion(JavaSdkVersion.JDK_1_7);
doTest(BASE_PATH + name + "/pck/AmbiguousMethodCall.java", BASE_PATH + "/" + name, false, false);
}
finally {
versionService.setTestVersion(null);
}
}
public void testAmbiguous() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousArrayInSubst() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousTypeParamExtends() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousTypeParamNmb() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousTypeParamNmb1() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousInheritance() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousInheritance1() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousVarargs() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousVarargs1() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousMultiIntInheritance() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousMultipleTypeParamExtends() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousMultipleTypeParamExtends1() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousMultipleTypeParamExtends2() throws Exception {
doTestAmbiguous();
}
public void testAmbiguousMultipleTypeParamExtends3() throws Exception {
doTestAmbiguous();
}
}