Skip to content

Commit

Permalink
Move PokoBuilder to standalone annotation class
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhamilton committed Dec 23, 2024
1 parent 82754ac commit a72de0c
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.drewhamilton.poko
import org.jetbrains.kotlin.name.Name

internal object PokoAnnotationNames {
val Builder = Name.identifier("Builder")
val ReadArrayContent = Name.identifier("ReadArrayContent")
val Skip = Name.identifier("Skip")
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public class PokoCompilerPluginRegistrar : CompilerPluginRegistrar() {
IrGenerationExtension.registerExtension(GeneratedDeclarationsIrBodyFiller())

FirExtensionRegistrarAdapter.registerExtension(
PokoFirExtensionRegistrar(pokoAnnotationClassId)
PokoFirExtensionRegistrar(
pokoAnnotation = pokoAnnotationClassId,
// TODO: Make this configurable
pokoBuilderAnnotation = ClassId.fromString("dev/drewhamilton/poko/PokoBuilder"),
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.drewhamilton.poko.builder

import dev.drewhamilton.poko.PokoAnnotationNames
import dev.drewhamilton.poko.fir.constructorProperties
import dev.drewhamilton.poko.fir.pokoFirExtensionSessionComponent
import dev.drewhamilton.poko.unSpecial
Expand Down Expand Up @@ -63,7 +62,7 @@ internal class BuilderFirDeclarationGenerationExtension(
private val pokoBuilderClasses by lazy {
session.predicateBasedProvider.getSymbolsByPredicate(pokoBuilderAnnotationPredicate)
.filterIsInstance<FirRegularClassSymbol>()
.associateBy { it.classId.createNestedClassId(PokoAnnotationNames.Builder) }
.associateBy { it.classId.createNestedClassId(BuilderClassName) }
}

override fun FirDeclarationPredicateRegistrar.registerPredicates() {
Expand All @@ -87,7 +86,7 @@ internal class BuilderFirDeclarationGenerationExtension(
classSymbol: FirClassSymbol<*>,
context: NestedClassGenerationContext,
): Set<Name> = when {
classSymbol in pokoBuilderClasses.values -> setOf(PokoAnnotationNames.Builder)
classSymbol in pokoBuilderClasses.values -> setOf(BuilderClassName)
else -> emptySet()
}

Expand All @@ -97,7 +96,7 @@ internal class BuilderFirDeclarationGenerationExtension(
context: NestedClassGenerationContext,
): FirClassLikeSymbol<*>? {
return when (name) {
PokoAnnotationNames.Builder -> {
BuilderClassName -> {
if (owner !in pokoBuilderClasses.values) return null
createNestedClass(
owner = owner,
Expand Down Expand Up @@ -227,6 +226,10 @@ internal class BuilderFirDeclarationGenerationExtension(
return fir.declarations.constructorProperties()
}

private companion object {
private val BuilderClassName = Name.identifier("Builder")
}

internal object Key : GeneratedDeclarationKey() {
override fun toString() = "Poko BuilderFirDeclarationGenerationExtension.Key"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import org.jetbrains.kotlin.name.ClassId

internal class PokoFirExtensionRegistrar(
private val pokoAnnotation: ClassId,
private val pokoBuilderAnnotation: ClassId,
) : FirExtensionRegistrar() {
override fun ExtensionRegistrarContext.configurePlugin() {
+PokoFirExtensionSessionComponent.getFactory(pokoAnnotation)
+PokoFirExtensionSessionComponent.getFactory(pokoAnnotation, pokoBuilderAnnotation)
+::PokoFirCheckersExtension
+::BuilderFirDeclarationGenerationExtension
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import org.jetbrains.kotlin.name.ClassId
internal class PokoFirExtensionSessionComponent(
session: FirSession,
internal val pokoAnnotation: ClassId,
internal val pokoBuilderAnnotation: ClassId,
) : FirExtensionSessionComponent(session) {

internal val pokoBuilderAnnotation: ClassId =
pokoAnnotation.createNestedClassId(PokoAnnotationNames.Builder)

internal val pokoSkipAnnotation: ClassId =
pokoAnnotation.createNestedClassId(PokoAnnotationNames.Skip)

internal companion object {
internal fun getFactory(pokoAnnotation: ClassId): Factory {
internal fun getFactory(
pokoAnnotation: ClassId,
pokoBuilderAnnotation: ClassId,
): Factory {
return Factory { session ->
PokoFirExtensionSessionComponent(session, pokoAnnotation)
PokoFirExtensionSessionComponent(session, pokoAnnotation, pokoBuilderAnnotation)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ class PokoCompilerPluginTest(
assumeTrue(k2) // FIR only works in K2

testCompilation(
"api/Buildable", "api/MyData",
pokoAnnotationName = "api/MyData",
"api/Buildable",
"dev/drewhamilton/poko/PokoBuilder",
) { result ->
assertThat(result.messages).all {
contains("Buildable.kt:3:1")
contains("Buildable.kt:5:1")
contains("The Poko Builder feature is incomplete, experimental, and private; your generated builder will not work")
}

Expand Down
6 changes: 4 additions & 2 deletions poko-compiler-plugin/src/test/resources/api/Buildable.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

@MyData.Builder
@MyData class Buildable(
import dev.drewhamilton.poko.PokoBuilder

@PokoBuilder
class Buildable(
val id: String,
)
9 changes: 0 additions & 9 deletions poko-compiler-plugin/src/test/resources/api/MyData.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.drewhamilton.poko

/**
* Temporary test class for builder generation.
*/
// TODO: Remove when real annotation is available.
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class PokoBuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.drewhamilton.poko

/**
* For testing experimental builder generation.
*/
// TODO: Replace with custom annotation
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class PokoBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ package dev.drewhamilton.poko.sample.jvm
*/
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class MyData {
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class Builder
}
annotation class MyData
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.drewhamilton.poko.sample.jvm

import dev.drewhamilton.poko.PokoBuilder

@Suppress("unused")
@MyData.Builder
@PokoBuilder
@MyData class Sample(
val int: Int,
val requiredString: String,
Expand Down

0 comments on commit a72de0c

Please sign in to comment.