poet.FunSpecBuilder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-code-generation Show documentation
Show all versions of kotlin-code-generation Show documentation
Wrapping core components for kotlin code generation with kotlin-poet.
package io.toolisticon.kotlin.generation.poet
import com.squareup.kotlinpoet.*
import javax.lang.model.element.Element
import javax.lang.model.element.Modifier
import kotlin.reflect.KClass
/**
* Wraps [FunSpec.Builder] for typesafe access.
*/
@ExperimentalKotlinPoetApi
class FunSpecBuilder(
override val builder: FunSpec.Builder
) : PoetSpecBuilder,
FunSpecSupplier,
PoetAnnotatableBuilder,
PoetContextReceivableBuilder,
PoetDocumentableBuilder,
PoetTaggableBuilder,
PoetOriginatingElementsHolderBuilder {
companion object {
fun FunSpec.Builder.wrap() = FunSpecBuilder(this)
fun builder(name: String): FunSpecBuilder = FunSpec.builder(name).wrap()
fun builder(memberName: MemberName): FunSpecBuilder = FunSpec.builder(memberName).wrap()
fun constructorBuilder(): FunSpecBuilder = FunSpec.constructorBuilder().wrap()
fun getterBuilder(): FunSpecBuilder = FunSpec.getterBuilder().wrap()
fun setterBuilder(): FunSpecBuilder = FunSpec.setterBuilder().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) }
// FunSpecBuilder
fun addModifiers(vararg modifiers: KModifier): FunSpecBuilder = apply { builder.addModifiers(*modifiers) }
fun addModifiers(modifiers: Iterable): FunSpecBuilder = apply { builder.addModifiers(modifiers) }
fun jvmModifiers(modifiers: Iterable) = builder.jvmModifiers(modifiers)
fun addTypeVariables(typeVariables: Iterable): FunSpecBuilder = apply { builder.addTypeVariables(typeVariables) }
fun addTypeVariable(typeVariable: TypeVariableName): FunSpecBuilder = apply { builder.addTypeVariable(typeVariable) }
fun receiver(receiverType: TypeName): FunSpecBuilder = apply { builder.receiver(receiverType) }
fun receiver(receiverType: TypeName, kdoc: CodeBlock): FunSpecBuilder = apply { builder.receiver(receiverType, kdoc) }
fun receiver(receiverType: KClass<*>): FunSpecBuilder = apply { builder.receiver(receiverType) }
fun receiver(receiverType: KClass<*>, kdoc: CodeBlock): FunSpecBuilder = apply { builder.receiver(receiverType, kdoc) }
fun receiver(receiverType: KClass<*>, kdoc: String, vararg args: Any): FunSpecBuilder = apply { builder.receiver(receiverType, kdoc, *args) }
fun returns(returnType: TypeName): FunSpecBuilder = apply { builder.returns(returnType) }
fun returns(returnType: TypeName, kdoc: CodeBlock): FunSpecBuilder = apply { builder.returns(returnType, kdoc) }
fun returns(returnType: KClass<*>): FunSpecBuilder = apply { builder.returns(returnType) }
fun returns(returnType: KClass<*>, kdoc: CodeBlock): FunSpecBuilder = apply { builder.returns(returnType, kdoc) }
fun returns(returnType: KClass<*>, kdoc: String, vararg args: Any): FunSpecBuilder = apply { builder.returns(returnType, kdoc, *args) }
fun addParameters(parameterSpecs: Iterable): FunSpecBuilder = apply { builder.addParameters(parameterSpecs) }
fun addParameter(parameterSpec: ParameterSpec): FunSpecBuilder = apply { builder.addParameter(parameterSpec) }
fun callThisConstructor(args: List): FunSpecBuilder = apply { builder.callThisConstructor(args) }
fun callThisConstructor(args: Iterable): FunSpecBuilder = apply { builder.callThisConstructor(args) }
fun callThisConstructor(vararg args: String): FunSpecBuilder = apply { builder.callThisConstructor(*args) }
fun callThisConstructor(vararg args: CodeBlock = emptyArray()): FunSpecBuilder = apply { builder.callThisConstructor(*args) }
fun callSuperConstructor(args: Iterable): FunSpecBuilder = apply { builder.callSuperConstructor(args) }
fun callSuperConstructor(args: List): FunSpecBuilder = apply { builder.callSuperConstructor(args) }
fun callSuperConstructor(vararg args: String): FunSpecBuilder = apply { builder.callSuperConstructor(*args) }
fun callSuperConstructor(vararg args: CodeBlock = emptyArray()): FunSpecBuilder = apply { builder.callSuperConstructor(*args) }
fun addParameter(name: String, type: TypeName, vararg modifiers: KModifier): FunSpecBuilder = apply { builder.addParameter(name, type, *modifiers) }
fun addParameter(name: String, type: KClass<*>, vararg modifiers: KModifier): FunSpecBuilder = apply { builder.addParameter(name, type, *modifiers) }
fun addParameter(name: String, type: TypeName, modifiers: Iterable): FunSpecBuilder = apply { builder.addParameter(name, type, modifiers) }
fun addParameter(name: String, type: KClass<*>, modifiers: Iterable): FunSpecBuilder = apply { builder.addParameter(name, type, modifiers) }
fun addCode(format: String, vararg args: Any?): FunSpecBuilder = apply { builder.addCode(format, *args) }
fun addNamedCode(format: String, args: Map): FunSpecBuilder = apply { builder.addNamedCode(format, args) }
fun addCode(codeBlock: CodeBlock): FunSpecBuilder = apply { builder.addCode(codeBlock) }
fun addComment(format: String, vararg args: Any): FunSpecBuilder = apply { builder.addComment(format, *args) }
fun beginControlFlow(controlFlow: String, vararg args: Any): FunSpecBuilder = apply { builder.beginControlFlow(controlFlow, *args) }
fun nextControlFlow(controlFlow: String, vararg args: Any): FunSpecBuilder = apply { builder.nextControlFlow(controlFlow, *args) }
fun endControlFlow(): FunSpecBuilder = apply { builder.endControlFlow() }
fun addStatement(format: String, vararg args: Any): FunSpecBuilder = apply { builder.addStatement(format, *args) }
fun clearBody(): FunSpecBuilder = apply { builder.clearBody() }
override fun tag(type: KClass<*>, tag: Any?): FunSpecBuilder = apply { builder.tag(type, tag) }
override fun build(): FunSpec = builder.build()
}
interface FunSpecSupplier : PoetSpecSupplier
typealias FunSpecBuilderReceiver = FunSpec.Builder.() -> Unit
© 2015 - 2024 Weber Informatics LLC | Privacy Policy