Skip to content

Commit

Permalink
Add extension functions that accept reified types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Dec 16, 2020
1 parent 37cc6d1 commit a290684
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/org/schabi/newpipe/ktx/Throwable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOException>()

/**
* 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 <reified T : Throwable> 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 <reified T : Throwable> Throwable?.hasAssignableCause() = hasAssignableCause(T::class.java)

/**
* Check if the throwable has some cause from the causes to check, or is itself in it.
*
Expand Down
80 changes: 40 additions & 40 deletions app/src/test/java/org/schabi/newpipe/ktx/ThrowableExtensionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Throwable>())
assertTrue(Exception().hasAssignableCause<Exception>())
assertTrue(IOException().hasAssignableCause<Exception>())

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<IOException>())
assertTrue(Exception(SocketException()).hasAssignableCause<IOException>())
assertTrue(Exception(IllegalStateException()).hasAssignableCause<RuntimeException>())
assertTrue(Exception(Exception(IOException())).hasAssignableCause<IOException>())
assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasAssignableCause<IOException>())
assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause<IOException>())
assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasAssignableCause<IOException>())
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause<IOException>())
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause<RuntimeException>())

assertTrue(IllegalStateException().hasAssignableCause(Throwable::class.java))
assertTrue(IllegalStateException().hasAssignableCause(Exception::class.java))
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(InterruptedIOException::class.java))
assertTrue(IllegalStateException().hasAssignableCause<Throwable>())
assertTrue(IllegalStateException().hasAssignableCause<Exception>())
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause<InterruptedIOException>())
}

@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<Exception>())
assertFalse(Exception().hasAssignableCause<IOException>())
assertFalse(Exception(IllegalStateException()).hasAssignableCause<IOException>())
assertFalse(Exception(NullPointerException()).hasAssignableCause<IOException>())
assertFalse(Exception(IllegalStateException(Exception(Exception()))).hasAssignableCause<IOException>())
assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause<InterruptedIOException>())
assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause<InterruptedException>())
}

@Test fun `exact causes`() {
assertTrue(Throwable().hasExactCause(Throwable::class.java))
assertTrue(Exception().hasExactCause(Exception::class.java))
assertTrue(Throwable().hasExactCause<Throwable>())
assertTrue(Exception().hasExactCause<Exception>())

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<IOException>())
assertTrue(Exception(SocketException()).hasExactCause<SocketException>())
assertTrue(Exception(Exception(IOException())).hasExactCause<IOException>())
assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasExactCause<IOException>())
assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause<SocketException>())
assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasExactCause<SSLException>())
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause<InterruptedIOException>())
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause<IllegalStateException>())
}

@Test fun `no exact causes`() {
assertFalse(Throwable().hasExactCause(Exception::class.java))
assertFalse(Exception().hasExactCause(Throwable::class.java))
assertFalse(Throwable().hasExactCause<Exception>())
assertFalse(Exception().hasExactCause<Throwable>())

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<IOException>())
assertFalse(IllegalStateException().hasExactCause<RuntimeException>())
assertFalse(Exception(SocketException()).hasExactCause<IOException>())
assertFalse(Exception(IllegalStateException(Exception(IOException()))).hasExactCause<RuntimeException>())
assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause<IOException>())
assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause<IOException>())
}
}

0 comments on commit a290684

Please sign in to comment.