Skip to content

Commit

Permalink
Emit name constant as part of Java codegen
Browse files Browse the repository at this point in the history
Summary:
We have the expected module name available as part of the codegen schema, so we can remove the need for developers to implement the `getName` method as part of their module implementation.

Note that this method is not actually used when the TurboModules infra is used, as the moduleName from the turbo module manager is passed through to the TurboModule base class instead. Moving the method to codegen will make it easier to remove this method altogether once the old architecture is fully removed.

Changelog: [Android][Added] Support generating `getName` in react-native-codegen for Java TurboModules

Reviewed By: mdvacca

Differential Revision: D41615387

fbshipit-source-id: 6b117645fa39e5e9ab014b21198496a52f6f2ae2
  • Loading branch information
javache authored and facebook-github-bot committed Dec 7, 2022
1 parent e1bca8f commit 9053890
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

package com.facebook.react.bridge;

import androidx.annotation.NonNull;
import com.facebook.proguard.annotations.DoNotStrip;
import javax.annotation.Nonnull;

/**
* A native module whose API can be provided to JS catalyst instances. {@link NativeModule}s whose
Expand All @@ -29,7 +29,7 @@ interface NativeMethod {
* @return the name of this module. This will be the name used to {@code require()} this module
* from javascript.
*/
@NonNull
@Nonnull
String getName();

