From fd92b79c40608b6ac8e3a2fdae0363e4284ba33b Mon Sep 17 00:00:00 2001 From: Andrew Datsenko Date: Tue, 10 Mar 2026 15:14:02 -0700 Subject: [PATCH] Convert YogaConfigJNIFinalizer and YogaNodeJNIFinalizer to Kotlin (#1915) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/1915 X-link: https://github.com/facebook/react-native/pull/56021 Changelog: [Internal] Convert the last two Java finalizer classes in the yoga package to Kotlin, continuing the React Native Kotlinification effort. Both classes are simple JNI native memory cleanup wrappers (32 and 36 lines) that extend already-Kotlin base classes. Changes: - Convert `YogaConfigJNIFinalizer.java` → `.kt` (ReactAndroid + xplat/yoga copies) - Convert `YogaNodeJNIFinalizer.java` → `.kt` (ReactAndroid + xplat/yoga copies) - Remove `super.finalize()` calls (no-op in parent chain, and `Object.finalize()` is not directly callable from Kotlin) The `:yoga` Buck target already has `language = "KOTLIN"` and globs both `*.java` and `*.kt`, so no build config changes are needed. Differential Revision: D95872345 --- .../facebook/yoga/YogaConfigJNIFinalizer.java | 31 ---------------- .../facebook/yoga/YogaConfigJNIFinalizer.kt | 30 ++++++++++++++++ .../facebook/yoga/YogaNodeJNIFinalizer.java | 35 ------------------- .../com/facebook/yoga/YogaNodeJNIFinalizer.kt | 33 +++++++++++++++++ 4 files changed, 63 insertions(+), 66 deletions(-) delete mode 100644 java/com/facebook/yoga/YogaConfigJNIFinalizer.java create mode 100644 java/com/facebook/yoga/YogaConfigJNIFinalizer.kt delete mode 100644 java/com/facebook/yoga/YogaNodeJNIFinalizer.java create mode 100644 java/com/facebook/yoga/YogaNodeJNIFinalizer.kt diff --git a/java/com/facebook/yoga/YogaConfigJNIFinalizer.java b/java/com/facebook/yoga/YogaConfigJNIFinalizer.java deleted file mode 100644 index a1fcf69cba..0000000000 --- a/java/com/facebook/yoga/YogaConfigJNIFinalizer.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.yoga; - -public class YogaConfigJNIFinalizer extends YogaConfigJNIBase { - public YogaConfigJNIFinalizer() { - super(); - } - - @Override - protected void finalize() throws Throwable { - try { - freeNatives(); - } finally { - super.finalize(); - } - } - - public void freeNatives() { - if (nativePointer != 0) { - long pointer = nativePointer; - nativePointer = 0; - YogaNative.jni_YGConfigFreeJNI(pointer); - } - } -} diff --git a/java/com/facebook/yoga/YogaConfigJNIFinalizer.kt b/java/com/facebook/yoga/YogaConfigJNIFinalizer.kt new file mode 100644 index 0000000000..8b17152042 --- /dev/null +++ b/java/com/facebook/yoga/YogaConfigJNIFinalizer.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.yoga + +public class YogaConfigJNIFinalizer public constructor() : YogaConfigJNIBase() { + + /* + * This is a valid use of finalize. No other mechanism is appropriate. + * YogaConfigJNIFinalizer exists specifically to release JNI-allocated native + * memory (via jni_YGConfigFreeJNI) when the Java object is garbage collected. + * This is the established pattern for JNI prevented leak classes in Yoga. + */ + @Throws(Throwable::class) + protected fun finalize() { + freeNatives() + } + + public fun freeNatives() { + if (nativePointer != 0L) { + val pointer = nativePointer + nativePointer = 0 + YogaNative.jni_YGConfigFreeJNI(pointer) + } + } +} diff --git a/java/com/facebook/yoga/YogaNodeJNIFinalizer.java b/java/com/facebook/yoga/YogaNodeJNIFinalizer.java deleted file mode 100644 index 23cf2a31e5..0000000000 --- a/java/com/facebook/yoga/YogaNodeJNIFinalizer.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.yoga; - -public class YogaNodeJNIFinalizer extends YogaNodeJNIBase { - public YogaNodeJNIFinalizer() { - super(); - } - - public YogaNodeJNIFinalizer(YogaConfig config) { - super(config); - } - - @Override - protected void finalize() throws Throwable { - try { - freeNatives(); - } finally { - super.finalize(); - } - } - - public void freeNatives() { - if (mNativePointer != 0) { - long nativePointer = mNativePointer; - mNativePointer = 0; - YogaNative.jni_YGNodeFinalizeJNI(nativePointer); - } - } -} diff --git a/java/com/facebook/yoga/YogaNodeJNIFinalizer.kt b/java/com/facebook/yoga/YogaNodeJNIFinalizer.kt new file mode 100644 index 0000000000..64f261127b --- /dev/null +++ b/java/com/facebook/yoga/YogaNodeJNIFinalizer.kt @@ -0,0 +1,33 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.yoga + +public class YogaNodeJNIFinalizer : YogaNodeJNIBase { + public constructor() : super() + + public constructor(config: YogaConfig) : super(config) + + /* + * This is a valid use of finalize. No other mechanism is appropriate. + * YogaNodeJNIFinalizer exists specifically to release JNI-allocated native + * memory (via jni_YGNodeFinalizeJNI) when the Java object is garbage collected. + * This is the established pattern for JNI prevented leak classes in Yoga. + */ + @Throws(Throwable::class) + protected fun finalize() { + freeNatives() + } + + public fun freeNatives() { + if (mNativePointer != 0L) { + val nativePointer = mNativePointer + mNativePointer = 0 + YogaNative.jni_YGNodeFinalizeJNI(nativePointer) + } + } +}