Skip to content

Commit aeed41d

Browse files
committed
Refactor RD processes
1 parent f1f36a5 commit aeed41d

File tree

15 files changed

+231
-255
lines changed

15 files changed

+231
-255
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
303303
* @see runInstrumentedProcessWithDebug
304304
* @see org.utbot.framework.process.SpringAnalyzerProcess
305305
*/
306-
var runSpringAnalyzerProcessWithDebug by getBooleanProperty(false)
306+
var runSpringAnalyzerProcessWithDebug by getBooleanProperty(true)
307307

308308
/**
309309
* The spring analyzer process JDWP agent's port.
@@ -335,15 +335,16 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
335335
/**
336336
* If true, runs the instrumented process with the ability to attach a debugger.
337337
*
338-
* To debug the instrumented process, set the breakpoint in the instrumentedProcessRunner.start() line
338+
* To debug the instrumented process, set the breakpoint in the
339+
* [org.utbot.instrumentation.rd.InstrumentedProcess.Companion.invoke]
339340
* and in the instrumented process's main function and run the main process.
340341
* Then run the remote JVM debug configuration in IDEA.
341342
* If you see the message in console about successful connection, then
342343
* the debugger is attached successfully.
343344
* Now you can put the breakpoints in the instrumented process and debug
344345
* both processes simultaneously.
345346
*
346-
* @see [org.utbot.instrumentation.process.InstrumentedProcessRunner.cmds]
347+
* @see [org.utbot.instrumentation.rd.InstrumentedProcess.Companion.invoke]
347348
*/
348349
var runInstrumentedProcessWithDebug by getBooleanProperty(false)
349350
// endregion
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.utbot.framework.process
2+
3+
import org.utbot.common.osSpecificJavaExecutable
4+
import org.utbot.framework.plugin.services.JdkInfoService
5+
import org.utbot.rd.rdPortArgument
6+
import java.io.File
7+
import kotlin.io.path.pathString
8+
9+
private val javaExecutablePathString =
10+
JdkInfoService.provide().path.resolve("bin${File.separatorChar}${osSpecificJavaExecutable()}")
11+
12+
abstract class AbstractRDProcessCompanion(
13+
private val debugPort: Int,
14+
private val runWithDebug: Boolean,
15+
private val suspendExecutionInDebugMode: Boolean,
16+
private val processSpecificCommandLineArgs: List<String>
17+
) {
18+
protected fun obtainProcessCommandLine(port: Int): List<String> = buildList {
19+
addAll(obtainCommonProcessCommandLineArgs())
20+
addAll(processSpecificCommandLineArgs)
21+
add(rdPortArgument(port))
22+
}
23+
24+
private fun obtainCommonProcessCommandLineArgs(): List<String> = buildList {
25+
val suspendValue = if (suspendExecutionInDebugMode) "y" else "n"
26+
val debugArgument =
27+
"-agentlib:jdwp=transport=dt_socket,server=n,suspend=${suspendValue},quiet=y,address=$debugPort"
28+
.takeIf { runWithDebug }
29+
30+
add(javaExecutablePathString.pathString)
31+
val javaVersionSpecificArgs = OpenModulesContainer.javaVersionSpecificArguments
32+
if (javaVersionSpecificArgs.isNotEmpty()) {
33+
addAll(javaVersionSpecificArgs)
34+
}
35+
debugArgument?.let { add(it) }
36+
}
37+
}

utbot-framework-api/src/main/kotlin/org/utbot/framework/process/CommonProcessArgs.kt

Lines changed: 0 additions & 29 deletions
This file was deleted.

utbot-framework/src/main/kotlin/org/utbot/framework/process/EngineProcessMain.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
8888
params.config,
8989
// TODO remove once spring-analyzer learns to find resources on its own, temporarily leaving it here for testing with hardcoded absolute paths
9090
propertyFilesPaths = emptyList(),
91-
xmlConfigurationPaths = emptyList()
91+
xmlConfigurationPaths = emptyList(),
92+
params.useSpringAnalyzer
9293
).toTypedArray()
9394
}
9495
springAnalyzerProcess.terminate()

