Use wrappers around java.util.* to emulate kotlin.collection.* behaviour

Backend: If kotlin class extends kotlin.collection.List
    write it as it's super interface (light class mode only)
IDE: Provide wrapper classes to java resolve
    that try to emulate backend behaviour

For example if kotlin class implements kotlin.collections.Map,
    we provide a superinterface that has abstract 'getEntries' method
    and 'entrySet' method that is considered default.
In reality all those methods are generated in the class itself.

In IDE supporting this case without hacks is not feasible performance-wise
    since kotlin.collection.* may not be an immediate supertype and we need
    to compute all supertypes just to calculate own methods of the class
This commit is contained in:
Pavel V. Talanov
2017-05-03 14:34:32 +03:00
parent c56f74cc81
commit 798c80ed07
24 changed files with 1182 additions and 25 deletions
@@ -0,0 +1,186 @@
public class EtendingMutableInterfaces {
public static class Lists {
<error>public static class ExtendIList1 implements IMutableList<String></error> {
}
<error>public static class ExtendIList2<E> implements IMutableList<E></error> {
}
// Compiler bug causes remove(int) to clash https://youtrack.jetbrains.com/issue/KT-17782
//public static class ExtendCList1<E> extends CMutableList<E> {
//
//}
//
//public static class ExtendCList2<E> extends CMutableList<String> {
//
//}
public static class ExtendSList extends SMutableList {
}
public static class ExtendAList extends AMutableList {
}
}
public static class Collections {
<error>public static class ExtendICollection1 implements IMutableCollection<String></error> {
}
<error>public static class ExtendICollection2<E> implements IMutableCollection<E></error> {
}
public static class ExtendCCollection1<E> extends CMutableCollection<E> {
}
public static class ExtendCCollection2<E> extends CMutableCollection<String> {
}
public static class ExtendSCollection extends SMutableCollection {
}
public static class ExtendACollection extends AMutableCollection {
}
}
public static class Sets {
<error>public static class ExtendISet1 implements IMutableSet<String></error> {
}
<error>public static class ExtendISet2<E> implements IMutableSet<E></error> {
}
public static class ExtendCSet1<E> extends CMutableSet<E> {
}
public static class ExtendCSet2<E> extends CMutableSet<String> {
}
public static class ExtendSSet extends SMutableSet {
}
public static class ExtendASet extends AMutableSet {
}
}
public static class Iterables {
<error>public static class ExtendIIterable1 implements IMutableIterable<String></error> {
}
<error>public static class ExtendIIterable2<E> implements IMutableIterable<E></error> {
}
public static class ExtendCIterable1<E> extends CMutableIterable<E> {
}
public static class ExtendCIterable2<E> extends CMutableIterable<String> {
}
public static class ExtendSIterable extends SMutableIterable {
}
public static class ExtendAIterable extends AMutableIterable {
}
}
public static class Iterators {
<error>public static class ExtendIIterator1 implements IMutableIterator<String></error> {
}
<error>public static class ExtendIIterator2<E> implements IMutableIterator<E></error> {
}
public static class ExtendCIterator1<E> extends CMutableIterator<E> {
}
public static class ExtendCIterator2<E> extends CMutableIterator<String> {
}
public static class ExtendSIterator extends SMutableIterator {
}
public static class ExtendAIterator extends AMutableIterator {
}
}
public static class Maps {
<error>public static class ExtendIMap1 implements IMutableMap<String, Integer></error> {
}
<error>public static class ExtendIMap2<E> implements IMutableMap<String, E></error> {
}
public static class ExtendCMap1<K, V> extends CMutableMap<K, V> {
}
public static class ExtendCMap2<V> extends CMutableMap<String, V> {
}
// NOTE: looks like a bug in compiler see KT-17738
//public static class ExtendSMap extends SMutableMap<A> {
//
//}
//
//public static class ExtendABMap extends ABMutableMap {
//
//}
}
public static class MapEntrys {
<error>public static class ExtendIMapEntry1 implements IMutableMapEntry<String, Integer></error> {
}
<error>public static class ExtendIMapEntry2<E> implements IMutableMapEntry<String, E></error> {
}
public static class ExtendCMapEntry1<K, V> extends CMutableMapEntry<K, V> {
}
public static class ExtendCMapEntry2<V> extends CMutableMapEntry<String, V> {
}
public static class ExtendSMapEntry extends SMutableMapEntry<A> {
}
public static class ExtendAMapEntry extends ABMutableMapEntry {
}
}
}
@@ -0,0 +1,44 @@
class A
class B
// kotlin.collection.MutableCollection
interface IMutableCollection<Elem> : MutableCollection<Elem>
abstract class CMutableCollection<Elem> : MutableCollection<Elem> by mutableListOf<Elem>()
abstract class SMutableCollection : MutableCollection<String> by mutableListOf<String>()
abstract class AMutableCollection : MutableCollection<A> by mutableListOf<A>()
// kotlin.collections.MutableIterable
interface IMutableIterable<Elem> : MutableIterable<Elem>
abstract class CMutableIterable<Elem> : MutableIterable<Elem> by mutableListOf<Elem>()
abstract class SMutableIterable : MutableIterable<String> by mutableListOf<String>()
abstract class AMutableIterable : MutableIterable<A> by mutableListOf<A>()
// kotlin.collections.MutableList
interface IMutableList<Elem> : MutableList<Elem>
abstract class CMutableList<Elem> : MutableList<Elem> by mutableListOf<Elem>()
abstract class SMutableList : MutableList<String> by mutableListOf<String>()
abstract class AMutableList : MutableList<A> by mutableListOf<A>()
// kotlin.collections.Set
interface IMutableSet<Elem> : MutableSet<Elem>
abstract class CMutableSet<Elem> : MutableSet<Elem> by mutableSetOf<Elem>()
abstract class SMutableSet : MutableSet<String> by mutableSetOf<String>()
abstract class AMutableSet : MutableSet<A> by mutableSetOf<A>()
// kotlin.collections.Iterator
interface IMutableIterator<Elem> : MutableIterator<Elem>
abstract class CMutableIterator<Elem> : MutableIterator<Elem> by mutableListOf<Elem>().iterator()
abstract class SMutableIterator : MutableIterator<String> by mutableListOf<String>().iterator()
abstract class AMutableIterator : MutableIterator<A> by mutableListOf<A>().iterator()
// kotlin.collections.Map
interface IMutableMap<KElem, VElem> : MutableMap<KElem, VElem>
abstract class CMutableMap<KElem, VElem> : MutableMap<KElem, VElem> by mutableMapOf<KElem, VElem>()
abstract class SMutableMap<VElem> : MutableMap<String, VElem> by mutableMapOf<String, VElem>()
abstract class ABMutableMap : MutableMap<A, B> by mutableMapOf<A, B>()
// kotlin.collections.Map.Entry
interface IMutableMapEntry<KElem, VElem> : MutableMap.MutableEntry<KElem, VElem>
abstract class CMutableMapEntry<KElem, VElem> : MutableMap.MutableEntry<KElem, VElem> by mutableMapOf<KElem, VElem>().entries.first()
abstract class SMutableMapEntry<VElem> : MutableMap.MutableEntry<String, VElem> by mutableMapOf<String, VElem>().entries.first()
abstract class ABMutableMapEntry : MutableMap.MutableEntry<A, B> by mutableMapOf<A, B>().entries.first()
@@ -0,0 +1,185 @@
public class EtendingReadOnlyInterfaces {
public static class Lists {
<error>public static class ExtendIList1 implements IList<String></error> {
}
<error>public static class ExtendIList2<E> implements IList<E></error> {
}
public static class ExtendCList1<E> extends CList<E> {
}
public static class ExtendCList2<E> extends CList<String> {
}
public static class ExtendSList extends SList {
}
public static class ExtendAList extends AList {
}
}
public static class Collections {
<error>public static class ExtendICollection1 implements ICollection<String></error> {
}
<error>public static class ExtendICollection2<E> implements ICollection<E></error> {
}
public static class ExtendCCollection1<E> extends CCollection<E> {
}
public static class ExtendCCollection2<E> extends CCollection<String> {
}
public static class ExtendSCollection extends SCollection {
}
public static class ExtendACollection extends ACollection {
}
}
public static class Sets {
<error>public static class ExtendISet1 implements ISet<String></error> {
}
<error>public static class ExtendISet2<E> implements ISet<E></error> {
}
public static class ExtendCSet1<E> extends CSet<E> {
}
public static class ExtendCSet2<E> extends CSet<String> {
}
public static class ExtendSSet extends SSet {
}
public static class ExtendASet extends ASet {
}
}
public static class Iterables {
<error>public static class ExtendIIterable1 implements IIterable<String></error> {
}
<error>public static class ExtendIIterable2<E> implements IIterable<E></error> {
}
public static class ExtendCIterable1<E> extends CIterable<E> {
}
public static class ExtendCIterable2<E> extends CIterable<String> {
}
public static class ExtendSIterable extends SIterable {
}
public static class ExtendAIterable extends AIterable {
}
}
public static class Iterators {
<error>public static class ExtendIIterator1 implements IIterator<String></error> {
}
<error>public static class ExtendIIterator2<E> implements IIterator<E></error> {
}
public static class ExtendCIterator1<E> extends CIterator<E> {
}
public static class ExtendCIterator2<E> extends CIterator<String> {
}
public static class ExtendSIterator extends SIterator {
}
public static class ExtendAIterator extends AIterator {
}
}
public static class Maps {
<error>public static class ExtendIMap1 implements IMap<String, Integer></error> {
}
<error>public static class ExtendIMap2<E> implements IMap<String, E></error> {
}
public static class ExtendCMap1<K, V> extends CMap<K, V> {
}
public static class ExtendCMap2<V> extends CMap<String, V> {
}
// NOTE: looks like a bug in compiler see KT-17738
//public static class ExtendSMap extends SMap<A> {
//
//}
//
//public static class ExtendABMap extends ABMap {
//
//}
}
public static class MapEntrys {
<error>public static class ExtendIMapEntry1 implements IMapEntry<String, Integer></error> {
}
<error>public static class ExtendIMapEntry2<E> implements IMapEntry<String, E></error> {
}
public static class ExtendCMapEntry1<K, V> extends CMapEntry<K, V> {
}
public static class ExtendCMapEntry2<V> extends CMapEntry<String, V> {
}
public static class ExtendSMapEntry extends SMapEntry<A> {
}
public static class ExtendAMapEntry extends ABMapEntry {
}
}
}
@@ -0,0 +1,44 @@
class A
class B
// kotlin.collections.Collection
interface ICollection<Elem> : Collection<Elem>
abstract class CCollection<Elem> : Collection<Elem> by emptyList<Elem>()
abstract class SCollection : Collection<String> by emptyList<String>()
abstract class ACollection : Collection<A> by emptyList<A>()
// kotlin.collections.Iterable
interface IIterable<Elem> : Iterable<Elem>
abstract class CIterable<Elem> : Iterable<Elem> by emptyList<Elem>()
abstract class SIterable : Iterable<String> by emptyList<String>()
abstract class AIterable : Iterable<A> by emptyList<A>()
// kotlin.collections.List
interface IList<Elem> : List<Elem>
abstract class CList<Elem> : List<Elem> by emptyList<Elem>()
abstract class SList : List<String> by emptyList<String>()
abstract class AList : List<A> by emptyList<A>()
// kotlin.collections.Set
interface ISet<Elem> : Set<Elem>
abstract class CSet<Elem> : Set<Elem> by emptySet<Elem>()
abstract class SSet : Set<String> by emptySet<String>()
abstract class ASet : Set<A> by emptySet<A>()
// kotlin.collections.Iterator
interface IIterator<Elem> : Iterator<Elem>
abstract class CIterator<Elem> : Iterator<Elem> by emptyList<Elem>().iterator()
abstract class SIterator : Iterator<String> by emptyList<String>().iterator()
abstract class AIterator : Iterator<A> by emptyList<A>().iterator()
// kotlin.collections.Map
interface IMap<KElem, VElem> : Map<KElem, VElem>
abstract class CMap<KElem, VElem> : Map<KElem, VElem> by emptyMap<KElem, VElem>()
abstract class SMap<VElem> : Map<String, VElem> by emptyMap<String, VElem>()
abstract class ABMap : Map<A, B> by emptyMap<A, B>()
// kotlin.collections.Map.Entry
interface IMapEntry<KElem, VElem> : Map.Entry<KElem, VElem>
abstract class CMapEntry<KElem, VElem> : Map.Entry<KElem, VElem> by emptyMap<KElem, VElem>().entries.first()
abstract class SMapEntry<VElem> : Map.Entry<String, VElem> by emptyMap<String, VElem>().entries.first()
abstract class ABMapEntry : Map.Entry<A, B> by emptyMap<A, B>().entries.first()
@@ -0,0 +1,16 @@
public class UsingMutableInterfaces {
public static class Lists {
public static <E> void useCMutableList(CMutableList<E> cMutableList, E elem, java.util.Collection<E> other) {
java.util.Iterator<E> iter = cMutableList.iterator();
cMutableList.addAll(other);
cMutableList.add(elem);
cMutableList.isEmpty();
cMutableList.clear();
cMutableList.getSize();
cMutableList.size();
boolean b = cMutableList.remove(elem);
E e1 = cMutableList.remove(3);
E e2 = cMutableList.removeAt(6);
}
}
}
@@ -0,0 +1,2 @@
// kotlin.collections.List
abstract class CMutableList<Elem> : MutableList<Elem> by mutableListOf<Elem>()
@@ -0,0 +1,44 @@
public class UsingReadOnlyInterfaces {
public static class Collections {
public static <E> void useICollection(ICollection<E> iCollection, E elem, java.util.Collection<E> other) {
java.util.Iterator<E> iter = iCollection.iterator();
iCollection.addAll(other);
iCollection.add(elem);
iCollection.isEmpty();
iCollection.clear();
iCollection.<error>getSize</error>(); // this is not an error when analyzing against kotlin sources (which is a bug), this inconsistency is hard to fix with the current approach
iCollection.size();
}
public static <E> void useCCollection(CCollection<E> cCollection, E elem, java.util.Collection<E> other) {
java.util.Iterator<E> iter = cCollection.iterator();
cCollection.addAll(other);
cCollection.add(elem);
cCollection.isEmpty();
cCollection.clear();
cCollection.getSize();
cCollection.size();
cCollection.contains(elem);
cCollection.contains("sasd");
cCollection.removeAll(other);
cCollection.retainAll(other);
}
public static class ExtendCCollection extends CCollection<String> {
@Override
public void clear() {}
}
}
public static class Maps {
public static <E, V> void useCMap(CMap<E, V> cMap) {
cMap.isEmpty();
java.util.Set<E> s = cMap.getKeys();
s = cMap.keySet();
java.util.Collection<V> v = cMap.getValues();
v = cMap.values();
java.util.Set<java.util.Map.Entry<E, V>> e = cMap.entrySet();
e = cMap.getEntries();
}
}
}
@@ -0,0 +1,44 @@
class A
class B
// kotlin.collections.Collection
interface ICollection<Elem> : Collection<Elem>
abstract class CCollection<Elem> : Collection<Elem> by emptyList<Elem>()
abstract class SCollection : Collection<String> by emptyList<String>()
abstract class ACollection : Collection<A> by emptyList<A>()
// kotlin.collections.Iterable
interface IIterable<Elem> : Iterable<Elem>
abstract class CIterable<Elem> : Iterable<Elem> by emptyList<Elem>()
abstract class SIterable : Iterable<String> by emptyList<String>()
abstract class AIterable : Iterable<A> by emptyList<A>()
// kotlin.collections.List
interface IList<Elem> : List<Elem>
abstract class CList<Elem> : List<Elem> by emptyList<Elem>()
abstract class SList : List<String> by emptyList<String>()
abstract class AList : List<A> by emptyList<A>()
// kotlin.collections.Set
interface ISet<Elem> : Set<Elem>
abstract class CSet<Elem> : Set<Elem> by emptySet<Elem>()
abstract class SSet : Set<String> by emptySet<String>()
abstract class ASet : Set<A> by emptySet<A>()
// kotlin.collections.Iterator
interface IIterator<Elem> : Iterator<Elem>
abstract class CIterator<Elem> : Iterator<Elem> by emptyList<Elem>().iterator()
abstract class SIterator : Iterator<String> by emptyList<String>().iterator()
abstract class AIterator : Iterator<A> by emptyList<A>().iterator()
// kotlin.collections.Map
interface IMap<KElem, VElem> : Map<KElem, VElem>
abstract class CMap<KElem, VElem> : Map<KElem, VElem> by emptyMap<KElem, VElem>()
abstract class SMap<VElem> : Map<String, VElem> by emptyMap<String, VElem>()
abstract class ABMap : Map<A, B> by emptyMap<A, B>()
// kotlin.collections.Map.Entry
interface IMapEntry<KElem, VElem> : Map.Entry<KElem, VElem>
abstract class CMapEntry<KElem, VElem> : Map.Entry<KElem, VElem> by emptyMap<KElem, VElem>().entries.first()
abstract class SMapEntry<VElem> : Map.Entry<String, VElem> by emptyMap<String, VElem>().entries.first()
abstract class ABMapEntry : Map.Entry<A, B> by emptyMap<A, B>().entries.first()