|
| 1 | +// Copyright 2020 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'ast.dart'; |
| 6 | +import 'generator_tools.dart'; |
| 7 | + |
| 8 | +/// Options that control how Java code will be generated. |
| 9 | +class JavaOptions { |
| 10 | + /// The name of the class that will house all the generated classes. |
| 11 | + String className; |
| 12 | + |
| 13 | + /// The package where the generated class will live. |
| 14 | + String package; |
| 15 | +} |
| 16 | + |
| 17 | +void _writeHostApi(Indent indent, Api api) { |
| 18 | + assert(api.location == ApiLocation.host); |
| 19 | + indent.writeln( |
| 20 | + '/** Generated interface from Pigeon that represents a handler of messages from Flutter.*/'); |
| 21 | + indent.write('public interface ${api.name} '); |
| 22 | + indent.scoped('{', '}', () { |
| 23 | + for (Method method in api.methods) { |
| 24 | + indent.writeln( |
| 25 | + '${method.returnType} ${method.name}(${method.argType} arg);'); |
| 26 | + } |
| 27 | + indent.addln(''); |
| 28 | + indent.writeln( |
| 29 | + '/** Sets up an instance of `${api.name}` to handle messages through the `binaryMessenger` */'); |
| 30 | + indent.write( |
| 31 | + 'public static void setup(BinaryMessenger binaryMessenger, ${api.name} api) '); |
| 32 | + indent.scoped('{', '}', () { |
| 33 | + for (Method method in api.methods) { |
| 34 | + final String channelName = makeChannelName(api, method); |
| 35 | + indent.write(''); |
| 36 | + indent.scoped('{', '}', () { |
| 37 | + indent.writeln('BasicMessageChannel<Object> channel ='); |
| 38 | + indent.inc(); |
| 39 | + indent.inc(); |
| 40 | + indent.writeln( |
| 41 | + 'new BasicMessageChannel<Object>(binaryMessenger, "$channelName", new StandardMessageCodec());'); |
| 42 | + indent.dec(); |
| 43 | + indent.dec(); |
| 44 | + indent.write( |
| 45 | + 'channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() '); |
| 46 | + indent.scoped('{', '});', () { |
| 47 | + indent.write( |
| 48 | + 'public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) '); |
| 49 | + indent.scoped('{', '}', () { |
| 50 | + final String argType = method.argType; |
| 51 | + final String returnType = method.returnType; |
| 52 | + indent.writeln( |
| 53 | + '$argType input = $argType.fromMap((HashMap)message);'); |
| 54 | + indent.writeln('$returnType output = api.${method.name}(input);'); |
| 55 | + indent.writeln('reply.reply(output.toMap());'); |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +// void _writeFlutterApi(Indent indent, Api api) { |
| 65 | +// assert(api.location == ApiLocation.flutter); |
| 66 | +// } |
| 67 | + |
| 68 | +String _makeGetter(Field field) { |
| 69 | + final String uppercased = |
| 70 | + field.name.substring(0, 1).toUpperCase() + field.name.substring(1); |
| 71 | + return 'get$uppercased'; |
| 72 | +} |
| 73 | + |
| 74 | +String _makeSetter(Field field) { |
| 75 | + final String uppercased = |
| 76 | + field.name.substring(0, 1).toUpperCase() + field.name.substring(1); |
| 77 | + return 'set$uppercased'; |
| 78 | +} |
| 79 | + |
| 80 | +/// Generates the ".java" file for the AST represented by [root] to [sink] with the |
| 81 | +/// provided [options]. |
| 82 | +void generateJava(JavaOptions options, Root root, StringSink sink) { |
| 83 | + final Indent indent = Indent(sink); |
| 84 | + indent.writeln('// Autogenerated from Pigeon, do not edit directly.'); |
| 85 | + indent.writeln('// See also: https://pub.dev/packages/pigeon'); |
| 86 | + indent.addln(''); |
| 87 | + if (options.package != null) { |
| 88 | + indent.writeln('package ${options.package};'); |
| 89 | + } |
| 90 | + indent.addln(''); |
| 91 | + |
| 92 | + indent.writeln('import java.util.HashMap;'); |
| 93 | + indent.addln(''); |
| 94 | + indent.writeln('import io.flutter.plugin.common.BasicMessageChannel;'); |
| 95 | + indent.writeln('import io.flutter.plugin.common.BinaryMessenger;'); |
| 96 | + indent.writeln('import io.flutter.plugin.common.StandardMessageCodec;'); |
| 97 | + |
| 98 | + indent.addln(''); |
| 99 | + assert(options.className != null); |
| 100 | + indent.writeln('/** Generated class from Pigeon. */'); |
| 101 | + indent.write('public class ${options.className} '); |
| 102 | + indent.scoped('{', '}', () { |
| 103 | + for (Class klass in root.classes) { |
| 104 | + indent.addln(''); |
| 105 | + indent.writeln( |
| 106 | + '/** Generated class from Pigeon that represents data sent in messages. */'); |
| 107 | + indent.write('public static class ${klass.name} '); |
| 108 | + indent.scoped('{', '}', () { |
| 109 | + for (Field field in klass.fields) { |
| 110 | + indent.writeln('private ${field.dataType} ${field.name};'); |
| 111 | + indent.writeln( |
| 112 | + 'public ${field.dataType} ${_makeGetter(field)}() { return ${field.name}; }'); |
| 113 | + indent.writeln( |
| 114 | + 'public void ${_makeSetter(field)}(${field.dataType} setterArg) { this.${field.name} = setterArg; }'); |
| 115 | + indent.addln(''); |
| 116 | + } |
| 117 | + indent.write('HashMap toMap() '); |
| 118 | + indent.scoped('{', '}', () { |
| 119 | + indent.writeln( |
| 120 | + 'HashMap<String, Object> toMapResult = new HashMap<String, Object>();'); |
| 121 | + for (Field field in klass.fields) { |
| 122 | + indent.writeln('toMapResult.put("${field.name}", ${field.name});'); |
| 123 | + } |
| 124 | + indent.writeln('return toMapResult;'); |
| 125 | + }); |
| 126 | + indent.write('static ${klass.name} fromMap(HashMap map) '); |
| 127 | + indent.scoped('{', '}', () { |
| 128 | + indent.writeln('${klass.name} fromMapResult = new ${klass.name}();'); |
| 129 | + for (Field field in klass.fields) { |
| 130 | + indent.writeln( |
| 131 | + 'fromMapResult.${field.name} = (${field.dataType})map.get("${field.name}");'); |
| 132 | + } |
| 133 | + indent.writeln('return fromMapResult;'); |
| 134 | + }); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + for (Api api in root.apis) { |
| 139 | + indent.addln(''); |
| 140 | + if (api.location == ApiLocation.host) { |
| 141 | + _writeHostApi(indent, api); |
| 142 | + } else if (api.location == ApiLocation.flutter) { |
| 143 | + // _writeFlutterApi(indent, api); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | +} |
0 commit comments