@@ -870,6 +870,7 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
870870 indent.write ('${sealed }class ${classDefinition .name } $implements ' );
871871 indent.addScoped ('{' , '}' , () {
872872 if (classDefinition.isSealed) {
873+ _writeSealedClassNativeInteropConversions (generatorOptions, indent, classDefinition);
873874 return ;
874875 }
875876 _writeConstructor (indent, classDefinition);
@@ -958,7 +959,59 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
958959 });
959960 }
960961
962+ /// Writes the native interop conversions of a sealed class.
963+ ///
964+ /// A sealed class has no native object of its own. Its subclasses implement
965+ /// `toJni` and `toFfi` , and `fromJni` and `fromFfi` pick the subclass from
966+ /// the runtime type of the native object.
967+ void _writeSealedClassNativeInteropConversions (
968+ InternalDartOptions generatorOptions,
969+ Indent indent,
970+ Class classDefinition,
971+ ) {
972+ if (generatorOptions.useJni) {
973+ final String jniName = _JniType .fromClass (classDefinition).jniName;
974+ indent.writeln ('$jniName toJni();' );
975+ indent.newln ();
976+ indent.writeScoped ('static ${classDefinition .name }? fromJni($jniName ? jniClass) {' , '}' , () {
977+ indent.writeScoped ('if (jniClass == null) {' , '}' , () {
978+ indent.writeln ('return null;' );
979+ });
980+ for (final Class child in classDefinition.children) {
981+ final String childJniName = _JniType .fromClass (child).jniName;
982+ indent.writeScoped ('if (jniClass.isA<$childJniName >($childJniName .type)) {' , '}' , () {
983+ indent.writeln ('return ${child .name }.fromJni(jniClass.as($childJniName .type));' );
984+ });
985+ }
986+ indent.writeln ('throw ArgumentError.value(jniClass);' );
987+ });
988+ }
989+ if (generatorOptions.useFfi) {
990+ if (generatorOptions.useJni) {
991+ indent.newln ();
992+ }
993+ final String ffiName = _FfiType .fromClass (classDefinition).getFfiName ();
994+ indent.writeln ('$ffiName toFfi();' );
995+ indent.newln ();
996+ indent.writeScoped ('static ${classDefinition .name }? fromFfi($ffiName ? ffiClass) {' , '}' , () {
997+ indent.writeScoped ('if (ffiClass == null) {' , '}' , () {
998+ indent.writeln ('return null;' );
999+ });
1000+ for (final Class child in classDefinition.children) {
1001+ final String childFfiName = _FfiType .fromClass (child).getFfiName ();
1002+ indent.writeScoped ('if ($childFfiName .isA(ffiClass)) {' , '}' , () {
1003+ indent.writeln ('return ${child .name }.fromFfi($childFfiName .as(ffiClass));' );
1004+ });
1005+ }
1006+ indent.writeln ('throw ArgumentError.value(ffiClass);' );
1007+ });
1008+ }
1009+ }
1010+
9611011 void _writeToJni (Indent indent, Class classDefinition) {
1012+ if (classDefinition.superClass != null ) {
1013+ indent.writeln ('@override' );
1014+ }
9621015 indent.writeScoped ('$_jniBridgePrefix .${classDefinition .name } toJni() {' , '}' , () {
9631016 indent.writeScoped ('return $_jniBridgePrefix .${classDefinition .name } (' , ');' , () {
9641017 for (final NamedType field in getFieldsInSerializationOrder (classDefinition)) {
@@ -971,6 +1024,9 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
9711024
9721025 void _writeToFfi (Indent indent, Class classDefinition) {
9731026 final _FfiType ffiClass = _FfiType .fromClass (classDefinition);
1027+ if (classDefinition.superClass != null ) {
1028+ indent.writeln ('@override' );
1029+ }
9741030 indent.writeScoped ('${ffiClass .getFfiName ()} toFfi() {' , '}' , () {
9751031 final Iterable <NamedType > fields = getFieldsInSerializationOrder (classDefinition);
9761032 if (fields.isEmpty) {
@@ -2525,6 +2581,9 @@ ${api.name}({
25252581 }
25262582
25272583 void _writeJniCodec (Indent indent, Root root) {
2584+ // Sealed classes have no native object of their own, and the branches for
2585+ // their subclasses already match every value they can hold.
2586+ final Iterable <Class > concreteClasses = root.classes.where ((Class c) => ! c.isSealed);
25282587 indent.newln ();
25292588 indent.format ('''
25302589class _PigeonJniCodec {
@@ -2581,7 +2640,7 @@ class _PigeonJniCodec {
25812640 res[readValue(entry.key)] = readValue(entry.value);
25822641 }
25832642 return res;
2584- ${root . classes .map ((Class dataClass ) {
2643+ ${concreteClasses .map ((Class dataClass ) {
25852644 final _JniType jniType = _JniType .fromClass (dataClass );
25862645 return '''
25872646 } else if (value.isA<${jniType .jniName }>(
@@ -2675,7 +2734,7 @@ class _PigeonJniCodec {
26752734 .map<JObject?, JObject?>((k, v) =>
26762735 MapEntry(writeValue<JObject?>(k), writeValue<JObject?>(v)))
26772736 .toJMap() as T;
2678- ${root . classes .map ((Class dataClass ) {
2737+ ${concreteClasses .map ((Class dataClass ) {
26792738 final _JniType jniType = _JniType .fromClass (dataClass );
26802739 return '''
26812740 } else if (value is ${jniType .type .baseName }) {
@@ -2750,6 +2809,9 @@ class _PigeonJniCodec {
27502809 }
27512810
27522811 void _writeFfiCodec (Indent indent, Root root) {
2812+ // Sealed classes have no native object of their own, and the branches for
2813+ // their subclasses already match every value they can hold.
2814+ final Iterable <Class > concreteClasses = root.classes.where ((Class c) => ! c.isSealed);
27532815 indent.newln ();
27542816 indent.format ('''
27552817class _PigeonFfiCodec {
@@ -2791,7 +2853,7 @@ class _PigeonFfiCodec {
27912853 return res;
27922854 } else if ($_ffiBridgePrefix .${_classNamePrefix }NumberWrapper.isA(value)) {
27932855 return _convertNumberWrapperToDart($_ffiBridgePrefix .${_classNamePrefix }NumberWrapper.as(value));
2794- ${root . classes .map ((Class dataClass ) {
2856+ ${concreteClasses .map ((Class dataClass ) {
27952857 final _FfiType ffiType = _FfiType .fromClass (dataClass );
27962858 return '''
27972859 } else if (${ffiType .getFfiName ()}.isA(value)) {
@@ -2876,7 +2938,7 @@ class _PigeonFfiCodec {
28762938 res.setObject(writeValue(entry.value, generic: true), forKey: NSCopying.as(writeValue(entry.key, generic: true)));
28772939 }
28782940 return res as T;
2879- ${root . classes .map ((Class dataClass ) {
2941+ ${concreteClasses .map ((Class dataClass ) {
28802942 final _FfiType ffiType = _FfiType .fromClass (dataClass );
28812943 return '''
28822944 } else if (value is ${ffiType .type .baseName }) {
0 commit comments