/
Annotations.kt
34 lines (26 loc) · 1.23 KB
/
Annotations.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package io.gitlab.arturbosch.detekt.generator.collection
import org.jetbrains.kotlin.psi.KtAnnotated
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import kotlin.reflect.KClass
fun KtAnnotated.isAnnotatedWith(annotation: KClass<out Annotation>): Boolean =
annotationEntries.any { it.isOfType(annotation) }
fun KtAnnotated.firstAnnotationParameter(annotation: KClass<out Annotation>): String =
checkNotNull(firstAnnotationParameterOrNull(annotation))
fun KtAnnotated.firstAnnotationParameterOrNull(annotation: KClass<out Annotation>): String? =
annotationEntries
.firstOrNull { it.isOfType(annotation) }
?.firstParameterOrNull()
private fun KtAnnotationEntry.isOfType(annotation: KClass<out Annotation>) =
shortName?.identifier == annotation.simpleName
private fun KtAnnotationEntry.firstParameterOrNull() =
valueArguments
.firstOrNull()
?.getArgumentExpression()
?.text
?.withoutQuotes()
internal fun String.withoutQuotes() = removeSurrounding(TRIPLE_QUOTES)
.removeSurrounding(SINGLE_QUOTES)
.replace(STRING_CONCAT_REGEX, "")
private const val SINGLE_QUOTES = "\""
private const val TRIPLE_QUOTES = "\"\"\""
private val STRING_CONCAT_REGEX = """"\s*\+[\n\s]*"""".toRegex()