Skip to content

Commit

Permalink
[J2KT] Add override of toArray to Collections readable
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 707928842
  • Loading branch information
martinkretzschmar authored and copybara-github committed Dec 19, 2024
1 parent 077bebd commit a2ac36b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ public boolean retainAll(Collection<?> c) {
c = convertCollection(c);
return super.retainAll(c);
}

@Override
public @Nullable Object[] toArray() {
return super.toArray();
}

@Override
public <T1 extends @Nullable Object> T1[] toArray(T1[] a) {
return super.toArray(a);
}
}

public static class CustomCollectionDisambiguatingOverrides<T extends @Nullable Object>
Expand Down Expand Up @@ -233,6 +243,16 @@ public int lastIndexOf(@Nullable Object o) {
o = convert(o);
return super.lastIndexOf(o);
}

@Override
public @Nullable Object[] toArray() {
return super.toArray();
}

@Override
public <T1 extends @Nullable Object> T1[] toArray(T1[] a) {
return super.toArray(a);
}
}

public static class CustomMap<K extends @Nullable Object, V extends @Nullable Object>
Expand Down Expand Up @@ -363,6 +383,29 @@ public boolean remove(@Nullable Object key, @Nullable Object value) {
}
}

public abstract static class AbstractCollectionWithToArrayOverride<E extends @Nullable Object>
implements Collection<E> {

@Override
public @Nullable Object[] toArray() {
return new Object[0];
}

@Override
public <T extends @Nullable Object> T[] toArray(T[] a) {
return a;
}
}

public interface CollectionInterfaceWithToArrayOverride<E extends @Nullable Object>
extends List<E> {
@Override
@Nullable Object[] toArray();

@Override
<T extends @Nullable Object> T[] toArray(T[] a);
}

private static <T extends @Nullable Object> T convert(T object) {
return object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import java.util.AbstractList
import java.util.AbstractMap
import java.util.Spliterator
import javaemul.lang.JavaCollection
import javaemul.lang.JavaList
import javaemul.lang.JavaMap
import javaemul.lang.uninitializedArrayOf
import kotlin.Any
import kotlin.Array
import kotlin.Boolean
import kotlin.Int
import kotlin.OptIn
Expand Down Expand Up @@ -301,6 +304,14 @@ open class Collections {
c_1 = Collections.convertCollection(c_1)
return super<AbstractCollection>.retainAll(c_1 as Collection<T>)
}

override fun toArray(): Array<Any?> {
return super<AbstractCollection>.toArray()
}

override fun <T1> toArray(a: Array<T1>): Array<T1> {
return super<AbstractCollection>.toArray<T1>(a)
}
}

@ObjCName("J2ktJ2ktCollections_CustomCollectionDisambiguatingOverrides", exact = true)
Expand Down Expand Up @@ -338,6 +349,14 @@ open class Collections {
o_1 = Collections.convert<Any?>(o_1)
return super<AbstractList>.lastIndexOf(o_1 as T)
}

override fun toArray(): Array<Any?> {
return super<AbstractList>.toArray()
}

override fun <T1> toArray(a: Array<T1>): Array<T1> {
return super<AbstractList>.toArray<T1>(a)
}
}

@ObjCName("J2ktJ2ktCollections_CustomMap", exact = true)
Expand Down Expand Up @@ -490,4 +509,22 @@ open class Collections {
return super<Collections.CustomMap>.getOrDefault(key_1 as String, defaultValue_1 as String) as String
}
}

@ObjCName("J2ktJ2ktCollections_AbstractCollectionWithToArrayOverride", exact = true)
abstract class AbstractCollectionWithToArrayOverride<E>: JavaCollection<E> {
override fun toArray(): Array<Any?> {
return uninitializedArrayOf<Any>(0) as Array<Any?>
}

override fun <T> toArray(a: Array<T>): Array<T> {
return a
}
}

@ObjCName("J2ktJ2ktCollections_CollectionInterfaceWithToArrayOverride", exact = true)
interface CollectionInterfaceWithToArrayOverride<E>: JavaList<E> {
override fun toArray(): Array<Any?>

override fun <T> toArray(a: Array<T>): Array<T>
}
}

0 comments on commit a2ac36b

Please sign in to comment.