File tree Expand file tree Collapse file tree 2 files changed +50
-3
lines changed
main/kotlin/com/tschuchort/compiletesting
test/kotlin/com/tschuchort/compiletesting Expand file tree Collapse file tree 2 files changed +50
-3
lines changed Original file line number Diff line number Diff line change @@ -259,8 +259,11 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {
259259 val messages : String
260260 ) {
261261 /* * class loader to load the compile classes */
262- val classLoader = URLClassLoader (arrayOf(outputDirectory.toURI().toURL()),
263- this ::class .java.classLoader)
262+ val classLoader = URLClassLoader (
263+ // Include the original classpaths and the output directory to be able to load classes from dependencies.
264+ classpaths.plus(outputDirectory).map { it.toURI().toURL() }.toTypedArray(),
265+ this ::class .java.classLoader
266+ )
264267
265268 /* * The directory where only the final output class and resources files will be */
266269 val outputDirectory: File get() = classesDir
Original file line number Diff line number Diff line change @@ -859,6 +859,50 @@ class KotlinCompilationTests {
859859 )
860860 fakeJdkHome.delete()
861861 }
862-
862+
863+ @Test
864+ fun `the classLoader from a 2nd compilation can load classes from the first compilation` () {
865+ val kSource1 = SourceFile .kotlin(
866+ " KSource1.kt" , """
867+ package com.tschuchort.compiletesting
868+
869+ interface KSource1
870+ """
871+ )
872+
873+ val result = defaultCompilerConfig()
874+ .apply {
875+ sources = listOf (kSource1)
876+ inheritClassPath = true
877+ }
878+ .compile()
879+ .apply {
880+ assertThat(exitCode).isEqualTo(ExitCode .OK )
881+ assertThat(outputDirectory.listFilesRecursively().map { it.name }).contains(" KSource1.class" )
882+ }
883+
884+
885+ val kSource2 = SourceFile .kotlin(
886+ " KSource2.kt" , """
887+ package com.tschuchort.compiletesting
888+
889+ interface KSource2 : KSource1
890+ """
891+ )
892+
893+ defaultCompilerConfig()
894+ .apply {
895+ sources = listOf (kSource2)
896+ inheritClassPath = true
897+ classpaths + = result.outputDirectory
898+ }
899+ .compile()
900+ .apply {
901+ assertThat(exitCode).isEqualTo(ExitCode .OK )
902+ assertThat(outputDirectory.listFilesRecursively().map { it.name }).contains(" KSource2.class" )
903+ assertThat(classLoader.loadClass(" com.tschuchort.compiletesting.KSource2" )).isNotNull
904+ }
905+ }
906+
863907 class InheritedClass {}
864908}
You can’t perform that action at this time.
0 commit comments