// TARGET_BACKEND: JVM
// FILE: derivedEmptyStringListAdd.kt

open class EmptyListBase<T : CharSequence> : List<T>, RandomAccess {
    override val size: Int get() = 0
    override fun isEmpty(): Boolean = true
    override fun contains(element: T): Boolean = false
    override fun containsAll(elements: Collection<T>): Boolean = elements.isEmpty()

    override fun get(index: Int): T = null!!
    override fun indexOf(element: T): Int = -1
    override fun lastIndexOf(element: T): Int = -1

    override fun iterator(): Iterator<T> = null!!
    override fun listIterator(): ListIterator<T> = null!!
    override fun listIterator(index: Int): ListIterator<T> = null!!

    override fun subList(fromIndex: Int, toIndex: Int): List<T> = null!!
}

object EmptyStringList : EmptyListBase<String>()

fun box(): String {
    try {
        J.add()
        return "Fail: no exception is thrown from J.add()"
    } catch (e: UnsupportedOperationException) {
        return "OK"
    } catch (e: Throwable) {
        throw AssertionError("Fail: incorrect exception is thrown from J.add()", e)
    }
}

// FILE: J.java
public class J {
    public static void add() {
        EmptyStringList.INSTANCE.add("");
    }
}
