Skip to content

Commit 82c0351

Browse files
authored
Consider containingClass when matching psi methods and KFuncitons #1398 (#1502)
1 parent a3fb966 commit 82c0351

File tree

7 files changed

+141
-122
lines changed

7 files changed

+141
-122
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.utbot.framework.plugin.api
2+
3+
import org.utbot.framework.plugin.api.util.declaringClazz
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.KParameter
6+
import kotlin.reflect.jvm.javaType
7+
8+
// Note that rules for obtaining signature here should correlate with PsiMethod.signature()
9+
fun KFunction<*>.methodDescription() =
10+
MethodDescription(
11+
this.name,
12+
this.declaringClazz.name,
13+
this.parameters.filter { it.kind != KParameter.Kind.INSTANCE }.map { it.type.javaType.typeName }
14+
)
15+
16+
// Similar to MethodId, but significantly simplified -- used only to match methods from psi and their reflections
17+
data class MethodDescription(val name: String, val containingClass: String?, val parameterTypes: List<String?>) {
18+
19+
fun normalized() = this.copy(
20+
containingClass = containingClass?.replace("$", "."), // normalize names of nested classes
21+
parameterTypes = parameterTypes.map {
22+
it?.replace("$", ".")
23+
}
24+
)
25+
}

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/SignatureUtil.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.

utbot-framework/src/main/kotlin/org/utbot/framework/process/EngineProcessMain.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
2121
import org.utbot.framework.codegen.domain.testFrameworkByName
2222
import org.utbot.framework.codegen.reports.TestsGenerationReport
2323
import org.utbot.framework.plugin.api.*
24-
import org.utbot.framework.plugin.api.Signature
24+
import org.utbot.framework.plugin.api.MethodDescription
2525
import org.utbot.framework.plugin.api.util.UtContext
2626
import org.utbot.framework.plugin.api.util.executableId
2727
import org.utbot.framework.plugin.api.util.id
@@ -159,19 +159,21 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
159159
}
160160
watchdog.wrapActiveCall(findMethodsInClassMatchingSelected) { params ->
161161
val classId = kryoHelper.readObject<ClassId>(params.classId)
162-
val selectedSignatures = params.signatures.map { Signature(it.name, it.parametersTypes) }
162+
val selectedMethodDescriptions =
163+
params.methodDescriptions.map { MethodDescription(it.name, it.containingClass, it.parametersTypes) }
163164
FindMethodsInClassMatchingSelectedResult(kryoHelper.writeObject(classId.jClass.allNestedClasses.flatMap { clazz ->
164-
clazz.id.allMethods.mapNotNull { it.method.kotlinFunction }.sortedWith(compareBy { selectedSignatures.indexOf(it.signature()) })
165-
.filter { it.signature().normalized() in selectedSignatures }
165+
clazz.id.allMethods.mapNotNull { it.method.kotlinFunction }
166+
.sortedWith(compareBy { selectedMethodDescriptions.indexOf(it.methodDescription()) })
167+
.filter { it.methodDescription().normalized() in selectedMethodDescriptions }
166168
.map { it.executableId }
167169
}))
168170
}
169171
watchdog.wrapActiveCall(findMethodParamNames) { params ->
170172
val classId = kryoHelper.readObject<ClassId>(params.classId)
171-
val bySignature = kryoHelper.readObject<Map<Signature, List<String>>>(params.bySignature)
173+
val byMethodDescription = kryoHelper.readObject<Map<MethodDescription, List<String>>>(params.bySignature)
172174
FindMethodParamNamesResult(kryoHelper.writeObject(
173175
classId.jClass.allNestedClasses.flatMap { clazz -> clazz.id.allMethods.mapNotNull { it.method.kotlinFunction } }
174-
.mapNotNull { method -> bySignature[method.signature()]?.let { params -> method.executableId to params } }
176+
.mapNotNull { method -> byMethodDescription[method.methodDescription()]?.let { params -> method.executableId to params } }
175177
.toMap()
176178
))
177179
}

utbot-framework/src/main/kotlin/org/utbot/framework/process/generated/EngineProcessModel.Generated.kt

Lines changed: 94 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class EngineProcessModel private constructor(
4242
serializers.register(RenderParams)
4343
serializers.register(RenderResult)
4444
serializers.register(SetupContextParams)
45-
serializers.register(Signature)
45+
serializers.register(MethodDescription)
4646
serializers.register(FindMethodsInClassMatchingSelectedArguments)
4747
serializers.register(FindMethodsInClassMatchingSelectedResult)
4848
serializers.register(FindMethodParamNamesArguments)
@@ -71,9 +71,9 @@ class EngineProcessModel private constructor(
7171
bind(lifetime, protocol, "EngineProcessModel")
7272
}
7373
}
74-
75-
76-
const val serializationHash = -621732450296355904L
74+
75+
76+
const val serializationHash = -6219345436129699239L
7777

