|
| 1 | +package wu.seal.jsontokotlin.model.classscodestruct |
| 2 | + |
| 3 | +import wu.seal.jsontokotlin.interceptor.IKotlinClassInterceptor |
| 4 | +import wu.seal.jsontokotlin.utils.LogUtil |
| 5 | +import wu.seal.jsontokotlin.utils.getCommentCode |
| 6 | +import wu.seal.jsontokotlin.utils.getIndent |
| 7 | +import java.lang.IllegalStateException |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by Seal.Wu on 2020-03-05 |
| 11 | + * Normal general class code struct |
| 12 | + * diff with data class is that there no 'data' modifier |
| 13 | + */ |
| 14 | +data class NormalClass( |
| 15 | + val annotations: List<Annotation> = listOf(), |
| 16 | + override val name: String, |
| 17 | + val properties: List<Property> = listOf(), |
| 18 | + val parentClassTemplate: String = "", |
| 19 | + override val modifiable: Boolean = true |
| 20 | +) : ModifiableKotlinClass, NoGenericKotlinClass { |
| 21 | + |
| 22 | + override val hasGeneric: Boolean = false |
| 23 | + |
| 24 | + override val referencedClasses: List<KotlinClass> |
| 25 | + get() { |
| 26 | + return properties.flatMap { property -> |
| 27 | + mutableListOf(property.typeObject).apply { |
| 28 | + addAll(property.typeObject.getAllGenericsRecursively()) |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + override fun rename(newName: String): KotlinClass = copy(name = newName) |
| 34 | + |
| 35 | + override fun replaceReferencedClasses(replaceRule: Map<KotlinClass, KotlinClass>): KotlinClass { |
| 36 | + val propertiesReferencedModifiableKotlinClass = properties.flatMap { |
| 37 | + if (it.typeObject is GenericKotlinClass) { |
| 38 | + it.typeObject.getAllGenericsRecursively().toMutableList().also { list -> list.add(it.typeObject) } |
| 39 | + } else { |
| 40 | + listOf(it.typeObject) |
| 41 | + } |
| 42 | + }.filter { it.modifiable } |
| 43 | + if (propertiesReferencedModifiableKotlinClass.size != replaceRule.size) { |
| 44 | + throw IllegalStateException("properties used kotlin classes size should be equal referenced classes size!") |
| 45 | + } |
| 46 | + if (!replaceRule.all { it.key.modifiable }) { |
| 47 | + throw IllegalStateException("to be replaced referenced class should be modifiable!") |
| 48 | + } |
| 49 | + val newProperties = properties.map { property -> |
| 50 | + property.typeObject.let { |
| 51 | + val newTypObj = when (it) { |
| 52 | + is GenericKotlinClass -> property.typeObject.replaceReferencedClasses(replaceRule) |
| 53 | + is ModifiableKotlinClass -> replaceRule[property.typeObject] ?: error("Modifiable Kotlin Class Must have a replacement") |
| 54 | + else -> it |
| 55 | + } |
| 56 | + LogUtil.i("replace type: ${property.type} to ${newTypObj.name}") |
| 57 | + return@let property.copy(type = newTypObj.name, typeObject = newTypObj) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return copy(properties = newProperties) |
| 62 | + } |
| 63 | + |
| 64 | + override fun getCode(): String { |
| 65 | + val indent = getIndent() |
| 66 | + val code = buildString { |
| 67 | + if (annotations.isNotEmpty()) { |
| 68 | + val annotationsCode = annotations.joinToString("\n") { it.getAnnotationString() } |
| 69 | + if (annotationsCode.isNotBlank()) { |
| 70 | + append(annotationsCode).append("\n") |
| 71 | + } |
| 72 | + } |
| 73 | + if (properties.isEmpty()) { |
| 74 | + append("class ").append(name).append("(").append("\n") |
| 75 | + } else { |
| 76 | + append("class ").append(name).append("(").append("\n") |
| 77 | + } |
| 78 | + properties.forEachIndexed { index, property -> |
| 79 | + val code = property.getCode() |
| 80 | + val addIndentCode = code.split("\n").joinToString("\n") { indent + it } |
| 81 | + append(addIndentCode) |
| 82 | + if (index != properties.size - 1) append(",") |
| 83 | + if (property.comment.isNotBlank()) append(" // ").append(getCommentCode(property.comment)) |
| 84 | + append("\n") |
| 85 | + } |
| 86 | + append(")") |
| 87 | + if (parentClassTemplate.isNotBlank()) { |
| 88 | + append(" : ") |
| 89 | + append(parentClassTemplate) |
| 90 | + } |
| 91 | + val nestedClasses = referencedClasses.filter { it.modifiable } |
| 92 | + if (nestedClasses.isNotEmpty()) { |
| 93 | + append(" {") |
| 94 | + append("\n") |
| 95 | + val nestedClassesCode = nestedClasses.joinToString("\n\n") { it.getCode() } |
| 96 | + append(nestedClassesCode.lines().joinToString("\n") { if (it.isNotBlank()) "$indent$it" else it }) |
| 97 | + append("\n") |
| 98 | + append("}") |
| 99 | + } |
| 100 | + } |
| 101 | + return code |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + override fun <T : KotlinClass> applyInterceptors(enabledKotlinClassInterceptors: List<IKotlinClassInterceptor<T>>): KotlinClass { |
| 106 | + val newProperties = mutableListOf<Property>() |
| 107 | + properties.forEach { |
| 108 | + newProperties.add(it.copy(typeObject = it.typeObject.applyInterceptors(enabledKotlinClassInterceptors))) |
| 109 | + } |
| 110 | + var newKotlinDataClass: KotlinClass = copy(properties = newProperties) |
| 111 | + enabledKotlinClassInterceptors.forEach { |
| 112 | + newKotlinDataClass = it.intercept(newKotlinDataClass) |
| 113 | + } |
| 114 | + return newKotlinDataClass |
| 115 | + } |
| 116 | + |
| 117 | + override fun getOnlyCurrentCode(): String { |
| 118 | + val newProperties = properties.map { it.copy(typeObject = KotlinClass.ANY) } |
| 119 | + return copy(properties = newProperties).getCode() |
| 120 | + } |
| 121 | +} |
0 commit comments