Skip to content

Commit

Permalink
enh(entry-gen): Disallow multiple constructors with same argument cou…
Browse files Browse the repository at this point in the history
…nt and simplify constructor registration so we get rid of nullable info need
  • Loading branch information
piiertho committed Jan 12, 2025
1 parent 8a2f8cf commit 816d24b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ class MultiArgsConstructorTest : Node {
@RegisterConstructor
constructor(i: Int, s: String, obj: Object?, variantArray: VariantArray<Any?>?, navMesh: NavigationMesh) : this()

constructor(
iShouldNOTFailAsImNotRegistered: String,
s: Int,
obj: Object?,
variantArray: VariantArray<Any?>?,
navMesh: NavigationMesh
) : this()

constructor(
i: Int,
s: String,
Expand All @@ -66,4 +58,11 @@ class MultiArgsConstructorTest : Node {
// constructor(iShouldFailAsOverloadingIsNotSupported: String, s: String, obj: Object?, variantArray: VariantArray<Any?>?, navMesh: NavigationMesh) : this()
// @RegisterConstructor
// constructor(i: Int, s: Int, obj: Object?, variantArray: VariantArray<Any?>?, navMesh: NavigationMesh, tooManyArgs: String) : this()
// constructor(
// iShouldFailAsImDuplicate5param: String,
// s: Int,
// obj: Object?,
// variantArray: VariantArray<Any?>?,
// navMesh: NavigationMesh
// ) : this()
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ fun ClassInfo.mapToClazz(settings: Settings): Clazz {
}
.map { it.mapFieldToRegisteredSignal(settings) }

val duplicateConstructorArgumentCount = constructorInfo
.groupBy { it.parameterInfo.size }
.toList()
.firstOrNull { it.second.size > 1 }
?.first

if (duplicateConstructorArgumentCount != null) {
throw Exception(
"Cannot have more than one constructor with $duplicateConstructorArgumentCount arguments in registered class $fqName"
)
}

val constructors = constructorInfo
.filter { constructor ->
constructor.isPublic &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,10 @@ object ConstructorRegistrationGenerator {
} else {
val templateArgs = mutableListOf<Any>()
val templateString = buildString {
append("{")
registeredConstructor.parameters.forEachIndexed { index, valueParameter ->
append("%L:·%T")
templateArgs.add(valueParameter.name)
templateArgs.add(
valueParameter.type.toTypeName()
) //setting nullables explicitly to false in case of type parameters for generic types, setting nullability later

if (valueParameter.typeArguments.isNotEmpty()) {
append("<")
append(
valueParameter.typeArguments.joinToString("") { typeArgument ->
templateArgs.add(
ClassName(
typeArgument.fqName.substringBeforeLast("."),
typeArgument.fqName.substringAfterLast(".")
)
)
if (typeArgument.isNullable) {
"%T?"
} else {
"%T"
}
}
)
append(">")
}

if (valueParameter.type.isNullable) {
append("?") //setting nullability now and not earlier in case of type parameters for generic types
}

if (index != registeredConstructor.parameters.size - 1) {
append("")
}
}

append("·->·%T(")
append("::%T")
templateArgs.add(className)

registeredConstructor.parameters.forEachIndexed { index, valueParameter ->
append(valueParameter.name)
if (index != registeredConstructor.parameters.size - 1) {
append("")
}
}
append(")},·")
append("")

registeredConstructor.parameters.forEachIndexed { index, valueParameter ->
append("%T")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,21 @@ internal fun KSClassDeclaration.mapToClazz(
val relativeSourcePath = absoluteSourcePath?.relativeTo(settings.projectBaseDir)?.invariantSeparatorsPath ?: ""

return if (shouldBeRegistered) {
val constructors = getConstructors()

val registeredConstructors = getConstructors()
val duplicateConstructorArgumentCount = constructors
.groupBy { it.parameters.size }
.toList()
.firstOrNull { it.second.size > 1 }
?.first

if (duplicateConstructorArgumentCount != null) {
throw Exception(
"Cannot have more than one constructor with $duplicateConstructorArgumentCount arguments in registered class $fqName"
)
}

val registeredConstructors = constructors
.filter { it.isPublic() }
.filter { constructor ->
constructor.annotations.any { it.fqNameUnsafe == RegisterConstructor::class.qualifiedName } ||
Expand Down

0 comments on commit 816d24b

Please sign in to comment.