Skip to content

Commit ad605b7

Browse files
authored
Fix finding utbot-instrumentation-shadow.jar #2277 (#2287)
1 parent a8672d0 commit ad605b7

File tree

2 files changed

+19
-43
lines changed

2 files changed

+19
-43
lines changed

utbot-instrumentation-tests/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ dependencies {
3333
processResources {
3434
// We will extract this jar in `InstrumentedProcess` class.
3535
from(configurations.fetchInstrumentationJar) {
36-
into "instrumentation-lib"
36+
into "lib"
3737
}
3838
}

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/rd/InstrumentedProcess.kt

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ package org.utbot.instrumentation.rd
22

33
import com.jetbrains.rd.util.lifetime.Lifetime
44
import mu.KotlinLogging
5-
import org.utbot.common.currentProcessPid
5+
import org.utbot.common.JarUtils
66
import org.utbot.common.debug
7-
import org.utbot.common.firstOrNullResourceIS
87
import org.utbot.common.getPid
98
import org.utbot.common.measureTime
10-
import org.utbot.common.nameOfPackage
11-
import org.utbot.common.scanForResourcesContaining
12-
import org.utbot.common.utBotTempDirectory
139
import org.utbot.framework.UtSettings
1410
import org.utbot.framework.plugin.api.UtModel
1511
import org.utbot.framework.plugin.services.WorkingDirService
1612
import org.utbot.framework.process.AbstractRDProcessCompanion
17-
import org.utbot.instrumentation.agent.DynamicClassTransformer
1813
import org.utbot.instrumentation.instrumentation.Instrumentation
1914
import org.utbot.instrumentation.process.DISABLE_SANDBOX_OPTION
2015
import org.utbot.instrumentation.process.generated.AddPathsParams
@@ -27,7 +22,6 @@ import org.utbot.rd.ProcessWithRdServer
2722
import org.utbot.rd.exceptions.InstantProcessDeathException
2823
import org.utbot.rd.generated.LoggerModel
2924
import org.utbot.rd.generated.loggerModel
30-
import org.utbot.rd.loggers.UtRdKLogger
3125
import org.utbot.rd.loggers.setup
3226
import org.utbot.rd.onSchedulerBlocking
3327
import org.utbot.rd.startBlocking
@@ -37,43 +31,25 @@ import java.io.File
3731

3832
private val logger = KotlinLogging.logger { }
3933

40-
private const val UTBOT_INSTRUMENTATION = "utbot-instrumentation-shadow"
41-
private const val INSTRUMENTATION_LIB = "lib"
42-
43-
private fun tryFindInstrumentationJarInResources(): File? {
44-
logger.debug("Trying to find jar in the resources.")
45-
val tempDir = utBotTempDirectory.toFile()
46-
val unzippedJarName = "$UTBOT_INSTRUMENTATION-${currentProcessPid}.jar"
47-
val instrumentationJarFile = File(tempDir, unzippedJarName)
48-
49-
InstrumentedProcess::class.java.classLoader
50-
.firstOrNullResourceIS(INSTRUMENTATION_LIB) { resourcePath ->
51-
resourcePath.contains(UTBOT_INSTRUMENTATION) && resourcePath.endsWith(".jar")
52-
}
53-
?.use { input ->
54-
instrumentationJarFile.writeBytes(input.readBytes())
55-
} ?: return null
56-
return instrumentationJarFile
57-
}
58-
59-
private fun tryFindInstrumentationJarOnClasspath(): File? {
60-
logger.debug("Trying to find it in the classpath.")
61-
return InstrumentedProcess::class.java.classLoader
62-
.scanForResourcesContaining(DynamicClassTransformer::class.java.nameOfPackage)
63-
.firstOrNull {
64-
it.absolutePath.contains(UTBOT_INSTRUMENTATION) && it.extension == "jar"
65-
}
66-
}
34+
private const val UTBOT_INSTRUMENTATION_JAR_FILENAME = "utbot-instrumentation-shadow.jar"
6735

6836
private val instrumentationJarFile: File =
69-
logger.debug().measureTime({ "Finding $UTBOT_INSTRUMENTATION jar" } ) {
70-
tryFindInstrumentationJarInResources() ?: run {
71-
logger.debug("Failed to find jar in the resources.")
72-
tryFindInstrumentationJarOnClasspath()
73-
} ?: error("""
74-
Can't find file: $UTBOT_INSTRUMENTATION.jar.
75-
Make sure you added $UTBOT_INSTRUMENTATION.jar to the resources folder from gradle.
76-
""".trimIndent())
37+
logger.debug().measureTime({ "Finding $UTBOT_INSTRUMENTATION_JAR_FILENAME jar" } ) {
38+
try {
39+
JarUtils.extractJarFileFromResources(
40+
jarFileName = UTBOT_INSTRUMENTATION_JAR_FILENAME,
41+
jarResourcePath = "lib/$UTBOT_INSTRUMENTATION_JAR_FILENAME",
42+
targetDirectoryName = "utbot-instrumentation"
43+
)
44+
} catch (e: Exception) {
45+
throw IllegalStateException(
46+
"""
47+
Can't find file: $UTBOT_INSTRUMENTATION_JAR_FILENAME.
48+
Make sure you added $UTBOT_INSTRUMENTATION_JAR_FILENAME to the resources folder from gradle.
49+
""".trimIndent(),
50+
e
51+
)
52+
}
7753
}
7854

7955
class InstrumentedProcessInstantDeathException :

0 commit comments

Comments
 (0)