Add extension functions that accept reified types.

This commit is contained in:
Isira Seneviratne 2020-12-15 16:45:34 +05:30
parent 486e720e00
commit 50dcf308a2
2 changed files with 51 additions and 41 deletions

View File

@ -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.
*

View File

@ -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>())
}
}