From 7d01060a8bf12342a321ec286116e4e20e84a9ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateo=20Guzm=C3=A1n?= Date: Mon, 30 Jun 2025 15:54:57 -0700 Subject: [PATCH] Kotlin: fix static code analysis weak warnings (6/n) (#52338) Summary: Static code analysis reports several weak warnings, many of which seem to be leftovers after Kotlin migration. This PR addresses quite a few: - [Accessor call that can be replaced with property access syntax](https://www.jetbrains.com/help/inspectopedia/UsePropertyAccessSyntax.html) ## Changelog: [INTERNAL] - Kotlin: fix static code analysis weak warnings (6/n) Pull Request resolved: https://github.com/facebook/react-native/pull/52338 Test Plan: ```sh yarn android yarn test-android ``` Reviewed By: NickGerleman Differential Revision: D77504913 Pulled By: cortinico fbshipit-source-id: 62661ba6adafb7893ce27811357020966d5ea4c1 --- .../modules/accessibilityinfo/AccessibilityInfoModule.kt | 6 +++--- .../com/facebook/react/modules/appstate/AppStateModule.kt | 4 ++-- .../facebook/react/modules/camera/ImageStoreManager.kt | 2 +- .../react/modules/core/DeviceEventManagerModule.kt | 2 +- .../react/modules/core/HeadlessJsTaskSupportModule.kt | 4 ++-- .../com/facebook/react/modules/debug/SourceCodeModule.kt | 2 +- .../react/modules/i18nmanager/I18nManagerModule.kt | 8 ++++---- .../com/facebook/react/modules/image/ImageLoaderModule.kt | 4 ++-- .../com/facebook/react/modules/intent/IntentModule.kt | 8 ++++---- .../react/modules/permissions/PermissionsModule.kt | 2 +- .../facebook/react/modules/sound/SoundManagerModule.kt | 2 +- .../java/com/facebook/react/modules/toast/ToastModule.kt | 8 +++----- .../facebook/react/modules/vibration/VibrationModule.kt | 4 ++-- 13 files changed, 27 insertions(+), 29 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt index 30460b78929..8fa27804016 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt @@ -54,7 +54,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) : } override fun onChange(selfChange: Boolean, uri: Uri?) { - if (getReactApplicationContext().hasActiveReactInstance()) { + if (reactApplicationContext.hasActiveReactInstance()) { updateAndSendReduceMotionChangeEvent() } } @@ -67,7 +67,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) : } override fun onChange(selfChange: Boolean, uri: Uri?) { - if (getReactApplicationContext().hasActiveReactInstance()) { + if (reactApplicationContext.hasActiveReactInstance()) { updateAndSendHighTextContrastChangeEvent() } } @@ -90,7 +90,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) : val appContext = context.applicationContext accessibilityManager = appContext.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager - contentResolver = getReactApplicationContext().getContentResolver() + contentResolver = reactApplicationContext.contentResolver touchExplorationEnabled = accessibilityManager.isTouchExplorationEnabled accessibilityServiceEnabled = accessibilityManager.isEnabled reduceMotionEnabled = isReduceMotionEnabledValue diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt index 16711705455..656f601f292 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt @@ -61,7 +61,7 @@ internal class AppStateModule(reactContext: ReactApplicationContext) : Arguments.createMap().apply { putString("app_state", appState) } private fun sendEvent(eventName: String, data: Any?) { - val reactApplicationContext = getReactApplicationContext() ?: return + val reactApplicationContext = reactApplicationContext ?: return // We don't gain anything interesting from logging here, and it's an extremely common // race condition for an AppState event to be triggered as the Catalyst instance is being // set up or torn down. So, just fail silently here. @@ -85,7 +85,7 @@ internal class AppStateModule(reactContext: ReactApplicationContext) : override fun invalidate() { super.invalidate() - getReactApplicationContext().removeLifecycleEventListener(this) + reactApplicationContext.removeLifecycleEventListener(this) } companion object { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt index 645931b766d..9b87f221123 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt @@ -36,7 +36,7 @@ internal class ImageStoreManager(reactContext: ReactApplicationContext) : val executor = Executors.newSingleThreadExecutor() executor.execute { try { - val contentResolver = getReactApplicationContext().getContentResolver() + val contentResolver = reactApplicationContext.contentResolver val parsedUri = Uri.parse(uri) val inputStream = contentResolver.openInputStream(parsedUri) as InputStream try { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt index a7c1475e89a..0f2eb060f05 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt @@ -55,7 +55,7 @@ public open class DeviceEventManagerModule( // There should be no need to check if the catalyst instance is alive. After initialization // the thread instances cannot be null, and scheduling on a thread after ReactApplicationContext // teardown is a noop. - getReactApplicationContext().runOnUiQueueThread(invokeDefaultBackPressRunnable) + reactApplicationContext.runOnUiQueueThread(invokeDefaultBackPressRunnable) } public companion object { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.kt index 95d560407b8..191c0f4b522 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.kt @@ -23,7 +23,7 @@ internal open class HeadlessJsTaskSupportModule(reactContext: ReactApplicationCo NativeHeadlessJsTaskSupportSpec(reactContext) { override fun notifyTaskRetry(taskIdDouble: Double, promise: Promise) { val taskId = taskIdDouble.toInt() - val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(getReactApplicationContext()) + val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactApplicationContext) if (headlessJsTaskContext.isTaskRunning(taskId)) { val retryPosted = headlessJsTaskContext.retryTask(taskId) promise.resolve(retryPosted) @@ -38,7 +38,7 @@ internal open class HeadlessJsTaskSupportModule(reactContext: ReactApplicationCo override fun notifyTaskFinished(taskIdDouble: Double) { val taskId = taskIdDouble.toInt() - val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(getReactApplicationContext()) + val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactApplicationContext) if (headlessJsTaskContext.isTaskRunning(taskId)) { headlessJsTaskContext.finishTask(taskId) } else { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.kt index 73e69486536..0c695f53f40 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.kt @@ -22,7 +22,7 @@ public class SourceCodeModule(reactContext: ReactApplicationContext) : mapOf( "scriptURL" to Assertions.assertNotNull( - getReactApplicationContext().getSourceURL(), + reactApplicationContext.getSourceURL(), "No source URL loaded, have you initialised the instance?")) public companion object { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.kt index 7551edda59f..5b79e3f0200 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.kt @@ -16,7 +16,7 @@ import com.facebook.react.module.annotations.ReactModule internal class I18nManagerModule(context: ReactApplicationContext?) : NativeI18nManagerSpec(context) { override fun getTypedExportedConstants(): Map { - val context = getReactApplicationContext() + val context = reactApplicationContext val locale = context.resources.configuration.locales[0] return mapOf( @@ -26,15 +26,15 @@ internal class I18nManagerModule(context: ReactApplicationContext?) : } override fun allowRTL(value: Boolean) { - I18nUtil.instance.allowRTL(getReactApplicationContext(), value) + I18nUtil.instance.allowRTL(reactApplicationContext, value) } override fun forceRTL(value: Boolean) { - I18nUtil.instance.forceRTL(getReactApplicationContext(), value) + I18nUtil.instance.forceRTL(reactApplicationContext, value) } override fun swapLeftAndRightInRTL(value: Boolean) { - I18nUtil.instance.swapLeftAndRightInRTL(getReactApplicationContext(), value) + I18nUtil.instance.swapLeftAndRightInRTL(reactApplicationContext, value) } companion object { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt index f2a5979ce5a..ae26dfd4882 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt @@ -81,7 +81,7 @@ internal class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventL promise.reject(ERROR_INVALID_URI, "Cannot get the size of an image for an empty URI") return } - val source = ImageSource(getReactApplicationContext(), uriString) + val source = ImageSource(reactApplicationContext, uriString) val request: ImageRequest = ImageRequestBuilder.newBuilderWithSource(source.uri).build() val dataSource: DataSource> = this.imagePipeline.fetchDecodedImage(request, this.callerContext) @@ -226,7 +226,7 @@ internal class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventL override fun queryCache(uris: ReadableArray, promise: Promise) { // perform cache interrogation in async task as disk cache checks are expensive @Suppress("DEPRECATION", "StaticFieldLeak") - object : GuardedAsyncTask(getReactApplicationContext()) { + object : GuardedAsyncTask(reactApplicationContext) { override fun doInBackgroundGuarded(vararg params: Void) { val result = buildReadableMap { val imagePipeline: ImagePipeline = this@ImageLoaderModule.imagePipeline diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.kt index 0dacd30048a..2818ba884bc 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.kt @@ -34,7 +34,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) : synchronized(this) { pendingOpenURLPromises.clear() initialURLListener - ?.let { listener -> getReactApplicationContext().removeLifecycleEventListener(listener) } + ?.let { listener -> reactApplicationContext.removeLifecycleEventListener(listener) } .also { initialURLListener = null } } super.invalidate() @@ -47,7 +47,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) : */ override fun getInitialURL(promise: Promise) { try { - val currentActivity = getReactApplicationContext().getCurrentActivity() + val currentActivity = reactApplicationContext.getCurrentActivity() if (currentActivity == null) { waitForActivityAndGetInitialURL(promise) return @@ -82,7 +82,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) : initialURLListener = object : LifecycleEventListener { override fun onHostResume() { - getReactApplicationContext().removeLifecycleEventListener(this) + reactApplicationContext.removeLifecycleEventListener(this) synchronized(this@IntentModule) { for (pendingPromise in pendingOpenURLPromises) { getInitialURL(pendingPromise) @@ -96,7 +96,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) : override fun onHostDestroy() = Unit } - getReactApplicationContext().addLifecycleEventListener(initialURLListener) + reactApplicationContext.addLifecycleEventListener(initialURLListener) } /** diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt index a948ce6f1e3..efbc38c7fba 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt @@ -38,7 +38,7 @@ public class PermissionsModule(reactContext: ReactApplicationContext?) : * permission had been granted, false otherwise. See [Activity.checkSelfPermission]. */ public override fun checkPermission(permission: String, promise: Promise): Unit { - val context = getReactApplicationContext().getBaseContext() + val context = reactApplicationContext.baseContext promise.resolve(context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.kt index a005a293278..dcc11ca5379 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.kt @@ -20,7 +20,7 @@ internal class SoundManagerModule(reactContext: ReactApplicationContext?) : NativeSoundManagerSpec(reactContext) { override fun playTouchSound() { val audioManager = - getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager + reactApplicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK) } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.kt index 29d704712dd..d0e03091a85 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.kt @@ -31,16 +31,14 @@ internal class ToastModule(reactContext: ReactApplicationContext) : override fun show(message: String?, durationDouble: Double) { val duration = durationDouble.toInt() - UiThreadUtil.runOnUiThread { - Toast.makeText(getReactApplicationContext(), message, duration).show() - } + UiThreadUtil.runOnUiThread { Toast.makeText(reactApplicationContext, message, duration).show() } } override fun showWithGravity(message: String?, durationDouble: Double, gravityDouble: Double) { val duration = durationDouble.toInt() val gravity = gravityDouble.toInt() UiThreadUtil.runOnUiThread { - val toast = Toast.makeText(getReactApplicationContext(), message, duration) + val toast = Toast.makeText(reactApplicationContext, message, duration) toast.setGravity(gravity, 0, 0) toast.show() } @@ -58,7 +56,7 @@ internal class ToastModule(reactContext: ReactApplicationContext) : val xOffset = xOffsetDouble.toInt() val yOffset = yOffsetDouble.toInt() UiThreadUtil.runOnUiThread { - val toast = Toast.makeText(getReactApplicationContext(), message, duration) + val toast = Toast.makeText(reactApplicationContext, message, duration) toast.setGravity(gravity, xOffset, yOffset) toast.show() } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt index 029c4380633..1f47a52f54f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt @@ -54,12 +54,12 @@ internal class VibrationModule(reactContext: ReactApplicationContext) : private fun getVibrator(): Vibrator? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val vibratorManager = - getReactApplicationContext().getSystemService(Context.VIBRATOR_MANAGER_SERVICE) + reactApplicationContext.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager? vibratorManager?.defaultVibrator } else { @Suppress("DEPRECATION") - getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE) as Vibrator? + reactApplicationContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator? } companion object {