Skip to content

Commit 9a0eec1

Browse files
authored
[pigeon] Handle int results from Flutter as Integer or Long. (#2324)
Java: Fixes int return values from FlutterApi()'s
1 parent f398279 commit 9a0eec1

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.2.6
2+
3+
* [java] Fixes returning int values from FlutterApi methods that fit in 32 bits.
4+
15
## 3.2.5
26

37
* [c++] Fixes style issues in `FlutterError` and `ErrorOr`. The names and

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'dart:mirrors';
99
import 'ast.dart';
1010

1111
/// The current version of pigeon. This must match the version in pubspec.yaml.
12-
const String pigeonVersion = '3.2.5';
12+
const String pigeonVersion = '3.2.6';
1313

1414
/// Read all the content from [stdin] to a String.
1515
String readStdin() {

packages/pigeon/lib/java_generator.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,13 @@ static MessageCodec<Object> getCodec() {
376376
} else {
377377
const String output = 'output';
378378
indent.writeln('@SuppressWarnings("ConstantConditions")');
379-
indent.writeln('$returnType $output = ($returnType)channelReply;');
379+
if (func.returnType.baseName == 'int') {
380+
indent.writeln(
381+
'$returnType $output = channelReply == null ? null : ((Number)channelReply).longValue();');
382+
} else {
383+
indent
384+
.writeln('$returnType $output = ($returnType)channelReply;');
385+
}
380386
indent.writeln('callback.reply($output);');
381387
}
382388
});

packages/pigeon/platform_tests/android_unit_tests/android/app/src/test/java/com/example/android_unit_tests/PrimitiveTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ private static BinaryMessenger makeMockBinaryMessenger() {
3030
message.position(0);
3131
ArrayList<Object> args =
3232
(ArrayList<Object>) PrimitiveFlutterApi.getCodec().decodeMessage(message);
33-
ByteBuffer replyData = PrimitiveFlutterApi.getCodec().encodeMessage(args.get(0));
33+
Object arg = args.get(0);
34+
if (arg instanceof Long) {
35+
Long longArg = (Long) arg;
36+
if (longArg.intValue() == longArg.longValue()) {
37+
// Value fits in the Integer so gets sent as such
38+
// https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-java-tab#codec
39+
arg = Integer.valueOf(longArg.intValue());
40+
}
41+
}
42+
ByteBuffer replyData = PrimitiveFlutterApi.getCodec().encodeMessage(arg);
3443
replyData.position(0);
3544
reply.reply(replyData);
3645
return null;
@@ -54,6 +63,20 @@ public void primitiveInt() {
5463
assertTrue(didCall[0]);
5564
}
5665

66+
@Test
67+
public void primitiveLongInt() {
68+
BinaryMessenger binaryMessenger = makeMockBinaryMessenger();
69+
PrimitiveFlutterApi api = new PrimitiveFlutterApi(binaryMessenger);
70+
boolean[] didCall = {false};
71+
api.anInt(
72+
1L << 50,
73+
(Long result) -> {
74+
didCall[0] = true;
75+
assertEquals(result.longValue(), 1L << 50);
76+
});
77+
assertTrue(didCall[0]);
78+
}
79+
5780
@Test
5881
public void primitiveIntHostApi() {
5982
PrimitiveHostApi mockApi = mock(PrimitiveHostApi.class);

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
5-
version: 3.2.5 # This must match the version in lib/generator_tools.dart
5+
version: 3.2.6 # This must match the version in lib/generator_tools.dart
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"

packages/pigeon/test/java_generator_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,32 @@ void main() {
836836
expect(code, contains('List<Long> output ='));
837837
});
838838

839+
test('flutter int return', () {
840+
final Root root = Root(
841+
apis: <Api>[
842+
Api(name: 'Api', location: ApiLocation.flutter, methods: <Method>[
843+
Method(
844+
name: 'doit',
845+
returnType:
846+
const TypeDeclaration(baseName: 'int', isNullable: false),
847+
arguments: <NamedType>[],
848+
isAsynchronous: true)
849+
])
850+
],
851+
classes: <Class>[],
852+
enums: <Enum>[],
853+
);
854+
final StringBuffer sink = StringBuffer();
855+
const JavaOptions javaOptions = JavaOptions(className: 'Messages');
856+
generateJava(javaOptions, root, sink);
857+
final String code = sink.toString();
858+
expect(code, contains('doit(Reply<Long> callback)'));
859+
expect(
860+
code,
861+
contains(
862+
'Long output = channelReply == null ? null : ((Number)channelReply).longValue();'));
863+
});
864+
839865
test('host multiple args', () {
840866
final Root root = Root(apis: <Api>[
841867
Api(name: 'Api', location: ApiLocation.host, methods: <Method>[

0 commit comments

Comments
 (0)