Skip to content

Commit

Permalink
Merge branch 'release/0.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Sep 1, 2024
2 parents c0db8ec + 1d353b8 commit 09d4ea3
Show file tree
Hide file tree
Showing 66 changed files with 331 additions and 106 deletions.
2 changes: 1 addition & 1 deletion _build/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-root</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion _build/report-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-parent</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion _itest/builder-itest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-itest-root</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
</parent>

<groupId>io.toolisticon.kotlin.generation.itest</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@file:OptIn(ExperimentalKotlinPoetApi::class, ExperimentalCompilerApi::class, ExperimentalKotlinPoetApi::class)

package io.toolisticon.kotlin.generation.itest

Expand All @@ -16,6 +15,7 @@ import org.junit.jupiter.api.Test
import kotlin.reflect.full.primaryConstructor
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat as compileAssertThat

@OptIn(ExperimentalKotlinPoetApi::class, ExperimentalCompilerApi::class)
internal class KotlinDataClassSpecITest {

@Test
Expand Down
2 changes: 1 addition & 1 deletion _itest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-parent</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<relativePath>../_build/parent/pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion _itest/spi-itest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-itest-root</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
</parent>

<groupId>io.toolisticon.kotlin.generation.itest</groupId>
Expand Down
2 changes: 1 addition & 1 deletion kotlin-code-generation-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-root</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
</parent>

<groupId>io.toolisticon.kotlin.generation</groupId>
Expand Down
2 changes: 1 addition & 1 deletion kotlin-code-generation-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-parent</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<relativePath>../_build/parent/pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ data class KotlinCompilationCommand(

operator fun plus(fileSpec: KotlinFileSpec) = copy(fileSpecs = fileSpecs + fileSpec)


val sourceFiles: List<SourceFile> by lazy { fileSpecs.map { it.sourceFile() } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ data class KotlinCompilationResult(
}
}

/**
* List all generated files
*/
val generatedSources: List<String> by lazy {
result.messages.lines()
.filter { line -> line.endsWith(".kt") }
.distinct().sorted()
.map { "file://$it" }
}

fun loadClass(className: ClassName): KClass<out Any> = result.classLoader.loadClass(className.canonicalName).kotlin

fun shouldBeOk() {
Expand All @@ -39,5 +49,12 @@ data class KotlinCompilationResult(
.isEqualTo(KotlinCompilation.ExitCode.OK)
}

override fun toString() = "${this::class.simpleName}(cmd=$cmd, exitCode=$exitCode, errors=$errors)"
override fun toString() = toString(false)
fun toString(includeCommand: Boolean) = "${this::class.simpleName}(" +
if (includeCommand) {
"cmd=$cmd, "
} else {
""
} +
"exitCode=$exitCode, errors=$errors, generatedSources=$generatedSources)"
}
2 changes: 1 addition & 1 deletion kotlin-code-generation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-parent</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<relativePath>../_build/parent/pom.xml</relativePath>
</parent>

Expand Down
24 changes: 20 additions & 4 deletions kotlin-code-generation/src/main/kotlin/KotlinCodeGeneration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ object KotlinCodeGeneration : KLogging() {
*/
fun annotationBuilder(type: ClassName) = KotlinAnnotationSpecBuilder.builder(type)

/**
* @see KotlinAnnotationSpecBuilder
*/
fun annotationBuilder(type: KClass<out Annotation>) = annotationBuilder(type.asClassName())

/**
* @see KotlinAnnotationSpecBuilder
*/
Expand Down Expand Up @@ -270,6 +275,11 @@ object KotlinCodeGeneration : KLogging() {
*/
fun constructorPropertyBuilder(name: PropertyName, type: TypeName) = KotlinConstructorPropertySpecBuilder.builder(name, type)

/**
* @see KotlinFunSpecBuilder
*/
fun constructorBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder.constructorBuilder()

/**
* @see KotlinDataClassSpecBuilder
*/
Expand Down Expand Up @@ -310,6 +320,11 @@ object KotlinCodeGeneration : KLogging() {
*/
fun funBuilder(name: FunctionName) = KotlinFunSpecBuilder.builder(name)

/**
* @see KotlinFunSpecBuilder
*/
fun getterBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder.getterBuilder()

/**
* @see KotlinInterfaceSpecBuilder
*/
Expand Down Expand Up @@ -350,6 +365,11 @@ object KotlinCodeGeneration : KLogging() {
*/
fun propertyBuilder(name: PropertyName, type: KClass<*>) = propertyBuilder(name, type.asTypeName())

/**
* @see KotlinFunSpecBuilder
*/
fun setterBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder.setterBuilder()

/**
* @see KotlinTypeAliasSpecBuilder
*/
Expand Down Expand Up @@ -419,10 +439,6 @@ object KotlinCodeGeneration : KLogging() {

fun Enum<*>.asMemberName(): MemberName = this::class.asClassName().member(this.name)

// FIXME: remove?
@Deprecated("not usable this way, fix or remove")
operator fun ClassName.plus(suffix: String?): ClassName = ClassName(this.packageName, this.simpleNames)

fun TypeName.nullable(nullable: Boolean = true): TypeName = this.copy(nullable = nullable)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class KotlinAnnotationClassSpecBuilder internal constructor(
val className: ClassName,
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinAnnotationClassSpecBuilder, KotlinAnnotationClassSpec>,
ConstructorPropertySupport<KotlinAnnotationClassSpecBuilder>,
KotlinAnnotatableBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinConstructorPropertySupport<KotlinAnnotationClassSpecBuilder>,
KotlinContextReceivableBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinMemberSpecHolderBuilder<KotlinAnnotationClassSpecBuilder>,
Expand All @@ -42,7 +42,6 @@ class KotlinAnnotationClassSpecBuilder internal constructor(
private var repeatable: Boolean = false
private var mustBeDocumented: Boolean = false

override fun addConstructorProperty(spec: KotlinConstructorPropertySpecSupplier) = apply { constructorProperties[spec.name] = spec }

fun mustBeDocumented() = apply { this.mustBeDocumented = true }
fun repeatable() = apply { this.repeatable = true }
Expand All @@ -53,6 +52,7 @@ class KotlinAnnotationClassSpecBuilder internal constructor(
}

override fun addAnnotation(spec: KotlinAnnotationSpecSupplier): KotlinAnnotationClassSpecBuilder = apply { delegate.addAnnotation(spec.get()) }
override fun addConstructorProperty(spec: KotlinConstructorPropertySpecSupplier) = apply { constructorProperties[spec.name] = spec }
override fun contextReceivers(vararg receiverTypes: TypeName): KotlinAnnotationClassSpecBuilder = builder { this.contextReceivers(*receiverTypes) }
override fun addFunction(funSpec: KotlinFunSpecSupplier): KotlinAnnotationClassSpecBuilder = apply { delegate.addFunction(funSpec.get()) }
override fun addKdoc(kdoc: KDoc): KotlinAnnotationClassSpecBuilder = apply { delegate.addKdoc(kdoc.get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class KotlinAnnotationSpecBuilder internal constructor(
fun addNumberMember(name: String, value: Number): KotlinAnnotationSpecBuilder = addMember(member.number(name, value))
fun addNumberMembers(name: String, vararg values: Number): KotlinAnnotationSpecBuilder = addMember(member.numbers(name, *values))

fun clearMembers() = apply {
delegate.clearMembers();
}

override fun builder(block: AnnotationSpecBuilderReceiver) = apply {
delegate.builder.block()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.simpleClassName
import io.toolisticon.kotlin.generation.builder.KotlinConstructorPropertySpecBuilder.Companion.primaryConstructorWithProperties
import io.toolisticon.kotlin.generation.poet.*
import io.toolisticon.kotlin.generation.spec.*
import io.toolisticon.kotlin.generation.support.SUPPRESS_UNUSED
Expand All @@ -19,6 +20,7 @@ class KotlinClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinClassSpecBuilder, KotlinClassSpec>,
KotlinAnnotatableBuilder<KotlinClassSpecBuilder>,
KotlinConstructorPropertySupport<KotlinClassSpecBuilder>,
KotlinContextReceivableBuilder<KotlinClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinClassSpecBuilder>,
KotlinMemberSpecHolderBuilder<KotlinClassSpecBuilder>,
Expand All @@ -33,7 +35,11 @@ class KotlinClassSpecBuilder internal constructor(

internal constructor(className: ClassName) : this(className, TypeSpecBuilder.classBuilder(className))

private val constructorProperties: LinkedHashMap<String, KotlinConstructorPropertySpecSupplier> = LinkedHashMap()
private var isSetPrimaryConstructor: Boolean = false

override fun addAnnotation(spec: KotlinAnnotationSpecSupplier): KotlinClassSpecBuilder = apply { delegate.addAnnotation(spec.get()) }
override fun addConstructorProperty(spec: KotlinConstructorPropertySpecSupplier) = apply { constructorProperties[spec.name] = spec }
override fun contextReceivers(vararg receiverTypes: TypeName): KotlinClassSpecBuilder = builder { this.contextReceivers(*receiverTypes) }
override fun addFunction(funSpec: KotlinFunSpecSupplier): KotlinClassSpecBuilder = apply { delegate.addFunction(funSpec.get()) }
override fun addKdoc(kdoc: KDoc): KotlinClassSpecBuilder = apply { delegate.addKdoc(kdoc.get()) }
Expand All @@ -43,7 +49,13 @@ class KotlinClassSpecBuilder internal constructor(

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = apply {
if (primaryConstructor != null) {
delegate.primaryConstructor(primaryConstructor.get())
isSetPrimaryConstructor = true
}
}

fun superclass(superclass: TypeName) = builder { this.superclass(superclass) }
fun superclass(superclass: KClass<*>) = builder { this.superclass(superclass) }

Expand All @@ -56,7 +68,16 @@ class KotlinClassSpecBuilder internal constructor(
fun addInitializerBlock(block: CodeBlock) = builder { this.addInitializerBlock(block) }

override fun builder(block: TypeSpecBuilderReceiver) = apply { delegate.builder.block() }
override fun build(): KotlinClassSpec = KotlinClassSpec(className = className, spec = delegate.build())
override fun build(): KotlinClassSpec {
val hasConstructorProperties = constructorProperties.isNotEmpty()
check(!(hasConstructorProperties && isSetPrimaryConstructor)) { "Decide if you want to use the constructorProperty support OR define a custom primary constructor, not both." }

if (hasConstructorProperties) {
delegate.primaryConstructorWithProperties(toList(constructorProperties.values))
}

return KotlinClassSpec(className = className, spec = delegate.build())
}
}

@ExperimentalKotlinPoetApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.toolisticon.kotlin.generation.spec.*
import io.toolisticon.kotlin.generation.support.SUPPRESS_UNUSED
import mu.KLogging
import javax.lang.model.element.Element
import kotlin.reflect.KClass

/**
* Builder for [KotlinDataClassSpec].
Expand All @@ -20,7 +19,7 @@ class KotlinDataClassSpecBuilder internal constructor(
private val className: ClassName,
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinDataClassSpecBuilder, KotlinDataClassSpec>,
ConstructorPropertySupport<KotlinDataClassSpecBuilder>,
KotlinConstructorPropertySupport<KotlinDataClassSpecBuilder>,
KotlinAnnotatableBuilder<KotlinDataClassSpecBuilder>,
KotlinContextReceivableBuilder<KotlinDataClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinDataClassSpecBuilder>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class KotlinInterfaceSpecBuilder internal constructor(

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }

override fun addSuperinterface(superinterface: TypeName, constructorParameter: String) = builder { this.addSuperinterface(superinterface, constructorParameter) }
override fun addSuperinterface(superinterface: TypeName, delegate: CodeBlock) = builder { this.addSuperinterface(superinterface, delegate) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.toolisticon.kotlin.generation.poet.*
import io.toolisticon.kotlin.generation.spec.*
import io.toolisticon.kotlin.generation.support.SUPPRESS_UNUSED
import javax.lang.model.element.Element
import kotlin.reflect.KClass

/**
* Builder for [KotlinValueClassSpec].
Expand All @@ -19,7 +18,7 @@ class KotlinValueClassSpecBuilder internal constructor(
val className: ClassName,
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinValueClassSpecBuilder, KotlinValueClassSpec>,
ConstructorPropertySupport<KotlinValueClassSpecBuilder>,
KotlinConstructorPropertySupport<KotlinValueClassSpecBuilder>,
KotlinAnnotatableBuilder<KotlinValueClassSpecBuilder>,
KotlinContextReceivableBuilder<KotlinValueClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinValueClassSpecBuilder>,
Expand Down Expand Up @@ -51,7 +50,6 @@ class KotlinValueClassSpecBuilder internal constructor(

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }
fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }

override fun addSuperinterface(superinterface: TypeName, constructorParameter: String) = builder { this.addSuperinterface(superinterface, constructorParameter) }
override fun addSuperinterface(superinterface: TypeName, delegate: CodeBlock) = builder { this.addSuperinterface(superinterface, delegate) }
Expand Down
2 changes: 1 addition & 1 deletion kotlin-code-generation/src/main/kotlin/builder/_types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ sealed interface KotlinGeneratorTypeSpecBuilder<SELF, SPEC : KotlinGeneratorType
* All typeSpecs that provide support for constructor properties use this shared code.
*/
@ExperimentalKotlinPoetApi
sealed interface ConstructorPropertySupport<SELF> {
sealed interface KotlinConstructorPropertySupport<SELF> {

/**
* Implementing builder needs to store the spec provided and apply it to the build.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class AnnotationSpecBuilder(
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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ interface KotlinCodeGenerationProcessor<CONTEXT : KotlinCodeGenerationContext<CO
/**
* Input is nullable because we could use processors solely based on context.
*/
operator fun invoke(context: CONTEXT, input: INPUT?, builder: BUILDER): BUILDER
override fun test(context: CONTEXT, input: Any?): Boolean = super.test(context, input)
operator fun invoke(context: CONTEXT, input: INPUT, builder: BUILDER): BUILDER
override fun test(context: CONTEXT, input: Any): Boolean = super.test(context, input)

/**
* Checks if this strategy should be applied (using `test`) and then runs `invoke`.
Expand All @@ -39,7 +39,7 @@ interface KotlinCodeGenerationProcessor<CONTEXT : KotlinCodeGenerationContext<CO
* @param builder the builder to modify
* @return the passed builder instance for fluent usage
*/
fun execute(context: CONTEXT, input: INPUT?, builder: BUILDER): BUILDER = if (test(context, input)) {
fun execute(context: CONTEXT, input: INPUT, builder: BUILDER): BUILDER = if (test(context, input)) {
invoke(context, input, builder)
} else {
builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.toolisticon.kotlin.generation.spi
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import java.util.function.BiPredicate
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf

/**
* Root interface for code generation spi.
Expand All @@ -13,7 +14,7 @@ import kotlin.reflect.KClass
* * [KotlinCodeGenerationProcessor] - visitor pattern to modify spec builders before the spec is build.
*/
@ExperimentalKotlinPoetApi
sealed interface KotlinCodeGenerationSpi<CONTEXT: KotlinCodeGenerationContext<CONTEXT>, INPUT : Any> : Comparable<KotlinCodeGenerationSpi<*, *>>, BiPredicate<CONTEXT, Any?> {
sealed interface KotlinCodeGenerationSpi<CONTEXT : KotlinCodeGenerationContext<CONTEXT>, INPUT : Any> : Comparable<KotlinCodeGenerationSpi<*, *>>, BiPredicate<CONTEXT, Any> {
companion object {
val metaInfServices = "META-INF/services/${KotlinCodeGenerationSpi::class.qualifiedName}"
const val DEFAULT_ORDER = 0
Expand Down Expand Up @@ -55,5 +56,5 @@ sealed interface KotlinCodeGenerationSpi<CONTEXT: KotlinCodeGenerationContext<CO
* @param input the concrete work item, for the check this is unbound and nullable, so we can check against calling with unsupported types.
* @return `true` when the spi shoud be applied.
*/
override fun test(context: CONTEXT, input: Any?): Boolean = input == null || inputType == input::class
override fun test(context: CONTEXT, input: Any): Boolean = context::class.isSubclassOf(contextType) && input::class.isSubclassOf(inputType)
}
Loading

0 comments on commit 09d4ea3

Please sign in to comment.