From 8e382fd0065af5080e5d69bd5e458f81c2b7d8c5 Mon Sep 17 00:00:00 2001 From: Kathy Gray Date: Thu, 13 Apr 2017 05:19:07 -0700 Subject: [PATCH] Find hasConstant status via preprocessing Reviewed By: javache Differential Revision: D4867563 fbshipit-source-id: 66e4505d142fc4776cd727a025005b43d500b167 --- .../react/animated/NativeAnimatedModule.java | 2 +- .../react/devsupport/JSCSamplingProfiler.java | 2 +- .../com/facebook/react/module/processing/BUCK | 4 ++-- .../processing/ReactModuleSpecProcessor.java | 18 +++++++++++++++++- .../react/modules/appstate/AppStateModule.java | 2 +- .../modules/core/DeviceEventManagerModule.java | 2 +- .../modules/core/ExceptionsManagerModule.java | 2 +- .../core/HeadlessJsTaskSupportModule.java | 2 +- .../facebook/react/modules/core/Timing.java | 2 +- .../modules/debug/AnimationsDebugModule.java | 2 +- .../modules/storage/AsyncStorageModule.java | 2 +- .../debug/DebugComponentOwnershipModule.java | 2 +- 12 files changed, 29 insertions(+), 13 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java index 217e1a827..d4fd0680f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java @@ -71,7 +71,7 @@ import com.facebook.react.uimanager.UIManagerModule; * isolates us from the problems that may be caused by concurrent updates of animated graph while UI * thread is "executing" the animation loop. */ -@ReactModule(name = NativeAnimatedModule.NAME, hasConstants = false) +@ReactModule(name = NativeAnimatedModule.NAME) public class NativeAnimatedModule extends ReactContextBaseJavaModule implements OnBatchCompleteListener, LifecycleEventListener { diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSCSamplingProfiler.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSCSamplingProfiler.java index eeec004f8..049f72c32 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSCSamplingProfiler.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSCSamplingProfiler.java @@ -23,7 +23,7 @@ import com.facebook.react.module.annotations.ReactModule; // This module is being called only by Java via the static method "poke" that // requires it to alreay be initialized, thus we eagerly initialize this module -@ReactModule(name = "JSCSamplingProfiler", needsEagerInit = true, hasConstants = false) +@ReactModule(name = "JSCSamplingProfiler", needsEagerInit = true) public class JSCSamplingProfiler extends ReactContextBaseJavaModule { public interface SamplingProfiler extends JavaScriptModule { void poke(int token); 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 42c9fe88b..063d0f7fa 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK @@ -14,8 +14,8 @@ java_annotation_processor( java_library( name = "processing-lib", srcs = glob(["*.java"]), - source = "7", - target = "7", + source = "8", + target = "8", deps = [ react_native_dep("third-party/java/infer-annotations:infer-annotations"), react_native_dep("third-party/java/javapoet:javapoet"), 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 a4b113dd7..f48f869c2 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,7 +10,9 @@ import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; +import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; import javax.lang.model.type.MirroredTypesException; @@ -19,6 +21,7 @@ import javax.lang.model.util.Elements; import java.io.IOException; import java.util.ArrayList; +import java.util.stream.Stream; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -149,19 +152,32 @@ public class ReactModuleSpecProcessor extends AbstractProcessor { String keyString = nativeModule + ".class"; TypeElement typeElement = mElements.getTypeElement(nativeModule); + if (typeElement == null) { + throw new ReactModuleSpecException( + keyString + " not found by ReactModuleSpecProcessor. " + + "Did you misspell the module?"); + } ReactModule reactModule = typeElement.getAnnotation(ReactModule.class); if (reactModule == null) { throw new ReactModuleSpecException( keyString + " not found by ReactModuleSpecProcessor. " + "Did you forget to add the @ReactModule annotation to the native module?"); } + + List elements = typeElement.getEnclosedElements(); + boolean hasConstants = false; + if (elements != null) { + hasConstants = elements.stream() + .anyMatch((Element m) -> m.getKind() == ElementKind.METHOD && m.getSimpleName().contentEquals("getConstants")); + } + String valueString = new StringBuilder() .append("new ReactModuleInfo(") .append("\"").append(reactModule.name()).append("\"").append(", ") .append(reactModule.canOverrideExistingModule()).append(", ") .append(reactModule.supportsWebWorkers()).append(", ") .append(reactModule.needsEagerInit()).append(", ") - .append(reactModule.hasConstants()) + .append(hasConstants) .append(")") .toString(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java index bf3dc276f..78be38352 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java @@ -19,7 +19,7 @@ import com.facebook.react.bridge.WritableMap; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter; -@ReactModule(name = AppStateModule.NAME, hasConstants = false) +@ReactModule(name = AppStateModule.NAME) public class AppStateModule extends ReactContextBaseJavaModule implements LifecycleEventListener { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.java index d76c79940..85b9c3853 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.java @@ -26,7 +26,7 @@ import com.facebook.react.module.annotations.ReactModule; /** * Native module that handles device hardware events like hardware back presses. */ -@ReactModule(name = "DeviceEventManager", hasConstants = false) +@ReactModule(name = "DeviceEventManager") public class DeviceEventManagerModule extends ReactContextBaseJavaModule { @SupportsWebWorkers diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/core/ExceptionsManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/core/ExceptionsManagerModule.java index f50c3d99f..b92d22ad7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/core/ExceptionsManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/core/ExceptionsManagerModule.java @@ -23,7 +23,7 @@ import com.facebook.react.common.JavascriptException; import com.facebook.react.common.ReactConstants; import com.facebook.react.module.annotations.ReactModule; -@ReactModule(name = ExceptionsManagerModule.NAME, hasConstants = false) +@ReactModule(name = ExceptionsManagerModule.NAME) public class ExceptionsManagerModule extends BaseJavaModule { protected static final String NAME = "ExceptionsManager"; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.java index fb2c370ee..5f4c0799d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.java @@ -20,7 +20,7 @@ import com.facebook.react.module.annotations.ReactModule; * Simple native module that allows JS to notify native of having completed some task work, so that * it can e.g. release any resources, stop timers etc. */ -@ReactModule(name = HeadlessJsTaskSupportModule.MODULE_NAME, hasConstants = false) +@ReactModule(name = HeadlessJsTaskSupportModule.MODULE_NAME) public class HeadlessJsTaskSupportModule extends ReactContextBaseJavaModule { protected static final String MODULE_NAME = "HeadlessJsTaskSupport"; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/core/Timing.java b/ReactAndroid/src/main/java/com/facebook/react/modules/core/Timing.java index 317f98fab..0e504d5d8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/core/Timing.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/core/Timing.java @@ -42,7 +42,7 @@ import com.facebook.react.module.annotations.ReactModule; /** * Native module for JS timer execution. Timers fire on frame boundaries. */ -@ReactModule(name = Timing.NAME, supportsWebWorkers = true, hasConstants = false) +@ReactModule(name = Timing.NAME, supportsWebWorkers = true) public final class Timing extends ReactContextBaseJavaModule implements LifecycleEventListener, OnExecutorUnregisteredListener, HeadlessJsTaskEventListener { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java index 454dd5baa..5e991d4c4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java @@ -29,7 +29,7 @@ import com.facebook.react.modules.debug.interfaces.DeveloperSettings; * Module that records debug information during transitions (animated navigation events such as * going from one screen to another). */ -@ReactModule(name = AnimationsDebugModule.NAME, hasConstants = false) +@ReactModule(name = AnimationsDebugModule.NAME) public class AnimationsDebugModule extends ReactContextBaseJavaModule { protected static final String NAME = "AnimationsDebugModule"; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/storage/AsyncStorageModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/storage/AsyncStorageModule.java index 28267e124..5fdcbeb48 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/storage/AsyncStorageModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/storage/AsyncStorageModule.java @@ -33,7 +33,7 @@ import static com.facebook.react.modules.storage.ReactDatabaseSupplier.KEY_COLUM import static com.facebook.react.modules.storage.ReactDatabaseSupplier.TABLE_CATALYST; import static com.facebook.react.modules.storage.ReactDatabaseSupplier.VALUE_COLUMN; -@ReactModule(name = AsyncStorageModule.NAME, hasConstants = false) +@ReactModule(name = AsyncStorageModule.NAME) public final class AsyncStorageModule extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable { diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/debug/DebugComponentOwnershipModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/debug/DebugComponentOwnershipModule.java index 2ba0dddf1..a38af1d06 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/debug/DebugComponentOwnershipModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/debug/DebugComponentOwnershipModule.java @@ -27,7 +27,7 @@ import com.facebook.react.module.annotations.ReactModule; * * Example returned owner hierarchy: ['RootView', 'Dialog', 'TitleView', 'Text'] */ -@ReactModule(name = "DebugComponentOwnershipModule", hasConstants = false) +@ReactModule(name = "DebugComponentOwnershipModule") public class DebugComponentOwnershipModule extends ReactContextBaseJavaModule { public interface RCTDebugComponentOwnership extends JavaScriptModule {