Skip to content

Commit

Permalink
java.reflect.Class support
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniilStepanov committed Aug 10, 2023
1 parent 51d3360 commit d853854
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object Versions {
const val ksmt = "0.5.6"
const val collections = "0.3.5"
const val coroutines = "1.6.4"
const val jcdb = "c45b18e30a"
const val jcdb = "1.2.0"
const val mockk = "1.13.4"
const val junitParams = "5.9.3"
const val logback = "1.4.8"
Expand Down
4 changes: 2 additions & 2 deletions usvm-jvm-instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ kotlin {
}

dependencies {
implementation("com.github.UnitTestBot.jacodb:jacodb-core:${Versions.jcdb}")
implementation("com.github.UnitTestBot.jacodb:jacodb-analysis:${Versions.jcdb}")
implementation("org.jacodb:jacodb-core:${Versions.jcdb}")
implementation("org.jacodb:jacodb-analysis:${Versions.jcdb}")

implementation("com.jetbrains.rd:rd-framework:${Versions.rd}")
implementation("org.ini4j:ini4j:${Versions.ini4j}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class UTestValueDescriptorSerializer(private val ctx: SerializationContext) {
UTestValueDescriptorKind.CHAR_ARRAY -> deserializeCharArray()
UTestValueDescriptorKind.OBJECT_ARRAY -> deserializeArray()
UTestValueDescriptorKind.ENUM_VALUE -> deserializeEnumValue()
UTestValueDescriptorKind.CLASS -> deserializeClass()
}
ctx.deserializedDescriptors[id] = deserializedExpression
}
Expand Down Expand Up @@ -83,6 +84,7 @@ class UTestValueDescriptorSerializer(private val ctx: SerializationContext) {
is UTestObjectDescriptor -> serialize(uTestValueDescriptor)
is UTestCyclicReferenceDescriptor -> serialize(uTestValueDescriptor)
is UTestEnumValueDescriptor -> serialize(uTestValueDescriptor)
is UTestClassDescriptor -> serialize(uTestValueDescriptor)
}
}

Expand Down Expand Up @@ -428,6 +430,18 @@ class UTestValueDescriptorSerializer(private val ctx: SerializationContext) {
return UTestObjectDescriptor(jcType, fields, refId)
}

private fun AbstractBuffer.serialize(uTestObjectDescriptor: UTestClassDescriptor) =
serialize(
uTestValueDescriptor = uTestObjectDescriptor,
kind = UTestValueDescriptorKind.CLASS,
serializeInternals = {},
serialize = { writeString(className) }
)

private fun AbstractBuffer.deserializeClass(): UTestClassDescriptor {
return UTestClassDescriptor(readString(), jcClasspath.findTypeOrNull<Class<*>>() ?: jcClasspath.objectType)
}

private fun AbstractBuffer.serialize(uTestValueDescriptor: UTestEnumValueDescriptor) =
serialize(
uTestValueDescriptor = uTestValueDescriptor,
Expand Down Expand Up @@ -492,7 +506,8 @@ class UTestValueDescriptorSerializer(private val ctx: SerializationContext) {
CHAR_ARRAY,
OBJECT_ARRAY,
CYCLIC_REF,
ENUM_VALUE
ENUM_VALUE,
CLASS
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Descriptor2ValueConverter(private val workerClassLoader: ClassLoader) {

fun buildObjectFromDescriptor(descriptor: UTestValueDescriptor): Any? =
descriptorToObject.getOrPut(descriptor) {
buildObject(descriptor)
build(descriptor)
}

private fun buildObject(descriptor: UTestValueDescriptor): Any? =
private fun build(descriptor: UTestValueDescriptor): Any? =
when (descriptor) {
is UTestArrayDescriptor.Array -> array(descriptor)
is UTestArrayDescriptor.BooleanArray -> descriptor.value
Expand Down Expand Up @@ -46,6 +46,7 @@ class Descriptor2ValueConverter(private val workerClassLoader: ClassLoader) {
}
is UTestObjectDescriptor -> `object`(descriptor)
is UTestEnumValueDescriptor -> `enum`(descriptor)
is UTestClassDescriptor -> workerClassLoader.loadClass(descriptor.className)
}

private fun array(descriptor: UTestArrayDescriptor.Array): Any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ open class Value2DescriptorConverter(
is FloatArray -> array(any, depth + 1)
is DoubleArray -> array(any, depth + 1)
is Array<*> -> array(any, depth + 1)
is Class<*> -> `class`(any)
else -> `object`(any, depth + 1)
}
}
Expand Down Expand Up @@ -146,6 +147,10 @@ open class Value2DescriptorConverter(
objectToDescriptor.remove(value)
}

private fun `class`(value: Any): UTestValueDescriptor {
return UTestClassDescriptor(value::class.java.name, jcClasspath.findTypeOrNull<Class<*>>() ?: jcClasspath.objectType)
}

private fun `object`(value: Any, depth: Int): UTestValueDescriptor {
val jcClass = jcClasspath.findClass(value::class.java.name)
if (jcClass.isEnum) return `enum`(jcClass, value, depth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ sealed class UTestArrayDescriptor<T>(
) : UTestArrayDescriptor<List<UTestValueDescriptor>>(elementType, length, value), UTestRefDescriptor {
override fun valueToString() = value.toString()
override fun structurallyEqual(other: UTestValueDescriptor): Boolean {
if (other !is UTestArrayDescriptor.Array) return false
if (other !is Array) return false
if (length != other.length) return false
if (elementType != other.elementType) return false
return value.zip(other.value).all { descriptorsAreEqual(it.first, it.second) }
Expand Down Expand Up @@ -185,6 +185,16 @@ class UTestEnumValueDescriptor(
}
}

class UTestClassDescriptor(
val className: String, override val type: JcType
): UTestValueDescriptor() {
override fun structurallyEqual(other: UTestValueDescriptor): Boolean =
other is UTestClassDescriptor && className == other.className

override fun toString(): String =
"UTestClassDescriptor(className = $className)"
}

//TODO: Avoid recursion via DescriptorPrinter
class UTestObjectDescriptor(
override val type: JcType,
Expand Down
4 changes: 2 additions & 2 deletions usvm-jvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ plugins {
dependencies {
implementation(project(":usvm-core"))

implementation("com.github.UnitTestBot.jacodb:jacodb-core:${Versions.jcdb}")
implementation("com.github.UnitTestBot.jacodb:jacodb-analysis:${Versions.jcdb}")
implementation("org.jacodb:jacodb-core:${Versions.jcdb}")
implementation("org.jacodb:jacodb-analysis:${Versions.jcdb}")

implementation("io.ksmt:ksmt-yices:${Versions.ksmt}")
implementation("io.ksmt:ksmt-cvc5:${Versions.ksmt}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ internal class ExceptionExamplesTest : JavaMethodTestRunner() {
}

@Test
@Disabled("Issue with reflection object creation")
fun testCreateException() {
checkDiscoveredProperties(
ExceptionExamples::createException,
Expand Down

0 comments on commit d853854

Please sign in to comment.