From a3e25b23c6be08ff648f73aaabeb3dae70dae97f Mon Sep 17 00:00:00 2001 From: Daniel Abramowitz Date: Thu, 26 May 2022 08:13:22 -0700 Subject: [PATCH] Improve exception thrown when classes can't compile to be more clear (#33897) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/33897 Now the exception will display the class which caused the exception as well as helpful information as to why. We've seen this happen a bunch due and have been very confused by the error message. It turns out that this processor runs before the classes listed are compiled. This means that if there's a compile error (or a missing import) the user will only see that this processor crashed, and not the compile error. The additional information in the error is: `java.lang.RuntimeException: Could not load classes set in ReactModuleList.nativeModules. Check that they exist and are imported correctly on class: com.meta.x.y.ReactPackage` In this case, `com.meta.x.y.ReactPackage` is the class which needs to be fixed. Before, the error message made no mention of this class or the annotation. Changelog: [Internal] This will change the way the annotation processor crashes. It will throw a RuntimeException instead of a ClassCastException. Reviewed By: javache Differential Revision: D36606279 fbshipit-source-id: aedf9682286fba49e23716b7eda16b2dd3b13422 --- .../main/java/com/facebook/react/module/processing/BUCK | 2 -- .../module/processing/ReactModuleSpecProcessor.java | 9 ++++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK b/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK index 6b2ab0bed76..e578bc87e9e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK @@ -22,8 +22,6 @@ rn_java_library( source = "8", target = "8", deps = [ - react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"), - react_native_target("java/com/facebook/react/common:common"), react_native_dep("third-party/java/infer-annotations:infer-annotations"), react_native_dep("third-party/java/javapoet:javapoet"), react_native_dep("third-party/java/jsr-305:jsr-305"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java b/ReactAndroid/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java index 9ea0b1d7fe9..03966127496 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java +++ b/ReactAndroid/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java @@ -10,9 +10,7 @@ package com.facebook.react.module.processing; import static javax.lang.model.element.Modifier.PUBLIC; import static javax.tools.Diagnostic.Kind.ERROR; -import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.SuppressFieldNotInitialized; -import com.facebook.react.common.ReactConstants; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.module.annotations.ReactModuleList; import com.facebook.react.module.model.ReactModuleInfo; @@ -94,9 +92,10 @@ public class ReactModuleSpecProcessor extends AbstractProcessor { try { reactModuleList = typeElement.getAnnotation(ReactModuleList.class); } catch (Exception ex) { - FLog.i( - ReactConstants.TAG, "Could not reactModuleList from typeElement.getAnnotation()", ex); - throw ex; + throw new RuntimeException( + "Could not load classes set in @ReactModuleList.nativeModules. Check that they exist and are imported correctly on class: " + + typeElement.getQualifiedName(), + ex); } if (reactModuleList == null) {