7878
}
7979
override val serializersOwner: ISerializersOwner get() = EngineProcessModel
@@ -180,7 +180,7 @@ val IProtocol.engineProcessModel get() = getOrCreateExtension(EngineProcessModel
180180

181181

182182
/**
183-
* #### Generated from [EngineProcessModel.kt:99]
183+
* #### Generated from [EngineProcessModel.kt:100]
184184
*/
185185
data class FindMethodParamNamesArguments (
186186
val classId: ByteArray,
@@ -243,7 +243,7 @@ data class FindMethodParamNamesArguments (
243243

244244

245245
/**
246-
* #### Generated from [EngineProcessModel.kt:103]
246+
* #### Generated from [EngineProcessModel.kt:104]
247247
*/
248248
data class FindMethodParamNamesResult (
249249
val paramNames: ByteArray
@@ -300,27 +300,27 @@ data class FindMethodParamNamesResult (
300300

301301

302302
/**
303-
* #### Generated from [EngineProcessModel.kt:92]
303+
* #### Generated from [EngineProcessModel.kt:93]
304304
*/
305305
data class FindMethodsInClassMatchingSelectedArguments (
306306
val classId: ByteArray,
307-
val signatures: List<Signature>
307+
val methodDescriptions: List<MethodDescription>
308308
) : IPrintable {
309309
//companion
310310

311311
companion object : IMarshaller<FindMethodsInClassMatchingSelectedArguments> {
312312
override val _type: KClass<FindMethodsInClassMatchingSelectedArguments> = FindMethodsInClassMatchingSelectedArguments::class
313313

314314
@Suppress("UNCHECKED_CAST")
315-
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): FindMethodsInClassMatchingSelectedArguments {
315+
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): FindMethodsInClassMatchingSelectedArguments {
316316
val classId = buffer.readByteArray()
317-
val signatures = buffer.readList { Signature.read(ctx, buffer) }
318-
return FindMethodsInClassMatchingSelectedArguments(classId, signatures)
317+
val methodDescriptions = buffer.readList { MethodDescription.read(ctx, buffer) }
318+
return FindMethodsInClassMatchingSelectedArguments(classId, methodDescriptions)
319319
}
320320

321321
override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: FindMethodsInClassMatchingSelectedArguments) {
322322
buffer.writeByteArray(value.classId)
323-
buffer.writeList(value.signatures) { v -> Signature.write(ctx, buffer, v) }
323+
buffer.writeList(value.methodDescriptions) { v -> MethodDescription.write(ctx, buffer, v) }
324324
}
325325

326326

@@ -335,25 +335,25 @@ data class FindMethodsInClassMatchingSelectedArguments (
335335
if (other == null || other::class != this::class) return false
336336

337337
other as FindMethodsInClassMatchingSelectedArguments
338-
338+
339339
if (!(classId contentEquals other.classId)) return false
340-
if (signatures != other.signatures) return false
340+
if (methodDescriptions != other.methodDescriptions) return false
341341

342342
return true
343343
}
344344
//hash code trait
345345
override fun hashCode(): Int {
346346
var __r = 0
347-
__r = __r*31 + classId.contentHashCode()
348-
__r = __r*31 + signatures.hashCode()
347+
__r = __r * 31 + classId.contentHashCode()
348+
__r = __r * 31 + methodDescriptions.hashCode()
349349
return __r
350350
}
351351
//pretty print
352352
override fun print(printer: PrettyPrinter) {
353353
printer.println("FindMethodsInClassMatchingSelectedArguments (")
354354
printer.indent {
355355
print("classId = "); classId.print(printer); println()
356-
print("signatures = "); signatures.print(printer); println()
356+
print("methodDescriptions = "); methodDescriptions.print(printer); println()
357357
}
358358
printer.print(")")
359359
}
@@ -363,7 +363,7 @@ data class FindMethodsInClassMatchingSelectedArguments (
363363

364364

365365
/**
366-
* #### Generated from [EngineProcessModel.kt:96]
366+
* #### Generated from [EngineProcessModel.kt:97]
367367
*/
368368
data class FindMethodsInClassMatchingSelectedResult (
369369
val executableIds: ByteArray
@@ -606,7 +606,7 @@ data class GenerateResult (
606606

607607

608608
/**
609-
* #### Generated from [EngineProcessModel.kt:111]
609+
* #### Generated from [EngineProcessModel.kt:112]
610610
*/
611611
data class GenerateTestReportArgs (
612612
val eventLogMessage: String?,
@@ -699,7 +699,7 @@ data class GenerateTestReportArgs (
699699

700700

701701
/**
702-
* #### Generated from [EngineProcessModel.kt:120]
702+
* #### Generated from [EngineProcessModel.kt:121]
703703
*/
704704
data class GenerateTestReportResult (
705705
val notifyMessage: String,
@@ -830,10 +830,82 @@ data class JdkInfo (
830830
}
831831

832832

833+
/**
834+
* #### Generated from [EngineProcessModel.kt:88]
835+
*/
836+
data class MethodDescription(
837+
val name: String,
838+
val containingClass: String?,
839+
val parametersTypes: List<String?>
840+
) : IPrintable {
841+
//companion
842+
843+
companion object : IMarshaller<MethodDescription> {
844+
override val _type: KClass<MethodDescription> = MethodDescription::class
845+
846+
@Suppress("UNCHECKED_CAST")
847+
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): MethodDescription {
848+
val name = buffer.readString()
849+
val containingClass = buffer.readNullable { buffer.readString() }
850+
val parametersTypes = buffer.readList { buffer.readNullable { buffer.readString() } }
851+
return MethodDescription(name, containingClass, parametersTypes)
852+
}
853+
854+
override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: MethodDescription) {
855+
buffer.writeString(value.name)
856+
buffer.writeNullable(value.containingClass) { buffer.writeString(it) }
857+
buffer.writeList(value.parametersTypes) { v -> buffer.writeNullable(v) { buffer.writeString(it) } }
858+
}
859+
860+
861+
}
862+
863+
//fields
864+
//methods
865+
//initializer
866+
//secondary constructor
867+
//equals trait
868+
override fun equals(other: Any?): Boolean {
869+
if (this === other) return true
870+
if (other == null || other::class != this::class) return false
871+
872+
other as MethodDescription
873+
874+
if (name != other.name) return false
875+
if (containingClass != other.containingClass) return false
876+
if (parametersTypes != other.parametersTypes) return false
877+
878+
return true
879+
}
880+
881+
//hash code trait
882+
override fun hashCode(): Int {
883+
var __r = 0
884+
__r = __r * 31 + name.hashCode()
885+
__r = __r * 31 + if (containingClass != null) containingClass.hashCode() else 0
886+
__r = __r * 31 + parametersTypes.hashCode()
887+
return __r
888+
}
889+
890+
//pretty print
891+
override fun print(printer: PrettyPrinter) {
892+
printer.println("MethodDescription (")
893+
printer.indent {
894+
print("name = "); name.print(printer); println()
895+
print("containingClass = "); containingClass.print(printer); println()
896+
print("parametersTypes = "); parametersTypes.print(printer); println()
897+
}
898+
printer.print(")")
899+
}
900+
//deepClone
901+
//contexts
902+
}
903+
904+
833905
/**
834906
* #### Generated from [EngineProcessModel.kt:64]
835907
*/
836-
data class RenderParams (
908+
data class RenderParams(
837909
val testSetsId: Long,
838910
val classUnderTest: ByteArray,
839911
val paramNames: ByteArray,
@@ -1091,69 +1163,6 @@ data class SetupContextParams (
10911163
}
10921164

10931165

1094-
/**
1095-
* #### Generated from [EngineProcessModel.kt:88]
1096-
*/
1097-
data class Signature (
1098-
val name: String,
1099-
val parametersTypes: List<String?>
1100-
) : IPrintable {
1101-
//companion
1102-
1103-
companion object : IMarshaller<Signature> {
1104-
override val _type: KClass<Signature> = Signature::class
1105-
1106-
@Suppress("UNCHECKED_CAST")
1107-
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): Signature {
1108-
val name = buffer.readString()
1109-
val parametersTypes = buffer.readList { buffer.readNullable { buffer.readString() } }
1110-
return Signature(name, parametersTypes)
1111-
}
1112-
1113-
override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: Signature) {
1114-
buffer.writeString(value.name)
1115-
buffer.writeList(value.parametersTypes) { v -> buffer.writeNullable(v) { buffer.writeString(it) } }
1116-
}
1117-
1118-
1119-
}
1120-
//fields
1121-
//methods
1122-
//initializer
1123-
//secondary constructor
1124-
//equals trait
1125-
override fun equals(other: Any?): Boolean {
1126-
if (this === other) return true
1127-
if (other == null || other::class != this::class) return false
1128-
1129-
other as Signature
1130-
1131-
if (name != other.name) return false
1132-
if (parametersTypes != other.parametersTypes) return false
1133-
1134-
return true
1135-
}
1136-
//hash code trait
1137-
override fun hashCode(): Int {
1138-
var __r = 0
1139-
__r = __r*31 + name.hashCode()
1140-
__r = __r*31 + parametersTypes.hashCode()
1141-
return __r
1142-
}
1143-
//pretty print
1144-
override fun print(printer: PrettyPrinter) {
1145-
printer.println("Signature (")
1146-
printer.indent {
1147-
print("name = "); name.print(printer); println()
1148-
print("parametersTypes = "); parametersTypes.print(printer); println()
1149-
}
1150-
printer.print(")")
1151-
}
1152-
//deepClone
1153-
//contexts
1154-
}
1155-
1156-
11571166
/**
11581167
* #### Generated from [EngineProcessModel.kt:36]
11591168
*/
@@ -1230,7 +1239,7 @@ data class TestGeneratorParams (
12301239

12311240

12321241
/**
1233-
* #### Generated from [EngineProcessModel.kt:106]
1242+
* #### Generated from [EngineProcessModel.kt:107]
12341243
*/
12351244
data class WriteSarifReportArguments (
12361245
val testSetsId: Long,

0 commit comments

Comments
 (0)