@@ -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- \t HashMap<String, Object> errorMap = new HashMap<>();
320+ indent.format (
321+ '''private static Map<String, Object> wrapError(Exception exception) {
322+ \t Map<String, Object> errorMap = new HashMap<>();
302323\t errorMap.put("${Keys .errorMessage }", exception.toString());
303324\t errorMap.put("${Keys .errorCode }", exception.getClass().getSimpleName());
304325\t errorMap.put("${Keys .errorDetails }", null);
0 commit comments