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

poet.AnnotationSpecBuilder.kt Maven / Gradle / Ivy

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

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget
import kotlin.reflect.KClass

/**
 * Wraps [AnnotationSpec.Builder] for typesafe access.
 */
class AnnotationSpecBuilder(
  override val builder: AnnotationSpec.Builder
) : PoetSpecBuilder,
  AnnotationSpecSupplier,
  PoetTaggableBuilder {
  companion object {
    internal fun AnnotationSpec.Builder.wrap() = AnnotationSpecBuilder(this)

    /**
     * Creates new builder.
     */
    fun builder(type: ClassName): AnnotationSpecBuilder = AnnotationSpec.builder(type).wrap()

    /**
     * Creates new builder.
     */
    fun builder(type: ParameterizedTypeName): AnnotationSpecBuilder = AnnotationSpec.builder(type).wrap()

    /**
     * Creates new builder.
     */
    fun builder(type: KClass): AnnotationSpecBuilder = builder(type.asClassName())
  }

  // Taggable
  override fun tag(type: KClass<*>, tag: Any?) = apply { builder.tag(type, tag) }

  // Annotatable
  fun addMember(format: String, vararg args: Any) = apply { builder.addMember(CodeBlock.of(format, *args)) }
  fun addMember(codeBlock: CodeBlock) = apply { builder.addMember(codeBlock) }
  fun useSiteTarget(useSiteTarget: UseSiteTarget?) = apply { builder.useSiteTarget(useSiteTarget) }

  /**
   * Remove all registered members.
   */
  fun clearMembers() = apply {
    builder.members.clear()
  }

  override fun build(): AnnotationSpec = builder.build()
}

interface AnnotationSpecSupplier : PoetSpecSupplier
typealias AnnotationSpecBuilderReceiver = AnnotationSpec.Builder.() -> Unit




© 2015 - 2024 Weber Informatics LLC | Privacy Policy