Skip to content

Commit 6b106d9

Browse files
committed
supporting array types and fixing some tests #21
1 parent c51df76 commit 6b106d9

File tree

8 files changed

+168
-136
lines changed

8 files changed

+168
-136
lines changed

core/src/main/kotlin/org/utbot/jcdb/impl/types/ArrayClassIdImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.utbot.jcdb.impl.signature.Raw
88

99
class ArrayClassIdImpl(override val elementClass: ClassId) : ArrayClassId {
1010

11-
override val name = elementClass.simpleName + "[]"
11+
override val name = elementClass.name + "[]"
1212
override val simpleName = elementClass.simpleName + "[]"
1313

1414
override val location: ByteCodeLocation?

core/src/main/kotlin/org/utbot/jcdb/impl/types/Objects.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package org.utbot.jcdb.impl.types
22

33
import kotlinx.serialization.Serializable
44

5+
@Serializable
6+
sealed class ClassInfoContainer
7+
58
@Serializable
69
class ClassInfo(
710
val name: String,
@@ -20,7 +23,7 @@ class ClassInfo(
2023
val innerClasses: List<String>,
2124
val interfaces: List<String>,
2225
val annotations: List<AnnotationInfo>
23-
)
26+
) : ClassInfoContainer()
2427

2528
@Serializable
2629
class OuterClassRef(
@@ -56,4 +59,12 @@ class AnnotationInfo(
5659
@Serializable
5760
class LocationClasses(
5861
val classes: List<ClassInfo>
59-
)
62+
)
63+
64+
@Serializable
65+
class PredefinedClassInfo(val name: String): ClassInfoContainer()
66+
67+
@Serializable
68+
class ArrayClassInfo(
69+
val elementInfo: ClassInfoContainer
70+
) : ClassInfoContainer()

core/src/main/kotlin/org/utbot/jcdb/impl/types/PredefinedPrimitive.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class PredefinedPrimitive(override val classpath: ClasspathSet, override val sim
6767

6868
override suspend fun access() = Opcodes.ACC_PUBLIC
6969

70+
val info = PredefinedClassInfo(name = name)
71+
7072
override fun equals(other: Any?): Boolean {
7173
if (this === other) return true
7274
if (javaClass != other?.javaClass) return false

remote-rd/src/main/kotlin/org/utbot/jcdb/remote/rd/InternalApi.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,22 @@ class GetClassReq(val cpKey: String, val className: String) {
5252
}
5353
}
5454

55-
class GetClassRes(val location: String, val bytes: ByteArray) {
55+
class GetClassRes(val location: String?, val serializedClassInfo: ByteArray) {
5656

5757
companion object : IMarshaller<GetClassRes> {
5858

5959
override val _type: KClass<GetClassRes> = GetClassRes::class
6060

6161
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): GetClassRes {
6262
return GetClassRes(
63-
buffer.readString(),
63+
buffer.readNullableString(),
6464
buffer.readByteArray()
6565
)
6666
}
6767

6868
override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: GetClassRes) {
69-
buffer.writeString(value.location)
70-
buffer.writeByteArray(value.bytes)
69+
buffer.writeNullableString(value.location)
70+
buffer.writeByteArray(value.serializedClassInfo)
7171
}
7272
}
7373
}

remote-rd/src/main/kotlin/org/utbot/jcdb/remote/rd/RDServer.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import com.jetbrains.rd.util.threading.SingleThreadScheduler
99
import kotlinx.coroutines.runBlocking
1010
import kotlinx.serialization.cbor.Cbor
1111
import kotlinx.serialization.encodeToByteArray
12+
import org.utbot.jcdb.api.ClassId
1213
import org.utbot.jcdb.api.ClasspathSet
1314
import org.utbot.jcdb.api.Hook
1415
import org.utbot.jcdb.compilationDatabase
1516
import org.utbot.jcdb.impl.CompilationDatabaseImpl
16-
import org.utbot.jcdb.impl.types.ClassIdImpl
17+
import org.utbot.jcdb.impl.types.*
1718
import org.utbot.jcdb.remote.rd.client.RemoteCompilationDatabase
1819
import java.io.File
1920
import java.util.concurrent.ConcurrentHashMap
@@ -53,13 +54,13 @@ class RDServer(port: Int, val db: CompilationDatabaseImpl) : Hook {
5354
val key = req.cpKey
5455
val cp = classpaths[key] ?: throw IllegalStateException("No classpath found by key $key. \n Create it first")
5556
runBlocking {
56-
val classId = cp.findClassOrNull(req.className) as? ClassIdImpl
57-
if (classId != null) {
58-
val bytes = Cbor.encodeToByteArray(classId.info())
59-
val url = classId.location.locationURL
60-
GetClassRes(url.toString(), bytes)
61-
} else {
62-
null
57+
when (val classId = cp.findClassOrNull(req.className)) {
58+
is ClassId -> {
59+
val bytes = Cbor.encodeToByteArray(classId.convertToContainer())
60+
val url = classId.location?.locationURL
61+
GetClassRes(url?.toString(), bytes)
62+
}
63+
else -> null
6364
}
6465
}
6566
}.makeAsync()
@@ -86,6 +87,15 @@ class RDServer(port: Int, val db: CompilationDatabaseImpl) : Hook {
8687
return x
8788
}
8889

90+
private suspend fun ClassId.convertToContainer(): ClassInfoContainer {
91+
return when (this) {
92+
is ArrayClassIdImpl -> ArrayClassInfo(elementClass.convertToContainer())
93+
is ClassIdImpl -> info()
94+
is PredefinedPrimitive -> info
95+
else -> throw IllegalStateException("Can't convert class ${name} to serializable class info")
96+
}
97+
}
98+
8999
}
90100

91101
fun <T, X> RdCall<T, X>.makeAsync(): RdCall<T, X> {

remote-rd/src/main/kotlin/org/utbot/jcdb/remote/rd/client/RemoteClassId.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import java.net.URL
1313
import java.nio.file.Paths
1414

1515
class RemoteClassId(
16-
private val locationURL: String,
16+
private val locationURL: String?,
1717
private val classInfo: ClassInfo,
1818
override val classpath: ClasspathSet
1919
) : ClassId, ByteCodeConverter {
@@ -24,11 +24,12 @@ class RemoteClassId(
2424
override suspend fun access() = classInfo.access
2525

2626
override val location: ByteCodeLocation? by lazy(LazyThreadSafetyMode.NONE) {
27-
Paths.get(URL(locationURL).toURI()).toFile().asByteCodeLocation(isRuntime = false)
27+
locationURL?.let {
28+
Paths.get(URL(it).toURI()).toFile().asByteCodeLocation(isRuntime = false)
29+
}
2830
}
2931

30-
override val simpleName: String
31-
get() = classInfo.name
32+
override val simpleName = classInfo.name.substringAfterLast(".")
3233

3334
private val lazyInterfaces = suspendableLazy {
3435
classInfo.interfaces.map {

remote-rd/src/main/kotlin/org/utbot/jcdb/remote/rd/client/RemoteClasspathSet.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.utbot.jcdb.api.ByteCodeLocation
77
import org.utbot.jcdb.api.ClassId
88
import org.utbot.jcdb.api.ClasspathSet
99
import org.utbot.jcdb.api.CompilationDatabase
10-
import org.utbot.jcdb.impl.types.ClassInfo
10+
import org.utbot.jcdb.impl.types.*
1111
import org.utbot.jcdb.remote.rd.GetClassReq
1212
import org.utbot.jcdb.remote.rd.GetClassRes
1313

@@ -27,8 +27,8 @@ class RemoteClasspathSet(
2727

2828
override suspend fun findClassOrNull(name: String): ClassId? {
2929
val res = getClass.startSuspending(GetClassReq(key, name)) ?: return null
30-
val info = Cbor.decodeFromByteArray<ClassInfo>(res.bytes)
31-
return RemoteClassId(res.location, info, this)
30+
val info = Cbor.decodeFromByteArray<ClassInfoContainer>(res.serializedClassInfo)
31+
return info.asClassId(res.location)
3232
}
3333

3434
override suspend fun findSubClasses(name: String, allHierarchy: Boolean): List<ClassId> {
@@ -51,4 +51,13 @@ class RemoteClasspathSet(
5151
close.start(key)
5252
}
5353

54+
private fun ClassInfoContainer.asClassId(location: String?): ClassId {
55+
return when (this) {
56+
is ArrayClassInfo -> ArrayClassIdImpl(elementInfo.asClassId(location))
57+
is ClassInfo -> RemoteClassId(location, this, this@RemoteClasspathSet)
58+
is PredefinedClassInfo -> PredefinedPrimitives.of(name, this@RemoteClasspathSet) ?: throw IllegalStateException("unsupported predefined name $name")
59+
else -> throw IllegalStateException("unsupported class info container type ${this.javaClass.name}")
60+
}
61+
}
62+
5463
}

0 commit comments

Comments
 (0)