-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added generation of builders that will be used for shallow copying in…
… persist method TODO: Generate shallow copy extension function
- Loading branch information
Showing
6 changed files
with
299 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...lin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentBuilderCodegen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.beeproduced.bee.persistent.blaze.processor.codegen | ||
|
||
import com.beeproduced.bee.generative.util.PoetMap | ||
import com.beeproduced.bee.generative.util.PoetMap.Companion.addNStatementBuilder | ||
import com.beeproduced.bee.generative.util.toPoetClassName | ||
import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentBuilderCodegen.PoetConstants.CLAZZ | ||
import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentBuilderCodegen.PoetConstants.FIELD | ||
import com.beeproduced.bee.persistent.blaze.processor.info.EntityInfo | ||
import com.beeproduced.bee.persistent.blaze.processor.info.EntityProperty | ||
import com.beeproduced.bee.persistent.blaze.processor.info.RepoInfo | ||
import com.beeproduced.bee.persistent.blaze.processor.utils.* | ||
import com.google.devtools.ksp.processing.CodeGenerator | ||
import com.google.devtools.ksp.processing.Dependencies | ||
import com.google.devtools.ksp.processing.KSPLogger | ||
import com.squareup.kotlinpoet.* | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
import com.squareup.kotlinpoet.ksp.toTypeName | ||
import com.squareup.kotlinpoet.ksp.writeTo | ||
|
||
/** | ||
* | ||
* | ||
* @author Kacper Urbaniec | ||
* @version 2024-01-13 | ||
*/ | ||
typealias FullyQualifiedName = String | ||
|
||
class BeePersistentBuilderCodegen( | ||
private val codeGenerator: CodeGenerator, | ||
private val dependencies: Dependencies, | ||
private val logger: KSPLogger, | ||
private val views: ViewInfo, | ||
private val entities: Map<FullyQualifiedName, EntityInfo>, | ||
private val config: BeePersistentBlazeConfig | ||
) { | ||
private val packageName = config.builderPackageName | ||
|
||
private val poetMap: PoetMap = PoetMap() | ||
private fun FunSpec.Builder.addNamedStmt(format: String) | ||
= addNStatementBuilder(format, poetMap) | ||
private fun CodeBlock.Builder.addNamedStmt(format: String) | ||
= addNStatementBuilder(format, poetMap) | ||
|
||
object PoetConstants { | ||
const val FIELD = "%field:T" | ||
const val CLAZZ = "%clazz:T" | ||
} | ||
|
||
init { | ||
poetMap.addMapping(FIELD, ClassName("java.lang.reflect", "Field")) | ||
} | ||
|
||
fun processRepoBuilder(repos: List<RepoInfo>) { | ||
for (repo in repos) | ||
processRepoBuilder(repo) | ||
} | ||
|
||
private fun processRepoBuilder(repo: RepoInfo) { | ||
logger.info("processRepoBuilder($repo)") | ||
val view = views.findViewFromRepo(repo) | ||
val entity = view.entity | ||
|
||
|
||
val className = "${entity.uniqueName}Builder" | ||
|
||
val file = FileSpec.builder(packageName, className) | ||
if (entity.subClasses == null) file.buildBuilderModule(entity) | ||
else { | ||
val subEntities = entity.subClasses.map { entities.getValue(it) } | ||
for (subEntity in subEntities) file.buildBuilderModule(subEntity) | ||
} | ||
|
||
file | ||
.build() | ||
.writeTo(codeGenerator, dependencies) | ||
} | ||
|
||
private fun FileSpec.Builder.buildBuilderModule(entity: EntityInfo) = apply { | ||
poetMap.addMapping(CLAZZ, entity.qualifiedName.toPoetClassName()) | ||
buildInfo(entity) // Generate reflection info | ||
buildBuilder(entity) // Generate builder for cloning | ||
// Gerate clone extension function | ||
} | ||
|
||
private fun FileSpec.Builder.buildBuilder(entity: EntityInfo) = apply { | ||
val entityBuilder = TypeSpec.classBuilder(entity.builderName()) | ||
|
||
val instance = "instance" | ||
val constructor = FunSpec.constructorBuilder() | ||
.addParameter(instance, poetMap.classMapping(CLAZZ)) | ||
|
||
entityBuilder.primaryConstructor(constructor.build()) | ||
|
||
val accessInfo = entity.accessInfo(false) | ||
|
||
for (prop in accessInfo.props) { | ||
val propType = prop.type.toTypeName() | ||
val builderProperty = PropertySpec.builder(prop.simpleName, propType) | ||
.initializer("$instance.${prop.simpleName}") | ||
.mutable(true) | ||
entityBuilder.addProperty(builderProperty.build()) | ||
} | ||
|
||
val infoObjName = entity.infoName() | ||
for (prop in accessInfo.reflectionProps) { | ||
val propType = prop.type.toTypeName() | ||
val getterName = prop.type.reflectionGettterName() | ||
val builderProperty = PropertySpec.builder(prop.simpleName, propType) | ||
.initializer("${infoObjName}.${prop.infoField()}.${getterName}(instance) as %T", propType) | ||
.mutable(true) | ||
entityBuilder.addProperty(builderProperty.build()) | ||
} | ||
|
||
addType(entityBuilder.build()) | ||
} | ||
|
||
private fun FileSpec.Builder.buildInfo(entity: EntityInfo) = apply { | ||
val construction = entity.accessInfo(false) | ||
if (construction.reflectionProps.isEmpty()) return@apply | ||
|
||
val infoObj = TypeSpec.objectBuilder(entity.infoName()) | ||
val entityClassName = entity.declaration.toClassName() | ||
for (property in construction.reflectionProps) { | ||
val reflectionField = PropertySpec | ||
.builder(property.infoField(), poetMap.classMapping(FIELD)) | ||
.initializer( | ||
"%T::class.java.getDeclaredField(\"${property.simpleName}\").apply { isAccessible = true }", | ||
entityClassName | ||
) | ||
infoObj.addProperty(reflectionField.build()) | ||
} | ||
addType(infoObj.build()) | ||
} | ||
|
||
private fun EntityInfo.builderName(): String = "${uniqueName}Builder" | ||
|
||
private fun EntityInfo.infoName(): String = "${uniqueName}BuilderInfo" | ||
|
||
private fun EntityProperty.infoField(): String = "${simpleName}Field" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...aze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/utils/Construction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.beeproduced.bee.persistent.blaze.processor.utils | ||
|
||
import com.beeproduced.bee.persistent.blaze.processor.info.BaseInfo | ||
import com.beeproduced.bee.persistent.blaze.processor.info.EntityProperty | ||
import com.google.devtools.ksp.symbol.KSType | ||
import com.google.devtools.ksp.symbol.Modifier | ||
|
||
/** | ||
* | ||
* | ||
* @author Kacper Urbaniec | ||
* @version 2024-01-13 | ||
*/ | ||
|
||
|
||
data class AccessInfo( | ||
val props: List<EntityProperty>, | ||
val reflectionProps: List<EntityProperty> | ||
) | ||
|
||
fun BaseInfo.accessInfo(jpaPropsOnly: Boolean = true): AccessInfo { | ||
val propsToUse = if (jpaPropsOnly) jpaProperties else properties | ||
|
||
val props = mutableListOf<EntityProperty>() | ||
val reflectionProps = mutableListOf<EntityProperty>() | ||
|
||
for (prop in propsToUse) { | ||
val modifiers = prop.declaration.modifiers | ||
if ( | ||
modifiers.contains(Modifier.PRIVATE) || | ||
modifiers.contains(Modifier.PROTECTED) || | ||
modifiers.contains(Modifier.INTERNAL) | ||
) { | ||
reflectionProps.add(prop) | ||
} else | ||
props.add(prop) | ||
} | ||
|
||
return AccessInfo(props, reflectionProps) | ||
} | ||
|
||
|
||
data class ConstructionInfo( | ||
val constructorProps: List<EntityProperty>, | ||
val setterProps: List<EntityProperty>, | ||
val reflectionProps: List<EntityProperty> | ||
) | ||
|
||
fun BaseInfo.constructionInfo(jpaPropsOnly: Boolean = true): ConstructionInfo { | ||
val constructor = declaration.primaryConstructor | ||
?: throw IllegalArgumentException("Class [${qualifiedName}] has no primary constructor.") | ||
|
||
val propsToUse = if (jpaPropsOnly) jpaProperties else properties | ||
val props = propsToUse | ||
.associateByTo(HashMap(propsToUse.count())) { | ||
it.simpleName | ||
} | ||
|
||
val constructorProperties = constructor.parameters.map { parameter -> | ||
val pName = requireNotNull(parameter.name).asString() | ||
if (!parameter.isVal && !parameter.isVar) { | ||
throw IllegalArgumentException("Class [${qualifiedName}] has non managed constructor parameter type [$pName]") | ||
} | ||
val jpaProp = props.remove(pName) | ||
?: throw IllegalArgumentException("Class [${qualifiedName}] has non managed constructor parameter type [$pName]") | ||
|
||
jpaProp | ||
} | ||
|
||
val setterProperties = mutableListOf<EntityProperty>() | ||
val reflectionProperties = mutableListOf<EntityProperty>() | ||
for (jpaProp in props.values) { | ||
val modifiers = jpaProp.declaration.modifiers | ||
if ( | ||
modifiers.contains(Modifier.PRIVATE) || | ||
modifiers.contains(Modifier.PROTECTED) || | ||
modifiers.contains(Modifier.INTERNAL) | ||
) { | ||
reflectionProperties.add(jpaProp) | ||
} else if (!jpaProp.declaration.isMutable) { | ||
reflectionProperties.add(jpaProp) | ||
} else setterProperties.add(jpaProp) | ||
} | ||
|
||
return ConstructionInfo( | ||
constructorProperties, | ||
setterProperties, | ||
reflectionProperties | ||
) | ||
} | ||
|
||
fun KSType.reflectionSetterName(): String { | ||
val rpType = declaration.simpleName.asString() | ||
return when (rpType) { | ||
"Double" -> "setDouble" | ||
"Float" -> "setFloat" | ||
"Long" -> "setLong" | ||
"Int" -> "setInt" | ||
"Short" -> "setShort" | ||
"Byte" -> "setByte" | ||
"Boolean" -> "setBoolean" | ||
"Char" -> "setChar" | ||
else -> "set" | ||
} | ||
} | ||
|
||
fun KSType.reflectionGettterName(): String { | ||
val rpType = declaration.simpleName.asString() | ||
return when (rpType) { | ||
"Double" -> "getDouble" | ||
"Float" -> "getFloat" | ||
"Long" -> "getLong" | ||
"Int" -> "getInt" | ||
"Short" -> "getShort" | ||
"Byte" -> "getByte" | ||
"Boolean" -> "getBoolean" | ||
"Char" -> "getChar" | ||
else -> "get" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...t/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/utils/View.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.beeproduced.bee.persistent.blaze.processor.utils | ||
|
||
import com.beeproduced.bee.persistent.blaze.processor.codegen.EntityViewInfo | ||
import com.beeproduced.bee.persistent.blaze.processor.codegen.ViewInfo | ||
import com.beeproduced.bee.persistent.blaze.processor.info.RepoInfo | ||
|
||
/** | ||
* | ||
* | ||
* @author Kacper Urbaniec | ||
* @version 2024-01-13 | ||
*/ | ||
|
||
fun ViewInfo.findViewFromRepo(repo: RepoInfo) : EntityViewInfo { | ||
val qualifiedName = repo.entityType.declaration.qualifiedName?.asString() | ||
return coreEntityViewsByQualifiedName[qualifiedName] | ||
?: throw IllegalArgumentException("No view for [$qualifiedName] found") | ||
} |