mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-30 22:12:42 +08:00
Add ability to lazy load Native Java Modules
Summary: Utilizes the build time annotation processor ReactModuleSpecProcessor that creates ReactModuleInfos for modules annotated with ReactModule and listed in the ReactModuleList annotation of LazyReactPackages. This way we don't have to instantiate the native modules to get the name, canOverrideExistingModule, and supportsWebWorkers values of the native modules. In the NativeModuleRegistry, we either store these ReactModuleInfos inside of a ModuleHolder or if we can't get the ReactModuleInfo for a specific module we instantiate that module to get the values (as we previously did) to store in a LegacyModuleInfo. Reviewed By: astreet Differential Revision: D3796561 fbshipit-source-id: f8fb9b4993f59b51ce595eb2f2c3425129b28ce5
This commit is contained in:
committed by
Facebook Github Bot 3
parent
1f9b765f81
commit
797ca6c219
@@ -28,6 +28,7 @@ import com.facebook.infer.annotation.SuppressFieldNotInitialized;
|
||||
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.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.CodeBlock;
|
||||
@@ -97,9 +98,8 @@ public class ReactModuleSpecProcessor extends AbstractProcessor {
|
||||
MethodSpec getReactModuleInfosMethod;
|
||||
try {
|
||||
getReactModuleInfosMethod = MethodSpec.methodBuilder("getReactModuleInfos")
|
||||
.addAnnotation(Override.class)
|
||||
.addModifiers(PUBLIC)
|
||||
// TODO add function to native module interface
|
||||
// .addAnnotation(Override.class)
|
||||
.addCode(getCodeBlockForReactModuleInfos(nativeModules))
|
||||
.returns(MAP_TYPE)
|
||||
.build();
|
||||
@@ -108,11 +108,12 @@ public class ReactModuleSpecProcessor extends AbstractProcessor {
|
||||
return false;
|
||||
}
|
||||
|
||||
TypeSpec reactModulesInfosTypeSpec = TypeSpec.classBuilder(
|
||||
fileName + "$$ReactModuleInfoProvider")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addMethod(getReactModuleInfosMethod)
|
||||
.build();
|
||||
TypeSpec reactModulesInfosTypeSpec = TypeSpec.classBuilder(
|
||||
fileName + "$$ReactModuleInfoProvider")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addMethod(getReactModuleInfosMethod)
|
||||
.addSuperinterface(ReactModuleInfoProvider.class)
|
||||
.build();
|
||||
|
||||
JavaFile javaFile = JavaFile.builder(packageName, reactModulesInfosTypeSpec)
|
||||
.addFileComment("Generated by " + getClass().getName())
|
||||
@@ -130,30 +131,35 @@ public class ReactModuleSpecProcessor extends AbstractProcessor {
|
||||
|
||||
private CodeBlock getCodeBlockForReactModuleInfos(List<String> nativeModules)
|
||||
throws ReactModuleSpecException {
|
||||
CodeBlock.Builder builder = CodeBlock.builder()
|
||||
.addStatement("$T map = new $T()", MAP_TYPE, INSTANTIATED_MAP_TYPE);
|
||||
CodeBlock.Builder builder = CodeBlock.builder();
|
||||
if (nativeModules == null || nativeModules.isEmpty()) {
|
||||
builder.addStatement("return Collections.emptyMap()");
|
||||
} else {
|
||||
builder.addStatement("$T map = new $T()", MAP_TYPE, INSTANTIATED_MAP_TYPE);
|
||||
|
||||
for (String nativeModule : nativeModules) {
|
||||
String keyString = nativeModule + ".class";
|
||||
for (String nativeModule : nativeModules) {
|
||||
String keyString = nativeModule + ".class";
|
||||
|
||||
TypeElement typeElement = mElements.getTypeElement(nativeModule);
|
||||
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 the the native module?");
|
||||
TypeElement typeElement = mElements.getTypeElement(nativeModule);
|
||||
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 the the native module?");
|
||||
}
|
||||
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(")")
|
||||
.toString();
|
||||
|
||||
builder.addStatement("map.put(" + keyString + ", " + valueString + ")");
|
||||
}
|
||||
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(")")
|
||||
.toString();
|
||||
|
||||
builder.addStatement("map.put(" + keyString + ", " + valueString + ")");
|
||||
builder.addStatement("return map");
|
||||
}
|
||||
builder.addStatement("return map");
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user