com.faendir.kotlin.autodsl.parameter.ParameterFactory.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of processor Show documentation
Show all versions of processor Show documentation
Auto-generates DSL for your Kotlin projects using annotations.
package com.faendir.kotlin.autodsl.parameter
import com.shadow.pluralize.singularize
import com.shadow.pluralize.utils.Plurality
import com.faendir.kotlin.autodsl.*
import com.faendir.kotlin.autodsl.parameter.CollectionType.ListType
import com.faendir.kotlin.autodsl.parameter.CollectionType.SetType
import com.squareup.kotlinpoet.asClassName
import kotlin.reflect.KClass
class ParameterFactory(private val resolver: SourceInfoResolver) {
private val set = Set::class.asClassName()
private val list = List::class.asClassName()
private val collection = Collection::class.asClassName()
private val iterable = Iterable::class.asClassName()
fun getParameters(constructor: C): List = resolver.run {
return constructor.getParameters().withIndex().map { (index, parameter) ->
val type = parameter.getTypeName()
val rawType = type.toRawType()
val (hasNestedDsl, collectionType) = when (rawType) {
set -> parameter.hasAnnotatedTypeArgument(AutoDsl::class) to SetType(findSingular(parameter, index))
list, collection, iterable -> parameter.hasAnnotatedTypeArgument(AutoDsl::class) to ListType(findSingular(parameter, index))
else -> (parameter.getTypeDeclaration()?.hasAnnotation(AutoDsl::class) == true) to null
}
Parameter(
type,
parameter.getName(),
parameter.getDoc(),
parameter.hasDefault(),
parameter.getAnnotationProperty(AutoDslRequired::class, AutoDslRequired::group),
index,
hasNestedDsl,
collectionType
)
}
}
private fun P.hasAnnotatedTypeArgument(annotation: KClass): Boolean = resolver.run {
return getTypeArguments().firstOrNull()?.hasAnnotation(annotation) ?: false
}
private fun findSingular(parameter: P, index: Int): String = resolver.run {
when {
parameter.hasAnnotation(AutoDslSingular::class) -> parameter.getAnnotationProperty(AutoDslSingular::class, AutoDslSingular::value)
parameter.getName().length < 3 -> parameter.getName()
else -> parameter.getName().singularize(Plurality.CouldBeEither)
} ?: "var$index"
}
}