KT-2688 Provide mutable/immutable interfaces for Java collections
#KT-2688 fixed
This commit is contained in:
@@ -33,10 +33,8 @@ import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||
import org.jetbrains.jet.lang.types.lang.PrimitiveType;
|
||||
import org.jetbrains.jet.lang.types.ref.ClassName;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author svtk
|
||||
@@ -64,18 +62,36 @@ public class KotlinToJavaTypesMap {
|
||||
private void init() {
|
||||
JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
|
||||
|
||||
register(JetStandardClasses.getAny(), OBJECT_TYPE);
|
||||
register(standardLibrary.getNumber(), JAVA_NUMBER_TYPE);
|
||||
register(standardLibrary.getString(), JAVA_STRING_TYPE);
|
||||
register(standardLibrary.getThrowable(), JAVA_THROWABLE_TYPE);
|
||||
register(standardLibrary.getCharSequence(), JAVA_CHAR_SEQUENCE_TYPE);
|
||||
register(standardLibrary.getComparable(), JAVA_COMPARABLE_TYPE);
|
||||
register(standardLibrary.getEnum(), JAVA_ENUM_TYPE);
|
||||
register(standardLibrary.getAnnotation(), JAVA_ANNOTATION_TYPE);
|
||||
register(standardLibrary.getIterable(), JAVA_ITERABLE_TYPE);
|
||||
register(standardLibrary.getMutableIterable(), JAVA_ITERABLE_TYPE);
|
||||
register(standardLibrary.getIterator(), JAVA_ITERATOR_TYPE);
|
||||
register(standardLibrary.getMutableIterator(), JAVA_ITERATOR_TYPE);
|
||||
register(JetStandardClasses.getAny(), Object.class);
|
||||
register(standardLibrary.getNumber(), Number.class);
|
||||
register(standardLibrary.getString(), String.class);
|
||||
register(standardLibrary.getThrowable(), Throwable.class);
|
||||
register(standardLibrary.getCharSequence(), CharSequence.class);
|
||||
register(standardLibrary.getComparable(), Comparable.class);
|
||||
register(standardLibrary.getEnum(), Enum.class);
|
||||
register(standardLibrary.getAnnotation(), Annotation.class);
|
||||
register(standardLibrary.getIterable(), Iterable.class);
|
||||
register(standardLibrary.getIterator(), Iterator.class);
|
||||
register(standardLibrary.getMutableIterable(), Iterable.class);
|
||||
register(standardLibrary.getMutableIterator(), Iterator.class);
|
||||
|
||||
register(standardLibrary.getCollection(), Collection.class);
|
||||
register(standardLibrary.getMutableCollection(), Collection.class);
|
||||
|
||||
register(standardLibrary.getList(), List.class);
|
||||
register(standardLibrary.getMutableList(), List.class);
|
||||
|
||||
register(standardLibrary.getSet(), Set.class);
|
||||
register(standardLibrary.getMutableSet(), Set.class);
|
||||
|
||||
register(standardLibrary.getMap(), Map.class);
|
||||
register(standardLibrary.getMutableMap(), Map.class);
|
||||
|
||||
register(standardLibrary.getMapEntry(), Map.Entry.class);
|
||||
register(standardLibrary.getMutableMapEntry(), Map.Entry.class);
|
||||
|
||||
register(standardLibrary.getListIterator(), ListIterator.class);
|
||||
register(standardLibrary.getMutableListIterator(), ListIterator.class);
|
||||
}
|
||||
|
||||
private void initPrimitives() {
|
||||
|
||||
+26
-3
@@ -69,6 +69,24 @@ public class JavaToKotlinTypesMap {
|
||||
|
||||
registerCovariant(Iterable.class, standardLibrary.getMutableIterable());
|
||||
registerCovariant(Iterator.class, standardLibrary.getMutableIterator());
|
||||
|
||||
register(Collection.class, standardLibrary.getCollection());
|
||||
registerCovariant(Collection.class, standardLibrary.getMutableCollection());
|
||||
|
||||
register(List.class, standardLibrary.getList());
|
||||
registerCovariant(List.class, standardLibrary.getMutableList());
|
||||
|
||||
register(Set.class, standardLibrary.getSet());
|
||||
registerCovariant(Set.class, standardLibrary.getMutableSet());
|
||||
|
||||
register(Map.class, standardLibrary.getMap());
|
||||
registerCovariant(Map.class, standardLibrary.getMutableMap());
|
||||
|
||||
register(Map.Entry.class, standardLibrary.getMapEntry());
|
||||
registerCovariant(Map.Entry.class, standardLibrary.getMutableMapEntry());
|
||||
|
||||
register(ListIterator.class, standardLibrary.getListIterator());
|
||||
registerCovariant(ListIterator.class, standardLibrary.getMutableListIterator());
|
||||
}
|
||||
|
||||
private void initPrimitives() {
|
||||
@@ -95,7 +113,8 @@ public class JavaToKotlinTypesMap {
|
||||
|
||||
@Nullable
|
||||
public ClassDescriptor getKotlinAnalog(@NotNull FqName fqName, @NotNull JavaTypeTransformer.TypeUsage typeUsage) {
|
||||
if (typeUsage == JavaTypeTransformer.TypeUsage.MEMBER_SIGNATURE_COVARIANT) {
|
||||
if (typeUsage == JavaTypeTransformer.TypeUsage.MEMBER_SIGNATURE_COVARIANT
|
||||
|| typeUsage == JavaTypeTransformer.TypeUsage.SUPERTYPE) {
|
||||
ClassDescriptor descriptor = classDescriptorMapForCovariantPositions.get(fqName);
|
||||
if (descriptor != null) {
|
||||
return descriptor;
|
||||
@@ -104,8 +123,12 @@ public class JavaToKotlinTypesMap {
|
||||
return classDescriptorMap.get(fqName);
|
||||
}
|
||||
|
||||
private static FqName getJavaClassFqName(@NotNull Class<?> javaClass) {
|
||||
return new FqName(javaClass.getName().replace("$", "."));
|
||||
}
|
||||
|
||||
private void register(@NotNull Class<?> javaClass, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
register(new FqName(javaClass.getName()), kotlinDescriptor);
|
||||
register(getJavaClassFqName(javaClass), kotlinDescriptor);
|
||||
}
|
||||
|
||||
private void register(@NotNull FqName javaClassName, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
@@ -114,7 +137,7 @@ public class JavaToKotlinTypesMap {
|
||||
}
|
||||
|
||||
private void registerCovariant(@NotNull Class<?> javaClass, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
registerCovariant(new FqName(javaClass.getName()), kotlinDescriptor);
|
||||
registerCovariant(getJavaClassFqName(javaClass), kotlinDescriptor);
|
||||
}
|
||||
|
||||
private void registerCovariant(@NotNull FqName javaClassName, @NotNull ClassDescriptor kotlinDescriptor) {
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package jet
|
||||
|
||||
public trait Collection<out E> : Iterable<E>, Hashable {
|
||||
// Query Operations
|
||||
public fun size() : Int
|
||||
public fun isEmpty() : Boolean
|
||||
public fun contains(o : Any?) : Boolean
|
||||
override fun iterator() : Iterator<E>
|
||||
public fun toArray() : Array<Any?>
|
||||
public fun <T> toArray(a : Array<out T>) : Array<T>
|
||||
|
||||
// Bulk Operations
|
||||
public fun containsAll(c : Collection<out Any?>) : Boolean
|
||||
}
|
||||
|
||||
public trait MutableCollection<E> : Collection<E>, MutableIterable<E> {
|
||||
// Query Operations
|
||||
override fun iterator() : MutableIterator<E>
|
||||
|
||||
// Modification Operations
|
||||
public fun add(e : E) : Boolean
|
||||
public fun remove(o : Any?) : Boolean
|
||||
|
||||
// Bulk Modification Operations
|
||||
public fun addAll(c : Collection<out E>) : Boolean
|
||||
public fun removeAll(c : Collection<out Any?>) : Boolean
|
||||
public fun retainAll(c : Collection<out Any?>) : Boolean
|
||||
public fun clear()
|
||||
}
|
||||
|
||||
public trait List<out E> : Collection<E> {
|
||||
// Query Operations
|
||||
override fun size() : Int
|
||||
override fun isEmpty() : Boolean
|
||||
override fun contains(o : Any?) : Boolean
|
||||
override fun iterator() : Iterator<E>
|
||||
override fun toArray() : Array<Any?>
|
||||
override fun <T> toArray(a : Array<out T>) : Array<T>
|
||||
|
||||
// Bulk Operations
|
||||
override fun containsAll(c : Collection<out Any?>) : Boolean
|
||||
|
||||
// Positional Access Operations
|
||||
public fun get(index : Int) : E
|
||||
|
||||
// Search Operations
|
||||
public fun indexOf(o : Any?) : Int
|
||||
public fun lastIndexOf(o : Any?) : Int
|
||||
|
||||
// List Iterators
|
||||
public fun listIterator() : ListIterator<E>
|
||||
public fun listIterator(index : Int) : ListIterator<E>
|
||||
|
||||
// View
|
||||
public fun subList(fromIndex : Int, toIndex : Int) : List<E>
|
||||
}
|
||||
|
||||
public trait MutableList<E> : List<E>, MutableCollection<E> {
|
||||
// Modification Operations
|
||||
override fun add(e: E) : Boolean
|
||||
override fun remove(o : Any?) : Boolean
|
||||
|
||||
// Bulk Modification Operations
|
||||
override fun addAll(c : Collection<out E>) : Boolean
|
||||
public fun addAll(index : Int, c : Collection<out E>) : Boolean
|
||||
override fun removeAll(c : Collection<out Any?>) : Boolean
|
||||
override fun retainAll(c : Collection<out Any?>) : Boolean
|
||||
override fun clear()
|
||||
|
||||
// Positional Access Operations
|
||||
public fun set(index : Int, element : E) : E
|
||||
public fun add(index : Int, element : E)
|
||||
public fun remove(index : Int) : E
|
||||
}
|
||||
|
||||
public trait Set<out E> : Collection<E> {
|
||||
// Query Operations
|
||||
override fun size() : Int
|
||||
override fun isEmpty() : Boolean
|
||||
override fun contains(o : Any?) : Boolean
|
||||
override fun iterator() : Iterator<E>
|
||||
override fun toArray() : Array<Any?>
|
||||
override fun <T> toArray(a : Array<out T>) : Array<T>
|
||||
|
||||
// Bulk Operations
|
||||
override fun containsAll(c : Collection<out Any?>) : Boolean
|
||||
}
|
||||
|
||||
public trait MutableSet<E> : Set<E>, MutableCollection<E> {
|
||||
// Query Operations
|
||||
override fun iterator() : MutableIterator<E>
|
||||
|
||||
// Modification Operations
|
||||
override fun add(e: E) : Boolean
|
||||
override fun remove(o : Any?) : Boolean
|
||||
|
||||
// Bulk Modification Operations
|
||||
override fun addAll(c : Collection<out E>) : Boolean
|
||||
override fun removeAll(c : Collection<out Any?>) : Boolean
|
||||
override fun retainAll(c : Collection<out Any?>) : Boolean
|
||||
override fun clear()
|
||||
}
|
||||
|
||||
public trait Map<K, V> {
|
||||
// Query Operations
|
||||
public fun size() : Int
|
||||
public fun isEmpty() : Boolean
|
||||
public fun containsKey(key : Any?) : Boolean
|
||||
public fun containsValue(value : Any?) : Boolean
|
||||
public fun get(key : Any?) : V?
|
||||
|
||||
// Views
|
||||
public fun keySet() : Set<K>
|
||||
public fun values() : Collection<V>
|
||||
public fun entrySet() : Set<Map.Entry<K, V>>
|
||||
|
||||
public trait Entry<out K, out V> : Hashable {
|
||||
public fun getKey() : K
|
||||
public fun getValue() : V
|
||||
}
|
||||
}
|
||||
|
||||
public trait MutableMap<K, V> : Map<K, V> {
|
||||
// Modification Operations
|
||||
public fun put(key : K, value : V) : V?
|
||||
public fun remove(key : Any?) : V?
|
||||
|
||||
// Bulk Modification Operations
|
||||
public fun putAll(m : Map<out K, out V>)
|
||||
public fun clear()
|
||||
|
||||
// Views
|
||||
override fun keySet() : MutableSet<K>
|
||||
override fun values() : MutableCollection<V>
|
||||
override fun entrySet() : MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
|
||||
public trait MutableEntry<K,V> : Map.Entry<K, V>, Hashable {
|
||||
public fun setValue(value : V) : V
|
||||
}
|
||||
}
|
||||
@@ -75,3 +75,24 @@ public fun FloatIterator.iterator() : FloatIterator
|
||||
|
||||
public fun LongIterator.iterator() : LongIterator
|
||||
|
||||
public trait ListIterator<T> : Iterator<T> {
|
||||
// Query Operations
|
||||
override fun hasNext() : Boolean
|
||||
override fun next() : T
|
||||
|
||||
public fun hasPrevious() : Boolean
|
||||
public fun previous() : T
|
||||
public fun nextIndex() : Int
|
||||
public fun previousIndex() : Int
|
||||
}
|
||||
|
||||
public trait MutableListIterator<T> : ListIterator<T>, MutableIterator<T> {
|
||||
// Query Operations
|
||||
override fun hasNext() : Boolean
|
||||
override fun next() : T
|
||||
|
||||
// Modification Operations
|
||||
override fun remove()
|
||||
public fun set(e : T)
|
||||
public fun add(e : T)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.intellij.psi.PsiFileFactory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
@@ -119,7 +120,8 @@ public class JetStandardLibrary {
|
||||
"Iterables.jet",
|
||||
"Iterators.jet",
|
||||
"Arrays.jet",
|
||||
"Enum.jet"
|
||||
"Enum.jet",
|
||||
"Collections.jet"
|
||||
);
|
||||
try {
|
||||
List<JetFile> files = new LinkedList<JetFile>();
|
||||
@@ -371,6 +373,70 @@ public class JetStandardLibrary {
|
||||
return annotationType;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getCollection() {
|
||||
return getStdClassByName("Collection");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMutableCollection() {
|
||||
return getStdClassByName("MutableCollection");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getList() {
|
||||
return getStdClassByName("List");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMutableList() {
|
||||
return getStdClassByName("MutableList");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getListIterator() {
|
||||
return getStdClassByName("ListIterator");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMutableListIterator() {
|
||||
return getStdClassByName("MutableListIterator");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getSet() {
|
||||
return getStdClassByName("Set");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMutableSet() {
|
||||
return getStdClassByName("MutableSet");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMap() {
|
||||
return getStdClassByName("Map");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMutableMap() {
|
||||
return getStdClassByName("MutableMap");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMapEntry() {
|
||||
ClassifierDescriptor entry = getMap().getDefaultType().getMemberScope().getClassifier(Name.identifier("Entry"));
|
||||
assert entry instanceof ClassDescriptor;
|
||||
return (ClassDescriptor) entry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassDescriptor getMutableMapEntry() {
|
||||
ClassifierDescriptor entry = getMutableMap().getDefaultType().getMemberScope().getClassifier(Name.identifier("MutableEntry"));
|
||||
assert entry instanceof ClassDescriptor;
|
||||
return (ClassDescriptor) entry;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JetType getPrimitiveJetType(PrimitiveType primitiveType) {
|
||||
return primitiveTypeToJetType.get(primitiveType);
|
||||
|
||||
Reference in New Issue
Block a user