From 46644f918a19d304dc8513b53eb53f771c5b4e0f Mon Sep 17 00:00:00 2001 From: Thijs Koppen Date: Fri, 25 Oct 2024 13:06:42 +0200 Subject: [PATCH] Create columnTypeClassName extension property --- src/main/kotlin/ch/icken/processor/ColumnType.kt | 6 +++++- src/main/kotlin/ch/icken/processor/Extensions.kt | 8 +++++++- .../ch/icken/processor/PanacheEntityBaseProcessor.kt | 10 ++-------- .../processor/PanacheEntityBaseProcessorMockTests.kt | 3 +-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/ch/icken/processor/ColumnType.kt b/src/main/kotlin/ch/icken/processor/ColumnType.kt index fafe908..6542c10 100644 --- a/src/main/kotlin/ch/icken/processor/ColumnType.kt +++ b/src/main/kotlin/ch/icken/processor/ColumnType.kt @@ -26,4 +26,8 @@ import kotlin.reflect.KClass @Target(PROPERTY) annotation class ColumnType( val type: KClass<*> -) +) { + companion object { + internal const val TYPE = "type" + } +} diff --git a/src/main/kotlin/ch/icken/processor/Extensions.kt b/src/main/kotlin/ch/icken/processor/Extensions.kt index 6353a22..8ee02f7 100644 --- a/src/main/kotlin/ch/icken/processor/Extensions.kt +++ b/src/main/kotlin/ch/icken/processor/Extensions.kt @@ -16,10 +16,13 @@ package ch.icken.processor +import ch.icken.processor.QualifiedNames.ProcessorColumnType import com.google.devtools.ksp.getAllSuperTypes import com.google.devtools.ksp.symbol.* import com.squareup.kotlinpoet.Annotatable import com.squareup.kotlinpoet.AnnotationSpec +import com.squareup.kotlinpoet.ClassName +import com.squareup.kotlinpoet.ksp.toClassName internal fun KSDeclaration.isClass(qualifiedClassName: String): Boolean = qualifiedName?.asString() == qualifiedClassName @@ -36,7 +39,7 @@ internal fun KSAnnotated.hasAnnotation(qualifiedAnnotationClassName: String): Bo internal fun KSAnnotated.annotation(qualifiedAnnotationClassName: String): KSAnnotation? = annotations.filter { it.isClass(qualifiedAnnotationClassName) }.singleOrNull() -internal operator fun List.get(name: String): Any? = find { it.name?.asString() == name }?.value +private operator fun List.get(name: String): Any? = find { it.name?.asString() == name }?.value internal fun KSAnnotation?.hasNonDefaultParameter(parameterName: String): Boolean = this != null && defaultArguments[parameterName] != arguments[parameterName] @@ -44,5 +47,8 @@ internal fun KSAnnotation?.hasNonDefaultParameter(parameterName: String): Boolea internal val KSPropertyDeclaration.typeName: String get() = type.resolve().declaration.simpleName.asString() +internal val KSPropertyDeclaration.columnTypeClassName: ClassName? + get() = (annotation(ProcessorColumnType)?.arguments?.get(ColumnType.TYPE) as? KSType)?.toClassName() + fun > T.addAnnotationIf(annotationSpec: AnnotationSpec, condition: Boolean) = apply { if (condition) addAnnotation(annotationSpec) } diff --git a/src/main/kotlin/ch/icken/processor/PanacheEntityBaseProcessor.kt b/src/main/kotlin/ch/icken/processor/PanacheEntityBaseProcessor.kt index e8b5c9e..6cd7dde 100644 --- a/src/main/kotlin/ch/icken/processor/PanacheEntityBaseProcessor.kt +++ b/src/main/kotlin/ch/icken/processor/PanacheEntityBaseProcessor.kt @@ -32,12 +32,10 @@ import ch.icken.processor.QualifiedNames.JakartaPersistenceManyToMany import ch.icken.processor.QualifiedNames.JakartaPersistenceOneToMany import ch.icken.processor.QualifiedNames.JakartaPersistenceOneToOne import ch.icken.processor.QualifiedNames.JakartaPersistenceTransient -import ch.icken.processor.QualifiedNames.ProcessorColumnType import com.google.devtools.ksp.processing.* import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.symbol.KSPropertyDeclaration -import com.google.devtools.ksp.symbol.KSType import com.google.devtools.ksp.validate import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter @@ -105,11 +103,8 @@ class PanacheEntityBaseProcessor( .initializer("%T(%S)", joinBaseClass, "$propertyName.") } else { val ksPropertyType = ksProperty.type.resolve() - val columnTypeParameter = ( - (ksProperty.annotation(ProcessorColumnType) - ?.arguments?.get(TYPE) as? KSType) - ?.toClassName() ?: ksPropertyType.toClassName() - ).copy(nullable = ksPropertyType.isMarkedNullable) + val columnTypeParameter = (ksProperty.columnTypeClassName ?: ksPropertyType.toClassName()) + .copy(nullable = ksPropertyType.isMarkedNullable) PropertySpec.builder(propertyName, ColumnClassName.plusParameter(columnTypeParameter)) .initializer("%T(%P)", ColumnClassName, @@ -137,7 +132,6 @@ class PanacheEntityBaseProcessor( companion object { internal const val MAPPED_BY = "mappedBy" - internal const val TYPE = "type" private val GeneratedAnnotation = generatedAnnotation(PanacheEntityBaseProcessor::class.java) } diff --git a/src/test/kotlin/ch/icken/processor/PanacheEntityBaseProcessorMockTests.kt b/src/test/kotlin/ch/icken/processor/PanacheEntityBaseProcessorMockTests.kt index 45c639b..76e7bc4 100644 --- a/src/test/kotlin/ch/icken/processor/PanacheEntityBaseProcessorMockTests.kt +++ b/src/test/kotlin/ch/icken/processor/PanacheEntityBaseProcessorMockTests.kt @@ -20,7 +20,6 @@ import ch.icken.processor.ClassNames.ListClassName import ch.icken.processor.ClassNames.StringClassName import ch.icken.processor.GenerationOptions.ADD_GENERATED_ANNOTATION import ch.icken.processor.PanacheEntityBaseProcessor.Companion.MAPPED_BY -import ch.icken.processor.PanacheEntityBaseProcessor.Companion.TYPE import ch.icken.processor.QualifiedNames.HibernatePanacheEntityBase import ch.icken.processor.QualifiedNames.JakartaPersistenceEntity import ch.icken.processor.QualifiedNames.JakartaPersistenceJoinColumn @@ -251,7 +250,7 @@ class PanacheEntityBaseProcessorMockTests : ProcessorMockTestCommon() { every { lastNameTypeReference.resolve() } returns lastNameType val lastNameColumnTypeParameterName = mockk() - every { lastNameColumnTypeParameterName.asString() } returns TYPE + every { lastNameColumnTypeParameterName.asString() } returns ColumnType.TYPE val lastNameColumnTypeParameter = mockk() every { lastNameColumnTypeParameter.name } returns lastNameColumnTypeParameterName