Skip to content

Commit d8db6ff

Browse files
committed
Support package-private classes and constructors in AssembleModelGenerator
1 parent 48e5641 commit d8db6ff

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.utbot.framework.plugin.api.util.defaultValueModel
3838
import org.utbot.framework.plugin.api.util.executableId
3939
import org.utbot.framework.plugin.api.util.jClass
4040
import org.utbot.framework.util.nextModelName
41+
import java.lang.reflect.Constructor
4142
import java.util.IdentityHashMap
4243

4344
/**
@@ -256,7 +257,7 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
256257
}
257258
//fill field value if it hasn't been filled by constructor, and it is not default
258259
if (fieldId in constructorInfo.affectedFields ||
259-
(fieldId !in constructorInfo.setFields && !fieldModel.hasDefaultValue())
260+
(fieldId !in constructorInfo.setFields && !fieldModel.hasDefaultValue())
260261
) {
261262
val modifierCall = modifierCall(this, fieldId, assembleModel(fieldModel))
262263
callChain.add(modifierCall)
@@ -357,15 +358,21 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
357358
*/
358359
private fun findBestConstructorOrNull(compositeModel: UtCompositeModel): ConstructorId? {
359360
val classId = compositeModel.classId
360-
if (!classId.isPublic || classId.isInner) return null
361+
if (!classId.isVisible || classId.isInner) return null
361362

362363
return classId.jClass.declaredConstructors
363-
.filter { it.isPublic || !it.isPrivate && it.declaringClass.packageName.startsWith(methodPackageName) }
364+
.filter { it.isVisible }
364365
.sortedByDescending { it.parameterCount }
365366
.map { it.executableId }
366367
.firstOrNull { constructorAnalyzer.isAppropriate(it) }
367368
}
368369

370+
private val ClassId.isVisible : Boolean
371+
get() = this.isPublic || !this.isPrivate && this.packageName.startsWith(methodPackageName)
372+
373+
private val Constructor<*>.isVisible : Boolean
374+
get() = this.isPublic || !this.isPrivate && this.declaringClass.packageName.startsWith(methodPackageName)
375+
369376
/**
370377
* Creates setter or direct setter call to set a field.
371378
*

utbot-framework/src/main/kotlin/org/utbot/framework/concrete/MockValueConstructor.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import kotlin.reflect.KClass
4545
import org.mockito.Mockito
4646
import org.mockito.stubbing.Answer
4747
import org.objectweb.asm.Type
48+
import org.utbot.common.withAccessibility
4849

4950
/**
5051
* Constructs values (including mocks) from models.
@@ -433,10 +434,18 @@ class MockValueConstructor(
433434
}
434435

435436
private fun MethodId.call(args: List<Any?>, instance: Any?): Any? =
436-
method.invokeCatching(obj = instance, args = args).getOrThrow()
437+
method.run {
438+
withAccessibility {
439+
invokeCatching(obj = instance, args = args).getOrThrow()
440+
}
441+
}
437442

438443
private fun ConstructorId.call(args: List<Any?>): Any? =
439-
constructor.newInstance(*args.toTypedArray())
444+
constructor.run {
445+
withAccessibility {
446+
newInstance(*args.toTypedArray())
447+
}
448+
}
440449

441450
/**
442451
* Fetches primitive value from NutsModel to create array of primitives.

0 commit comments

Comments
 (0)