Refactor ViewManagerInterfaces codegen to generate kotlin classes (#51735)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51735

This diff refactors the ViewManagerInterfaces codegen to generate kotlin classes,

As a consequence of this change, there are some ViewManagerInterfaces that have changed their APIs

## Changelog: [Android][Breaking] - Migrate ViewManagerInterfaces to kotlin. Some types in code generated ViewManagerInterfaces might differ. e.g. this will start enforcing nullability in parameters of viewManagerInterface methods (e.g. String commands parameters are not nullable, view params are not nullable in any method, etc)

Reviewed By: javache

Differential Revision: D75719542

fbshipit-source-id: 7e9aa7ccc24e827bd7b6df72b3302e852932e731
This commit is contained in:
David Vacca
2025-07-03 12:19:42 -07:00
committed by Facebook GitHub Bot
parent 253606239b
commit 76ff1aa5c6
4 changed files with 377 additions and 432 deletions
@@ -48,19 +48,15 @@ const FileTemplate = ({
* ${'@'}generated by codegen project: GeneratePropsJavaInterface.js
*/
package ${packageName};
package ${packageName}
${imports}
public interface ${className}<T extends ${extendClasses}> extends ViewManagerWithGeneratedInterface {
public interface ${className}<T: ${extendClasses}>: ViewManagerWithGeneratedInterface {
${methods}
}
`;
function addNullable(imports: Set<string>) {
imports.add('import androidx.annotation.Nullable;');
}
function getJavaValueForProp(
prop: NamedShape<PropTypeAnnotation>,
imports: Set<string>,
@@ -70,65 +66,52 @@ function getJavaValueForProp(
switch (typeAnnotation.type) {
case 'BooleanTypeAnnotation':
if (typeAnnotation.default === null) {
addNullable(imports);
return '@Nullable Boolean value';
return 'value: Boolean?';
} else {
return 'boolean value';
return 'value: Boolean';
}
case 'StringTypeAnnotation':
addNullable(imports);
return '@Nullable String value';
return 'value: String?';
case 'Int32TypeAnnotation':
return 'int value';
return 'value: Int';
case 'DoubleTypeAnnotation':
return 'double value';
return 'value: Double';
case 'FloatTypeAnnotation':
if (typeAnnotation.default === null) {
addNullable(imports);
return '@Nullable Float value';
return 'value: Float?';
} else {
return 'float value';
return 'value: Float';
}
case 'ReservedPropTypeAnnotation':
switch (typeAnnotation.name) {
case 'ColorPrimitive':
addNullable(imports);
return '@Nullable Integer value';
return 'value: Int?';
case 'ImageSourcePrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'ImageRequestPrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'PointPrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'EdgeInsetsPrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'DimensionPrimitive':
addNullable(imports);
return '@Nullable YogaValue value';
return 'value: YogaValue?';
default:
(typeAnnotation.name: empty);
throw new Error('Received unknown ReservedPropTypeAnnotation');
}
case 'ArrayTypeAnnotation': {
addNullable(imports);
return '@Nullable ReadableArray value';
return 'value: ReadableArray?';
}
case 'ObjectTypeAnnotation': {
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
}
case 'StringEnumTypeAnnotation':
addNullable(imports);
return '@Nullable String value';
return 'value: String?';
case 'Int32EnumTypeAnnotation':
addNullable(imports);
return '@Nullable Integer value';
return 'value: Int?';
case 'MixedTypeAnnotation':
return 'Dynamic value';
return 'value: Dynamic';
default:
(typeAnnotation: empty);
throw new Error('Received invalid typeAnnotation');
@@ -142,9 +125,9 @@ function generatePropsString(component: ComponentShape, imports: Set<string>) {
return component.props
.map(prop => {
return `void set${toSafeJavaString(
return `public fun set${toSafeJavaString(
prop.name,
)}(T view, ${getJavaValueForProp(prop, imports)});`;
)}(view: T, ${getJavaValueForProp(prop, imports)}): Unit`;
})
.join('\n' + ' ');
}
@@ -156,19 +139,19 @@ function getCommandArgJavaType(param: NamedShape<CommandParamTypeAnnotation>) {
case 'ReservedTypeAnnotation':
switch (typeAnnotation.name) {
case 'RootTag':
return 'double';
return 'Double';
default:
(typeAnnotation.name: empty);
throw new Error(`Receieved invalid type: ${typeAnnotation.name}`);
}
case 'BooleanTypeAnnotation':
return 'boolean';
return 'Boolean';
case 'DoubleTypeAnnotation':
return 'double';
return 'Double';
case 'FloatTypeAnnotation':
return 'float';
return 'Float';
case 'Int32TypeAnnotation':
return 'int';
return 'Int';
case 'StringTypeAnnotation':
return 'String';
case 'ArrayTypeAnnotation':
@@ -184,11 +167,11 @@ function getCommandArguments(
componentName: string,
): string {
return [
'T view',
'view: T',
...command.typeAnnotation.params.map(param => {
const commandArgJavaType = getCommandArgJavaType(param);
return `${commandArgJavaType} ${param.name}`;
return `${param.name}: ${commandArgJavaType}`;
}),
].join(', ');
}
@@ -201,10 +184,10 @@ function generateCommandsString(
.map(command => {
const safeJavaName = toSafeJavaString(command.name, false);
return `void ${safeJavaName}(${getCommandArguments(
return `public fun ${safeJavaName}(${getCommandArguments(
command,
componentName,
)});`;
)}): Unit`;
})
.join('\n' + ' ');
}
@@ -287,7 +270,7 @@ module.exports = {
.trimRight(),
});
files.set(`${outputDir}/${className}.java`, replacedTemplate);
files.set(`${outputDir}/${className}.kt`, replacedTemplate);
});
});