Emit name constant as part of Java codegen

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
This commit is contained in:
Pieter De Baets
2022-12-07 06:35:01 -08:00
committed by Facebook GitHub Bot
parent e1bca8f98c
commit 90538909f9
17 changed files with 221 additions and 247 deletions
@@ -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).
@@ -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}
}
`;
@@ -441,6 +449,7 @@ module.exports = {
const {
aliases,
excludedPlatforms,
moduleName,
spec: {properties},
} = nativeModules[hasteModuleName];
if (excludedPlatforms != null && excludedPlatforms.includes('android')) {
@@ -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 => {
@@ -528,6 +538,7 @@ module.exports = {
FileTemplate({
packageName: normalizedPackageName,
className,
jsName: moduleName,
methods: methods.filter(Boolean).join('\n\n'),
imports: Array.from(imports)
.sort()