Skip to content

Commit 67be3e6

Browse files
Refactor: remove graph parameter from Traverser constructor
1 parent dd06e5c commit 67be3e6

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
@@ -262,6 +262,7 @@ import sun.reflect.generics.reflectiveObjects.TypeVariableImpl
262262
import sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
263263
import java.lang.reflect.Method
264264
import java.util.concurrent.atomic.AtomicInteger
265+
import org.utbot.framework.plugin.api.jimpleBody
265266

266267
private val logger = KotlinLogging.logger {}
267268
val pathLogger = KotlinLogging.logger(logger.name + ".path")
@@ -313,14 +314,17 @@ private fun pathSelector(graph: InterProceduralUnitGraph, typeRegistry: TypeRegi
313314
class Traverser(
314315
private val controller: EngineController,
315316
private val methodUnderTest: UtMethod<*>,
316-
private val graph: ExceptionalUnitGraph,
317317
classpath: String,
318318
dependencyPaths: String,
319319
mockStrategy: MockStrategy = NO_MOCKS,
320320
chosenClassesToMockAlways: Set<ClassId>,
321321
private val solverTimeoutInMillis: Int = checkSolverTimeoutMillis
322322
) : UtContextInitializer() {
323323

324+
private val graph = jimpleBody(methodUnderTest).also {
325+
logger.trace { "JIMPLE for $methodUnderTest:\n$this" }
326+
}.graph()
327+
324328
private val methodUnderAnalysisStmts: Set<Stmt> = graph.stmts.toSet()
325329
private val visitedStmts: MutableSet<Stmt> = mutableSetOf()
326330
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),
@@ -414,31 +410,22 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
414410
else -> error("Cannot map API Mock Strategy model to Engine model: $mockStrategyApi")
415411
}
416412

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

432-
return graph
433-
}
415+
fun graph(method: UtMethod<*>): ExceptionalUnitGraph {
416+
val methodBody = jimpleBody(method)
417+
return methodBody.graph()
418+
}
434419

435-
fun jimpleBody(method: UtMethod<*>): JimpleBody {
436-
val clazz = Scene.v().classes.single { it.name == method.clazz.java.name }
437-
val signature = method.callable.signature
438-
val sootMethod = clazz.methods.single { it.pureJavaSignature == signature }
420+
fun jimpleBody(method: UtMethod<*>): JimpleBody {
421+
val className = method.clazz.java.name
422+
val clazz = Scene.v().classes.singleOrNull { it.name == className }
423+
?: error("No such $className found in the Scene")
424+
val signature = method.callable.signature
425+
val sootMethod = clazz.methods.singleOrNull { it.pureJavaSignature == signature }
426+
?: error("No such $signature found")
439427

440-
return sootMethod.jimpleBody()
441-
}
428+
return sootMethod.jimpleBody()
442429
}
443430

444431
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

@@ -2342,7 +2343,7 @@ abstract class AbstractTestCaseGeneratorTest(
23422343
walk(utMethod, mockStrategy, additionalDependenciesClassPath)
23432344
}
23442345
testCase.summarize(searchDirectory)
2345-
val valueTestCase = testCase.toValueTestCase(UtBotTestCaseGenerator.jimpleBody(utMethod))
2346+
val valueTestCase = testCase.toValueTestCase(jimpleBody(utMethod))
23462347

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

0 commit comments

Comments
 (0)