From 0b6cd69dd42864930a9eb10e92aa35868f45d4d1 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Fri, 31 Jan 2020 17:04:02 -0800 Subject: [PATCH] Return null when requesting constants for nonexistent View Managers in RN Android Summary: This diff changes the behavior of UIImplementation.resolveViewManager() to return null instead of throwing an exception when trying to find an unexistent viewManager during the computation of constants for view Managers. The C++/JS code manages exceptions and null results when a view manager doesn't exists, this diff simplifies the way this method operates. changeLog: [internal] Reviewed By: rickhanlonii Differential Revision: D19624423 fbshipit-source-id: df31dcfae9a588bf325b61d529cec6ead59fb19d --- .../react/uimanager/UIImplementation.java | 4 +- .../react/uimanager/ViewManagerRegistry.java | 40 ++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java index 6328a810758072..3bdce77ef36fc0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java @@ -128,8 +128,8 @@ public final ReactShadowNode resolveShadowNode(int reactTag) { return mShadowNodeRegistry.getNode(reactTag); } - protected final ViewManager resolveViewManager(String className) { - return mViewManagers.get(className); + protected final @Nullable ViewManager resolveViewManager(String className) { + return mViewManagers.getViewManagerIfExists(className); } /*package*/ UIViewOperationQueue getUIViewOperationQueue() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java index debd5ecef2e727..73c37c10f12124 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java @@ -42,19 +42,49 @@ public ViewManagerRegistry(Map viewManagerMap) { mViewManagerResolver = null; } + /** + * @param className {@link String} that identifies the {@link ViewManager} inside the {@link + * ViewManagerRegistry}. This methods {@throws IllegalViewOperationException} if there is no + * view manager registered for the className received as a parameter. + * @return the {@link ViewManager} registered to the className received as a parameter + */ public ViewManager get(String className) { ViewManager viewManager = mViewManagers.get(className); if (viewManager != null) { return viewManager; } if (mViewManagerResolver != null) { - viewManager = mViewManagerResolver.getViewManager(className); - if (viewManager != null) { - mViewManagers.put(className, viewManager); - return viewManager; - } + viewManager = getViewManagerFromResolver(className); + if (viewManager != null) return viewManager; throw new IllegalViewOperationException("ViewManagerResolver returned null for " + className); } throw new IllegalViewOperationException("No ViewManager found for class " + className); } + + private @Nullable ViewManager getViewManagerFromResolver(String className) { + @Nullable ViewManager viewManager; + viewManager = mViewManagerResolver.getViewManager(className); + if (viewManager != null) { + mViewManagers.put(className, viewManager); + } + return viewManager; + } + + /** + * @param className {@link String} that identifies the {@link ViewManager} inside the {@link + * ViewManagerRegistry}. + * @return the {@link ViewManager} registered to the className received as a parameter or null if + * there is no ViewManager associated to the className received as a parameter. + */ + @Nullable + ViewManager getViewManagerIfExists(String className) { + ViewManager viewManager = mViewManagers.get(className); + if (viewManager != null) { + return viewManager; + } + if (mViewManagerResolver != null) { + return getViewManagerFromResolver(className); + } + return null; + } }