Skip to content

Commit

Permalink
Enable module annotation processor in OSS
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Nov 15, 2021
1 parent 63208dd commit 4927b5b
Show file tree
Hide file tree
Showing 29 changed files with 137 additions and 143 deletions.
1 change: 1 addition & 0 deletions .circleci/Dockerfiles/Dockerfile.android
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ADD ReactAndroid /app/ReactAndroid
ADD ReactCommon /app/ReactCommon
ADD React /app/React
ADD keystores /app/keystores
ADD packages/annotations-compiler /app/packages/annotations-compiler
ADD packages/react-native-codegen /app/packages/react-native-codegen
ADD tools /app/tools

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ project.xcworkspace
# Gradle
/build/
/packages/react-native-gradle-plugin/build/
/packages/annotations-compiler/annotations/build/
/packages/annotations-compiler/compiler/build/
/packages/rn-tester/android/app/.cxx/
/packages/rn-tester/android/app/build/
/packages/rn-tester/android/app/gradle/
Expand Down
2 changes: 2 additions & 0 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ android {
}

dependencies {
api(project(':react-native-annotations'))
annotationProcessor(project(':react-native-annotations-compiler'))
api("com.facebook.infer.annotation:infer-annotation:0.18.0")
api("com.facebook.yoga:proguard-annotations:1.19.0")
api("javax.inject:javax.inject:1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.devsupport.LogBoxModule;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.module.annotations.ReactModuleList;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.modules.bundleloader.NativeDevSplitBundleLoaderModule;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
Expand All @@ -31,22 +29,17 @@
import com.facebook.react.modules.debug.SourceCodeModule;
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
import com.facebook.react.modules.systeminfo.AndroidInfoModule;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.ViewManagerResolver;
import com.facebook.systrace.Systrace;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* This is the basic module to support React Native. The debug modules are now in DebugCorePackage.
*/
@ReactModuleList(
// WARNING: If you modify this list, ensure that the list below in method
// getReactModuleInfoByInitialization is also updated
nativeModules = {
AndroidInfoModule.class,
DeviceEventManagerModule.class,
Expand Down Expand Up @@ -79,64 +72,9 @@ public CoreModulesPackage(
mMinTimeLeftInFrameForNonBatchedOperationMs = minTimeLeftInFrameForNonBatchedOperationMs;
}

/**
* This method is overridden, since OSS does not run the annotation processor to generate {@link
* CoreModulesPackage$$ReactModuleInfoProvider} class. Here we check if it exists. If it does not
* exist, we generate one manually in {@link
* CoreModulesPackage#getReactModuleInfoByInitialization()} and return that instead.
*/
@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
try {
Class<?> reactModuleInfoProviderClass =
Class.forName("com.facebook.react.CoreModulesPackage$$ReactModuleInfoProvider");
return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance();
} catch (ClassNotFoundException e) {
// In OSS case, the annotation processor does not run. We fall back on creating this byhand
Class<? extends NativeModule>[] moduleList =
new Class[] {
AndroidInfoModule.class,
DeviceEventManagerModule.class,
DeviceInfoModule.class,
DevSettingsModule.class,
ExceptionsManagerModule.class,
LogBoxModule.class,
HeadlessJsTaskSupportModule.class,
SourceCodeModule.class,
TimingModule.class,
UIManagerModule.class,
NativeDevSplitBundleLoaderModule.class,
};

final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
for (Class<? extends NativeModule> moduleClass : moduleList) {
ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class);

reactModuleInfoMap.put(
reactModule.name(),
new ReactModuleInfo(
reactModule.name(),
moduleClass.getName(),
reactModule.canOverrideExistingModule(),
reactModule.needsEagerInit(),
reactModule.hasConstants(),
reactModule.isCxxModule(),
TurboModule.class.isAssignableFrom(moduleClass)));
}

return new ReactModuleInfoProvider() {
@Override
public Map<String, ReactModuleInfo> getReactModuleInfos() {
return reactModuleInfoMap;
}
};
} catch (InstantiationException e) {
throw new RuntimeException(
"No ReactModuleInfoProvider for CoreModulesPackage$$ReactModuleInfoProvider", e);
} catch (IllegalAccessException e) {
throw new RuntimeException(
"No ReactModuleInfoProvider for CoreModulesPackage$$ReactModuleInfoProvider", e);
}
return new CoreModulesPackage$$ReactModuleInfoProvider();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,10 @@
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.devsupport.JSCHeapCapture;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.module.annotations.ReactModuleList;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import java.util.HashMap;
import java.util.Map;

/**
* Package defining core framework modules (e.g. UIManager). It should be used for modules that
* require special integration with other framework parts (e.g. with the list of packages to load
* view managers from).
*/
/** Package defining core debug only modules. */
@ReactModuleList(
nativeModules = {
JSCHeapCapture.class,
Expand All @@ -43,42 +34,6 @@ public NativeModule getModule(String name, ReactApplicationContext reactContext)

@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
try {
Class<?> reactModuleInfoProviderClass =
Class.forName("com.facebook.react.DebugCorePackage$$ReactModuleInfoProvider");
return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance();
} catch (ClassNotFoundException e) {
// In OSS case, the annotation processor does not run. We fall back on creating this by hand
Class<? extends NativeModule>[] moduleList = new Class[] {JSCHeapCapture.class};

final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
for (Class<? extends NativeModule> moduleClass : moduleList) {
ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class);

reactModuleInfoMap.put(
reactModule.name(),
new ReactModuleInfo(
reactModule.name(),
moduleClass.getName(),
reactModule.canOverrideExistingModule(),
reactModule.needsEagerInit(),
reactModule.hasConstants(),
reactModule.isCxxModule(),
TurboModule.class.isAssignableFrom(moduleClass)));
}

return new ReactModuleInfoProvider() {
@Override
public Map<String, ReactModuleInfo> getReactModuleInfos() {
return reactModuleInfoMap;
}
};
} catch (InstantiationException e) {
throw new RuntimeException(
"No ReactModuleInfoProvider for DebugCorePackage$$ReactModuleInfoProvider", e);
} catch (IllegalAccessException e) {
throw new RuntimeException(
"No ReactModuleInfoProvider for DebugCorePackage$$ReactModuleInfoProvider", e);
}
return new com.facebook.react.DebugCorePackage$$ReactModuleInfoProvider();
}
}
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ rn_android_library(
exported_deps = [
react_native_dep("java/com/facebook/proguard/annotations:annotations"),
react_native_dep("third-party/java/jsr-330:jsr-330"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import androidx.annotation.NonNull;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.module.annotations.BaseNativeModule;

/**
* A native module whose API can be provided to JS catalyst instances. {@link NativeModule}s whose
Expand All @@ -18,7 +19,7 @@
* themselves using {@link CxxModuleWrapper}.
*/
@DoNotStrip
public interface NativeModule {
public interface NativeModule extends BaseNativeModule {
interface NativeMethod {
void invoke(JSInstance jsInstance, ReadableArray parameters);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")
load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library")

rn_android_library(
name = "annotations",
srcs = glob(["**/*.java"]),
autoglob = False,
labels = ["supermodule:xplat/default/public.react_native.infra"],
required_for_source_only_abi = True,
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react/bridge:interfaces"),
exported_deps = [
"//packages/annotations-compiler/annotations/src/main/java/com/facebook/react/module/annotations:annotations",
],
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_library")
load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library")

rn_android_library(
name = "model",
srcs = glob(["**/*.java"]),
autoglob = False,
labels = ["supermodule:xplat/default/public.react_native.infra"],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/jsr-305:jsr-305"),
exported_deps = [
"//packages/annotations-compiler/annotations/src/main/java/com/facebook/react/module/model:model",
],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_java_annotation_processor", "rn_java_library")
load("//tools/build_defs/oss:rn_defs.bzl", "rn_java_annotation_processor")

rn_java_annotation_processor(
name = "processing",
Expand All @@ -8,22 +8,6 @@ rn_java_annotation_processor(
"PUBLIC",
],
deps = [
":processing-lib",
],
)

rn_java_library(
name = "processing-lib",
srcs = glob(["*.java"]),
labels = ["supermodule:xplat/default/public.react_native.infra"],
source = "8",
target = "8",
deps = [
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"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/module/model:model"),
react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"),
"//packages/annotations-compiler/compiler/src/main/java/com/facebook/react/module/processing:processing-lib",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("//tools/build_defs/oss:rn_defs.bzl", "rn_prebuilt_jar")
rn_prebuilt_jar(
name = "infer-annotations",
binary_jar = ":infer-annotations.jar",
visibility = ["//ReactAndroid/..."],
visibility = ["//ReactAndroid/...", "//packages/annotations-compiler/..."],
)

fb_native.remote_file(
Expand Down
2 changes: 1 addition & 1 deletion ReactAndroid/src/main/third-party/java/javapoet/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("//tools/build_defs/oss:rn_defs.bzl", "rn_prebuilt_jar")
rn_prebuilt_jar(
name = "javapoet",
binary_jar = ":jsr305-binary.jar",
visibility = ["//ReactAndroid/..."],
visibility = ["//ReactAndroid/...", "//packages/annotations-compiler/..."],
)

fb_native.remote_file(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"Libraries",
"LICENSE",
"local-cli",
"packages/annotations-compiler",
"React-Core.podspec",
"react-native.config.js",
"react.gradle",
Expand Down
17 changes: 17 additions & 0 deletions packages/annotations-compiler/annotations/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

apply plugin: 'java'

dependencies {

}

java {
sourceCompatibility(JavaVersion.VERSION_1_8)
targetCompatibility(JavaVersion.VERSION_1_8)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")

rn_android_library(
name = "annotations",
srcs = glob(["**/*.java"]),
autoglob = False,
labels = ["supermodule:xplat/default/public.react_native.infra"],
required_for_source_only_abi = True,
visibility = [
"PUBLIC",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.module.annotations;

/**
* Used to make sure classes passed to the ReactModule annotation are actually an instance of
* NativeModule without needing a dependency on NativeModule that is in the ReactAndroid target.
*/
public interface BaseNativeModule {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import com.facebook.react.bridge.NativeModule;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -27,5 +26,5 @@
*
* @return List of Native modules in the package.
*/
Class<? extends NativeModule>[] nativeModules();
Class<? extends BaseNativeModule>[] nativeModules();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_library")

rn_android_library(
name = "model",
srcs = glob(["**/*.java"]),
autoglob = False,
labels = ["supermodule:xplat/default/public.react_native.infra"],
visibility = [
"PUBLIC",
],
)
Loading

0 comments on commit 4927b5b

Please sign in to comment.