diff --git a/utbot-core/src/main/kotlin/org/utbot/common/KClassUtil.kt b/utbot-core/src/main/kotlin/org/utbot/common/KClassUtil.kt index 69b14167f7..92e4d8d398 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/KClassUtil.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/KClassUtil.kt @@ -8,12 +8,8 @@ import java.lang.reflect.Method * NOTE: vararg parameters must be passed as an array of the corresponding type. */ fun Method.invokeCatching(obj: Any?, args: List) = try { - val invocation = - try { - invoke(obj, *args.toTypedArray()) - } catch (e: Throwable) { - null - } + val invocation = invoke(obj, *args.toTypedArray()) + Result.success(invocation) } catch (e: InvocationTargetException) { Result.failure(e.targetException) diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt index fbdcaf4642..68267044d3 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt @@ -240,7 +240,7 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS /** * Set to true to start fuzzing if symbolic execution haven't return anything */ - var useFuzzing: Boolean by getBooleanProperty(false) + var useFuzzing: Boolean by getBooleanProperty(true) /** * Set to true to use grey-box fuzzing diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt index b3799f47ba..1813769347 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt @@ -480,7 +480,7 @@ data class UtArrayModel( * @param instantiationCall is an [UtExecutableCallModel] to instantiate represented object. It **must** not return `null`. * @param modificationsChain is a chain of [UtStatementModel] to construct object state. */ -data class UtAssembleModel constructor( +data class UtAssembleModel private constructor( override val id: Int?, override val classId: ClassId, override val modelName: String, diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/UtContext.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/UtContext.kt index ad064bfb3a..9152d986c3 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/UtContext.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/UtContext.kt @@ -5,7 +5,7 @@ import org.utbot.common.currentThreadInfo import org.utbot.framework.plugin.api.util.UtContext.Companion.setUtContext import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.ThreadContextElement -//import mu.KotlinLogging +import mu.KotlinLogging val utContext: UtContext get() = UtContext.currentContext() @@ -75,7 +75,7 @@ inline fun withUtContext(context: UtContext, block: () -> T): T = setUtConte try { return@use block.invoke() } catch (e: Exception) { - //KotlinLogging.logger("withUtContext").error { e } + KotlinLogging.logger("withUtContext").error { e } throw e } } diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/constructor/ValueConstructor.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/constructor/ValueConstructor.kt index f845801f83..962fd54446 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/constructor/ValueConstructor.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/constructor/ValueConstructor.kt @@ -42,7 +42,6 @@ import org.utbot.framework.plugin.api.util.anyInstance import org.utbot.framework.plugin.api.util.constructLambda import org.utbot.framework.plugin.api.util.constructStaticLambda import org.utbot.framework.plugin.api.util.constructor -import org.utbot.framework.plugin.api.util.id import org.utbot.framework.plugin.api.util.isStatic import org.utbot.framework.plugin.api.util.jClass import org.utbot.framework.plugin.api.util.jField diff --git a/utbot-framework/build.gradle b/utbot-framework/build.gradle index 5d28b5c817..be62217c6d 100644 --- a/utbot-framework/build.gradle +++ b/utbot-framework/build.gradle @@ -6,7 +6,6 @@ repositories { configurations { z3native - fetchInstrumentationJar } dependencies { @@ -36,10 +35,6 @@ dependencies { implementation group: 'io.github.microutils', name: 'kotlin-logging', version: kotlinLoggingVersion implementation group: 'org.jacoco', name: 'org.jacoco.report', version: jacocoVersion implementation group: 'org.apache.commons', name: 'commons-text', version: apacheCommonsTextVersion - implementation "org.javaruntype:javaruntype:1.3" - implementation "ru.vyarus:generics-resolver:3.0.3" - implementation "ognl:ognl:3.3.2" - // we need this for construction mocks from composite models implementation group: 'org.mockito', name: 'mockito-core', version: '4.2.0' @@ -51,7 +46,6 @@ dependencies { z3native group: 'com.microsoft.z3', name: 'z3-native-win64', version: z3Version, ext: 'zip' z3native group: 'com.microsoft.z3', name: 'z3-native-linux64', version: z3Version, ext: 'zip' z3native group: 'com.microsoft.z3', name: 'z3-native-osx', version: z3Version, ext: 'zip' - fetchInstrumentationJar project(path: ':utbot-instrumentation', configuration:'instrumentationArchive') } processResources { @@ -60,7 +54,4 @@ processResources { into "lib/x64" } } - from(configurations.fetchInstrumentationJar) { - into "lib" - } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt index cfb1e85682..943dc7401b 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt @@ -46,7 +46,6 @@ import org.utbot.fuzzing.utils.Trie import org.utbot.greyboxfuzzer.GreyBoxFuzzer import org.utbot.greyboxfuzzer.util.FuzzerUtModelConstructor import org.utbot.instrumentation.ConcreteExecutor -import ru.vyarus.java.generics.resolver.context.GenericsInfoFactory import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionData import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult import org.utbot.instrumentation.instrumentation.execution.UtExecutionInstrumentation diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt index cc3b736644..356d07270a 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt @@ -551,9 +551,8 @@ data class CgContext( } } - private var nestedClassIndex = 0 private fun createClassIdForNestedClass(testClassModel: TestClassModel): ClassId { - val simpleName = "${testClassModel.classUnderTest.simpleName}Test${nestedClassIndex++}" + val simpleName = "${testClassModel.classUnderTest.simpleName}Test" return BuiltinClassId( canonicalName = currentTestClass.canonicalName + "." + simpleName, simpleName = simpleName, diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/CgElement.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/CgElement.kt index b68f751b13..ba98a9fd4c 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/CgElement.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/CgElement.kt @@ -134,7 +134,7 @@ class CgClass( val body: CgClassBody, val isStatic: Boolean, val isNested: Boolean, -): CgStatement { +): CgElement { val packageName get() = id.packageName diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgAbstractRenderer.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgAbstractRenderer.kt index f2dd9f1fff..a2b7056f05 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgAbstractRenderer.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgAbstractRenderer.kt @@ -623,15 +623,7 @@ abstract class CgAbstractRenderer( is Boolean -> toStringConstant() // String is "\"" + "str" + "\"", RawString is "str" is String -> if (asRawString) "$this".escapeCharacters() else toStringConstant() - else -> { - val t = this@toStringConstant.type - val illegalType = t.toString().contains("$") || !t.isPublic - if (this == null && UtSettings.greyBoxFuzzingCompetitionMode && !illegalType) { - "(${this@toStringConstant.type}) null" - } else { - "$this" - } - } + else -> "$this" } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgJavaRenderer.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgJavaRenderer.kt index 6493d8df5a..b6371cac60 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgJavaRenderer.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgJavaRenderer.kt @@ -238,11 +238,6 @@ internal class CgJavaRenderer(context: CgRendererContext, printer: CgPrinter = C renderExceptions(element) } - private fun getTypeStringRepresentation(typeId: ClassId): String = - when { - typeId.isArray -> getTypeStringRepresentation(typeId.elementClassId!!) + "[]" - else -> typeId.toString() - } override fun renderMethodSignature(element: CgMockMethod) { val returnType = element.returnType.asString() diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/MockFrameworkManager.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/MockFrameworkManager.kt index 59d471a62c..0e8c05a7ce 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/MockFrameworkManager.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/MockFrameworkManager.kt @@ -20,30 +20,78 @@ import org.utbot.framework.codegen.domain.builtin.thenReturnMethodId import org.utbot.framework.codegen.domain.builtin.whenMethodId import org.utbot.framework.codegen.domain.context.CgContext import org.utbot.framework.codegen.domain.context.CgContextOwner -import org.utbot.framework.codegen.domain.models.* +import org.utbot.framework.codegen.domain.models.CgAnnotation +import org.utbot.framework.codegen.domain.models.CgAnonymousFunction +import org.utbot.framework.codegen.domain.models.CgAssignment +import org.utbot.framework.codegen.domain.models.CgClass +import org.utbot.framework.codegen.domain.models.CgClassBody +import org.utbot.framework.codegen.domain.models.CgConstructorCall +import org.utbot.framework.codegen.domain.models.CgDeclaration +import org.utbot.framework.codegen.domain.models.CgDocumentationComment +import org.utbot.framework.codegen.domain.models.CgExecutableCall +import org.utbot.framework.codegen.domain.models.CgExpression +import org.utbot.framework.codegen.domain.models.CgLiteral +import org.utbot.framework.codegen.domain.models.CgMethodCall +import org.utbot.framework.codegen.domain.models.CgMethodsCluster +import org.utbot.framework.codegen.domain.models.CgMockMethod +import org.utbot.framework.codegen.domain.models.CgParameterDeclaration +import org.utbot.framework.codegen.domain.models.CgRunnable +import org.utbot.framework.codegen.domain.models.CgSimpleRegion +import org.utbot.framework.codegen.domain.models.CgStatement +import org.utbot.framework.codegen.domain.models.CgStatementExecutableCall +import org.utbot.framework.codegen.domain.models.CgStaticRunnable +import org.utbot.framework.codegen.domain.models.CgSwitchCase +import org.utbot.framework.codegen.domain.models.CgSwitchCaseLabel +import org.utbot.framework.codegen.domain.models.CgValue +import org.utbot.framework.codegen.domain.models.CgVariable import org.utbot.framework.codegen.services.access.CgCallableAccessManager import org.utbot.framework.codegen.services.access.CgCallableAccessManagerImpl import org.utbot.framework.codegen.tree.* +import org.utbot.framework.codegen.tree.buildClassBody +import org.utbot.framework.codegen.tree.CgStatementConstructor +import org.utbot.framework.codegen.tree.CgVariableConstructor import org.utbot.framework.codegen.tree.CgStatementConstructorImpl import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getVariableConstructorBy import org.utbot.framework.codegen.tree.hasAmbiguousOverloadsOf import org.utbot.framework.codegen.util.isAccessibleFrom -import org.utbot.framework.plugin.api.* -import org.utbot.framework.plugin.api.util.* -import java.lang.IllegalStateException +import org.utbot.framework.plugin.api.BuiltinClassId +import org.utbot.framework.plugin.api.ClassId +import org.utbot.framework.plugin.api.ConstructorId +import org.utbot.framework.plugin.api.ExecutableId +import org.utbot.framework.plugin.api.MethodId +import org.utbot.framework.plugin.api.TypeParameters +import org.utbot.framework.plugin.api.UtCompositeModel +import org.utbot.framework.plugin.api.UtModel +import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation +import org.utbot.framework.plugin.api.UtStaticMethodInstrumentation +import org.utbot.framework.plugin.api.util.atomicIntegerClassId +import org.utbot.framework.plugin.api.util.atomicIntegerGet +import org.utbot.framework.plugin.api.util.atomicIntegerGetAndIncrement +import org.utbot.framework.plugin.api.util.booleanClassId +import org.utbot.framework.plugin.api.util.byteClassId +import org.utbot.framework.plugin.api.util.charClassId +import org.utbot.framework.plugin.api.util.doubleClassId +import org.utbot.framework.plugin.api.util.floatClassId +import org.utbot.framework.plugin.api.util.id +import org.utbot.framework.plugin.api.util.intClassId +import org.utbot.framework.plugin.api.util.longClassId +import org.utbot.framework.plugin.api.util.shortClassId +import org.utbot.framework.plugin.api.util.voidClassId +import org.utbot.framework.plugin.api.util.isRefType +import org.utbot.framework.plugin.api.util.exceptions abstract class CgVariableConstructorComponent(val context: CgContext) : - CgContextOwner by context, - CgCallableAccessManager by CgCallableAccessManagerImpl(context), - CgStatementConstructor by CgStatementConstructorImpl(context) { + CgContextOwner by context, + CgCallableAccessManager by CgCallableAccessManagerImpl(context), + CgStatementConstructor by CgStatementConstructorImpl(context) { val variableConstructor: CgVariableConstructor by lazy { getVariableConstructorBy(context) } fun mockitoArgumentMatchersFor(executable: ExecutableId): Array = - executable.parameters.map { - val matcher = it.mockitoAnyMatcher(executable.classId.hasAmbiguousOverloadsOf(executable)) - if (matcher != anyOfClass) argumentMatchersClassId[matcher]() else matchByClass(it) - }.toTypedArray() + executable.parameters.map { + val matcher = it.mockitoAnyMatcher(executable.classId.hasAmbiguousOverloadsOf(executable)) + if (matcher != anyOfClass) argumentMatchersClassId[matcher]() else matchByClass(it) + }.toTypedArray() /** * Clears all resources required for [currentExecution]. @@ -63,22 +111,22 @@ abstract class CgVariableConstructorComponent(val context: CgContext) : } fun ClassId.mockitoAnyMatcher(withExplicitClass: Boolean): MethodId = - when (this) { - byteClassId -> anyByte - charClassId -> anyChar - shortClassId -> anyShort - intClassId -> anyInt - longClassId -> anyLong - floatClassId -> anyFloat - doubleClassId -> anyDouble - booleanClassId -> anyBoolean - // we cannot match by string here - // because anyString accepts only non-nullable strings but argument could be null - else -> if (withExplicitClass) anyOfClass else any - } + when (this) { + byteClassId -> anyByte + charClassId -> anyChar + shortClassId -> anyShort + intClassId -> anyInt + longClassId -> anyLong + floatClassId -> anyFloat + doubleClassId -> anyDouble + booleanClassId -> anyBoolean + // we cannot match by string here + // because anyString accepts only non-nullable strings but argument could be null + else -> if (withExplicitClass) anyOfClass else any + } private fun matchByClass(id: ClassId): CgMethodCall = - argumentMatchersClassId[anyOfClass](getClassOf(id)) + argumentMatchersClassId[anyOfClass](getClassOf(id)) } /** @@ -127,7 +175,8 @@ private class InPlaceMocker(context: CgContext) : ObjectMocker(context) { } }.catch(Throwable::class.id) { throwStatement { - val constructor = IllegalStateException::class.java.id.allConstructors.first { it.parameters.isEmpty() } + val constructor = + IllegalStateException::class.java.id.allConstructors.first { it.parameters.isEmpty() } constructor() } } @@ -139,14 +188,14 @@ private class InPlaceMocker(context: CgContext) : ObjectMocker(context) { ) } val methodsCluster = CgSimpleRegion(null, methods) - val declaration = buildClass { + val classDeclaration = buildClass { id = mockClassId interfaces.add(model.classId) body = buildClassBody(mockClassId) { methodRegions += CgMethodsCluster(header = null, content = listOf(methodsCluster)) } } - currentBlock += declaration + currentBlock += CgClassAsStatement(classDeclaration) ++mockID val constructorId = ConstructorId(mockClassId, emptyList()) return newVar(model.classId, baseName) { @@ -162,6 +211,34 @@ private class InPlaceMocker(context: CgContext) : ObjectMocker(context) { TODO("Not yet implemented") } + private class CgClassAsStatement( + val id: ClassId, + val documentation: CgDocumentationComment?, + val annotations: List, + val superclass: ClassId?, + val interfaces: List, + val body: CgClassBody, + val isStatic: Boolean, + val isNested: Boolean + ) : CgStatement { + constructor(cgClass: CgClass) : this( + cgClass.id, + cgClass.documentation, + cgClass.annotations, + cgClass.superclass, + cgClass.interfaces, + cgClass.body, + cgClass.isStatic, + cgClass.isNested + ) + + val packageName + get() = id.packageName + + val simpleName + get() = id.simpleName + } + } class MockFrameworkManager(context: CgContext) : CgVariableConstructorComponent(context) { @@ -200,7 +277,7 @@ class MockFrameworkManager(context: CgContext) : CgVariableConstructorComponent( } private abstract class ObjectMocker( - context: CgContext + context: CgContext ) : CgVariableConstructorComponent(context) { abstract fun createMock(model: UtCompositeModel, baseName: String): CgVariable @@ -210,7 +287,7 @@ private abstract class ObjectMocker( } private abstract class StaticMocker( - context: CgContext + context: CgContext ) : CgVariableConstructorComponent(context) { abstract fun mockNewInstance(mock: UtNewInstanceInstrumentation) abstract fun mockStaticMethodsOfClass(classId: ClassId, methodMocks: List) @@ -232,7 +309,7 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) { when (executable) { is MethodId -> { if (executable.parameters.any { !it.isAccessibleFrom(testClassPackageName) }) { - error("Cannot mock method $executable with not accessible parameters") + error("Cannot mock method $executable with not accessible parameters" ) } val matchers = mockitoArgumentMatchersFor(executable) @@ -243,7 +320,6 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) { val results = values.map { variableConstructor.getOrCreateVariable(it) }.toTypedArray() `when`(mockObject[executable](*matchers)).thenReturn(executable.returnType, *results) } - else -> error("ConstructorId was not expected to appear in simple mocker but got $executable") } } @@ -252,10 +328,10 @@ private class MockitoMocker(context: CgContext) : ObjectMocker(context) { } override fun mock(clazz: CgExpression): CgMethodCall = - mockitoClassId[mockMethodId](clazz) + mockitoClassId[mockMethodId](clazz) override fun `when`(call: CgExecutableCall): CgMethodCall = - mockitoClassId[whenMethodId](call) + mockitoClassId[whenMethodId](call) } private class MockitoStaticMocker(context: CgContext, private val mocker: ObjectMocker) : StaticMocker(context) { @@ -301,7 +377,7 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object override fun mockStaticMethodsOfClass(classId: ClassId, methodMocks: List) { for ((methodId, values) in methodMocks) { if (methodId.parameters.any { !it.isAccessibleFrom(testClassPackageName) }) { - error("Cannot mock static method $methodId with not accessible parameters") + error("Cannot mock static method $methodId with not accessible parameters" ) } val matchers = mockitoArgumentMatchersFor(methodId) @@ -316,10 +392,10 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object listOf( CgStatementExecutableCall( CgMethodCall( - caller = null, - methodId, - matchers.toList() - ) + caller = null, + methodId, + matchers.toList() + ) ) ) ) @@ -389,7 +465,6 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object mocker.`when`(mockParameter[executable](*matchers))[thenReturnMethodId](*results) ) } - else -> error("Expected MethodId but got ConstructorId $executable") } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/Builders.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/Builders.kt index ce858d1c93..90b5d0b54e 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/Builders.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/Builders.kt @@ -85,7 +85,7 @@ interface CgMethodBuilder : CgBuilder { class CgTestMethodBuilder : CgMethodBuilder { override lateinit var name: String - override var returnType: ClassId = voidClassId + override val returnType: ClassId = voidClassId override lateinit var parameters: List override lateinit var statements: List override val exceptions: MutableSet = mutableSetOf() diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt index 09d978248a..8d98e8bcd5 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt @@ -5,7 +5,6 @@ import org.utbot.common.WorkaroundReason import org.utbot.common.isStatic import org.utbot.common.workaround import org.utbot.engine.ArtificialError -import org.utbot.engine.OverflowDetectionError import org.utbot.greyboxfuzzer.util.UtGreyBoxFuzzedExecution import org.utbot.framework.assemble.assemble import org.utbot.framework.codegen.domain.ForceStaticMocking @@ -134,7 +133,6 @@ import java.lang.reflect.InvocationTargetException import java.security.AccessControlException import java.lang.reflect.ParameterizedType import org.utbot.framework.UtSettings -import org.utbot.framework.codegen.domain.models.* import org.utbot.framework.codegen.services.access.CgCallableAccessManager import org.utbot.framework.codegen.services.access.CgFieldStateManagerImpl import org.utbot.framework.codegen.services.framework.TestFrameworkManager diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt index 7c6d8f4b05..cf5d44ed9d 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt @@ -197,18 +197,22 @@ open class CgVariableConstructor(val context: CgContext) : is UtDirectSetFieldModel -> { val instance = declareOrGet(statementModel.instance) // fields here are supposed to be accessible, so we assign them directly without any checks - val fieldValue = declareOrGet(statementModel.fieldModel) - if (statementModel.fieldId.canBeSetFrom(context) && fieldValue.type isSubtypeOf statementModel.fieldId.type) { - instance[statementModel.fieldId] `=` fieldValue - } else { - with(statementModel) { - +utilsClassId[setField]( - instance, - fieldId.declaringClass.name, - fieldId.name, - fieldValue - ) + if (UtSettings.useGreyBoxFuzzing) { + val fieldValue = declareOrGet(statementModel.fieldModel) + if (statementModel.fieldId.canBeSetFrom(context) && fieldValue.type isSubtypeOf statementModel.fieldId.type) { + instance[statementModel.fieldId] `=` fieldValue + } else { + with(statementModel) { + +utilsClassId[setField]( + instance, + fieldId.declaringClass.name, + fieldId.name, + fieldValue + ) + } } + } else { + instance[statementModel.fieldId] `=` declareOrGet(statementModel.fieldModel) } } is UtExecutableCallModel -> { diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/util/DslUtil.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/util/DslUtil.kt index 2429d6b0ac..228654457d 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/util/DslUtil.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/util/DslUtil.kt @@ -46,8 +46,6 @@ infix fun CgExpression.greaterThan(other: Any?): CgGreaterThan = // TODO: is it OK to use Object as a type of null literal? fun nullLiteral() = CgLiteral(objectClassId, null) -fun nullLiteralWithCast(classId: ClassId) = CgLiteral(classId, null) - fun intLiteral(num: Int) = CgLiteral(intClassId, num) fun longLiteral(num: Long) = CgLiteral(longClassId, num) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt index a13b5b1558..b0832725c4 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt @@ -43,7 +43,6 @@ import org.utbot.instrumentation.warmup import java.io.File import java.nio.file.Path import kotlin.coroutines.cancellation.CancellationException -import kotlin.io.path.appendText import kotlin.math.min /** @@ -231,6 +230,9 @@ open class TestCaseGenerator( } ConcreteExecutor.defaultPool.close() // TODO: think on appropriate way to close instrumented processes + forceMockListener.detach(this, forceMockListener) + forceStaticMockListener.detach(this, forceStaticMockListener) + if (UtSettings.useGreyBoxFuzzing) { GreyBoxFuzzingStatisticPrinter.printFuzzingStats(method2executions) } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt index 26c749c920..e95e76f5ea 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt @@ -131,6 +131,7 @@ val ExecutableId.sootMethod: SootMethod fun jimpleBody(method: ExecutableId): JimpleBody = method.sootMethod.jimpleBody() + private fun addBasicClasses(vararg classes: Class<*>) { classes.forEach { Scene.v().addBasicClass(it.name, SootClass.BODIES) diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/et/TraceHandler.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/et/TraceHandler.kt index 9968382e0f..e377adf17a 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/et/TraceHandler.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/et/TraceHandler.kt @@ -233,12 +233,6 @@ class TraceHandler { return instructionsList!! } - fun getMethodInstructions(className: String, methodSignature: String) = - processingStorage.instructionsData.entries.filter { - val insClassName = processingStorage.computeClassNameAndLocalId(it.key).first - insClassName == className && it.value.methodSignature == methodSignature - } - fun computePutStatics(): List = computeInstructionList().map { it.instructionData } .filterIsInstance() diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentation.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentation.kt index eb7a939bb0..accb4f3321 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentation.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentation.kt @@ -20,7 +20,6 @@ import org.utbot.framework.plugin.api.util.singleExecutableId import org.utbot.instrumentation.instrumentation.ArgumentList import org.utbot.instrumentation.instrumentation.Instrumentation import org.utbot.instrumentation.instrumentation.InvokeInstrumentation -import org.utbot.instrumentation.instrumentation.et.EtInstruction import org.utbot.instrumentation.instrumentation.et.TraceHandler import org.utbot.instrumentation.instrumentation.instrumenter.Instrumenter import org.utbot.instrumentation.instrumentation.mock.MockClassVisitor @@ -198,12 +197,3 @@ object UtExecutionInstrumentation : Instrumentation { return instrumenter.classByteCode } } - -/** - * Transforms a list of internal [EtInstruction]s to a list of api [Instruction]s. - */ -internal fun List.toApiCoverage(instructionsCount: Long? = null): Coverage = - Coverage( - map { Instruction(it.className, it.methodSignature, it.line, it.id) }, - instructionsCount - ) diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentationWithStatsCollection.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentationWithStatsCollection.kt index b5ed9e2a97..2bb8b0c069 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentationWithStatsCollection.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentationWithStatsCollection.kt @@ -1,7 +1,6 @@ package org.utbot.framework.concrete import org.utbot.framework.plugin.api.* -import org.utbot.instrumentation.instrumentation.ArgumentList import org.utbot.instrumentation.instrumentation.Instrumentation import org.utbot.instrumentation.instrumentation.InvokeInstrumentation import org.utbot.instrumentation.instrumentation.et.ExplicitThrowInstruction diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtFuzzingExecutionInstrumentation.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtFuzzingExecutionInstrumentation.kt index 6c36aef223..7020822a61 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtFuzzingExecutionInstrumentation.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtFuzzingExecutionInstrumentation.kt @@ -14,9 +14,6 @@ import org.utbot.instrumentation.instrumentation.execution.phases.PhaseError import org.utbot.instrumentation.instrumentation.execution.phases.PhasesController import org.utbot.instrumentation.instrumentation.execution.phases.start import java.security.AccessControlException -import kotlin.io.path.Path -import kotlin.io.path.appendText -import kotlin.io.path.writeText object UtFuzzingExecutionInstrumentation : UtExecutionInstrumentationWithStatsCollection { diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/MockValueConstructor.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/MockValueConstructor.kt index 96bca189a8..3d821248b2 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/MockValueConstructor.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/MockValueConstructor.kt @@ -83,7 +83,7 @@ class MockValueConstructor( } // TODO: JIRA:1379 -- replace UtReferenceModel with Int - private val constructedObjects = HashMap() + private val constructedObjects = HashMap() private val mockInfo = mutableListOf() private var mockTarget: MockTarget? = null private var mockCounter = 0 @@ -213,12 +213,7 @@ class MockValueConstructor( check(Reflection.isModifiersAccessible()) val target = mockTarget(fieldModel) { - FieldMockTarget( - fieldModel.classId.name, - model.classId.name, - UtConcreteValue(classInstance), - fieldId.name - ) + FieldMockTarget(fieldModel.classId.name, model.classId.name, UtConcreteValue(classInstance), fieldId.name) } val value = construct(fieldModel, target).value val instance = if (Modifier.isStatic(declaredField.modifiers)) null else classInstance @@ -367,10 +362,9 @@ class MockValueConstructor( val value = construct(elementModel, null).value try { java.lang.reflect.Array.set(instance, i, value) - } catch (iae: IllegalArgumentException) { + } catch (iae:IllegalArgumentException) { throw IllegalArgumentException( - iae.message + " array: ${instance.javaClass.name}; value: ${value?.javaClass?.name}", - iae + iae.message + " array: ${instance.javaClass.name}; value: ${value?.javaClass?.name}" , iae ) } } @@ -383,18 +377,15 @@ class MockValueConstructor( /** * Constructs object with [UtAssembleModel]. */ - private fun constructFromAssembleModel(assembleModel: UtAssembleModel): Any? { + private fun constructFromAssembleModel(assembleModel: UtAssembleModel): Any { constructedObjects[assembleModel]?.let { return it } val instantiationExecutableCall = assembleModel.instantiationCall val result = updateWithExecutableCallModel(instantiationExecutableCall) -// checkNotNull(result) { -// "Tracked instance can't be null for call ${instantiationExecutableCall.executable} in model $assembleModel" -// } - constructedObjects[assembleModel] = result - if (result == null) { - return null + checkNotNull(result) { + "Tracked instance can't be null for call ${instantiationExecutableCall.executable} in model $assembleModel" } + constructedObjects[assembleModel] = result assembleModel.modificationsChain.forEach { statementModel -> when (statementModel) { @@ -510,13 +501,8 @@ class MockValueConstructor( } private fun ConstructorId.call(args: List): Any? = - try { - constructor.runSandbox { - this.isAccessible = true - newInstance(*args.toTypedArray()) - } - } catch (e: Throwable) { - null + constructor.runSandbox { + newInstance(*args.toTypedArray()) } /** diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/mock/MockClassVisitor.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/mock/MockClassVisitor.kt index 4ce875c12a..ec108974e3 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/mock/MockClassVisitor.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/mock/MockClassVisitor.kt @@ -96,36 +96,36 @@ class MockClassVisitor( private val afterLabels: MutableList