Skip to content

Commit a135f45

Browse files
Enable module annotation processor in OSS
1 parent 6651d8c commit a135f45

File tree

18 files changed

+83
-117
lines changed

18 files changed

+83
-117
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ project.xcworkspace
3636
/ReactAndroid/gradlew.bat
3737
/template/android/app/build/
3838
/template/android/build/
39+
/android/annotations/build/
40+
/android/annotations-compiler/build/
3941

4042
# Buck
4143
.buckd
@@ -58,7 +60,6 @@ buck-out
5860
.gradle
5961
local.properties
6062
*.iml
61-
/android/
6263

6364
# Node
6465
node_modules

ReactAndroid/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ android {
503503

504504
dependencies {
505505
api("com.facebook.infer.annotation:infer-annotation:0.18.0")
506+
api(project(':react-native-annotations'))
507+
annotationProcessor(project(':react-native-annotations-compiler'))
508+
509+
api("com.facebook.infer.annotation:infer-annotation:0.11.2")
506510
api("com.facebook.yoga:proguard-annotations:1.19.0")
507511
api("javax.inject:javax.inject:1")
508512
api("androidx.appcompat:appcompat:1.0.2")

ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@
77

88
package com.facebook.react;
99

10-
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END;
11-
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_START;
12-
import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_END;
13-
import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_START;
14-
1510
import androidx.annotation.Nullable;
11+
1612
import com.facebook.react.bridge.NativeModule;
1713
import com.facebook.react.bridge.ReactApplicationContext;
1814
import com.facebook.react.bridge.ReactMarker;
1915
import com.facebook.react.devsupport.LogBoxModule;
20-
import com.facebook.react.module.annotations.ReactModule;
2116
import com.facebook.react.module.annotations.ReactModuleList;
22-
import com.facebook.react.module.model.ReactModuleInfo;
2317
import com.facebook.react.module.model.ReactModuleInfoProvider;
2418
import com.facebook.react.modules.bundleloader.NativeDevSplitBundleLoaderModule;
2519
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
@@ -31,22 +25,23 @@
3125
import com.facebook.react.modules.debug.SourceCodeModule;
3226
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
3327
import com.facebook.react.modules.systeminfo.AndroidInfoModule;
34-
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
3528
import com.facebook.react.uimanager.UIImplementationProvider;
3629
import com.facebook.react.uimanager.UIManagerModule;
3730
import com.facebook.react.uimanager.ViewManager;
3831
import com.facebook.react.uimanager.ViewManagerResolver;
3932
import com.facebook.systrace.Systrace;
40-
import java.util.HashMap;
33+
4134
import java.util.List;
42-
import java.util.Map;
35+
36+
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END;
37+
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_START;
38+
import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_END;
39+
import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_START;
4340

4441
/**
4542
* This is the basic module to support React Native. The debug modules are now in DebugCorePackage.
4643
*/
4744
@ReactModuleList(
48-
// WARNING: If you modify this list, ensure that the list below in method
49-
// getReactModuleInfoByInitialization is also updated
5045
nativeModules = {
5146
AndroidInfoModule.class,
5247
DeviceEventManagerModule.class,
@@ -79,64 +74,9 @@ public CoreModulesPackage(
7974
mMinTimeLeftInFrameForNonBatchedOperationMs = minTimeLeftInFrameForNonBatchedOperationMs;
8075
}
8176

82-
/**
83-
* This method is overridden, since OSS does not run the annotation processor to generate {@link
84-
* CoreModulesPackage$$ReactModuleInfoProvider} class. Here we check if it exists. If it does not
85-
* exist, we generate one manually in {@link
86-
* CoreModulesPackage#getReactModuleInfoByInitialization()} and return that instead.
87-
*/
8877
@Override
8978
public ReactModuleInfoProvider getReactModuleInfoProvider() {
90-
try {
91-
Class<?> reactModuleInfoProviderClass =
92-
Class.forName("com.facebook.react.CoreModulesPackage$$ReactModuleInfoProvider");
93-
return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance();
94-
} catch (ClassNotFoundException e) {
95-
// In OSS case, the annotation processor does not run. We fall back on creating this byhand
96-
Class<? extends NativeModule>[] moduleList =
97-
new Class[] {
98-
AndroidInfoModule.class,
99-
DeviceEventManagerModule.class,
100-
DeviceInfoModule.class,
101-
DevSettingsModule.class,
102-
ExceptionsManagerModule.class,
103-
LogBoxModule.class,
104-
HeadlessJsTaskSupportModule.class,
105-
SourceCodeModule.class,
106-
TimingModule.class,
107-
UIManagerModule.class,
108-
NativeDevSplitBundleLoaderModule.class,
109-
};
110-
111-
final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
112-
for (Class<? extends NativeModule> moduleClass : moduleList) {
113-
ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class);
114-
115-
reactModuleInfoMap.put(
116-
reactModule.name(),
117-
new ReactModuleInfo(
118-
reactModule.name(),
119-
moduleClass.getName(),
120-
reactModule.canOverrideExistingModule(),
121-
reactModule.needsEagerInit(),
122-
reactModule.hasConstants(),
123-
reactModule.isCxxModule(),
124-
TurboModule.class.isAssignableFrom(moduleClass)));
125-
}
126-
127-
return new ReactModuleInfoProvider() {
128-
@Override
129-
public Map<String, ReactModuleInfo> getReactModuleInfos() {
130-
return reactModuleInfoMap;
131-
}
132-
};
133-
} catch (InstantiationException e) {
134-
throw new RuntimeException(
135-
"No ReactModuleInfoProvider for CoreModulesPackage$$ReactModuleInfoProvider", e);
136-
} catch (IllegalAccessException e) {
137-
throw new RuntimeException(
138-
"No ReactModuleInfoProvider for CoreModulesPackage$$ReactModuleInfoProvider", e);
139-
}
79+
return new CoreModulesPackage$$ReactModuleInfoProvider();
14080
}
14181

14282
@Override

ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@
1010
import com.facebook.react.bridge.NativeModule;
1111
import com.facebook.react.bridge.ReactApplicationContext;
1212
import com.facebook.react.devsupport.JSCHeapCapture;
13-
import com.facebook.react.module.annotations.ReactModule;
1413
import com.facebook.react.module.annotations.ReactModuleList;
15-
import com.facebook.react.module.model.ReactModuleInfo;
1614
import com.facebook.react.module.model.ReactModuleInfoProvider;
17-
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
18-
import java.util.HashMap;
19-
import java.util.Map;
2015

2116
/**
22-
* Package defining core framework modules (e.g. UIManager). It should be used for modules that
23-
* require special integration with other framework parts (e.g. with the list of packages to load
24-
* view managers from).
17+
* Package defining core debug only modules.
2518
*/
2619
@ReactModuleList(
2720
nativeModules = {
@@ -43,42 +36,6 @@ public NativeModule getModule(String name, ReactApplicationContext reactContext)
4336

4437
@Override
4538
public ReactModuleInfoProvider getReactModuleInfoProvider() {
46-
try {
47-
Class<?> reactModuleInfoProviderClass =
48-
Class.forName("com.facebook.react.DebugCorePackage$$ReactModuleInfoProvider");
49-
return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance();
50-
} catch (ClassNotFoundException e) {
51-
// In OSS case, the annotation processor does not run. We fall back on creating this by hand
52-
Class<? extends NativeModule>[] moduleList = new Class[] {JSCHeapCapture.class};
53-
54-
final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
55-
for (Class<? extends NativeModule> moduleClass : moduleList) {
56-
ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class);
57-
58-
reactModuleInfoMap.put(
59-
reactModule.name(),
60-
new ReactModuleInfo(
61-
reactModule.name(),
62-
moduleClass.getName(),
63-
reactModule.canOverrideExistingModule(),
64-
reactModule.needsEagerInit(),
65-
reactModule.hasConstants(),
66-
reactModule.isCxxModule(),
67-
TurboModule.class.isAssignableFrom(moduleClass)));
68-
}
69-
70-
return new ReactModuleInfoProvider() {
71-
@Override
72-
public Map<String, ReactModuleInfo> getReactModuleInfos() {
73-
return reactModuleInfoMap;
74-
}
75-
};
76-
} catch (InstantiationException e) {
77-
throw new RuntimeException(
78-
"No ReactModuleInfoProvider for DebugCorePackage$$ReactModuleInfoProvider", e);
79-
} catch (IllegalAccessException e) {
80-
throw new RuntimeException(
81-
"No ReactModuleInfoProvider for DebugCorePackage$$ReactModuleInfoProvider", e);
82-
}
39+
return new com.facebook.react.DebugCorePackage$$ReactModuleInfoProvider();
8340
}
8441
}

ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import androidx.annotation.NonNull;
1111
import com.facebook.proguard.annotations.DoNotStrip;
12+
import com.facebook.react.module.annotations.BaseNativeModule;
1213

1314
/**
1415
* A native module whose API can be provided to JS catalyst instances. {@link NativeModule}s whose
@@ -18,7 +19,7 @@
1819
* themselves using {@link CxxModuleWrapper}.
1920
*/
2021
@DoNotStrip
21-
public interface NativeModule {
22+
public interface NativeModule extends BaseNativeModule {
2223
interface NativeMethod {
2324
void invoke(JSInstance jsInstance, ReadableArray parameters);
2425

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
apply plugin: 'java'
9+
10+
sourceSets.configureEach { sourceSet ->
11+
tasks.named(sourceSet.compileJavaTaskName).configure {
12+
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/annotationProcessor/java/${sourceSet.name}")
13+
}
14+
}
15+
16+
java {
17+
sourceCompatibility(JavaVersion.VERSION_1_8)
18+
targetCompatibility(JavaVersion.VERSION_1_8)
19+
}
20+
21+
dependencies {
22+
implementation('com.squareup:javapoet:1.8.0')
23+
implementation('com.facebook.infer.annotation:infer-annotation:0.11.2')
24+
implementation(project(':react-native-annotations'))
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.facebook.react.module.processing.ReactModuleSpecProcessor

android/annotations/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
apply plugin: 'java'
9+
10+
dependencies {
11+
12+
}
13+
14+
java {
15+
sourceCompatibility(JavaVersion.VERSION_1_8)
16+
targetCompatibility(JavaVersion.VERSION_1_8)
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.module.annotations;
9+
10+
/**
11+
* Used to make sure classes passed to the ReactModule annotation are actually an instance of
12+
* NativeModule without needing a dependency on NativeModule that is in the ReactAndroid target.
13+
*/
14+
public interface BaseNativeModule {
15+
}

ReactAndroid/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java renamed to android/annotations/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import static java.lang.annotation.ElementType.TYPE;
1111
import static java.lang.annotation.RetentionPolicy.SOURCE;
1212

13-
import com.facebook.react.bridge.NativeModule;
1413
import java.lang.annotation.Retention;
1514
import java.lang.annotation.Target;
1615

@@ -27,5 +26,5 @@
2726
*
2827
* @return List of Native modules in the package.
2928
*/
30-
Class<? extends NativeModule>[] nativeModules();
29+
Class<? extends BaseNativeModule>[] nativeModules();
3130
}

settings.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ if (File("template/node_modules/").exists()) {
2929
name = "template-android"
3030
}
3131
}
32+
33+
include(":react-native-annotations")
34+
project(":react-native-annotations").projectDir = File(rootProject.projectDir, "android/annotations")
35+
36+
include(":react-native-annotations-compiler")
37+
project(":react-native-annotations-compiler").projectDir = File(rootProject.projectDir, "android/annotations-compiler")

0 commit comments

Comments
 (0)