Add temporary hack for wildcards in Collections

By default we would render 'MutableCollection<String>.addAll(Collection<String>)' as
'(LCollection<String>;)' (without wildcard) because String is final and
effectively it's the same as '(LCollection<? extends String>;)'.

But that's wrong signature in a sense that java.util.Collection has different
signature: '(LCollection<? extends E>)'.

Actually the problem is much wider than collections,
it concerns any Java code that uses Kotlin classes with covariant
parameters without '? extends E' wildcards.

Temporary solution is just to hardcode/enumerate builtin methods
with special signature.
This commit is contained in:
Denis Zharkov
2015-11-24 13:42:46 +03:00
parent 0255be7deb
commit 20cbceb56d
9 changed files with 124 additions and 39 deletions

View File

@@ -0,0 +1,13 @@
import java.util.*;
public class J {
private static class MyList<E> extends KList<E> {}
public static String foo() {
Collection<String> collection = new MyList<String>();
if (!collection.contains("ABCDE")) return "fail 1";
if (!collection.containsAll(Arrays.asList(1, 2, 3))) return "fail 2";
return "OK";
}
}

View File

@@ -0,0 +1,40 @@
open class KList<E> : List<E> {
override val size: Int
get() = throw UnsupportedOperationException()
override fun isEmpty(): Boolean {
throw UnsupportedOperationException()
}
override fun contains(o: E) = true
override fun containsAll(c: Collection<E>) = true
override fun iterator(): Iterator<E> {
throw UnsupportedOperationException()
}
override fun get(index: Int): E {
throw UnsupportedOperationException()
}
override fun indexOf(element: E): Int {
throw UnsupportedOperationException()
}
override fun lastIndexOf(element: E): Int {
throw UnsupportedOperationException()
}
override fun listIterator(): ListIterator<E> {
throw UnsupportedOperationException()
}
override fun listIterator(index: Int): ListIterator<E> {
throw UnsupportedOperationException()
}
override fun subList(fromIndex: Int, toIndex: Int): List<E> {
throw UnsupportedOperationException()
}
}
fun box() = J.foo()

View File

@@ -1,4 +1,4 @@
open class KList : MutableList<String> {
abstract class KList : MutableList<String> {
override val size: Int
get() = throw UnsupportedOperationException()