Skip to content

Commit 3b53aca

Browse files
GijsWeteringsfacebook-github-bot
authored andcommitted
Fix Nullsafe FIXMEs for DevSupportManagerBase.java (#50068)
Summary: Note: this involved tightening up some signatures in Kotlin files Gone trough all the FIXMEs added in the previous diff by the nullsafe tool, marked the class as nullsafe and ensured no remaining violations. Changelog: [Android][Fixed] Made DevSupportManagerBase.java nullsafe Reviewed By: mdvacca Differential Revision: D71126381
1 parent c4e3068 commit 3b53aca

File tree

5 files changed

+59
-63
lines changed

5 files changed

+59
-63
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ public void downloadBundleFromURL(
327327
DevBundleDownloadListener callback,
328328
File outputFile,
329329
String bundleURL,
330-
BundleDownloader.BundleInfo bundleInfo) {
330+
@Nullable BundleDownloader.BundleInfo bundleInfo) {
331331
mBundleDownloader.downloadBundleFromURL(callback, outputFile, bundleURL, bundleInfo);
332332
}
333333

334334
public void downloadBundleFromURL(
335335
DevBundleDownloadListener callback,
336336
File outputFile,
337337
String bundleURL,
338-
BundleDownloader.BundleInfo bundleInfo,
338+
@Nullable BundleDownloader.BundleInfo bundleInfo,
339339
Request.Builder requestBuilder) {
340340
mBundleDownloader.downloadBundleFromURL(
341341
callback, outputFile, bundleURL, bundleInfo, requestBuilder);

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java

+37-41
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import androidx.annotation.UiThread;
3535
import com.facebook.common.logging.FLog;
3636
import com.facebook.infer.annotation.Assertions;
37+
import com.facebook.infer.annotation.Nullsafe;
3738
import com.facebook.react.R;
3839
import com.facebook.react.bridge.DefaultJSExceptionHandler;
3940
import com.facebook.react.bridge.JSBundleLoader;
@@ -74,6 +75,7 @@
7475
import java.util.Map;
7576
import java.util.Set;
7677

78+
@Nullsafe(Nullsafe.Mode.LOCAL)
7779
public abstract class DevSupportManagerBase implements DevSupportManager {
7880

7981
public interface CallbackWithBundleLoader {
@@ -222,16 +224,13 @@ private void logJSException(Exception e) {
222224

223225
if (e instanceof JavascriptException) {
224226
FLog.e(ReactConstants.TAG, "Exception in native call from JS", e);
225-
showNewError(
226-
// NULLSAFE_FIXME[Nullable Dereference]
227-
e.getMessage().toString(), new StackFrame[] {}, JSEXCEPTION_ERROR_COOKIE, ErrorType.JS);
227+
showNewError(e.getMessage(), new StackFrame[] {}, JSEXCEPTION_ERROR_COOKIE, ErrorType.JS);
228228
} else {
229229
showNewJavaError(message.toString(), e);
230230
}
231231
}
232232

233233
@Override
234-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
235234
public void showNewJavaError(@Nullable String message, Throwable e) {
236235
FLog.e(ReactConstants.TAG, "Exception in native call", e);
237236
showNewError(
@@ -244,19 +243,17 @@ public void showNewJavaError(@Nullable String message, Throwable e) {
244243
* called.
245244
*/
246245
@Override
247-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
248246
public void addCustomDevOption(String optionName, DevOptionHandler optionHandler) {
249247
mCustomDevOptions.put(optionName, optionHandler);
250248
}
251249

252250
@Override
253-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
254-
public void showNewJSError(String message, ReadableArray details, int errorCookie) {
251+
public void showNewJSError(
252+
@Nullable String message, @Nullable ReadableArray details, int errorCookie) {
255253
showNewError(message, StackTraceHelper.convertJsStackTrace(details), errorCookie, ErrorType.JS);
256254
}
257255

258256
@Override
259-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
260257
public void registerErrorCustomizer(ErrorCustomizer errorCustomizer) {
261258
if (mErrorCustomizers == null) {
262259
mErrorCustomizers = new ArrayList<>();
@@ -265,7 +262,6 @@ public void registerErrorCustomizer(ErrorCustomizer errorCustomizer) {
265262
}
266263

267264
@Override
268-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
269265
public Pair<String, StackFrame[]> processErrorCustomizers(Pair<String, StackFrame[]> errorInfo) {
270266
if (mErrorCustomizers != null) {
271267
for (ErrorCustomizer errorCustomizer : mErrorCustomizers) {
@@ -287,14 +283,14 @@ public void hideRedboxDialog() {
287283
mRedBoxSurfaceDelegate.hide();
288284
}
289285

290-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
291286
public @Nullable View createRootView(String appKey) {
292287
return mReactInstanceDevHelper.createRootView(appKey);
293288
}
294289

295-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
296-
public void destroyRootView(View rootView) {
297-
mReactInstanceDevHelper.destroyRootView(rootView);
290+
public void destroyRootView(@Nullable View rootView) {
291+
if (rootView != null) {
292+
mReactInstanceDevHelper.destroyRootView(rootView);
293+
}
298294
}
299295

300296
private void hideDevOptionsDialog() {
@@ -526,14 +522,12 @@ public View getView(int position, @Nullable View convertView, ViewGroup parent)
526522
mDevOptionsDialog =
527523
new AlertDialog.Builder(context)
528524
.setCustomTitle(header)
529-
// NULLSAFE_FIXME[Not Vetted Third-Party]
530525
.setAdapter(
531526
adapter,
532527
(dialog, which) -> {
533528
optionHandlers[which].onOptionSelected();
534529
mDevOptionsDialog = null;
535530
})
536-
// NULLSAFE_FIXME[Not Vetted Third-Party]
537531
.setOnCancelListener(dialog -> mDevOptionsDialog = null)
538532
.create();
539533
mDevOptionsDialog.show();
@@ -544,11 +538,10 @@ public View getView(int position, @Nullable View convertView, ViewGroup parent)
544538
}
545539
}
546540

547-
private String getJSExecutorDescription() {
541+
private @Nullable String getJSExecutorDescription() {
548542
try {
549543
return getReactInstanceDevHelper().getJavaScriptExecutorFactory().toString();
550544
} catch (IllegalStateException e) {
551-
// NULLSAFE_FIXME[Return Not Nullable]
552545
return null;
553546
}
554547
}
@@ -634,19 +627,20 @@ public boolean hasUpToDateJSBundleInCache() {
634627
if (mIsDevSupportEnabled && mJSBundleDownloadedFile.exists()) {
635628
try {
636629
String packageName = mApplicationContext.getPackageName();
637-
PackageInfo thisPackage =
638-
// NULLSAFE_FIXME[Nullable Dereference]
639-
mApplicationContext.getPackageManager().getPackageInfo(packageName, 0);
640-
if (mJSBundleDownloadedFile.lastModified() > thisPackage.lastUpdateTime) {
641-
// Base APK has not been updated since we downloaded JS, but if app is using exopackage
642-
// it may only be a single dex that has been updated. We check for exopackage dir update
643-
// time in that case.
644-
File exopackageDir =
645-
new File(String.format(Locale.US, EXOPACKAGE_LOCATION_FORMAT, packageName));
646-
if (exopackageDir.exists()) {
647-
return mJSBundleDownloadedFile.lastModified() > exopackageDir.lastModified();
630+
PackageManager packageManager = mApplicationContext.getPackageManager();
631+
if (packageManager != null) {
632+
PackageInfo thisPackage = packageManager.getPackageInfo(packageName, 0);
633+
if (mJSBundleDownloadedFile.lastModified() > thisPackage.lastUpdateTime) {
634+
// Base APK has not been updated since we downloaded JS, but if app is using exopackage
635+
// it may only be a single dex that has been updated. We check for exopackage dir update
636+
// time in that case.
637+
File exopackageDir =
638+
new File(String.format(Locale.US, EXOPACKAGE_LOCATION_FORMAT, packageName));
639+
if (exopackageDir.exists()) {
640+
return mJSBundleDownloadedFile.lastModified() > exopackageDir.lastModified();
641+
}
642+
return true;
648643
}
649-
return true;
650644
}
651645
} catch (PackageManager.NameNotFoundException e) {
652646
// Ignore this error and just fallback to loading JS from assets
@@ -675,8 +669,10 @@ private void resetCurrentContext(@Nullable ReactContext reactContext) {
675669
if (mCurrentReactContext != null) {
676670
try {
677671
URL sourceUrl = new URL(getSourceUrl());
678-
// NULLSAFE_FIXME[Nullable Dereference]
679-
String path = sourceUrl.getPath().substring(1); // strip initial slash in path
672+
String path = sourceUrl.getPath();
673+
if (path != null) {
674+
path = path.substring(1); // strip initial slash in path
675+
}
680676
String host = sourceUrl.getHost();
681677
String scheme = sourceUrl.getProtocol();
682678
int port = sourceUrl.getPort() != -1 ? sourceUrl.getPort() : sourceUrl.getDefaultPort();
@@ -798,7 +794,6 @@ public void onProgress(
798794
}
799795

800796
@Override
801-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
802797
public void onFailure(Exception cause) {
803798
UiThreadUtil.runOnUiThread(
804799
DevSupportManagerBase.this::hideSplitBundleDevLoadingView);
@@ -807,7 +802,6 @@ public void onFailure(Exception cause) {
807802
},
808803
bundleFile,
809804
bundleUrl,
810-
// NULLSAFE_FIXME[Parameter Not Nullable]
811805
null);
812806
});
813807
}
@@ -836,7 +830,6 @@ public void isPackagerRunning(final PackagerStatusCallback callback) {
836830
}
837831

838832
@Override
839-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
840833
public @Nullable File downloadBundleResourceFromUrlSync(
841834
final String resourceURL, final File outputFile) {
842835
return mDevServerHelper.downloadBundleResourceFromUrlSync(resourceURL, outputFile);
@@ -904,7 +897,6 @@ public void onProgress(
904897
}
905898

906899
@Override
907-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
908900
public void onFailure(final Exception cause) {
909901
hideDevLoadingView();
910902
if (mBundleDownloadListener != null) {
@@ -991,10 +983,16 @@ private void reload() {
991983

992984
// start shake gesture detector
993985
if (!mIsShakeDetectorStarted) {
994-
mShakeDetector.start(
995-
// NULLSAFE_FIXME[Parameter Not Nullable]
996-
(SensorManager) mApplicationContext.getSystemService(Context.SENSOR_SERVICE));
997-
mIsShakeDetectorStarted = true;
986+
SensorManager sensorManager =
987+
(SensorManager) mApplicationContext.getSystemService(Context.SENSOR_SERVICE);
988+
if (sensorManager != null) {
989+
mShakeDetector.start(sensorManager);
990+
mIsShakeDetectorStarted = true;
991+
} else {
992+
FLog.w(
993+
ReactConstants.TAG,
994+
"Couldn't register shake gesture detector, sensor service is null");
995+
}
998996
}
999997

1000998
// register reload app broadcast receiver
@@ -1078,7 +1076,6 @@ private static String getReloadAppAction(Context context) {
10781076
}
10791077

10801078
@Override
1081-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
10821079
public void setPackagerLocationCustomizer(
10831080
DevSupportManager.PackagerLocationCustomizer packagerLocationCustomizer) {
10841081
mPackagerLocationCustomizer = packagerLocationCustomizer;
@@ -1090,7 +1087,6 @@ public void setPackagerLocationCustomizer(
10901087
}
10911088

10921089
@Override
1093-
// NULLSAFE_FIXME[Inconsistent Subclass Parameter Annotation]
10941090
public @Nullable SurfaceDelegate createSurfaceDelegate(String moduleName) {
10951091
if (mSurfaceDelegateFactory == null) {
10961092
return null;

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/ReleaseDevSupportManager.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public open class ReleaseDevSupportManager : DevSupportManager {
3636

3737
private val defaultJSExceptionHandler: DefaultJSExceptionHandler = DefaultJSExceptionHandler()
3838

39-
public override fun showNewJavaError(message: String?, e: Throwable?): Unit = Unit
39+
public override fun showNewJavaError(message: String?, e: Throwable): Unit = Unit
4040

4141
public override fun addCustomDevOption(
42-
optionName: String?,
43-
optionHandler: DevOptionHandler?
42+
optionName: String,
43+
optionHandler: DevOptionHandler
4444
): Unit = Unit
4545

4646
public override fun showNewJSError(
@@ -49,7 +49,7 @@ public open class ReleaseDevSupportManager : DevSupportManager {
4949
errorCookie: Int
5050
): Unit = Unit
5151

52-
public override fun createRootView(appKey: String?): View? = null
52+
public override fun createRootView(appKey: String): View? = null
5353

5454
public override fun destroyRootView(rootView: View?): Unit = Unit
5555

@@ -111,7 +111,7 @@ public open class ReleaseDevSupportManager : DevSupportManager {
111111

112112
public override fun downloadBundleResourceFromUrlSync(
113113
resourceURL: String,
114-
outputFile: File?
114+
outputFile: File
115115
): File? = null
116116

117117
public override val lastErrorTitle: String?
@@ -125,14 +125,14 @@ public open class ReleaseDevSupportManager : DevSupportManager {
125125

126126
public override val lastErrorCookie: Int = 0
127127

128-
public override fun registerErrorCustomizer(errorCustomizer: ErrorCustomizer?): Unit = Unit
128+
public override fun registerErrorCustomizer(errorCustomizer: ErrorCustomizer): Unit = Unit
129129

130130
public override fun processErrorCustomizers(
131-
errorInfo: Pair<String, Array<StackFrame>>?
132-
): Pair<String, Array<StackFrame>>? = errorInfo
131+
errorInfo: Pair<String, Array<StackFrame>>
132+
): Pair<String, Array<StackFrame>> = errorInfo
133133

134134
public override fun setPackagerLocationCustomizer(
135-
packagerLocationCustomizer: PackagerLocationCustomizer?
135+
packagerLocationCustomizer: PackagerLocationCustomizer
136136
): Unit = Unit
137137

138138
public override fun handleException(e: Exception) {
@@ -145,7 +145,7 @@ public open class ReleaseDevSupportManager : DevSupportManager {
145145
public override val currentReactContext: ReactContext?
146146
get() = null
147147

148-
public override fun createSurfaceDelegate(moduleName: String?): SurfaceDelegate? = null
148+
public override fun createSurfaceDelegate(moduleName: String): SurfaceDelegate? = null
149149

150150
public override fun openDebugger(): Unit = Unit
151151

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/interfaces/DevBundleDownloadListener.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ public interface DevBundleDownloadListener {
1212

1313
public fun onProgress(status: String?, done: Int?, total: Int?)
1414

15-
public fun onFailure(cause: Exception?)
15+
public fun onFailure(cause: Exception)
1616
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/interfaces/DevSupportManager.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public interface DevSupportManager : JSExceptionHandler {
3838

3939
public var devSupportEnabled: Boolean
4040

41-
public fun showNewJavaError(message: String?, e: Throwable?)
41+
public fun showNewJavaError(message: String?, e: Throwable)
4242

43-
public fun addCustomDevOption(optionName: String?, optionHandler: DevOptionHandler?)
43+
public fun addCustomDevOption(optionName: String, optionHandler: DevOptionHandler)
4444

45-
public fun createRootView(appKey: String?): View?
45+
public fun createRootView(appKey: String): View?
4646

4747
public fun destroyRootView(rootView: View?)
4848

@@ -78,23 +78,23 @@ public interface DevSupportManager : JSExceptionHandler {
7878

7979
public fun toggleElementInspector()
8080

81-
public fun downloadBundleResourceFromUrlSync(resourceURL: String, outputFile: File?): File?
81+
public fun downloadBundleResourceFromUrlSync(resourceURL: String, outputFile: File): File?
8282

83-
public fun registerErrorCustomizer(errorCustomizer: ErrorCustomizer?)
83+
public fun registerErrorCustomizer(errorCustomizer: ErrorCustomizer)
8484

8585
public fun processErrorCustomizers(
86-
errorInfo: Pair<String, Array<StackFrame>>?
87-
): Pair<String, Array<StackFrame>>?
86+
errorInfo: Pair<String, Array<StackFrame>>
87+
): Pair<String, Array<StackFrame>>
8888

89-
public fun setPackagerLocationCustomizer(packagerLocationCustomizer: PackagerLocationCustomizer?)
89+
public fun setPackagerLocationCustomizer(packagerLocationCustomizer: PackagerLocationCustomizer)
9090

9191
/**
9292
* Create the surface delegate that the provided module should use to interact with
9393
*
9494
* @param moduleName the module name that helps decide which surface it should interact with
9595
* @return a [SurfaceDelegate] instance
9696
*/
97-
public fun createSurfaceDelegate(moduleName: String?): SurfaceDelegate?
97+
public fun createSurfaceDelegate(moduleName: String): SurfaceDelegate?
9898

9999
/** Attempt to open the JS debugger on the host machine. */
100100
public fun openDebugger()

0 commit comments

Comments
 (0)