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) + } + } +}