writeHostApi method

  1. @override
void writeHostApi(
  1. JavaOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. AstHostApi api, {
  5. required String dartPackageName,
})
override

Write the java code that represents a host Api, api. Example: public interface Foo { int add(int x, int y); static void setUp(BinaryMessenger binaryMessenger, Foo api) {...} }

Implementation

@override
void writeHostApi(
  JavaOptions generatorOptions,
  Root root,
  Indent indent,
  AstHostApi api, {
  required String dartPackageName,
}) {
  if (getCodecClasses(api, root).isNotEmpty) {
    _writeCodec(indent, api, root);
  }
  const List<String> generatedMessages = <String>[
    ' Generated interface from Pigeon that represents a handler of messages from Flutter.'
  ];
  addDocumentationComments(indent, api.documentationComments, _docCommentSpec,
      generatorComments: generatedMessages);

  indent.write('public interface ${api.name} ');
  indent.addScoped('{', '}', () {
    for (final Method method in api.methods) {
      _writeInterfaceMethod(generatorOptions, root, indent, api, method);
    }
    indent.newln();
    final String codecName = _getCodecName(api);
    indent.writeln('/** The codec used by ${api.name}. */');
    indent.write('static @NonNull MessageCodec<Object> getCodec() ');
    indent.addScoped('{', '}', () {
      indent.write('return ');
      if (getCodecClasses(api, root).isNotEmpty) {
        indent.addln('$codecName.INSTANCE;');
      } else {
        indent.addln('new $_standardMessageCodec();');
      }
    });

    indent.writeln(
        '${_docCommentPrefix}Sets up an instance of `${api.name}` to handle messages through the `binaryMessenger`.$_docCommentSuffix');
    indent.writeScoped(
        'static void setUp(@NonNull BinaryMessenger binaryMessenger, @Nullable ${api.name} api) {',
        '}', () {
      indent.writeln('setUp(binaryMessenger, "", api);');
    });
    indent.write(
        'static void setUp(@NonNull BinaryMessenger binaryMessenger, @NonNull String messageChannelSuffix, @Nullable ${api.name} api) ');
    indent.addScoped('{', '}', () {
      indent.writeln(
          'messageChannelSuffix = messageChannelSuffix.isEmpty() ? "" : "." + messageChannelSuffix;');
      for (final Method method in api.methods) {
        _writeMethodSetUp(
          generatorOptions,
          root,
          indent,
          api,
          method,
          dartPackageName: dartPackageName,
        );
      }
    });
  });
}