Skip to content

Commit

Permalink
Added a minor generation problems
Browse files Browse the repository at this point in the history
Added a cache to comprehend possible cycles in the generation loops as well as instantiated all the array's elements
  • Loading branch information
khbminus committed Aug 20, 2024
1 parent 3578f17 commit 1467ee7
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,38 +103,44 @@ fun Parameters<Descriptor>.replaceNullsWithDefaultValues(method: Method, cm: Cla
return Parameters(instance, parametersWithoutNulls, statics)
}

private fun transformExistingDescriptor(descriptor: Descriptor, type: KexType, cm: ClassManager): Descriptor {
private fun transformExistingDescriptor(descriptor: Descriptor, type: KexType, cm: ClassManager, alreadyCopied: MutableMap<Descriptor, Descriptor> = mutableMapOf()): Descriptor {
if (descriptor in alreadyCopied) return alreadyCopied[descriptor]!!
return when (descriptor) {
is ConstantDescriptor.Null -> type.newInstance(cm)
is ConstantDescriptor -> descriptor
is ArrayDescriptor -> ArrayDescriptor(descriptor.elementType, descriptor.length)
.apply {
descriptor.elements.forEach { (index, value) ->
this[index] = transformExistingDescriptor(value, descriptor.elementType, cm)
(0 until descriptor.length).forEach { index ->
this[index] = transformExistingDescriptor(
descriptor[index] ?: ConstantDescriptor.Null,
descriptor.elementType,
cm,
alreadyCopied
)
}
}

is ObjectDescriptor -> ObjectDescriptor(descriptor.klass).apply {
descriptor.fields.forEach { (key, desc) ->
val (name, fieldType) = key
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm))
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied))
}
}

is ClassDescriptor -> ClassDescriptor(descriptor.klass).apply {
descriptor.fields.forEach { (key, desc) ->
val (name, fieldType) = key
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm))
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied))
}
}

is MockDescriptor -> MockDescriptor(descriptor.klass).apply {
descriptor.fields.forEach { (key, desc) ->
val (name, fieldType) = key
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm))
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied))
}
}
}
}.also { alreadyCopied[descriptor] = it }
}

private fun KexType.newInstance(cm: ClassManager): Descriptor {
Expand All @@ -143,10 +149,10 @@ private fun KexType.newInstance(cm: ClassManager): Descriptor {
val kfgClass = (this as KexClass).kfgClass(cm.type)
kfgClass
.fields
.filterNot { it.isStatic}
.filterNot { it.isStatic }
.forEach {
if (!it.isStatic)
descriptor[it.name to this] = it.type.kexType.newInstance(cm)
}
if (!it.isStatic)
descriptor[it.name to this] = it.type.kexType.newInstance(cm)
}
return descriptor
}

0 comments on commit 1467ee7

Please sign in to comment.