Skip to content

Commit be26f87

Browse files
andrewdacenkometa-codesync[bot]
authored andcommitted
Convert YogaConfigJNIFinalizer and YogaNodeJNIFinalizer to Kotlin (facebook#56021)
Summary: X-link: facebook/yoga#1915 Pull Request resolved: facebook#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
1 parent b8876b5 commit be26f87

File tree

4 files changed

+63
-66
lines changed

4 files changed

+63
-66
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaConfigJNIFinalizer.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.yoga
9+
10+
public class YogaConfigJNIFinalizer public constructor() : YogaConfigJNIBase() {
11+
12+
/*
13+
* This is a valid use of finalize. No other mechanism is appropriate.
14+
* YogaConfigJNIFinalizer exists specifically to release JNI-allocated native
15+
* memory (via jni_YGConfigFreeJNI) when the Java object is garbage collected.
16+
* This is the established pattern for JNI prevented leak classes in Yoga.
17+
*/
18+
@Throws(Throwable::class)
19+
protected fun finalize() {
20+
freeNatives()
21+
}
22+
23+
public fun freeNatives() {
24+
if (nativePointer != 0L) {
25+
val pointer = nativePointer
26+
nativePointer = 0
27+
YogaNative.jni_YGConfigFreeJNI(pointer)
28+
}
29+
}
30+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaNodeJNIFinalizer.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.yoga
9+
10+
public class YogaNodeJNIFinalizer : YogaNodeJNIBase {
11+
public constructor() : super()
12+
13+
public constructor(config: YogaConfig) : super(config)
14+
15+
/*
16+
* This is a valid use of finalize. No other mechanism is appropriate.
17+
* YogaNodeJNIFinalizer exists specifically to release JNI-allocated native
18+
* memory (via jni_YGNodeFinalizeJNI) when the Java object is garbage collected.
19+
* This is the established pattern for JNI prevented leak classes in Yoga.
20+
*/
21+
@Throws(Throwable::class)
22+
protected fun finalize() {
23+
freeNatives()
24+
}
25+
26+
public fun freeNatives() {
27+
if (mNativePointer != 0L) {
28+
val nativePointer = mNativePointer
29+
mNativePointer = 0
30+
YogaNative.jni_YGNodeFinalizeJNI(nativePointer)
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)