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

poet.PropertySpecBuilder.kt Maven / Gradle / Ivy

package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import java.lang.reflect.Type
import javax.lang.model.element.Element
import kotlin.reflect.KClass

/**
 * Wraps [PropertySpec.Builder] for typesafe access.
 */
@ExperimentalKotlinPoetApi
class PropertySpecBuilder(
  override val builder: PropertySpec.Builder
) : PoetSpecBuilder,
  PropertySpecSupplier,
  PoetAnnotatableBuilder,
  PoetContextReceivableBuilder,
  PoetTaggableBuilder,
  PoetDocumentableBuilder,
  PoetOriginatingElementsHolderBuilder {
  companion object {
    fun PropertySpec.Builder.wrap() = PropertySpecBuilder(builder = this)

    fun builder(name: String, type: TypeName, vararg modifiers: KModifier, ): PropertySpecBuilder = PropertySpec.builder(name, type, *modifiers).wrap()

    fun builder(name: String, type: Type, vararg modifiers: KModifier): PropertySpecBuilder = builder(
      name = name,
      type = type.asTypeName(),
      modifiers = modifiers
    )

    fun builder(name: String, type: KClass<*>, vararg modifiers: KModifier): PropertySpecBuilder = builder(name, type.asTypeName(), *modifiers)

    fun builder(name: String, type: TypeName, modifiers: Iterable): PropertySpecBuilder = PropertySpec.builder(name, type, modifiers).wrap()

    fun builder(name: String, type: KClass<*>, modifiers: Iterable): PropertySpecBuilder = PropertySpec.builder(name, type.asTypeName(), modifiers).wrap()
  }


  // AnnotatableBuilder
  override fun addAnnotation(annotationSpec: AnnotationSpec) = apply { builder.addAnnotation(annotationSpec) }
  override fun addAnnotations(annotationSpecs: Iterable) = apply { builder.addAnnotations(annotationSpecs) }

  // ContextReceiverBuilder
  override fun contextReceivers(receiverTypes: Iterable) = apply { builder.contextReceivers(receiverTypes) }

  override fun contextReceivers(vararg receiverTypes: TypeName) = apply { builder.contextReceivers(*receiverTypes) }

  // DocumentableBuilder
  override fun addKdoc(format: String, vararg args: Any) = apply { builder.addKdoc(format, *args) }
  override fun addKdoc(block: CodeBlock) = apply { builder.addKdoc(block) }

  // OriginatingElementBuilder
  override fun addOriginatingElement(originatingElement: Element) = apply { builder.addOriginatingElement(originatingElement) }

  fun mutable(mutable: Boolean = true): PropertySpecBuilder = apply { builder.mutable(mutable) }
  fun addModifiers(vararg modifiers: KModifier): PropertySpecBuilder = apply { builder.addModifiers(*modifiers) }
  fun addModifiers(modifiers: Iterable): PropertySpecBuilder = apply { builder.addModifiers(modifiers) }
  fun addTypeVariables(typeVariables: Iterable): PropertySpecBuilder = apply { builder.addTypeVariables(typeVariables) }
  fun addTypeVariable(typeVariable: TypeVariableName): PropertySpecBuilder = apply { builder.addTypeVariable(typeVariable) }
  fun initializer(format: String, vararg args: Any?): PropertySpecBuilder = apply { builder.initializer(format, *args) }
  fun initializer(codeBlock: CodeBlock?): PropertySpecBuilder = apply { builder.initializer(codeBlock) }
  fun delegate(format: String, vararg args: Any?): PropertySpecBuilder = apply { builder.delegate(format, *args) }
  fun delegate(codeBlock: CodeBlock): PropertySpecBuilder = apply { builder.delegate(codeBlock) }
  fun getter(getter: FunSpec?): PropertySpecBuilder = apply { builder.getter(getter) }
  fun setter(setter: FunSpec?): PropertySpecBuilder = apply { builder.setter(setter) }
  fun receiver(receiverType: TypeName?): PropertySpecBuilder = apply { builder.receiver(receiverType) }
  fun receiver(receiverType: KClass<*>): PropertySpecBuilder = apply { builder.receiver(receiverType) }

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

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

interface PropertySpecSupplier : PoetSpecSupplier
typealias PropertySpecBuilderReceiver = PropertySpec.Builder.() -> Unit




© 2015 - 2025 Weber Informatics LLC | Privacy Policy