Skip to content

Commit

Permalink
Better propagate nullability for AttributeSet (#86)
Browse files Browse the repository at this point in the history
These can be nullable everywhere except layout/xml inflation
  • Loading branch information
ZacSweers committed Apr 5, 2023
1 parent 6bea1df commit ccf6d43
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
Expand Up @@ -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?
}
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
)
Expand Down
Expand Up @@ -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<out View>, attrs: AttributeSet): View? {
fun create(context: Context, clazz: Class<out View>, attrs: AttributeSet?): View? {
return inflate(InflateRequest(
context = context,
name = clazz.name,
Expand Down Expand Up @@ -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<out View>, attrs: AttributeSet): View? {
fun create(context: Context, clazz: Class<out View>, attrs: AttributeSet?): View? {
@Suppress("DEPRECATION_ERROR")
return get()
.inflate(InflateRequest(
Expand Down
Expand Up @@ -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<out View>
Expand Down
Expand Up @@ -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?
}
Expand Up @@ -40,7 +40,6 @@ internal class `-ViewPumpLayoutInflater`(
setUpLayoutFactories(cloned)
}


/**
* We use this for internal cloning to be a little more efficient with memory.
*/
Expand Down Expand Up @@ -147,7 +146,7 @@ internal class `-ViewPumpLayoutInflater`(
view: View,
name: String,
context: Context,
attrs: AttributeSet
attrs: AttributeSet?
): View? {
return viewPump
.inflate(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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.
Expand Down Expand Up @@ -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) }
}
}

Expand Down Expand Up @@ -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) }
}
}

Expand Down Expand Up @@ -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
)
}
}
Expand Down

0 comments on commit ccf6d43

Please sign in to comment.