/**
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-codegen/DEFS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def rn_codegen_modules(
autoglob = False,
labels = library_labels + ["codegen_rule"],
language = "JAVA",
required_for_source_only_abi = True,
visibility = ["PUBLIC"],
deps = [
react_native_dep("third-party/java/jsr-305:jsr-305"),
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export type NativeModuleSchema = $ReadOnly<{
type: 'NativeModule',
aliases: NativeModuleAliasMap,
spec: NativeModuleSpec,
moduleNames: $ReadOnlyArray<string>,
moduleName: string,
// Use for modules that are not used on other platforms.
// TODO: It's clearer to define `restrictedToPlatforms` instead, but
// `excludedPlatforms` is used here to be consistent with ComponentSchema.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const SCHEMA_WITH_TM_AND_FC: SchemaType = {
},
],
},
moduleNames: ['Calculator'],
moduleName: 'Calculator',
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ module.exports = {
const {
aliases,
spec: {properties},
moduleNames,
moduleName,
} = nativeModule;
const resolveAlias = createAliasResolver(aliases);
const hostFunctions = properties.map(property =>
Expand All @@ -254,8 +254,7 @@ module.exports = {
return ModuleTemplate({
hasteModuleName,
hostFunctions,
// TODO: What happens when there are more than one NativeModule requires?
moduleName: moduleNames[0],
moduleName,
methods: properties.map(
({name: propertyName, typeAnnotation: nullableTypeAnnotation}) => {
const [{params}] = unwrapNullable(nullableTypeAnnotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ module.exports = {
const {
aliases,
spec: {properties},
moduleNames: [moduleName],
moduleName,
} = nativeModules[hasteModuleName];
const resolveAlias = createAliasResolver(aliases);
const structs = createStructs(moduleName, aliases, resolveAlias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ function FileTemplate(
config: $ReadOnly<{
packageName: string,
className: string,
jsName: string,
methods: string,
imports: string,
}>,
): string {
const {packageName, className, methods, imports} = config;
const {packageName, className, jsName, methods, imports} = config;
return `
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
Expand All @@ -52,10 +53,17 @@ package ${packageName};
${imports}
public abstract class ${className} extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
public static final String NAME = "${jsName}";
public ${className}(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public @Nonnull String getName() {
return NAME;
}
${methods}
}
`;
Expand Down Expand Up @@ -441,6 +449,7 @@ module.exports = {
const {
aliases,
excludedPlatforms,
moduleName,
spec: {properties},
} = nativeModules[hasteModuleName];
if (excludedPlatforms != null && excludedPlatforms.includes('android')) {
Expand All @@ -457,6 +466,7 @@ module.exports = {
'com.facebook.react.bridge.ReactModuleWithSpec',
'com.facebook.react.turbomodule.core.interfaces.TurboModule',
'com.facebook.proguard.annotations.DoNotStrip',
'javax.annotation.Nonnull',
]);
const methods = properties.map(method => {
Expand Down Expand Up @@ -528,6 +538,7 @@ module.exports = {
FileTemplate({
packageName: normalizedPackageName,
className,
jsName: moduleName,
methods: methods.filter(Boolean).join('\n\n'),
imports: Array.from(imports)
.sort()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ const FileTemplate = ({
libraryName: string,
include: string,
modules: string,
moduleLookups: $ReadOnlyArray<
$ReadOnly<{
hasteModuleName: string,
moduleName: string,
}>,
>,
moduleLookups: $ReadOnlyArray<{
hasteModuleName: string,
moduleName: string,
}>,
}>) => {
return `
/**
Expand Down Expand Up @@ -487,8 +485,10 @@ module.exports = {
})
.join('\n');

// $FlowFixMe[missing-type-arg]
const moduleLookups = Object.keys(nativeModules)
const moduleLookups: $ReadOnlyArray<{
hasteModuleName: string,
moduleName: string,
}> = Object.keys(nativeModules)
.filter(hasteModuleName => {
const module = nativeModules[hasteModuleName];
return !(
Expand All @@ -497,26 +497,19 @@ module.exports = {
);
})
.sort((a, b) => {
const moduleA = nativeModules[a];
const moduleB = nativeModules[b];
const nameA = moduleA.moduleNames[0];
const nameB = moduleB.moduleNames[0];
const nameA = nativeModules[a].moduleName;
const nameB = nativeModules[b].moduleName;
if (nameA < nameB) {
return -1;
} else if (nameA > nameB) {
return 1;
}
return 0;
})
.flatMap<{moduleName: string, hasteModuleName: string}>(
(hasteModuleName: string) => {
const {moduleNames} = nativeModules[hasteModuleName];
return moduleNames.map(moduleName => ({
moduleName,
hasteModuleName,
}));
},
);
.map((hasteModuleName: string) => ({
moduleName: nativeModules[hasteModuleName].moduleName,
hasteModuleName,
}));

const fileName = `${libraryName}-generated.cpp`;
const replacedTemplate = FileTemplate({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const EMPTY_NATIVE_MODULES: SchemaType = {
spec: {
properties: [],
},
moduleNames: ['SampleTurboModule'],
moduleName: 'SampleTurboModule',
},
},
};
Expand Down Expand Up @@ -332,7 +332,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = {
},
],
},
moduleNames: ['SampleTurboModule'],
moduleName: 'SampleTurboModule',
},
},
};
Expand All @@ -357,7 +357,7 @@ const TWO_MODULES_DIFFERENT_FILES: SchemaType = {
},
],
},
moduleNames: ['SampleTurboModule'],
moduleName: 'SampleTurboModule',
},
NativeSampleTurboModule2: {
type: 'NativeModule',
Expand Down Expand Up @@ -389,7 +389,7 @@ const TWO_MODULES_DIFFERENT_FILES: SchemaType = {
},
],
},
moduleNames: ['SampleTurboModule2'],
moduleName: 'SampleTurboModule2',
},
},
};
Expand Down Expand Up @@ -755,7 +755,7 @@ const COMPLEX_OBJECTS: SchemaType = {
},
],
},
moduleNames: ['SampleTurboModule'],
moduleName: 'SampleTurboModule',
},
},
};
Expand Down Expand Up @@ -890,7 +890,7 @@ const NATIVE_MODULES_WITH_TYPE_ALIASES: SchemaType = {
},
],
},
moduleNames: ['AliasTurboModule'],
moduleName: 'AliasTurboModule',
},
},
};
Expand Down Expand Up @@ -1222,7 +1222,7 @@ const REAL_MODULE_EXAMPLE: SchemaType = {
},
],
},
moduleNames: ['CameraRollManager'],
moduleName: 'CameraRollManager',
},
NativeExceptionsManager: {
type: 'NativeModule',
Expand Down Expand Up @@ -1485,7 +1485,7 @@ const REAL_MODULE_EXAMPLE: SchemaType = {
},
],
},
moduleNames: ['ExceptionsManager'],
moduleName: 'ExceptionsManager',
},
},
};
Expand Down Expand Up @@ -1638,7 +1638,7 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = {
},
],
},
moduleNames: ['SampleTurboModuleCxx'],
moduleName: 'SampleTurboModuleCxx',
excludedPlatforms: ['iOS', 'android'],
},
},
Expand All @@ -1652,7 +1652,7 @@ const SAMPLE_WITH_UPPERCASE_NAME: SchemaType = {
spec: {
properties: [],
},
moduleNames: ['SampleTurboModule'],
moduleName: 'SampleTurboModule',
},
},
};
Expand Down

0 comments on commit 9053890

Please sign in to comment.