Skip to content

Commit

Permalink
supporting array types and fixing some tests #21
Browse files Browse the repository at this point in the history
  • Loading branch information
lehvolk committed Jul 20, 2022
1 parent c51df76 commit 6b106d9
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.utbot.jcdb.impl.signature.Raw

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

override val name = elementClass.simpleName + "[]"
override val name = elementClass.name + "[]"
override val simpleName = elementClass.simpleName + "[]"

override val location: ByteCodeLocation?
Expand Down
15 changes: 13 additions & 2 deletions core/src/main/kotlin/org/utbot/jcdb/impl/types/Objects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.utbot.jcdb.impl.types

import kotlinx.serialization.Serializable

@Serializable
sealed class ClassInfoContainer

@Serializable
class ClassInfo(
val name: String,
Expand All @@ -20,7 +23,7 @@ class ClassInfo(
val innerClasses: List<String>,
val interfaces: List<String>,
val annotations: List<AnnotationInfo>
)
) : ClassInfoContainer()

@Serializable
class OuterClassRef(
Expand Down Expand Up @@ -56,4 +59,12 @@ class AnnotationInfo(
@Serializable
class LocationClasses(
val classes: List<ClassInfo>
)
)

@Serializable
class PredefinedClassInfo(val name: String): ClassInfoContainer()

@Serializable
class ArrayClassInfo(
val elementInfo: ClassInfoContainer
) : ClassInfoContainer()
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class PredefinedPrimitive(override val classpath: ClasspathSet, override val sim

override suspend fun access() = Opcodes.ACC_PUBLIC

val info = PredefinedClassInfo(name = name)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ class GetClassReq(val cpKey: String, val className: String) {
}
}

class GetClassRes(val location: String, val bytes: ByteArray) {
class GetClassRes(val location: String?, val serializedClassInfo: ByteArray) {

companion object : IMarshaller<GetClassRes> {

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

override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): GetClassRes {
return GetClassRes(
buffer.readString(),
buffer.readNullableString(),
buffer.readByteArray()
)
}

override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: GetClassRes) {
buffer.writeString(value.location)
buffer.writeByteArray(value.bytes)
buffer.writeNullableString(value.location)
buffer.writeByteArray(value.serializedClassInfo)
}
}
}
26 changes: 18 additions & 8 deletions remote-rd/src/main/kotlin/org/utbot/jcdb/remote/rd/RDServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import com.jetbrains.rd.util.threading.SingleThreadScheduler
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.encodeToByteArray
import org.utbot.jcdb.api.ClassId
import org.utbot.jcdb.api.ClasspathSet
import org.utbot.jcdb.api.Hook
import org.utbot.jcdb.compilationDatabase
import org.utbot.jcdb.impl.CompilationDatabaseImpl
import org.utbot.jcdb.impl.types.ClassIdImpl
import org.utbot.jcdb.impl.types.*
import org.utbot.jcdb.remote.rd.client.RemoteCompilationDatabase
import java.io.File
import java.util.concurrent.ConcurrentHashMap
Expand Down Expand Up @@ -53,13 +54,13 @@ class RDServer(port: Int, val db: CompilationDatabaseImpl) : Hook {
val key = req.cpKey
val cp = classpaths[key] ?: throw IllegalStateException("No classpath found by key $key. \n Create it first")
runBlocking {
val classId = cp.findClassOrNull(req.className) as? ClassIdImpl
if (classId != null) {
val bytes = Cbor.encodeToByteArray(classId.info())
val url = classId.location.locationURL
GetClassRes(url.toString(), bytes)
} else {
null
when (val classId = cp.findClassOrNull(req.className)) {
is ClassId -> {
val bytes = Cbor.encodeToByteArray(classId.convertToContainer())
val url = classId.location?.locationURL
GetClassRes(url?.toString(), bytes)
}
else -> null
}
}
}.makeAsync()
Expand All @@ -86,6 +87,15 @@ class RDServer(port: Int, val db: CompilationDatabaseImpl) : Hook {
return x
}

private suspend fun ClassId.convertToContainer(): ClassInfoContainer {
return when (this) {
is ArrayClassIdImpl -> ArrayClassInfo(elementClass.convertToContainer())
is ClassIdImpl -> info()
is PredefinedPrimitive -> info
else -> throw IllegalStateException("Can't convert class ${name} to serializable class info")
}
}

}

fun <T, X> RdCall<T, X>.makeAsync(): RdCall<T, X> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.net.URL
import java.nio.file.Paths

class RemoteClassId(
private val locationURL: String,
private val locationURL: String?,
private val classInfo: ClassInfo,
override val classpath: ClasspathSet
) : ClassId, ByteCodeConverter {
Expand All @@ -24,11 +24,12 @@ class RemoteClassId(
override suspend fun access() = classInfo.access

override val location: ByteCodeLocation? by lazy(LazyThreadSafetyMode.NONE) {
Paths.get(URL(locationURL).toURI()).toFile().asByteCodeLocation(isRuntime = false)
locationURL?.let {
Paths.get(URL(it).toURI()).toFile().asByteCodeLocation(isRuntime = false)
}
}

override val simpleName: String
get() = classInfo.name
override val simpleName = classInfo.name.substringAfterLast(".")

private val lazyInterfaces = suspendableLazy {
classInfo.interfaces.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.utbot.jcdb.api.ByteCodeLocation
import org.utbot.jcdb.api.ClassId
import org.utbot.jcdb.api.ClasspathSet
import org.utbot.jcdb.api.CompilationDatabase
import org.utbot.jcdb.impl.types.ClassInfo
import org.utbot.jcdb.impl.types.*
import org.utbot.jcdb.remote.rd.GetClassReq
import org.utbot.jcdb.remote.rd.GetClassRes

Expand All @@ -27,8 +27,8 @@ class RemoteClasspathSet(

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

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

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

}
Loading

0 comments on commit 6b106d9

Please sign in to comment.