Skip to content

Commit 259078b

Browse files
Refactor: remove graph parameter from Traverser constructor
1 parent fbc0541 commit 259078b

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ import sun.reflect.generics.reflectiveObjects.TypeVariableImpl
266266
import sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
267267
import java.lang.reflect.Method
268268
import java.util.concurrent.atomic.AtomicInteger
269+
import org.utbot.framework.plugin.api.jimpleBody
269270

270271
private val logger = KotlinLogging.logger {}
271272
val pathLogger = KotlinLogging.logger(logger.name + ".path")
@@ -317,14 +318,17 @@ private fun pathSelector(graph: InterProceduralUnitGraph, typeRegistry: TypeRegi
317318
class Traverser(
318319
private val controller: EngineController,
319320
private val methodUnderTest: UtMethod<*>,
320-
private val graph: ExceptionalUnitGraph,
321321
classpath: String,
322322
dependencyPaths: String,
323323
mockStrategy: MockStrategy = NO_MOCKS,
324324
chosenClassesToMockAlways: Set<ClassId>,
325325
private val solverTimeoutInMillis: Int = checkSolverTimeoutMillis
326326
) : UtContextInitializer() {
327327

328+
private val graph = jimpleBody(methodUnderTest).also {
329+
logger.trace { "JIMPLE for $methodUnderTest:\n$this" }
330+
}.graph()
331+
328332
private val methodUnderAnalysisStmts: Set<Stmt> = graph.stmts.toSet()
329333
private val visitedStmts: MutableSet<Stmt> = mutableSetOf()
330334
private val globalGraph = InterProceduralUnitGraph(graph)

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/UtBotTestCaseGenerator.kt

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import kotlinx.coroutines.launch
4848
import kotlinx.coroutines.runBlocking
4949
import kotlinx.coroutines.yield
5050
import mu.KotlinLogging
51-
import org.utbot.engine.*
5251
import soot.Scene
5352
import soot.jimple.JimpleBody
5453
import soot.toolkits.graph.ExceptionalUnitGraph
@@ -202,12 +201,9 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
202201
): Traverser {
203202
// TODO: create classLoader from buildDir/classpath and migrate from UtMethod to MethodId?
204203
logger.debug("Starting symbolic execution for $method --$mockStrategy--")
205-
val graph = graph(method)
206-
207204
return Traverser(
208205
controller,
209206
method,
210-
graph,
211207
classpathForEngine,
212208
dependencyPaths = dependencyPaths,
213209
mockStrategy = apiToModel(mockStrategy),
@@ -419,31 +415,22 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
419415
else -> error("Cannot map API Mock Strategy model to Engine model: $mockStrategyApi")
420416
}
421417

422-
private fun graph(method: UtMethod<*>): ExceptionalUnitGraph {
423-
val className = method.clazz.java.name
424-
val clazz = Scene.v().classes.singleOrNull { it.name == className }
425-
?: error("No such $className found in the Scene")
426-
val signature = method.callable.signature
427-
val sootMethod = clazz.methods.singleOrNull { it.pureJavaSignature == signature }
428-
?: error("No such $signature found")
429-
if (!sootMethod.canRetrieveBody()) {
430-
error("No method body for $sootMethod found")
431-
}
432-
val methodBody = sootMethod.jimpleBody()
433-
val graph = methodBody.graph()
434-
435-
logger.trace { "JIMPLE for $method:\n${methodBody}" }
418+
}
436419

437-
return graph
438-
}
420+
fun graph(method: UtMethod<*>): ExceptionalUnitGraph {
421+
val methodBody = jimpleBody(method)
422+
return methodBody.graph()
423+
}
439424

440-
fun jimpleBody(method: UtMethod<*>): JimpleBody {
441-
val clazz = Scene.v().classes.single { it.name == method.clazz.java.name }
442-
val signature = method.callable.signature
443-
val sootMethod = clazz.methods.single { it.pureJavaSignature == signature }
425+
fun jimpleBody(method: UtMethod<*>): JimpleBody {
426+
val className = method.clazz.java.name
427+
val clazz = Scene.v().classes.singleOrNull { it.name == className }
428+
?: error("No such $className found in the Scene")
429+
val signature = method.callable.signature
430+
val sootMethod = clazz.methods.singleOrNull { it.pureJavaSignature == signature }
431+
?: error("No such $signature found")
444432

445-
return sootMethod.jimpleBody()
446-
}
433+
return sootMethod.jimpleBody()
447434
}
448435

449436
fun JimpleBody.graph() = ExceptionalUnitGraph(this)

utbot-framework/src/test/kotlin/org/utbot/examples/AbstractTestCaseGeneratorTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import kotlin.reflect.KFunction5
6868
import mu.KotlinLogging
6969
import org.junit.jupiter.api.Assertions.assertTrue
7070
import org.utbot.framework.PathSelectorType
71+
import org.utbot.framework.plugin.api.jimpleBody
7172

7273
val logger = KotlinLogging.logger {}
7374

@@ -2343,7 +2344,7 @@ abstract class AbstractTestCaseGeneratorTest(
23432344
walk(utMethod, mockStrategy, additionalDependenciesClassPath)
23442345
}
23452346
testCase.summarize(searchDirectory)
2346-
val valueTestCase = testCase.toValueTestCase(UtBotTestCaseGenerator.jimpleBody(utMethod))
2347+
val valueTestCase = testCase.toValueTestCase(jimpleBody(utMethod))
23472348

23482349
assertTrue(testCase.errors.isEmpty()) {
23492350
"We have errors: ${
@@ -2478,7 +2479,7 @@ abstract class AbstractTestCaseGeneratorTest(
24782479
additionalDependenciesClassPath: String = ""
24792480
): MethodResult {
24802481
val testCase = executions(method, mockStrategy, additionalDependenciesClassPath)
2481-
val jimpleBody = UtBotTestCaseGenerator.jimpleBody(method)
2482+
val jimpleBody = jimpleBody(method)
24822483
val methodCoverage = methodCoverage(
24832484
method,
24842485
testCase.toValueTestCase(jimpleBody).executions,

0 commit comments

Comments
 (0)