Skip to content

Commit 0fb3fc3

Browse files
Support package-private classes and constructors in AssembleModelGenerator (#356)
Support package-private classes and constructors in AssembleModelGenerator
1 parent 3f338fa commit 0fb3fc3

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

utbot-framework/src/main/kotlin/org/utbot/engine/ValueConstructor.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.utbot.engine
33
import org.utbot.common.findField
44
import org.utbot.common.findFieldOrNull
55
import org.utbot.common.invokeCatching
6+
import org.utbot.common.withAccessibility
67
import org.utbot.framework.plugin.api.ClassId
78
import org.utbot.framework.plugin.api.ConstructorId
89
import org.utbot.framework.plugin.api.EnvironmentModels
@@ -390,12 +391,19 @@ class ValueConstructor {
390391
*/
391392
private fun value(model: UtModel) = construct(model, null).value
392393

393-
394394
private fun MethodId.call(args: List<Any?>, instance: Any?): Any? =
395-
method.invokeCatching(obj = instance, args = args).getOrThrow()
395+
method.run {
396+
withAccessibility {
397+
invokeCatching(obj = instance, args = args).getOrThrow()
398+
}
399+
}
396400

397401
private fun ConstructorId.call(args: List<Any?>): Any? =
398-
constructor.newInstance(*args.toTypedArray())
402+
constructor.run {
403+
withAccessibility {
404+
newInstance(*args.toTypedArray())
405+
}
406+
}
399407

400408
/**
401409
* Fetches primitive value from NutsModel to create array of primitives.

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)