Skip to content

Commit b080a90

Browse files
Enable module annotation processor in OSS
1 parent a4757d2 commit b080a90

File tree

18 files changed

+66
-120
lines changed

18 files changed

+66
-120
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ project.xcworkspace
3131
/ReactAndroid/gradle/
3232
/ReactAndroid/gradlew
3333
/ReactAndroid/gradlew.bat
34+
/android/annotations/build/
35+
/android/annotations-compiler/build/
3436

3537
# Buck
3638
.buckd
@@ -51,7 +53,6 @@ buck-out
5153
.gradle
5254
local.properties
5355
*.iml
54-
/android/
5556

5657
# Node
5758
node_modules

ReactAndroid/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ android {
437437
}
438438

439439
dependencies {
440+
api(project(':react-native-annotations'))
441+
annotationProcessor(project(':react-native-annotations-compiler'))
442+
440443
api("com.facebook.infer.annotation:infer-annotation:0.11.2")
441444
api("com.facebook.yoga:proguard-annotations:1.14.1")
442445
api("javax.inject:javax.inject:1")

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

Lines changed: 8 additions & 67 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.core.DefaultHardwareBackBtnHandler;
2519
import com.facebook.react.modules.core.DeviceEventManagerModule;
@@ -30,21 +24,22 @@
3024
import com.facebook.react.modules.debug.SourceCodeModule;
3125
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
3226
import com.facebook.react.modules.systeminfo.AndroidInfoModule;
33-
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
3427
import com.facebook.react.uimanager.UIImplementationProvider;
3528
import com.facebook.react.uimanager.UIManagerModule;
3629
import com.facebook.react.uimanager.ViewManager;
3730
import com.facebook.systrace.Systrace;
38-
import java.util.HashMap;
31+
3932
import java.util.List;
40-
import java.util.Map;
33+
34+
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END;
35+
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_START;
36+
import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_END;
37+
import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_START;
4138

4239
/**
4340
* This is the basic module to support React Native. The debug modules are now in DebugCorePackage.
4441
*/
4542
@ReactModuleList(
46-
// WARNING: If you modify this list, ensure that the list below in method
47-
// getReactModuleInfoByInitialization is also updated
4843
nativeModules = {
4944
AndroidInfoModule.class,
5045
DeviceEventManagerModule.class,
@@ -76,63 +71,9 @@ public CoreModulesPackage(
7671
mMinTimeLeftInFrameForNonBatchedOperationMs = minTimeLeftInFrameForNonBatchedOperationMs;
7772
}
7873

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

13879
@Override

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

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +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.devsupport.JSDevSupport;
14-
import com.facebook.react.module.annotations.ReactModule;
1513
import com.facebook.react.module.annotations.ReactModuleList;
16-
import com.facebook.react.module.model.ReactModuleInfo;
1714
import com.facebook.react.module.model.ReactModuleInfoProvider;
18-
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
19-
import java.util.HashMap;
20-
import java.util.Map;
2115

2216
/**
23-
* Package defining core framework modules (e.g. UIManager). It should be used for modules that
24-
* require special integration with other framework parts (e.g. with the list of packages to load
25-
* view managers from).
17+
* Package defining core debug only modules.
2618
*/
2719
@ReactModuleList(
2820
nativeModules = {
@@ -44,45 +36,6 @@ public NativeModule getModule(String name, ReactApplicationContext reactContext)
4436

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

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

android/annotations/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apply plugin: 'java'
2+
3+
dependencies {
4+
5+
}
6+
7+
java {
8+
sourceCompatibility(JavaVersion.VERSION_1_8)
9+
targetCompatibility(JavaVersion.VERSION_1_8)
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.facebook.react.module.annotations;
2+
3+
/**
4+
* Used to make sure classes passed to the ReactModule annotation are actually an instance of
5+
* NativeModule without needing a dependency on NativeModule that is in the ReactAndroid target.
6+
*/
7+
public interface BaseNativeModule {
8+
}

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
@@ -9,3 +9,9 @@ include(
99
":ReactAndroid",
1010
":RNTester:android:app"
1111
)
12+
13+
include(":react-native-annotations")
14+
project(":react-native-annotations").projectDir = File(rootProject.projectDir, "android/annotations")
15+
16+
include(":react-native-annotations-compiler")
17+
project(":react-native-annotations-compiler").projectDir = File(rootProject.projectDir, "android/annotations-compiler")

0 commit comments

Comments
 (0)