All Downloads are FREE. Search and download functionalities are using the official Maven repository.

support.GeneratedAnnotation.kt Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
package io.toolisticon.kotlin.generation.support

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.asTypeName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotation
import io.toolisticon.kotlin.generation.spec.KotlinAnnotationSpec
import io.toolisticon.kotlin.generation.spec.KotlinAnnotationSpecSupplier
import jakarta.annotation.Generated
import java.time.Instant
import kotlin.reflect.KClass

/**
 * Builder to create a `@Generated` annotation.
 */
@ExperimentalKotlinPoetApi
@Suppress(SUPPRESS_UNUSED)
data class GeneratedAnnotation(
  val value: String = KotlinCodeGeneration::class.asTypeName().toString(),
  val date: Instant = Instant.now(),
  val comments: List = emptyList()
) : KotlinAnnotationSpecSupplier {

  fun generator(type: KClass<*>) = copy(value = type.asTypeName().toString())
  fun date(instant: Instant) = copy(date = instant)
  fun comment(comment: Pair) = copy(comments = this.comments + "${comment.first} = ${comment.second}")

  override fun spec(): KotlinAnnotationSpec = buildAnnotation(Generated::class) {
    addStringMembers("value", value)
    addStringMember("date", date.toString())
    if (comments.isNotEmpty()) {
      addStringMember("comments", comments.joinToString(separator = "; "))
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy