Is it possible for kotlin.collections functions to...
# android
t
Is it possible for kotlin.collections functions to return a mixture of immutable and mutable lists? I’m trying to java.collections sort a list mutated by .sortByDescending, and a very small percentage, but a substantial amount, of my users are crashing due to an UnsupportedOperationException being thrown by
AbstractList.set(int index, E element)
Copy code
public E set(int index, E element) {
        throw new UnsupportedOperationException();
    }
p
Do you have the code you are using to create the list?
And also, the code that is invoked from Java to get the list
p
Bear in mind that the Kotlin
List
returned from
CollectionsKt.listOf("a", "b")
is not going to be mutable but, if accessed from Java it will resolve it as a Java
List
which has no safety regarding mutability
t
That could be resolved by returning .toMutableList()
p
Indeed
t
Its interesting it doesnt happen on 8.0 and higher
p
Now, that’s interesting, yeah 🤔
t
Ok, well thanks for weighing in.. I appreciate it
p
I wonder if
java.util.Arrays$ArrayList
changed it’s implementation in the JVM and/or Dalvik/ART
t
The thing is, that on an emulator with 7.0 running, I’ve seen both of those list types pass through Collections.sort without throwing
so the safest bet is to use kotlin collections to sort and have java be read only.. maybe using inference to pass a consistent list type
this has been an interesting exercise in kotlin/java interop 🙂
p
Yeah, I would also try to move as much as possible to Kotlin, haha (if you can)
t
I agree 100%, we’re trying to but the app is big, its a delicate balance. Its really crazy though when you start migrating a huge app to Kotlin from Java with a significant number of users, you find strange things like this happen with almost every refactor
p
Yeah, it’s tricky. I’ve experience a couple of these WTF issues myself before in a large project that we are slowly migrating
t
Next I’m trying to figure out how we got here: https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/AbstractList.java#L132 From kotlins .map() and .sortByDescending()
p
Ah! it just hit me… If there is only one thing in the list there is the potential that it returns a
SingletonList
(I’m not 100% sure) which will throw this exception.
t
Really? Thats great info
I dont see SingletonList in the collections.kt source
I’ll add a response on SO 😄
t
Thank you 🙂
Now, any reason it doesnt happen in 8.0+ ?
p
Hmm, bad luck? 😅
or good
t
I dont think so, we have enough users so that statistically it should be happening
Any way, you’ve been a huge help! I’ll mark your SO answer as accepted once it comes in
p
I’m still puzzled by why it works on 8.0 and up
t
I’m going to verify that, to be 100% sure now that I have the knowledge to reproduce it
👍 1
Previously I couldnt repro, and no users on 8.0+ were crashing according to our crash reports
Yeah so I just verified, its not happening on 9.0. I stepped over Collections.sort on a SingletonList, whereas on 7.0 when attempting to step over, the exception is thrown
p
Thinking about this, it may be a later fix on the JDK as a list of one shouldn't be modified when sorting. Need to check the SC