Fix kotlin nullable errors in net-test-utils and NetworkStaticLibTests
Fix kotlin nullable errors that were exposed by setting the retention
of android.annotation.NonNull and android.annotation.Nullable to
class retention.
Bug: 294110802
Test: builds
Change-Id: I2a2c91188b64619fca190a9bd7812d32f9610abd
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt b/staticlibs/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt
index 9fb025b..e23f999 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt
@@ -63,9 +63,6 @@
assertEquals(0, CollectionUtils.indexOfSubArray(haystack, byteArrayOf()))
assertEquals(-1, CollectionUtils.indexOfSubArray(byteArrayOf(), byteArrayOf(3, 2)))
assertEquals(0, CollectionUtils.indexOfSubArray(byteArrayOf(), byteArrayOf()))
- assertThrows(NullPointerException::class.java) {
- CollectionUtils.indexOfSubArray(haystack, null)
- }
}
@Test
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/FakeDns.kt b/staticlibs/testutils/devicetests/com/android/testutils/FakeDns.kt
index 825d748..1f82a35 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/FakeDns.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/FakeDns.kt
@@ -81,9 +81,9 @@
var type = if (posType != -1) it.arguments[posType] as Int else TYPE_UNSPECIFIED
val answer = getAnswer(hostname, type)
- if (!answer?.addresses.isNullOrEmpty()) {
+ if (answer != null && !answer.addresses.isNullOrEmpty()) {
Handler(Looper.getMainLooper()).post({ executor.execute({
- callback.onAnswer(answer?.addresses, 0); }) })
+ callback.onAnswer(answer.addresses, 0); }) })
}
}
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/PacketBridge.kt b/staticlibs/testutils/devicetests/com/android/testutils/PacketBridge.kt
index da3508d..d50f78a 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/PacketBridge.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/PacketBridge.kt
@@ -48,8 +48,8 @@
private val natMap = NatMap()
private val binder = Binder()
- private val cm = context.getSystemService(ConnectivityManager::class.java)
- private val tnm = context.getSystemService(TestNetworkManager::class.java)
+ private val cm = context.getSystemService(ConnectivityManager::class.java)!!
+ private val tnm = context.getSystemService(TestNetworkManager::class.java)!!
// Create test networks.
private val internalIface = tnm.createTunInterface(listOf(internalAddr))
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt b/staticlibs/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt
index b743b6c..84fb47b 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt
@@ -91,7 +91,7 @@
lp: LinkProperties?,
setupTimeoutMs: Long = 10_000L
): TestNetworkTracker {
- val tnm = context.getSystemService(TestNetworkManager::class.java)
+ val tnm = context.getSystemService(TestNetworkManager::class.java)!!
val iface = if (isAtLeastS()) tnm.createTunInterface(linkAddrs)
else tnm.createTunInterface(linkAddrs.toTypedArray())
val lpWithIface = if (lp == null) null else LinkProperties(lp).apply {
@@ -112,7 +112,7 @@
val lp: LinkProperties?,
setupTimeoutMs: Long
) : TestableNetworkCallback.HasNetwork {
- private val cm = context.getSystemService(ConnectivityManager::class.java)
+ private val cm = context.getSystemService(ConnectivityManager::class.java)!!
private val binder = Binder()
private val networkCallback: NetworkCallback