utbot-framework/src/main/kotlin/org/utbot/framework/process/SpringAnalyzerProcess.kt

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import org.utbot.rd.generated.loggerModel
1414
import org.utbot.rd.loggers.UtRdKLogger
1515
import org.utbot.rd.loggers.setup
1616
import org.utbot.rd.onSchedulerBlocking
17-
import org.utbot.rd.rdPortArgument
1817
import org.utbot.rd.startBlocking
1918
import org.utbot.rd.startUtProcessWithRdServer
2019
import org.utbot.rd.terminateOnException
@@ -24,58 +23,55 @@ import org.utbot.rd.generated.springAnalyzerProcessModel
2423
import java.nio.file.Files
2524

2625
class SpringAnalyzerProcessInstantDeathException :
27-
InstantProcessDeathException(UtSettings.springAnalyzerProcessDebugPort, UtSettings.runSpringAnalyzerProcessWithDebug)
26+
InstantProcessDeathException(
27+
UtSettings.springAnalyzerProcessDebugPort,
28+
UtSettings.runSpringAnalyzerProcessWithDebug
29+
)
2830

2931
private const val SPRING_ANALYZER_JAR_FILENAME = "utbot-spring-analyzer-shadow.jar"
3032
private const val SPRING_ANALYZER_JAR_PATH = "lib/$SPRING_ANALYZER_JAR_FILENAME"
3133
private const val UNKNOWN_MODIFICATION_TIME = 0L
3234
private val logger = KotlinLogging.logger {}
3335
private val rdLogger = UtRdKLogger(logger, "")
3436

