Skip to content

Commit e55b1cc

Browse files
[pigeon] Adds Kotlin lint tests to example code and fix lints (#9034)
Fixes flutter/flutter#166780 Also moves Kotlin utility methods into a private object so that they don't trigger the `SyntheticAccessor` lint. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 04de46e commit e55b1cc

File tree

16 files changed

+833
-709
lines changed

16 files changed

+833
-709
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 25.3.1
2+
3+
* [kotlin] Fixes Kotlin InstanceManager not properly removing callbacks from handler.
4+
* [kotlin] Fixes `SyntheticAccessor` lint caused by private utility methods.
5+
16
## 25.3.0
27

38
* [swift] Adds equality methods to generated data classes.

packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/EventChannelMessages.g.kt

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,32 @@ import io.flutter.plugin.common.StandardMethodCodec
1212
import java.io.ByteArrayOutputStream
1313
import java.nio.ByteBuffer
1414

15-
private fun deepEqualsEventChannelMessages(a: Any?, b: Any?): Boolean {
16-
if (a is ByteArray && b is ByteArray) {
17-
return a.contentEquals(b)
18-
}
19-
if (a is IntArray && b is IntArray) {
20-
return a.contentEquals(b)
21-
}
22-
if (a is LongArray && b is LongArray) {
23-
return a.contentEquals(b)
24-
}
25-
if (a is DoubleArray && b is DoubleArray) {
26-
return a.contentEquals(b)
27-
}
28-
if (a is Array<*> && b is Array<*>) {
29-
return a.size == b.size && a.indices.all { deepEqualsEventChannelMessages(a[it], b[it]) }
30-
}
31-
if (a is List<*> && b is List<*>) {
32-
return a.size == b.size && a.indices.all { deepEqualsEventChannelMessages(a[it], b[it]) }
33-
}
34-
if (a is Map<*, *> && b is Map<*, *>) {
35-
return a.size == b.size &&
36-
a.all {
37-
(b as Map<Any?, Any?>).containsKey(it.key) &&
38-
deepEqualsEventChannelMessages(it.value, b[it.key])
39-
}
15+
private object EventChannelMessagesPigeonUtils {
16+
fun deepEquals(a: Any?, b: Any?): Boolean {
17+
if (a is ByteArray && b is ByteArray) {
18+
return a.contentEquals(b)
19+
}
20+
if (a is IntArray && b is IntArray) {
21+
return a.contentEquals(b)
22+
}
23+
if (a is LongArray && b is LongArray) {
24+
return a.contentEquals(b)
25+
}
26+
if (a is DoubleArray && b is DoubleArray) {
27+
return a.contentEquals(b)
28+
}
29+
if (a is Array<*> && b is Array<*>) {
30+
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
31+
}
32+
if (a is List<*> && b is List<*>) {
33+
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
34+
}
35+
if (a is Map<*, *> && b is Map<*, *>) {
36+
return a.size == b.size &&
37+
a.all { (b as Map<Any?, Any?>).containsKey(it.key) && deepEquals(it.value, b[it.key]) }
38+
}
39+
return a == b
4040
}
41-
return a == b
4241
}
4342

4443
/**
@@ -68,7 +67,7 @@ data class IntEvent(val data: Long) : PlatformEvent() {
6867
if (this === other) {
6968
return true
7069
}
71-
return deepEqualsEventChannelMessages(toList(), other.toList())
70+
return EventChannelMessagesPigeonUtils.deepEquals(toList(), other.toList())
7271
}
7372

7473
override fun hashCode(): Int = toList().hashCode()
@@ -96,7 +95,7 @@ data class StringEvent(val data: String) : PlatformEvent() {
9695
if (this === other) {
9796
return true
9897
}
99-
return deepEqualsEventChannelMessages(toList(), other.toList())
98+
return EventChannelMessagesPigeonUtils.deepEquals(toList(), other.toList())
10099
}
101100

102101
override fun hashCode(): Int = toList().hashCode()

packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/Messages.g.kt

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,53 @@ import io.flutter.plugin.common.StandardMessageCodec
1313
import java.io.ByteArrayOutputStream
1414
import java.nio.ByteBuffer
1515

16-
private fun wrapResult(result: Any?): List<Any?> {
17-
return listOf(result)
18-
}
16+
private object MessagesPigeonUtils {
1917

20-
private fun wrapError(exception: Throwable): List<Any?> {
21-
return if (exception is FlutterError) {
22-
listOf(exception.code, exception.message, exception.details)
23-
} else {
24-
listOf(
25-
exception.javaClass.simpleName,
26-
exception.toString(),
27-
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception))
18+
fun createConnectionError(channelName: String): FlutterError {
19+
return FlutterError(
20+
"channel-error", "Unable to establish connection on channel: '$channelName'.", "")
21+
}
22+
23+
fun wrapResult(result: Any?): List<Any?> {
24+
return listOf(result)
25+
}
26+
27+
fun wrapError(exception: Throwable): List<Any?> {
28+
return if (exception is FlutterError) {
29+
listOf(exception.code, exception.message, exception.details)
30+
} else {
31+
listOf(
32+
exception.javaClass.simpleName,
33+
exception.toString(),
34+
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception))
35+
}
2836
}
29-
}
3037

31-
private fun createConnectionError(channelName: String): FlutterError {
32-
return FlutterError(
33-
"channel-error", "Unable to establish connection on channel: '$channelName'.", "")
38+
fun deepEquals(a: Any?, b: Any?): Boolean {
39+
if (a is ByteArray && b is ByteArray) {
40+
return a.contentEquals(b)
41+
}
42+
if (a is IntArray && b is IntArray) {
43+
return a.contentEquals(b)
44+
}
45+
if (a is LongArray && b is LongArray) {
46+
return a.contentEquals(b)
47+
}
48+
if (a is DoubleArray && b is DoubleArray) {
49+
return a.contentEquals(b)
50+
}
51+
if (a is Array<*> && b is Array<*>) {
52+
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
53+
}
54+
if (a is List<*> && b is List<*>) {
55+
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
56+
}
57+
if (a is Map<*, *> && b is Map<*, *>) {
58+
return a.size == b.size &&
59+
a.all { (b as Map<Any?, Any?>).containsKey(it.key) && deepEquals(it.value, b[it.key]) }
60+
}
61+
return a == b
62+
}
3463
}
3564

3665
/**
@@ -46,34 +75,6 @@ class FlutterError(
4675
val details: Any? = null
4776
) : Throwable()
4877

49-
private fun deepEqualsMessages(a: Any?, b: Any?): Boolean {
50-
if (a is ByteArray && b is ByteArray) {
51-
return a.contentEquals(b)
52-
}
53-
if (a is IntArray && b is IntArray) {
54-
return a.contentEquals(b)
55-
}
56-
if (a is LongArray && b is LongArray) {
57-
return a.contentEquals(b)
58-
}
59-
if (a is DoubleArray && b is DoubleArray) {
60-
return a.contentEquals(b)
61-
}
62-
if (a is Array<*> && b is Array<*>) {
63-
return a.size == b.size && a.indices.all { deepEqualsMessages(a[it], b[it]) }
64-
}
65-
if (a is List<*> && b is List<*>) {
66-
return a.size == b.size && a.indices.all { deepEqualsMessages(a[it], b[it]) }
67-
}
68-
if (a is Map<*, *> && b is Map<*, *>) {
69-
return a.size == b.size &&
70-
a.all {
71-
(b as Map<Any?, Any?>).containsKey(it.key) && deepEqualsMessages(it.value, b[it.key])
72-
}
73-
}
74-
return a == b
75-
}
76-
7778
enum class Code(val raw: Int) {
7879
ONE(0),
7980
TWO(1);
@@ -118,7 +119,7 @@ data class MessageData(
118119
if (this === other) {
119120
return true
120121
}
121-
return deepEqualsMessages(toList(), other.toList())
122+
return MessagesPigeonUtils.deepEquals(toList(), other.toList())
122123
}
123124

124125
override fun hashCode(): Int = toList().hashCode()
@@ -184,7 +185,7 @@ interface ExampleHostApi {
184185
try {
185186
listOf(api.getHostLanguage())
186187
} catch (exception: Throwable) {
187-
wrapError(exception)
188+
MessagesPigeonUtils.wrapError(exception)
188189
}
189190
reply.reply(wrapped)
190191
}
@@ -207,7 +208,7 @@ interface ExampleHostApi {
207208
try {
208209
listOf(api.add(aArg, bArg))
209210
} catch (exception: Throwable) {
210-
wrapError(exception)
211+
MessagesPigeonUtils.wrapError(exception)
211212
}
212213
reply.reply(wrapped)
213214
}
@@ -228,10 +229,10 @@ interface ExampleHostApi {
228229
api.sendMessage(messageArg) { result: Result<Boolean> ->
229230
val error = result.exceptionOrNull()
230231
if (error != null) {
231-
reply.reply(wrapError(error))
232+
reply.reply(MessagesPigeonUtils.wrapError(error))
232233
} else {
233234
val data = result.getOrNull()
234-
reply.reply(wrapResult(data))
235+
reply.reply(MessagesPigeonUtils.wrapResult(data))
235236
}
236237
}
237238
}
@@ -274,7 +275,7 @@ class MessageFlutterApi(
274275
callback(Result.success(output))
275276
}
276277
} else {
277-
callback(Result.failure(createConnectionError(channelName)))
278+
callback(Result.failure(MessagesPigeonUtils.createConnectionError(channelName)))
278279
}
279280
}
280281
}

packages/pigeon/lib/src/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'generator.dart';
1515
/// The current version of pigeon.
1616
///
1717
/// This must match the version in pubspec.yaml.
18-
const String pigeonVersion = '25.3.0';
18+
const String pigeonVersion = '25.3.1';
1919

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

0 commit comments

Comments
 (0)