Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Aug 16, 2024
1 parent b21b7ae commit 94fdb45
Show file tree
Hide file tree
Showing 104 changed files with 473 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotationClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.itest.KotlinCodeGenerationITest.ROOT_PACKAGE

@ExperimentalKotlinPoetApi
object MyCustomAnnotationSpec {
val name = ClassName(ROOT_PACKAGE, "MyCustomAnnotation")

Expand Down
4 changes: 4 additions & 0 deletions _itest/builder-itest/src/test/kotlin/HelloWorldExampleTest.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFun
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test

@ExperimentalKotlinPoetApi
@ExperimentalCompilerApi
internal class HelloWorldExampleTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.asTypeName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildDataClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.dataClassBuilder
import io.toolisticon.kotlin.generation.builder.KotlinDataClassSpecBuilder
import org.junit.jupiter.api.Test

@ExperimentalKotlinPoetApi
internal class KotlinDataClassSpecTest {

@Test
Expand Down
2 changes: 2 additions & 0 deletions _itest/builder-itest/src/test/kotlin/KotlinFileSpecTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.FileSpec
import io.toolisticon.kotlin.generation.builder.KotlinFileSpecBuilder
import org.junit.jupiter.api.Test

@ExperimentalKotlinPoetApi
internal class KotlinFileSpecTest {

@Target(AnnotationTarget.FILE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat as assertThatCompilation


@ExperimentalCompilerApi
@ExperimentalKotlinPoetApi
internal class MyCustomAnnotationSpecTest {

@Test
Expand Down
6 changes: 0 additions & 6 deletions _itest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi</arg>
<arg>-opt-in=com.squareup.kotlinpoet.ExperimentalKotlinPoetApi</arg>
</args>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
9 changes: 4 additions & 5 deletions _itest/spi-itest/src/main/kotlin/TestContext.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.toolisticon.kotlin.generation.itest.spi

import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationContext
import io.toolisticon.kotlin.generation.spi.AbstractKotlinCodeGenerationSpiRegistry
import kotlin.reflect.KClass
import io.toolisticon.kotlin.generation.context.AbstractKotlinCodeGenerationContext
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationSpiRegistry

class TestContext(override val registry: AbstractKotlinCodeGenerationSpiRegistry) : KotlinCodeGenerationContext {
override val contextType: KClass<*> = TestContext::class
class TestContext(registry: KotlinCodeGenerationSpiRegistry) : AbstractKotlinCodeGenerationContext<TestContext>(registry) {
override val contextType = TestContext::class
}
2 changes: 2 additions & 0 deletions _itest/spi-itest/src/main/kotlin/TestDataClassStrategy.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.toolisticon.kotlin.generation.itest.spi

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import io.toolisticon.kotlin.generation.builder.KotlinDataClassSpecBuilder
import io.toolisticon.kotlin.generation.spec.KotlinDataClassSpec
import io.toolisticon.kotlin.generation.spi.strategy.DataClassSpecStrategy

@ExperimentalKotlinPoetApi
class TestDataClassStrategy : DataClassSpecStrategy<TestContext, MapInput>(contextType = TestContext::class, inputType = MapInput::class) {

override fun invoke(context: TestContext, input: MapInput): KotlinDataClassSpec {
Expand Down
13 changes: 9 additions & 4 deletions _itest/spi-itest/src/test/kotlin/SpiITest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package io.toolisticon.kotlin.generation.itest.spi

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.className
import io.toolisticon.kotlin.generation.spec.KotlinDataClassSpec
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test

@ExperimentalCompilerApi
@ExperimentalKotlinPoetApi
internal class SpiITest {

@Test
Expand All @@ -19,20 +23,21 @@ internal class SpiITest {
val input = MapInput(
className = className(packageName = "foo.bar", simpleName = "ExampleDataClass"),
fields = mapOf(
"name" to String::class
"name" to String::class,
"foo" to Long::class
)
)

val strategy = context.registry.getStrategy<TestContext, MapInput, KotlinDataClassSpec>()
val strategy = context.findStrategies(MapInput::class, KotlinDataClassSpec::class).single()

val spec = strategy(context = context, input = input)
val file = KotlinCodeGeneration.builder.fileBuilder(input.className).addType(spec).build()

val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(file))
KotlinCodeGenerationTest.assertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)

val foo = result.loadClass(input.className).java.getDeclaredConstructor(String::class.java).newInstance("Foo")
val foo = result.loadClass(input.className).java.getDeclaredConstructor(String::class.java, Long::class.java).newInstance("Foo", 5L)

assertThat(foo).hasToString("ExampleDataClass(name=Foo)")
assertThat(foo).hasToString("ExampleDataClass(name=Foo, foo=5)")
}
}
6 changes: 0 additions & 6 deletions kotlin-code-generation-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi</arg>
<arg>-opt-in=com.squareup.kotlinpoet.ExperimentalKotlinPoetApi</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.toolisticon.kotlin.generation.test

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import io.toolisticon.kotlin.generation.spec.KotlinFileSpec
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationResult
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import java.io.ByteArrayOutputStream

/**
Expand All @@ -29,6 +31,8 @@ import java.io.ByteArrayOutputStream
* }.compile()
* ```
*/
@ExperimentalCompilerApi
@ExperimentalKotlinPoetApi
object KotlinCodeGenerationTest {

fun assertThat(actual: KotlinCompilationResult): KotlinCompilationAssert = KotlinCompilationAssert(actual)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package io.toolisticon.kotlin.generation.test

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationResult
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.Assertions
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi


@ExperimentalKotlinPoetApi
@ExperimentalCompilerApi
class KotlinCompilationAssert(
actual: KotlinCompilationResult,
) : AbstractAssert<KotlinCompilationAssert, KotlinCompilationResult>(actual, KotlinCompilationAssert::class.java) {

fun errorMessages() = Assertions.assertThat(actual.errors)
fun errorMessages() = Assertions.assertThat(actual.errors)

fun hasExitCode(exitCode: KotlinCompilation.ExitCode) : KotlinCompilationAssert = apply {
fun hasExitCode(exitCode: KotlinCompilation.ExitCode): KotlinCompilationAssert = apply {
Assertions.assertThat(actual.exitCode).isEqualTo(exitCode)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.toolisticon.kotlin.generation.test.model

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.SourceFile
import io.toolisticon.kotlin.generation.spec.KotlinFileSpec
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.sourceFile
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi

@ExperimentalKotlinPoetApi
@ExperimentalCompilerApi
data class KotlinCompilationCommand(
val fileSpecs: List<KotlinFileSpec>
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package io.toolisticon.kotlin.generation.test.model

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import kotlin.reflect.KClass

@ExperimentalCompilerApi
@ExperimentalKotlinPoetApi
data class KotlinCompilationResult(
val cmd: KotlinCompilationCommand,
val result: JvmCompilationResult
Expand Down
6 changes: 0 additions & 6 deletions kotlin-code-generation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi</arg>
<arg>-opt-in=com.squareup.kotlinpoet.ExperimentalKotlinPoetApi</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import io.toolisticon.kotlin.generation.poet.FormatSpecifier.asCodeBlock
import io.toolisticon.kotlin.generation.spec.*
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationContext
import io.toolisticon.kotlin.generation.spi.AbstractKotlinCodeGenerationSpiRegistry
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationSpiRegistry
import io.toolisticon.kotlin.generation.spi.registry.KotlinCodeGenerationServiceRepository
import io.toolisticon.kotlin.generation.support.SuppressAnnotation.Companion.CLASS_NAME
import io.toolisticon.kotlin.generation.support.SuppressAnnotation.Companion.MEMBER_VISIBILITY_CAN_BE_PRIVATE
import kotlin.reflect.KClass

@ExperimentalKotlinPoetApi
object KotlinCodeGeneration {

inline fun buildAnnotation(type: KClass<*>, block: KotlinAnnotationSpecBuilderReceiver = {}): KotlinAnnotationSpec = buildAnnotation(type.asClassName(), block)
Expand Down Expand Up @@ -48,6 +50,7 @@ object KotlinCodeGeneration {
KotlinFileSpecBuilder.builder(it.className).addType(it).build()
}

@ExperimentalKotlinPoetApi
@Suppress(CLASS_NAME, MEMBER_VISIBILITY_CAN_BE_PRIVATE)
object builder {
fun annotationBuilder(type: ClassName) = KotlinAnnotationSpecBuilder.builder(type)
Expand Down Expand Up @@ -83,9 +86,9 @@ object KotlinCodeGeneration {
val defaultClassLoader: () -> ClassLoader = { Thread.currentThread().contextClassLoader }

fun repository(
contextType: KClass<out KotlinCodeGenerationContext>,
contextType: KClass<*>,
classLoader: ClassLoader = defaultClassLoader()
): AbstractKotlinCodeGenerationSpiRegistry = KotlinCodeGenerationServiceRepository.load(contextType = contextType, classLoader = classLoader)
): KotlinCodeGenerationSpiRegistry = KotlinCodeGenerationServiceRepository.load(contextType = contextType, classLoader = classLoader)
}

@Suppress(CLASS_NAME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotation
Expand All @@ -9,6 +10,7 @@ import io.toolisticon.kotlin.generation.poet.*
import io.toolisticon.kotlin.generation.spec.*
import javax.lang.model.element.Element

@ExperimentalKotlinPoetApi
class KotlinAnnotationClassSpecBuilder internal constructor(
val className: ClassName,
private val delegate: TypeSpecBuilder
Expand Down Expand Up @@ -91,4 +93,5 @@ class KotlinAnnotationClassSpecBuilder internal constructor(

}

@ExperimentalKotlinPoetApi
typealias KotlinAnnotationClassSpecBuilderReceiver = KotlinAnnotationClassSpecBuilder.() -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.toolisticon.kotlin.generation.spec.KotlinAnnotationSpec
import io.toolisticon.kotlin.generation.spec.KotlinAnnotationSpecSupplier
import kotlin.reflect.KClass

@ExperimentalKotlinPoetApi
class KotlinAnnotationSpecBuilder internal constructor(
private val delegate: AnnotationSpecBuilder
) : BuilderSupplier<KotlinAnnotationSpec, AnnotationSpec>,
Expand Down Expand Up @@ -66,4 +67,5 @@ class KotlinAnnotationSpecBuilder internal constructor(
override fun get(): AnnotationSpec = build().get()
}

@ExperimentalKotlinPoetApi
typealias KotlinAnnotationSpecBuilderReceiver = KotlinAnnotationSpecBuilder.() -> Unit
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.poet.*
import io.toolisticon.kotlin.generation.spec.KotlinAnonymousClassSpec
import io.toolisticon.kotlin.generation.spec.KotlinFunSpecSupplier
import io.toolisticon.kotlin.generation.spec.KotlinPropertySpecSupplier
import javax.lang.model.element.Element
import kotlin.reflect.KClass


@ExperimentalKotlinPoetApi
class KotlinAnonymousClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinAnonymousClassSpecBuilder, KotlinAnonymousClassSpec>,
Expand Down Expand Up @@ -67,4 +64,5 @@ class KotlinAnonymousClassSpecBuilder internal constructor(
override fun build(): KotlinAnonymousClassSpec = KotlinAnonymousClassSpec(delegate.build())
}

@ExperimentalKotlinPoetApi
typealias KotlinAnonymousClassSpecBuilderReceiver = KotlinAnonymousClassSpecBuilder.() -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import javax.lang.model.element.Element
import kotlin.reflect.KClass


@ExperimentalKotlinPoetApi
class KotlinClassSpecBuilder internal constructor(
private val className: ClassName,
private val delegate: TypeSpecBuilder
Expand Down Expand Up @@ -158,4 +159,5 @@ class KotlinClassSpecBuilder internal constructor(
//
// }

@ExperimentalKotlinPoetApi
typealias KotlinClassSpecBuilderReceiver = KotlinClassSpecBuilder.() -> Unit
Loading

0 comments on commit 94fdb45

Please sign in to comment.