From a2e0f2d42cb1528f3ca0297ceded8c21ea256fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Faria?= Date: Mon, 28 Mar 2022 16:57:30 -0400 Subject: [PATCH] Fix exception on annotation traversal when target is not present (#51) --- .../java/com/slack/eithernet/Annotations.kt | 2 +- .../com/slack/eithernet/ResultTypeTest.kt | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/slack/eithernet/Annotations.kt b/src/main/java/com/slack/eithernet/Annotations.kt index fdbee51..47d1bde 100644 --- a/src/main/java/com/slack/eithernet/Annotations.kt +++ b/src/main/java/com/slack/eithernet/Annotations.kt @@ -80,7 +80,7 @@ private fun Array.nextAnnotations(type: Class): Pai if (type.isInstance(next)) { @Suppress("UNCHECKED_CAST") resultType = next as A - } else { + } else if (nextIndex < nextAnnotations.size) { nextAnnotations[nextIndex] = next nextIndex++ } diff --git a/src/test/kotlin/com/slack/eithernet/ResultTypeTest.kt b/src/test/kotlin/com/slack/eithernet/ResultTypeTest.kt index 9436309..8cf39db 100644 --- a/src/test/kotlin/com/slack/eithernet/ResultTypeTest.kt +++ b/src/test/kotlin/com/slack/eithernet/ResultTypeTest.kt @@ -62,7 +62,7 @@ class ResultTypeTest { } @Test - fun errorType() { + fun errorType_present() { val annotations = Array(4) { ResultTypeTest::class.java.getAnnotation(SampleAnnotation::class.java) } @@ -74,7 +74,15 @@ class ResultTypeTest { } @Test - fun statusCode() { + fun errorType_absent() { + val annotations = Array(4) { + ResultTypeTest::class.java.getAnnotation(SampleAnnotation::class.java) + } + assertThat(annotations.errorType()).isNull() + } + + @Test + fun statusCode_present() { val annotations = Array(4) { ResultTypeTest::class.java.getAnnotation(SampleAnnotation::class.java) } @@ -85,6 +93,14 @@ class ResultTypeTest { assertThat(statusCode).isSameInstanceAs(statusCodeAnnotation) } + @Test + fun statusCode_absent() { + val annotations = Array(4) { + ResultTypeTest::class.java.getAnnotation(SampleAnnotation::class.java) + } + assertThat(annotations.statusCode()).isNull() + } + private class A private class B