37+
private val jarFile = Files.createDirectories(utBotTempDirectory.toFile().resolve("spring-analyzer").toPath())
38+
.toFile().resolve(SPRING_ANALYZER_JAR_FILENAME).also { jarFile ->
39+
val resource = SpringAnalyzerProcess::class.java.classLoader.getResource(SPRING_ANALYZER_JAR_PATH)
40+
?: error("Unable to find \"$SPRING_ANALYZER_JAR_PATH\" in resources, make sure it's on the classpath")
41+
val resourceConnection = resource.openConnection()
42+
val lastResourceModification = try {
43+
resourceConnection.lastModified
44+
} finally {
45+
resourceConnection.getInputStream().close()
46+
}
47+
if (
48+
!jarFile.exists() ||
49+
jarFile.lastModified() == UNKNOWN_MODIFICATION_TIME ||
50+
lastResourceModification == UNKNOWN_MODIFICATION_TIME ||
51+
jarFile.lastModified() < lastResourceModification
52+
)
53+
FileUtils.copyURLToFile(resource, jarFile)
54+
}
55+
3556
class SpringAnalyzerProcess private constructor(
3657
rdProcess: ProcessWithRdServer
3758
) : ProcessWithRdServer by rdProcess {
3859

39-
companion object {
40-
private val jarFile by lazy {
41-
Files.createDirectories(utBotTempDirectory.toFile().resolve("spring-analyzer").toPath())
42-
.toFile().resolve(SPRING_ANALYZER_JAR_FILENAME).also { jarFile ->
43-
val resource = this::class.java.classLoader.getResource(SPRING_ANALYZER_JAR_PATH)
44-
?: error("Unable to find \"$SPRING_ANALYZER_JAR_PATH\" in resources, make sure it's on the classpath")
45-
val resourceConnection = resource.openConnection()
46-
val lastResourceModification = try {
47-
resourceConnection.lastModified
48-
} finally {
49-
resourceConnection.getInputStream().close()
50-
}
51-
if (
52-
!jarFile.exists() ||
53-
jarFile.lastModified() == UNKNOWN_MODIFICATION_TIME ||
54-
lastResourceModification == UNKNOWN_MODIFICATION_TIME ||
55-
jarFile.lastModified() < lastResourceModification
56-
)
57-
FileUtils.copyURLToFile(resource, jarFile)
58-
}
59-
}
60-
61-
private fun obtainSpringAnalyzerProcessCommandLine(port: Int): List<String> {
62-
return CommonProcessArgs.obtainCommonProcessCommandLineArgs(
63-
debugPort = UtSettings.springAnalyzerProcessDebugPort,
64-
runWithDebug = UtSettings.runSpringAnalyzerProcessWithDebug,
65-
suspendExecutionInDebugMode = UtSettings.suspendSpringAnalyzerProcessExecutionInDebugMode,
66-
) + listOf(
67-
"-Dorg.apache.commons.logging.LogFactory=org.utbot.spring.loggers.RDApacheCommonsLogFactory",
68-
"-jar",
69-
jarFile.path,
70-
rdPortArgument(port)
71-
)
72-
}
73-
60+
companion object : AbstractRDProcessCompanion(
61+
debugPort = UtSettings.springAnalyzerProcessDebugPort,
62+
runWithDebug = UtSettings.runSpringAnalyzerProcessWithDebug,
63+
suspendExecutionInDebugMode = UtSettings.suspendSpringAnalyzerProcessExecutionInDebugMode,
64+
processSpecificCommandLineArgs = listOf(
65+
"-Dorg.apache.commons.logging.LogFactory=org.utbot.spring.loggers.RDApacheCommonsLogFactory",
66+
"-jar",
67+
jarFile.path
68+
)
69+
) {
7470
fun createBlocking() = runBlocking { SpringAnalyzerProcess() }
7571

7672
suspend operator fun invoke(): SpringAnalyzerProcess = LifetimeDefinition().terminateOnException { lifetime ->
7773
val rdProcess = startUtProcessWithRdServer(lifetime) { port ->
78-
val cmd = obtainSpringAnalyzerProcessCommandLine(port)
74+
val cmd = obtainProcessCommandLine(port)
7975
val process = ProcessBuilder(cmd)
8076
.directory(Files.createTempDirectory(utBotTempDirectory, "spring-analyzer").toFile())
8177
.start()
@@ -100,13 +96,15 @@ class SpringAnalyzerProcess private constructor(
10096
classpath: List<String>,
10197
configuration: String,
10298
propertyFilesPaths: List<String>,
103-
xmlConfigurationPaths: List<String>
99+
xmlConfigurationPaths: List<String>,
100+
useSpringAnalyzer: Boolean
104101
): List<String> {
105102
val params = SpringAnalyzerParams(
106103
classpath.toTypedArray(),
107104
configuration,
108105
propertyFilesPaths.toTypedArray(),
109-
xmlConfigurationPaths.toTypedArray()
106+
xmlConfigurationPaths.toTypedArray(),
107+
useSpringAnalyzer
110108
)
111109
val result = springAnalyzerModel.analyze.startBlocking(params)
112110
return result.beanTypes.toList()

utbot-framework/src/main/kotlin/org/utbot/framework/process/generated/EngineProcessModel.Generated.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class EngineProcessModel private constructor(
7272

7373
private val __StringArraySerializer = FrameworkMarshallers.String.array()
7474

75-
const val serializationHash = -5097607716462442558L
75+
const val serializationHash = 1955031277042475752L
7676

7777
}
7878
override val serializersOwner: ISerializersOwner get() = EngineProcessModel
@@ -179,7 +179,7 @@ val IProtocol.engineProcessModel get() = getOrCreateExtension(EngineProcessModel
179179

180180

181181
/**
182-
* #### Generated from [EngineProcessModel.kt:103]
182+
* #### Generated from [EngineProcessModel.kt:104]
183183
*/
184184
data class FindMethodParamNamesArguments (
185185
val classId: ByteArray,
@@ -242,7 +242,7 @@ data class FindMethodParamNamesArguments (
242242

243243

244244
/**
245-
* #### Generated from [EngineProcessModel.kt:107]
245+
* #### Generated from [EngineProcessModel.kt:108]
246246
*/
247247
data class FindMethodParamNamesResult (
248248
val paramNames: ByteArray
@@ -299,7 +299,7 @@ data class FindMethodParamNamesResult (
299299

300300

301301
/**
302-
* #### Generated from [EngineProcessModel.kt:96]
302+
* #### Generated from [EngineProcessModel.kt:97]
303303
*/
304304
data class FindMethodsInClassMatchingSelectedArguments (
305305
val classId: ByteArray,
@@ -362,7 +362,7 @@ data class FindMethodsInClassMatchingSelectedArguments (
362362

363363

364364
/**
365-
* #### Generated from [EngineProcessModel.kt:100]
365+
* #### Generated from [EngineProcessModel.kt:101]
366366
*/
367367
data class FindMethodsInClassMatchingSelectedResult (
368368
val executableIds: ByteArray
@@ -587,7 +587,7 @@ data class GenerateResult (
587587

588588

589589
/**
590-
* #### Generated from [EngineProcessModel.kt:115]
590+
* #### Generated from [EngineProcessModel.kt:116]
591591
*/
592592
data class GenerateTestReportArgs (
593593
val eventLogMessage: String?,
@@ -680,7 +680,7 @@ data class GenerateTestReportArgs (
680680

681681

682682
/**
683-
* #### Generated from [EngineProcessModel.kt:124]
683+
* #### Generated from [EngineProcessModel.kt:125]
684684
*/
685685
data class GenerateTestReportResult (
686686
val notifyMessage: String,
@@ -753,7 +753,8 @@ data class GenerateTestReportResult (
753753
*/
754754
data class GetSpringBeanQualifiedNamesParams (
755755
val classpath: Array<String>,
756-
val config: String
756+
val config: String,
757+
val useSpringAnalyzer: Boolean
757758
) : IPrintable {
758759
//companion
759760

@@ -764,12 +765,14 @@ data class GetSpringBeanQualifiedNamesParams (
764765
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): GetSpringBeanQualifiedNamesParams {
765766
val classpath = buffer.readArray {buffer.readString()}
766767
val config = buffer.readString()
767-
return GetSpringBeanQualifiedNamesParams(classpath, config)
768+
val useSpringAnalyzer = buffer.readBool()
769+
return GetSpringBeanQualifiedNamesParams(classpath, config, useSpringAnalyzer)
768770
}
769771

770772
override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: GetSpringBeanQualifiedNamesParams) {
771773
buffer.writeArray(value.classpath) { buffer.writeString(it) }
772774
buffer.writeString(value.config)
775+
buffer.writeBool(value.useSpringAnalyzer)
773776
}
774777

775778

@@ -787,6 +790,7 @@ data class GetSpringBeanQualifiedNamesParams (
787790

788791
if (!(classpath contentDeepEquals other.classpath)) return false
789792
if (config != other.config) return false
793+
if (useSpringAnalyzer != other.useSpringAnalyzer) return false
790794

791795
return true
792796
}
@@ -795,6 +799,7 @@ data class GetSpringBeanQualifiedNamesParams (
795799
var __r = 0
796800
__r = __r*31 + classpath.contentDeepHashCode()
797801
__r = __r*31 + config.hashCode()
802+
__r = __r*31 + useSpringAnalyzer.hashCode()
798803
return __r
799804
}
800805
//pretty print
@@ -803,6 +808,7 @@ data class GetSpringBeanQualifiedNamesParams (
803808
printer.indent {
804809
print("classpath = "); classpath.print(printer); println()
805810
print("config = "); config.print(printer); println()
811+
print("useSpringAnalyzer = "); useSpringAnalyzer.print(printer); println()
806812
}
807813
printer.print(")")
808814
}
@@ -875,7 +881,7 @@ data class JdkInfo (
875881

876882

877883
/**
878-
* #### Generated from [EngineProcessModel.kt:91]
884+
* #### Generated from [EngineProcessModel.kt:92]
879885
*/
880886
data class MethodDescription (
881887
val name: String,
@@ -1292,7 +1298,7 @@ data class TestGeneratorParams (
12921298

12931299

12941300
/**
1295-
* #### Generated from [EngineProcessModel.kt:110]
1301+
* #### Generated from [EngineProcessModel.kt:111]
12961302
*/
12971303
data class WriteSarifReportArguments (
12981304
val testSetsId: Long,

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/ConcreteExecutor.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import org.utbot.framework.plugin.api.FieldId
2727
import org.utbot.framework.plugin.api.util.UtContext
2828
import org.utbot.framework.plugin.api.util.signature
2929
import org.utbot.instrumentation.instrumentation.Instrumentation
30-
import org.utbot.instrumentation.process.InstrumentedProcessRunner
3130
import org.utbot.instrumentation.process.generated.ComputeStaticFieldParams
3231
import org.utbot.instrumentation.process.generated.InvokeMethodCommandParams
3332
import org.utbot.instrumentation.rd.InstrumentedProcess
@@ -112,7 +111,6 @@ class ConcreteExecutor<TIResult, TInstrumentation : Instrumentation<TIResult>> p
112111
internal val pathsToUserClasses: String
113112
) : Closeable, Executor<TIResult> {
114113
private val ldef: LifetimeDefinition = LifetimeDefinition()
115-
private val instrumentedProcessRunner: InstrumentedProcessRunner = InstrumentedProcessRunner()
116114

117115
companion object {
118116

@@ -161,7 +159,6 @@ class ConcreteExecutor<TIResult, TInstrumentation : Instrumentation<TIResult>> p
161159
if (proc == null || !proc.lifetime.isAlive) {
162160
proc = InstrumentedProcess(
163161
ldef,
164-
instrumentedProcessRunner,
165162
instrumentation,
166163
pathsToUserClasses,
167164
classLoader

0 commit comments

Comments
 (0)