Tencent/matrix · error · IllegalArgumentException

NOT allow to add MultiSourceStatefulOwner as source…

Error message

NOT allow to add MultiSourceStatefulOwner as source, consider to add its shadow owner

What it means

MultiSourceStatefulOwner aggregates several source owners via a private shadow owner. Nesting a MultiSourceStatefulOwner directly as a source of another owner is unsupported, so register() throws IllegalArgumentException and directs the caller to add its shadow owner instead.

Solutions

  1. Pass multiSourceOwner.shadowOwner instead of the MultiSourceStatefulOwner itself to addSourceOwner.
  2. Flatten the composition: add the individual underlying owners as sources directly.
  3. Refactor so composed owners are not fed into other StatefulOwners.

Example fix

// before
owner.addSourceOwner(multiSourceOwner) // throws
// after
owner.addSourceOwner(multiSourceOwner.shadowOwner)
Defensive patterns

Strategy: type-guard

Validate before calling

fun safeAddSource(owner: IStatefulOwner, source: IStatefulOwner) {
    val src = (source as? MultiSourceStatefulOwner)?.shadowOwner ?: source
    owner.addSourceOwner(src)
}

Type guard

fun isComposableSource(s: IStatefulOwner): Boolean = s !is MultiSourceStatefulOwner

Try / catch

try {
    owner.addSourceOwner(source)
} catch (e: IllegalArgumentException) {
    MatrixLog.w(TAG, "use shadowOwner for MultiSourceStatefulOwner", e)
}

Prevention

When it happens

Trigger: Calling addSourceOwner(multiSourceOwner) where multiSourceOwner is a MultiSourceStatefulOwner instance; the private register() detects the type and throws.

Common situations: Building chains of combined/derived lifecycle owners and passing an already-composed owner as a source; misunderstanding that MultiSourceStatefulOwner exposes a shadow owner for composition.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/e33e05b436d79b3d. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/lifecycle/StatefulOwner.kt:296

 * @since 2021/9/24
 */
open class MultiSourceStatefulOwner(
    private val reduceOperator: (statefuls: Collection<IStateful>) -> Boolean,
    vararg statefulOwners: IStatefulOwner,
) : StatefulOwner(true), IStateObserver, IMultiSourceOwner {

    private val sourceOwners = ConcurrentLinkedQueue<IStatefulOwner>()

    init {
        statefulOwners.forEach {
            register(it)
        }
    }

    private fun register(owner: IStatefulOwner) {
        owner.let {
            if (it is MultiSourceStatefulOwner) {
                throw IllegalArgumentException("NOT allow to add MultiSourceStatefulOwner as source, consider to add its shadow owner")
            }
            sourceOwners.add(it)
            it.observeForever(this)
        }
    }

    private fun unregister(owner: StatefulOwner) {
        owner.also {
            sourceOwners.remove(it)
            it.removeObserver(this)
            onStateChanged()
        }
    }

    override fun addSourceOwner(owner: StatefulOwner) = register(owner)

    open fun addSourceOwner(owner: LifecycleOwner): StatefulOwner =
        owner.toStateOwner().also { addSourceOwner(it) }

View on GitHub (pinned to 3b8293bd65)