1
1
package org.utbot.fuzzing.providers
2
2
3
+ import com.google.common.reflect.TypeToken
3
4
import org.utbot.framework.plugin.api.*
4
5
import org.utbot.framework.plugin.api.util.*
5
6
import org.utbot.fuzzer.FuzzedType
6
7
import org.utbot.fuzzer.FuzzedValue
7
8
import org.utbot.fuzzer.IdGenerator
8
9
import org.utbot.fuzzer.fuzzed
10
+ import org.utbot.fuzzer.jType
9
11
import org.utbot.fuzzing.*
10
12
import org.utbot.fuzzing.utils.hex
11
13
import kotlin.reflect.KClass
@@ -80,9 +82,9 @@ class EmptyCollectionValueProvider(
80
82
class MapValueProvider (
81
83
idGenerator : IdGenerator <Int >
82
84
) : CollectionValueProvider(idGenerator, java.util.Map : :class.id) {
85
+
83
86
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)
86
88
when (type.classId) {
87
89
java.util.Map ::class .id -> {
88
90
if (keyGeneric.classId isSubtypeOf Comparable ::class ) {
@@ -108,8 +110,9 @@ class MapValueProvider(
108
110
class ListSetValueProvider (
109
111
idGenerator : IdGenerator <Int >
110
112
) : CollectionValueProvider(idGenerator, java.util.Collection : :class.id) {
113
+
111
114
override fun resolveType (description : FuzzedDescription , type : FuzzedType ) = sequence {
112
- val generic = type.generics.firstOrNull() ? : FuzzedType (objectClassId )
115
+ val ( generic) = resolveGenericsOfSuperClass< Collection < * >>(description, type )
113
116
when (type.classId) {
114
117
java.util.Queue ::class .id,
115
118
java.util.Deque ::class .id-> {
@@ -222,7 +225,7 @@ class IteratorValueProvider(val idGenerator: IdGenerator<Int>) : JavaValueProvid
222
225
}
223
226
224
227
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 )
226
229
return sequenceOf(Seed .Recursive (
227
230
construct = Routine .Create (listOf (FuzzedType (iterableClassId, listOf (generic)))) { v ->
228
231
val id = idGenerator.createId()
@@ -261,4 +264,21 @@ class IteratorValueProvider(val idGenerator: IdGenerator<Int>) : JavaValueProvid
261
264
}
262
265
))
263
266
}
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
+ }
264
284
}
0 commit comments