[pigeon] Fixes some generated Java code's warning. (#2654)
diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md
index 1ae9408..d0beccf 100644
--- a/packages/pigeon/CHANGELOG.md
+++ b/packages/pigeon/CHANGELOG.md
@@ -1,3 +1,9 @@
+## 4.2.3
+
+* [java] Adds assert `args != null`.
+* [java] Changes the args of a single element to `ArrayList` from `Arrays.asList` to `Collections.singletonList`.
+* [java] Removes cast for `Object`.
+
## 4.2.2
* Removes unneeded custom codecs for all languages.
diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart
index e23dc62..1f9a641 100644
--- a/packages/pigeon/lib/generator_tools.dart
+++ b/packages/pigeon/lib/generator_tools.dart
@@ -9,7 +9,7 @@
import 'ast.dart';
/// The current version of pigeon. This must match the version in pubspec.yaml.
-const String pigeonVersion = '4.2.2';
+const String pigeonVersion = '4.2.3';
/// Read all the content from [stdin] to a String.
String readStdin() {
diff --git a/packages/pigeon/lib/java_generator.dart b/packages/pigeon/lib/java_generator.dart
index 0734569..a1a3b30 100644
--- a/packages/pigeon/lib/java_generator.dart
+++ b/packages/pigeon/lib/java_generator.dart
@@ -224,6 +224,7 @@
if (method.arguments.isNotEmpty) {
indent.writeln(
'ArrayList<Object> args = (ArrayList<Object>)message;');
+ indent.writeln('assert args != null;');
enumerate(method.arguments, (int index, NamedType arg) {
// The StandardMessageCodec can give us [Integer, Long] for
// a Dart 'int'. To keep things simple we just use 64bit
@@ -238,7 +239,7 @@
String accessor = 'args.get($index)';
if (isEnum(arg.type)) {
accessor = _intToEnum(accessor, arg.type.baseName);
- } else {
+ } else if (argType != 'Object') {
accessor = '($argType)$accessor';
}
indent.writeln('$argType $argName = $accessor;');
@@ -396,8 +397,13 @@
.map((NamedType e) => _nullsafeJavaTypeForDartType(e.type));
final Iterable<String> argNames =
indexMap(func.arguments, _getSafeArgumentName);
- sendArgument =
- 'new ArrayList<Object>(Arrays.asList(${argNames.join(', ')}))';
+ if (func.arguments.length == 1) {
+ sendArgument =
+ 'new ArrayList<Object>(Collections.singletonList(${argNames.first}))';
+ } else {
+ sendArgument =
+ 'new ArrayList<Object>(Arrays.asList(${argNames.join(', ')}))';
+ }
final String argsSignature =
map2(argTypes, argNames, (String x, String y) => '$x $y')
.join(', ');
@@ -542,6 +548,7 @@
indent.writeln('import java.nio.ByteBuffer;');
indent.writeln('import java.util.Arrays;');
indent.writeln('import java.util.ArrayList;');
+ indent.writeln('import java.util.Collections;');
indent.writeln('import java.util.List;');
indent.writeln('import java.util.Map;');
indent.writeln('import java.util.HashMap;');
diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml
index 84029e2..c87de52 100644
--- a/packages/pigeon/pubspec.yaml
+++ b/packages/pigeon/pubspec.yaml
@@ -2,7 +2,7 @@
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
-version: 4.2.2 # This must match the version in lib/generator_tools.dart
+version: 4.2.3 # This must match the version in lib/generator_tools.dart
environment:
sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/pigeon/test/java_generator_test.dart b/packages/pigeon/test/java_generator_test.dart
index bbe6db8..d3822c5 100644
--- a/packages/pigeon/test/java_generator_test.dart
+++ b/packages/pigeon/test/java_generator_test.dart
@@ -870,6 +870,28 @@
'Long output = api.add((xArg == null) ? null : xArg.longValue(), (yArg == null) ? null : yArg.longValue())'));
});
+ test('if host argType is Object not cast', () {
+ final Root root = Root(apis: <Api>[
+ Api(name: 'Api', location: ApiLocation.host, methods: <Method>[
+ Method(
+ name: 'objectTest',
+ arguments: <NamedType>[
+ NamedType(
+ name: 'x',
+ type: const TypeDeclaration(
+ isNullable: false, baseName: 'Object')),
+ ],
+ returnType: const TypeDeclaration.voidDeclaration(),
+ )
+ ])
+ ], classes: <Class>[], enums: <Enum>[]);
+ final StringBuffer sink = StringBuffer();
+ const JavaOptions javaOptions = JavaOptions(className: 'Api');
+ generateJava(javaOptions, root, sink);
+ final String code = sink.toString();
+ expect(code, contains('Object xArg = args.get(0)'));
+ });
+
test('flutter multiple args', () {
final Root root = Root(apis: <Api>[
Api(name: 'Api', location: ApiLocation.flutter, methods: <Method>[
@@ -906,6 +928,31 @@
'channel.send(new ArrayList<Object>(Arrays.asList(xArg, yArg)), channelReply ->'));
});
+ test('flutter single args', () {
+ final Root root = Root(apis: <Api>[
+ Api(name: 'Api', location: ApiLocation.flutter, methods: <Method>[
+ Method(
+ name: 'send',
+ arguments: <NamedType>[
+ NamedType(
+ name: 'x',
+ type:
+ const TypeDeclaration(isNullable: false, baseName: 'int')),
+ ],
+ returnType: const TypeDeclaration(baseName: 'int', isNullable: false),
+ )
+ ])
+ ], classes: <Class>[], enums: <Enum>[]);
+ final StringBuffer sink = StringBuffer();
+ const JavaOptions javaOptions = JavaOptions(className: 'Messages');
+ generateJava(javaOptions, root, sink);
+ final String code = sink.toString();
+ expect(
+ code,
+ contains(
+ 'channel.send(new ArrayList<Object>(Collections.singletonList(xArg)), channelReply ->'));
+ });
+
test('return nullable host', () {
final Root root = Root(
apis: <Api>[