11package org.utbot.fuzzing.providers
22
3+ import com.google.common.reflect.TypeToken
34import org.utbot.framework.plugin.api.*
45import org.utbot.framework.plugin.api.util.*
56import org.utbot.fuzzer.FuzzedType
67import org.utbot.fuzzer.FuzzedValue
78import org.utbot.fuzzer.IdGenerator
89import org.utbot.fuzzer.fuzzed
10+ import org.utbot.fuzzer.jType
911import org.utbot.fuzzing.*
1012import org.utbot.fuzzing.utils.hex
1113import kotlin.reflect.KClass
@@ -80,9 +82,9 @@ class EmptyCollectionValueProvider(
8082class MapValueProvider (
8183 idGenerator : IdGenerator <Int >
8284) : CollectionValueProvider(idGenerator, java.util.Map : :class.id) {
85+
8386 override fun resolveType (description : FuzzedDescription , type : FuzzedType ) = sequence {
84- val keyGeneric = type.generics.getOrNull(0 ) ? : FuzzedType (objectClassId)
85- val valueGeneric = type.generics.getOrNull(1 ) ? : FuzzedType (objectClassId)
87+ val (keyGeneric, valueGeneric) = resolveGenericsOfSuperClass<Map <* , * >>(description, type)
8688 when (type.classId) {
8789 java.util.Map ::class .id -> {
8890 if (keyGeneric.classId isSubtypeOf Comparable ::class ) {
@@ -108,8 +110,9 @@ class MapValueProvider(
108110class ListSetValueProvider (
109111 idGenerator : IdGenerator <Int >
110112) : CollectionValueProvider(idGenerator, java.util.Collection : :class.id) {
113+
111114 override fun resolveType (description : FuzzedDescription , type : FuzzedType ) = sequence {
112- val generic = type.generics.firstOrNull() ? : FuzzedType (objectClassId )
115+ val ( generic) = resolveGenericsOfSuperClass< Collection < * >>(description, type )
113116 when (type.classId) {
114117 java.util.Queue ::class .id,
115118 java.util.Deque ::class .id-> {
@@ -222,7 +225,7 @@ class IteratorValueProvider(val idGenerator: IdGenerator<Int>) : JavaValueProvid
222225 }
223226
224227 override fun generate (description : FuzzedDescription , type : FuzzedType ): Sequence <Seed <FuzzedType , FuzzedValue >> {
225- val generic = type.generics.firstOrNull() ? : FuzzedType (objectClassId )
228+ val ( generic) = resolveGenericsOfSuperClass< Iterator < * >>(description, type )
226229 return sequenceOf(Seed .Recursive (
227230 construct = Routine .Create (listOf (FuzzedType (iterableClassId, listOf (generic)))) { v ->
228231 val id = idGenerator.createId()
@@ -261,4 +264,21 @@ class IteratorValueProvider(val idGenerator: IdGenerator<Int>) : JavaValueProvid
261264 }
262265 ))
263266 }
267+ }
268+
269+ private inline fun <reified T > resolveGenericsOfSuperClass (
270+ description : FuzzedDescription ,
271+ type : FuzzedType ,
272+ ): List <FuzzedType > {
273+ val superClass = T ::class .java
274+ return try {
275+ check(superClass.isAssignableFrom(type.classId.jClass)) { " $superClass isn't super class of $type " }
276+ @Suppress(" UNCHECKED_CAST" )
277+ toFuzzerType(
278+ TypeToken .of(type.jType).getSupertype(superClass as Class <in Any >).type,
279+ description.typeCache
280+ ).generics
281+ } catch (e: Throwable ) {
282+ superClass.typeParameters.map { toFuzzerType(it, description.typeCache) }
283+ }
264284}
0 commit comments