Visitar URL original
[pigeon] Improve java generation (#214) · flutter/packages@958ce10 · GitHub
Skip to content

Commit 958ce10

Browse files
authored
[pigeon] Improve java generation (#214)
- Use generic Map and List instead of specific ArrayList and HashMap - Allow callback of the flutter api method to be null This fixes flutter/flutter#66453 This resolves java side of the flutter/flutter#58913
1 parent 60e31ec commit 958ce10

2 files changed

Lines changed: 51 additions & 30 deletions

File tree

‎packages/pigeon/lib/java_generator.dart‎

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const Map<String, String> _javaTypeForDartTypeMap = <String, String>{
1414
'Int32List': 'int[]',
1515
'Int64List': 'long[]',
1616
'Float64List': 'double[]',
17-
'List': 'ArrayList',
18-
'Map': 'HashMap',
17+
'List': 'List<Object>',
18+
'Map': 'Map<String, Object>',
1919
};
2020

2121
/// Options that control how Java code will be generated.
@@ -77,15 +77,14 @@ void _writeHostApi(Indent indent, Api api) {
7777
indent.scoped('{', '});', () {
7878
final String argType = method.argType;
7979
final String returnType = method.returnType;
80-
indent.writeln(
81-
'HashMap<String, HashMap> wrapped = new HashMap<>();');
80+
indent.writeln('Map<String, Object> wrapped = new HashMap<>();');
8281
indent.write('try ');
8382
indent.scoped('{', '}', () {
8483
final List<String> methodArgument = <String>[];
8584
if (argType != 'void') {
8685
indent.writeln('@SuppressWarnings("ConstantConditions")');
8786
indent.writeln(
88-
'$argType input = $argType.fromMap((HashMap)message);');
87+
'$argType input = $argType.fromMap((Map<String, Object>)message);');
8988
methodArgument.add('input');
9089
}
9190
if (method.isAsynchronous) {
@@ -168,21 +167,41 @@ void _writeFlutterApi(Indent indent, Api api) {
168167
indent.dec();
169168
indent.dec();
170169
if (func.argType != 'void') {
171-
indent.writeln('HashMap inputMap = argInput.toMap();');
170+
indent.writeln('Map<String, Object> inputMap = argInput.toMap();');
172171
}
173-
indent.write('channel.send($sendArgument, channelReply -> ');
174-
indent.scoped('{', '});', () {
175-
if (func.returnType == 'void') {
176-
indent.writeln('callback.reply(null);');
177-
} else {
178-
indent.writeln('HashMap outputMap = (HashMap)channelReply;');
179-
indent.writeln('@SuppressWarnings("ConstantConditions")');
180-
indent.writeln(
181-
'${func.returnType} output = ${func.returnType}.fromMap(outputMap);');
182-
indent.writeln('callback.reply(output);');
183-
}
172+
indent.write('if (callback != null)');
173+
indent.scoped('{', '}', () {
174+
indent.write('channel.send($sendArgument, channelReply -> ');
175+
indent.scoped('{', '});', () {
176+
if (func.returnType == 'void') {
177+
indent.writeln('callback.reply(null);');
178+
} else {
179+
indent.writeln('Map outputMap = (Map)channelReply;');
180+
indent.writeln('@SuppressWarnings("ConstantConditions")');
181+
indent.writeln(
182+
'${func.returnType} output = ${func.returnType}.fromMap(outputMap);');
183+
indent.writeln('callback.reply(output);');
184+
}
185+
});
186+
});
187+
indent.write(' else ');
188+
indent.scoped('{', '}', () {
189+
indent.writeln('channel.send($sendArgument, null);');
184190
});
185191
});
192+
193+
if (func.argType == 'void') {
194+
indent.write('public void ${func.name}() ');
195+
} else {
196+
indent.write('public void ${func.name}(${func.argType} argInput) ');
197+
}
198+
indent.scoped('{', '}', () {
199+
if (func.argType == 'void') {
200+
indent.writeln('${func.name}(null);');
201+
} else {
202+
indent.writeln('${func.name}(argInput, null);');
203+
}
204+
});
186205
}
187206
});
188207
}
@@ -210,7 +229,7 @@ String _castObject(Field field, List<Class> classes, String varName) {
210229
return '($varName == null) ? null : (($varName instanceof Integer) ? (Integer)$varName : (${hostDatatype.datatype})$varName)';
211230
} else if (!hostDatatype.isBuiltin &&
212231
classes.map((Class x) => x.name).contains(field.dataType)) {
213-
return '${hostDatatype.datatype}.fromMap((HashMap)$varName)';
232+
return '${hostDatatype.datatype}.fromMap((Map)$varName)';
214233
} else {
215234
return '(${hostDatatype.datatype})$varName';
216235
}
@@ -232,13 +251,15 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
232251
indent.writeln('import io.flutter.plugin.common.BasicMessageChannel;');
233252
indent.writeln('import io.flutter.plugin.common.BinaryMessenger;');
234253
indent.writeln('import io.flutter.plugin.common.StandardMessageCodec;');
235-
indent.writeln('import java.util.ArrayList;');
254+
indent.writeln('import java.util.List;');
255+
indent.writeln('import java.util.Map;');
236256
indent.writeln('import java.util.HashMap;');
237257

238258
indent.addln('');
239259
assert(options.className != null);
240260
indent.writeln('/** Generated class from Pigeon. */');
241-
indent.writeln('@SuppressWarnings("unused")');
261+
indent.writeln(
262+
'@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression"})');
242263
indent.write('public class ${options.className} ');
243264
indent.scoped('{', '}', () {
244265
for (Class klass in root.classes) {
@@ -257,10 +278,9 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
257278
'public void ${_makeSetter(field)}(${hostDatatype.datatype} setterArg) { this.${field.name} = setterArg; }');
258279
indent.addln('');
259280
}
260-
indent.write('HashMap toMap() ');
281+
indent.write('Map<String, Object> toMap() ');
261282
indent.scoped('{', '}', () {
262-
indent.writeln(
263-
'HashMap<String, Object> toMapResult = new HashMap<>();');
283+
indent.writeln('Map<String, Object> toMapResult = new HashMap<>();');
264284
for (Field field in klass.fields) {
265285
final HostDatatype hostDatatype =
266286
getHostDatatype(field, root.classes, _javaTypeForDartType);
@@ -275,7 +295,7 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
275295
}
276296
indent.writeln('return toMapResult;');
277297
});
278-
indent.write('static ${klass.name} fromMap(HashMap map) ');
298+
indent.write('static ${klass.name} fromMap(Map<String, Object> map) ');
279299
indent.scoped('{', '}', () {
280300
indent.writeln('${klass.name} fromMapResult = new ${klass.name}();');
281301
for (Field field in klass.fields) {
@@ -297,8 +317,9 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
297317
}
298318
}
299319

300-
indent.format('''private static HashMap wrapError(Exception exception) {
301-
\tHashMap<String, Object> errorMap = new HashMap<>();
320+
indent.format(
321+
'''private static Map<String, Object> wrapError(Exception exception) {
322+
\tMap<String, Object> errorMap = new HashMap<>();
302323
\terrorMap.put("${Keys.errorMessage}", exception.toString());
303324
\terrorMap.put("${Keys.errorCode}", exception.getClass().getSimpleName());
304325
\terrorMap.put("${Keys.errorDetails}", null);

‎packages/pigeon/test/java_generator_test.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void main() {
4646
generateJava(javaOptions, root, sink);
4747
final String code = sink.toString();
4848
expect(code, contains('package com.google.foobar;'));
49-
expect(code, contains('HashMap toMap()'));
49+
expect(code, contains('Map<String, Object> toMap()'));
5050
});
5151

5252
test('gen one host api', () {
@@ -242,7 +242,7 @@ void main() {
242242
generateJava(javaOptions, root, sink);
243243
final String code = sink.toString();
244244
expect(code, contains('public static class Foobar'));
245-
expect(code, contains('private ArrayList field1;'));
245+
expect(code, contains('private List<Object> field1;'));
246246
});
247247

248248
test('gen map', () {
@@ -257,7 +257,7 @@ void main() {
257257
generateJava(javaOptions, root, sink);
258258
final String code = sink.toString();
259259
expect(code, contains('public static class Foobar'));
260-
expect(code, contains('private HashMap field1;'));
260+
expect(code, contains('private Map<String, Object> field1;'));
261261
});
262262

263263
test('gen nested', () {
@@ -287,7 +287,7 @@ void main() {
287287
expect(code, contains('public static class Outer'));
288288
expect(code, contains('public static class Nested'));
289289
expect(code, contains('private Nested nested;'));
290-
expect(code, contains('Nested.fromMap((HashMap)nested);'));
290+
expect(code, contains('Nested.fromMap((Map)nested);'));
291291
expect(code, contains('put("nested", nested.toMap());'));
292292
});
293293

0 commit comments

Comments
 (0)