From 50dcf308a266296c92be92e7186299eac20f46d9 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Tue, 15 Dec 2020 16:45:34 +0530 Subject: [PATCH] Add extension functions that accept reified types. --- .../java/org/schabi/newpipe/ktx/Throwable.kt | 12 ++- .../newpipe/ktx/ThrowableExtensionsTest.kt | 80 +++++++++---------- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ktx/Throwable.kt b/app/src/main/java/org/schabi/newpipe/ktx/Throwable.kt index e05a55f3f..b95f46fd4 100644 --- a/app/src/main/java/org/schabi/newpipe/ktx/Throwable.kt +++ b/app/src/main/java/org/schabi/newpipe/ktx/Throwable.kt @@ -15,18 +15,28 @@ val Throwable.isInterruptedCaused: Boolean * @return if throwable is related to network issues, or one of its causes is. */ val Throwable.isNetworkRelated: Boolean - get() = hasAssignableCause(IOException::class.java) + get() = hasAssignableCause() /** * Calls [hasCause] with the `checkSubtypes` parameter set to false. */ fun Throwable.hasExactCause(vararg causesToCheck: Class<*>) = hasCause(false, *causesToCheck) +/** + * Calls [hasCause] with a reified [Throwable] type. + */ +inline fun Throwable.hasExactCause() = hasExactCause(T::class.java) + /** * Calls [hasCause] with the `checkSubtypes` parameter set to true. */ fun Throwable?.hasAssignableCause(vararg causesToCheck: Class<*>) = hasCause(true, *causesToCheck) +/** + * Calls [hasCause] with a reified [Throwable] type. + */ +inline fun Throwable?.hasAssignableCause() = hasAssignableCause(T::class.java) + /** * Check if the throwable has some cause from the causes to check, or is itself in it. * diff --git a/app/src/test/java/org/schabi/newpipe/ktx/ThrowableExtensionsTest.kt b/app/src/test/java/org/schabi/newpipe/ktx/ThrowableExtensionsTest.kt index 8132b8fbc..ca2d04c85 100644 --- a/app/src/test/java/org/schabi/newpipe/ktx/ThrowableExtensionsTest.kt +++ b/app/src/test/java/org/schabi/newpipe/ktx/ThrowableExtensionsTest.kt @@ -10,58 +10,58 @@ import javax.net.ssl.SSLException class ThrowableExtensionsTest { @Test fun `assignable causes`() { - assertTrue(Throwable().hasAssignableCause(Throwable::class.java)) - assertTrue(Exception().hasAssignableCause(Exception::class.java)) - assertTrue(IOException().hasAssignableCause(Exception::class.java)) + assertTrue(Throwable().hasAssignableCause()) + assertTrue(Exception().hasAssignableCause()) + assertTrue(IOException().hasAssignableCause()) - assertTrue(IOException().hasAssignableCause(IOException::class.java)) - assertTrue(Exception(SocketException()).hasAssignableCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException()).hasAssignableCause(RuntimeException::class.java)) - assertTrue(Exception(Exception(IOException())).hasAssignableCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasAssignableCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasAssignableCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(RuntimeException::class.java)) + assertTrue(IOException().hasAssignableCause()) + assertTrue(Exception(SocketException()).hasAssignableCause()) + assertTrue(Exception(IllegalStateException()).hasAssignableCause()) + assertTrue(Exception(Exception(IOException())).hasAssignableCause()) + assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasAssignableCause()) + assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause()) + assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasAssignableCause()) + assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause()) + assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause()) - assertTrue(IllegalStateException().hasAssignableCause(Throwable::class.java)) - assertTrue(IllegalStateException().hasAssignableCause(Exception::class.java)) - assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(InterruptedIOException::class.java)) + assertTrue(IllegalStateException().hasAssignableCause()) + assertTrue(IllegalStateException().hasAssignableCause()) + assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause()) } @Test fun `no assignable causes`() { - assertFalse(Throwable().hasAssignableCause(Exception::class.java)) - assertFalse(Exception().hasAssignableCause(IOException::class.java)) - assertFalse(Exception(IllegalStateException()).hasAssignableCause(IOException::class.java)) - assertFalse(Exception(NullPointerException()).hasAssignableCause(IOException::class.java)) - assertFalse(Exception(IllegalStateException(Exception(Exception()))).hasAssignableCause(IOException::class.java)) - assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause(InterruptedIOException::class.java)) - assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(InterruptedException::class.java)) + assertFalse(Throwable().hasAssignableCause()) + assertFalse(Exception().hasAssignableCause()) + assertFalse(Exception(IllegalStateException()).hasAssignableCause()) + assertFalse(Exception(NullPointerException()).hasAssignableCause()) + assertFalse(Exception(IllegalStateException(Exception(Exception()))).hasAssignableCause()) + assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause()) + assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause()) } @Test fun `exact causes`() { - assertTrue(Throwable().hasExactCause(Throwable::class.java)) - assertTrue(Exception().hasExactCause(Exception::class.java)) + assertTrue(Throwable().hasExactCause()) + assertTrue(Exception().hasExactCause()) - assertTrue(IOException().hasExactCause(IOException::class.java)) - assertTrue(Exception(SocketException()).hasExactCause(SocketException::class.java)) - assertTrue(Exception(Exception(IOException())).hasExactCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasExactCause(IOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause(SocketException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasExactCause(SSLException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause(InterruptedIOException::class.java)) - assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause(IllegalStateException::class.java)) + assertTrue(IOException().hasExactCause()) + assertTrue(Exception(SocketException()).hasExactCause()) + assertTrue(Exception(Exception(IOException())).hasExactCause()) + assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasExactCause()) + assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause()) + assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasExactCause()) + assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause()) + assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause()) } @Test fun `no exact causes`() { - assertFalse(Throwable().hasExactCause(Exception::class.java)) - assertFalse(Exception().hasExactCause(Throwable::class.java)) + assertFalse(Throwable().hasExactCause()) + assertFalse(Exception().hasExactCause()) - assertFalse(SocketException().hasExactCause(IOException::class.java)) - assertFalse(IllegalStateException().hasExactCause(RuntimeException::class.java)) - assertFalse(Exception(SocketException()).hasExactCause(IOException::class.java)) - assertFalse(Exception(IllegalStateException(Exception(IOException()))).hasExactCause(RuntimeException::class.java)) - assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause(IOException::class.java)) - assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause(IOException::class.java)) + assertFalse(SocketException().hasExactCause()) + assertFalse(IllegalStateException().hasExactCause()) + assertFalse(Exception(SocketException()).hasExactCause()) + assertFalse(Exception(IllegalStateException(Exception(IOException()))).hasExactCause()) + assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause()) + assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause()) } }