From ccf6d43e30af6ead503ff8dcc1d430a610c09826 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Wed, 5 Apr 2023 18:05:19 -0400 Subject: [PATCH] Better propagate nullability for `AttributeSet` (#86) These can be nullable everywhere except layout/xml inflation --- .../viewpump/FallbackViewCreator.kt | 2 +- .../inflationx/viewpump/InflateRequest.kt | 4 +-- .../io/github/inflationx/viewpump/ViewPump.kt | 4 +-- .../-ReflectiveFallbackViewCreator.kt | 2 +- .../internal/-ViewPumpActivityFactory.kt | 2 +- .../internal/-ViewPumpLayoutInflater.kt | 25 +++++++++---------- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/viewpump/src/main/java/io/github/inflationx/viewpump/FallbackViewCreator.kt b/viewpump/src/main/java/io/github/inflationx/viewpump/FallbackViewCreator.kt index d8eb45d..4daabab 100644 --- a/viewpump/src/main/java/io/github/inflationx/viewpump/FallbackViewCreator.kt +++ b/viewpump/src/main/java/io/github/inflationx/viewpump/FallbackViewCreator.kt @@ -5,5 +5,5 @@ import android.util.AttributeSet import android.view.View fun interface FallbackViewCreator { - fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? + fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet?): View? } diff --git a/viewpump/src/main/java/io/github/inflationx/viewpump/InflateRequest.kt b/viewpump/src/main/java/io/github/inflationx/viewpump/InflateRequest.kt index 2daba39..4ebc1f6 100644 --- a/viewpump/src/main/java/io/github/inflationx/viewpump/InflateRequest.kt +++ b/viewpump/src/main/java/io/github/inflationx/viewpump/InflateRequest.kt @@ -10,7 +10,7 @@ data class InflateRequest( @get:JvmName("context") val context: Context, @get:JvmName("attrs") - val attrs: AttributeSet, + val attrs: AttributeSet? = null, @get:JvmName("parent") val parent: View? = null, @get:JvmName("fallbackViewCreator") @@ -61,7 +61,7 @@ data class InflateRequest( fun build() = InflateRequest(name = name ?: throw IllegalStateException("name == null"), context = context ?: throw IllegalStateException("context == null"), - attrs = attrs ?: throw IllegalStateException("attrs == null"), + attrs = attrs, parent = parent, fallbackViewCreator = fallbackViewCreator ?: throw IllegalStateException("fallbackViewCreator == null") ) diff --git a/viewpump/src/main/java/io/github/inflationx/viewpump/ViewPump.kt b/viewpump/src/main/java/io/github/inflationx/viewpump/ViewPump.kt index 040ee36..8f0ca5b 100644 --- a/viewpump/src/main/java/io/github/inflationx/viewpump/ViewPump.kt +++ b/viewpump/src/main/java/io/github/inflationx/viewpump/ViewPump.kt @@ -44,7 +44,7 @@ class ViewPump private constructor( * @param clazz The class of View to be created. * @return The processed view, which might not necessarily be the same type as clazz. */ - fun create(context: Context, clazz: Class, attrs: AttributeSet): View? { + fun create(context: Context, clazz: Class, attrs: AttributeSet?): View? { return inflate(InflateRequest( context = context, name = clazz.name, @@ -204,7 +204,7 @@ class ViewPump private constructor( @Deprecated("Global singletons are bad for testing, scoping, and composition. Use local ViewPump instances instead.") @JvmName("staticCreateDeprecated") @JvmStatic - fun create(context: Context, clazz: Class, attrs: AttributeSet): View? { + fun create(context: Context, clazz: Class, attrs: AttributeSet?): View? { @Suppress("DEPRECATION_ERROR") return get() .inflate(InflateRequest( diff --git a/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ReflectiveFallbackViewCreator.kt b/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ReflectiveFallbackViewCreator.kt index bd3372e..e86f284 100644 --- a/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ReflectiveFallbackViewCreator.kt +++ b/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ReflectiveFallbackViewCreator.kt @@ -17,7 +17,7 @@ internal class `-ReflectiveFallbackViewCreator` : FallbackViewCreator { } override fun onCreateView(parent: View?, name: String, context: Context, - attrs: AttributeSet): View? { + attrs: AttributeSet?): View? { try { val clazz = Class.forName(name).asSubclass(View::class.java) var constructor: Constructor diff --git a/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpActivityFactory.kt b/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpActivityFactory.kt index ba630b1..a56ec93 100644 --- a/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpActivityFactory.kt +++ b/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpActivityFactory.kt @@ -35,5 +35,5 @@ internal interface `-ViewPumpActivityFactory` { * @see android.view.LayoutInflater.Factory2 */ fun onActivityCreateView(parent: View?, view: View, name: String, context: Context, - attrs: AttributeSet): View? + attrs: AttributeSet?): View? } diff --git a/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpLayoutInflater.kt b/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpLayoutInflater.kt index f80eaf9..25edf0c 100644 --- a/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpLayoutInflater.kt +++ b/viewpump/src/main/java/io/github/inflationx/viewpump/internal/-ViewPumpLayoutInflater.kt @@ -40,7 +40,6 @@ internal class `-ViewPumpLayoutInflater`( setUpLayoutFactories(cloned) } - /** * We use this for internal cloning to be a little more efficient with memory. */ @@ -147,7 +146,7 @@ internal class `-ViewPumpLayoutInflater`( view: View, name: String, context: Context, - attrs: AttributeSet + attrs: AttributeSet? ): View? { return viewPump .inflate( @@ -169,7 +168,7 @@ internal class `-ViewPumpLayoutInflater`( * BUT only for none CustomViews. */ @Throws(ClassNotFoundException::class) - override fun onCreateView(parent: View?, name: String, attrs: AttributeSet): View? { + override fun onCreateView(parent: View?, name: String, attrs: AttributeSet?): View? { return viewPump .inflate( InflateRequest( @@ -189,7 +188,7 @@ internal class `-ViewPumpLayoutInflater`( * Basically if this method doesn't inflate the View nothing probably will. */ @Throws(ClassNotFoundException::class) - override fun onCreateView(name: String, attrs: AttributeSet): View? { + override fun onCreateView(name: String, attrs: AttributeSet?): View? { return viewPump .inflate( InflateRequest( @@ -284,7 +283,7 @@ internal class `-ViewPumpLayoutInflater`( parent: View?, name: String, context: Context, - attrs: AttributeSet + attrs: AttributeSet? ): View? { return inflater.createCustomViewInternal(view, name, context, attrs) } @@ -296,7 +295,7 @@ internal class `-ViewPumpLayoutInflater`( override fun onCreateView( parent: View?, name: String, context: Context, - attrs: AttributeSet + attrs: AttributeSet? ): View? { return inflater.superOnCreateView(parent, name, attrs) } @@ -310,7 +309,7 @@ internal class `-ViewPumpLayoutInflater`( parent: View?, name: String, context: Context, - attrs: AttributeSet + attrs: AttributeSet? ): View? { // This mimics the {@code PhoneLayoutInflater} in the way it tries to inflate the base // classes, if this fails its pretty certain the app will fail at this point. @@ -367,9 +366,9 @@ internal class `-ViewPumpLayoutInflater`( parent: View?, name: String, context: Context, - attrs: AttributeSet + attrs: AttributeSet? ): View? { - return factory.onCreateView(name, context, attrs) + return attrs?.let { factory.onCreateView(name, context, it) } } } @@ -414,9 +413,9 @@ internal class `-ViewPumpLayoutInflater`( parent: View?, name: String, context: Context, - attrs: AttributeSet + attrs: AttributeSet? ): View? { - return factory2.onCreateView(parent, name, context, attrs) + return attrs?.let { factory2.onCreateView(parent, name, context, it) } } } @@ -460,10 +459,10 @@ internal class `-ViewPumpLayoutInflater`( parent: View?, name: String, context: Context, - attrs: AttributeSet + attrs: AttributeSet? ): View? { return inflater.createCustomViewInternal( - factory2.onCreateView(parent, name, context, attrs), name, context, attrs + factory2.onCreateView(parent, name, context, checkNotNull(attrs) { "Should never happen!" }), name, context, attrs ) } }