Skip to content

Commit 07fa478

Browse files
Replacement of classLoaders: init
1 parent 7b1b42c commit 07fa478

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package org.utbot.cli.util
22

3+
import kotlinx.coroutines.runBlocking
4+
import org.utbot.framework.plugin.api.util.jcdb
5+
import org.utbot.jcdb.api.ClasspathSet
36
import java.io.File
47
import java.net.URL
58
import java.net.URLClassLoader
9+
import org.utbot.jcdb.api.CompilationDatabase
610

7-
private fun String.toUrl(): URL = File(this).toURI().toURL()
11+
private fun String.toUrl(): File = File(this)
812

9-
fun createClassLoader(classPath: String? = "", absoluteFileNameWithClasses: String? = null): URLClassLoader {
10-
val urlSet = mutableSetOf<URL>()
13+
fun createClassLoader(classPath: String? = "", absoluteFileNameWithClasses: String? = null): ClasspathSet {
14+
val urlSet = mutableSetOf<File>()
1115
classPath?.run {
1216
urlSet.addAll(this.split(File.pathSeparatorChar).map { it.toUrl() }.toMutableSet())
1317
}
1418
absoluteFileNameWithClasses?.run {
1519
urlSet.addAll(File(absoluteFileNameWithClasses).readLines().map { it.toUrl() }.toMutableSet())
1620
}
17-
val urls = urlSet.toTypedArray()
18-
return URLClassLoader(urls)
21+
return runBlocking {
22+
23+
}jcdb.classpathSet(urlSet.toList())
1924
}

utbot-framework-api/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies {
1212
// TODO do we really need apache commons?
1313
implementation group: 'org.apache.commons', name: 'commons-lang3', version: commons_lang_version
1414
implementation group: 'io.github.microutils', name: 'kotlin-logging', version: kotlin_logging_version
15-
implementation 'com.github.UnitTestBot:java-compilation-database:aad83be780'
15+
api 'com.github.UnitTestBot:java-compilation-database:aad83be780'
1616
}
1717

1818
shadowJar {

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@ import org.utbot.common.currentThreadInfo
55
import org.utbot.framework.plugin.api.util.UtContext.Companion.setUtContext
66
import kotlin.coroutines.CoroutineContext
77
import kotlinx.coroutines.ThreadContextElement
8+
import kotlinx.coroutines.runBlocking
9+
import org.utbot.jcdb.api.ClasspathSet
10+
import org.utbot.jcdb.api.CompilationDatabase
11+
import org.utbot.jcdb.compilationDatabase
12+
import java.io.File
813

914
val utContext: UtContext
1015
get() = UtContext.currentContext()
1116
?: error("No context is set. Please use `withUtContext() {...}` or `setUtContext().use {...}`. Thread: ${currentThreadInfo()}")
1217

18+
val jcdb = runBlocking {
19+
val jarsOrDirs = listOf<File>()
20+
compilationDatabase {
21+
this.jre = TODO()
22+
this.predefinedDirOrJars = jarsOrDirs
23+
}
24+
}
1325

14-
class UtContext(val classLoader: ClassLoader) : ThreadContextElement<UtContext?> {
26+
class UtContext(val classpathSet: ClasspathSet) : ThreadContextElement<UtContext?> {
1527

1628
// This StopWatch is used to respect bytecode transforming time while invoking with timeout
1729
var stopWatch: StopWatch? = null
1830
private set
1931

20-
constructor(classLoader: ClassLoader, stopWatch: StopWatch) : this(classLoader) {
21-
this.stopWatch = stopWatch
22-
}
23-
2432
override fun toString() = "UtContext(classLoader=$classLoader, hashCode=${hashCode()})"
2533

2634
private class Cookie(context: UtContext) : AutoCloseable {
@@ -70,4 +78,10 @@ class UtContext(val classLoader: ClassLoader) : ThreadContextElement<UtContext?>
7078
}
7179
}
7280

81+
fun test() {
82+
withUtContext(UtContext(classpath)) {
83+
utContext.classpathSet.findClassOrNull("kek")
84+
}
85+
}
86+
7387
inline fun <T> withUtContext(context: UtContext, block: () -> T): T = setUtContext(context).use { block() }

utbot-junit-contest/src/main/kotlin/org/utbot/contest/ClassUnderTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.utbot.framework.plugin.api.ClassId
66
import org.utbot.framework.plugin.api.util.utContext
77
import java.io.File
88
import java.nio.file.Paths
9+
import kotlin.reflect.KClass
910

1011
class ClassUnderTest(
1112
val classId: ClassId,
@@ -17,7 +18,7 @@ class ClassUnderTest(
1718
get() = classId.name
1819
val classLoader: ClassLoader
1920
get() = utContext.classLoader
20-
val kotlinClass
21+
val kotlinClass: KClass<*>
2122
get() = classLoader.loadClass(fqn).kotlin
2223

2324
/**

0 commit comments

Comments
 (0)