Skip to content

Commit 249b13f

Browse files
cortinicometa-codesync[bot]
authored andcommitted
Remove !! from YogaNodeJNIBase
Summary: Follow-up to D104666335 (Yoga Java→Kotlin migration of `YogaNodeJNIBase`). The original migration kept several `!!` (not-null assertion) operators. Per reviewer feedback, replace them with safer Kotlin idioms: - `addChildAt`: replace the `if (children == null) { children = ArrayList(4) } children!!.add(...)` pattern with a single `val list = children ?: ArrayList<YogaNodeJNIBase>(4).also { children = it }` so the local `val` is statically non-null and lazy-initializes the backing field in one expression. - `swapChildAt`: capture `children` into a local `val` via `checkNotNull(children) { "YogaNode does not have children" }` instead of `children!!.removeAt(...)` / `children!!.add(...)`. Surfaces a clearer `IllegalStateException` instead of a bare `KotlinNullPointerException`. - `cloneWithChildren`: collapse `if (clonedYogaNode.children != null) { clonedYogaNode.children = ArrayList(clonedYogaNode.children!!) }` into `clonedYogaNode.children?.let { clonedYogaNode.children = ArrayList(it) }`. - `measure`: fold the existing `if (!isMeasureDefined) throw RuntimeException(...)` guard into `val mf = checkNotNull(measureFunction) { "Measure function isn't defined!" }`. Same behavior, no double-read of the mutable property. - `baseline`: convert the expression body using `baselineFunction!!.baseline(...)` to a block body that uses `checkNotNull(baselineFunction) { "Baseline function isn't defined!" }`. Yields a clearer error than a bare NPE if `baseline()` is ever invoked when no `YogaBaselineFunction` was set. Both mirrored copies (`xplat/yoga/...` and `xplat/js/react-native-github/...`) are kept in sync. Changelog: [Internal] - Differential Revision: D105300348
1 parent 35e842f commit 249b13f

1 file changed

Lines changed: 12 additions & 16 deletions

File tree

java/com/facebook/yoga/YogaNodeJNIBase.kt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ public abstract class YogaNodeJNIBase : YogaNode, Cloneable {
7070
throw IllegalStateException("Child already has a parent, it must be removed first.")
7171
}
7272

73-
if (children == null) {
74-
children = ArrayList(4)
75-
}
76-
children!!.add(i, child)
73+
val list = children ?: ArrayList<YogaNodeJNIBase>(4).also { children = it }
74+
list.add(i, child)
7775
child.owner = this
7876
YogaNative.jni_YGNodeInsertChildJNI(nativePointer, child.nativePointer, i)
7977
}
@@ -89,18 +87,17 @@ public abstract class YogaNodeJNIBase : YogaNode, Cloneable {
8987
if (newChild !is YogaNodeJNIBase) {
9088
return
9189
}
92-
children!!.removeAt(position)
93-
children!!.add(position, newChild)
90+
val list = checkNotNull(children) { "YogaNode does not have children" }
91+
list.removeAt(position)
92+
list.add(position, newChild)
9493
newChild.owner = this
9594
YogaNative.jni_YGNodeSwapChildJNI(nativePointer, newChild.nativePointer, position)
9695
}
9796

9897
override fun cloneWithChildren(): YogaNodeJNIBase {
9998
try {
10099
val clonedYogaNode = super.clone() as YogaNodeJNIBase
101-
if (clonedYogaNode.children != null) {
102-
clonedYogaNode.children = ArrayList(clonedYogaNode.children!!)
103-
}
100+
clonedYogaNode.children?.let { clonedYogaNode.children = ArrayList(it) }
104101
val clonedNativePointer = YogaNative.jni_YGNodeCloneJNI(nativePointer)
105102
clonedYogaNode.owner = null
106103
clonedYogaNode.nativePointer = clonedNativePointer
@@ -540,11 +537,8 @@ public abstract class YogaNodeJNIBase : YogaNode, Cloneable {
540537
// methods are final by default, which enforces this constraint.
541538
@DoNotStrip
542539
public fun measure(width: Float, widthMode: Int, height: Float, heightMode: Int): Long {
543-
if (!isMeasureDefined) {
544-
throw RuntimeException("Measure function isn't defined!")
545-
}
546-
547-
return measureFunction!!.measure(
540+
val mf = checkNotNull(measureFunction) { "Measure function isn't defined!" }
541+
return mf.measure(
548542
this,
549543
width,
550544
YogaMeasureMode.fromInt(widthMode),
@@ -560,8 +554,10 @@ public abstract class YogaNodeJNIBase : YogaNode, Cloneable {
560554

561555
// Same JNI jmethodid caching concern as measure() — must not be overridden.
562556
@DoNotStrip
563-
public fun baseline(width: Float, height: Float): Float =
564-
baselineFunction!!.baseline(this, width, height)
557+
public fun baseline(width: Float, height: Float): Float {
558+
val bf = checkNotNull(baselineFunction) { "Baseline function isn't defined!" }
559+
return bf.baseline(this, width, height)
560+
}
565561

566562
override val isMeasureDefined: Boolean
567563
get() = measureFunction != null

0 commit comments

Comments
 (0)