Skip to content

Commit 89908ac

Browse files
committed
Get host classpath under JDK 9 or later
1 parent 04b9a40 commit 89908ac

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

src/test/kotlin/KotlinCompilation.kt

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.tschuchort.kotlinelements
16+
package com.tschuchort.compiletest
1717

1818
import okio.Buffer
1919
import okio.buffer
@@ -61,7 +61,7 @@ class KotlinCompilation(
6161
/** Source files to be compiled */
6262
val sources: List<SourceFile> = emptyList(),
6363
/** Services to be passed to kapt */
64-
val services: List<Service<*,*>> = emptyList(),
64+
val services: List<Service<*, *>> = emptyList(),
6565
/**
6666
* Path to the JDK to be used
6767
*
@@ -87,7 +87,8 @@ class KotlinCompilation(
8787
toolsJar: File = findToolsJar(findJavaHome()),
8888
/** Inherit classpath from calling process */
8989
inheritClassPath: Boolean = false,
90-
correctErrorTypes: Boolean = false,
90+
val jvmTarget: String? = null,
91+
correctErrorTypes: Boolean = true,
9192
val skipRuntimeVersionCheck: Boolean = false,
9293
val verbose: Boolean = false,
9394
val suppressWarnings: Boolean = false,
@@ -147,8 +148,11 @@ class KotlinCompilation(
147148

148149
add(servicesJar.absolutePath)
149150

150-
if(inheritClassPath)
151-
addAll(getHostProcessClasspathFiles().map(File::getAbsolutePath))
151+
if(inheritClassPath) {
152+
val hostClasspaths = getHostProcessClasspaths().map(File::getAbsolutePath)
153+
addAll(hostClasspaths)
154+
}
155+
152156
}
153157

154158
/** Returns arguments necessary to enable and configure kapt3. */
@@ -205,6 +209,10 @@ class KotlinCompilation(
205209
kotlinHome = it.absolutePath
206210
}
207211

212+
this@KotlinCompilation.jvmTarget?.let {
213+
jvmTarget = it
214+
}
215+
208216
verbose = this@KotlinCompilation.verbose
209217
suppressWarnings = this@KotlinCompilation.suppressWarnings
210218
allWarningsAsErrors = this@KotlinCompilation.allWarningsAsErrors
@@ -225,29 +233,52 @@ class KotlinCompilation(
225233

226234

227235
/** Returns the files on the host process' classpath. */
228-
private fun getHostProcessClasspathFiles(): List<File> {
229-
val classLoader: ClassLoader = SmokeTests::class.java.classLoader
230-
if (classLoader !is URLClassLoader) {
231-
throw UnsupportedOperationException("unable to extract classpath from $classLoader")
236+
private fun getHostProcessClasspaths(): List<File> {
237+
val classLoader: ClassLoader = this::class.java.classLoader
238+
if (classLoader is URLClassLoader) {
239+
return classLoader.urLs.map { url ->
240+
if (url.protocol != "file") {
241+
throw UnsupportedOperationException("unable to handle classpath element $url")
242+
}
243+
244+
File(URLDecoder.decode(url.path, "UTF-8"))
245+
}
232246
}
247+
else {
248+
val jarsOrZips = classLoader.getResources("META-INF").toList().mapNotNull {
249+
if(it.path.matches(Regex("file:/.*?(\\.jar|\\.zip)!/META-INF")))
250+
it.path.removeSurrounding("file:/", "!/META-INF")
251+
else
252+
null
253+
}
254+
255+
val dirs = classLoader.getResources("").toList().map { it.path }
256+
val wildcards = classLoader.getResources("*").toList().map { it.path }
233257

234-
return classLoader.urLs.map { url ->
235-
if (url.protocol != "file") {
236-
throw UnsupportedOperationException("unable to handle classpath element $url")
258+
val result = (jarsOrZips + dirs + wildcards).map {
259+
// paths may contain percent-encoded characters like %20 for space
260+
File(URLDecoder.decode(it, "UTF-8"))
237261
}
238262

239-
File(URLDecoder.decode(url.path, "UTF-8"))
240-
}
263+
return result
264+
265+
/*val x = this::class.java.classLoader.run {
266+
unnamedModule.packages.map {
267+
val res = getResource(it)
268+
res
269+
}
270+
}*/
271+
}
241272
}
242273

243274
/** Returns the path to the kotlin-annotation-processing .jar file. */
244275
private fun getKapt3Jar(): File {
245-
return getHostProcessClasspathFiles().firstOrNull { file ->
276+
return getHostProcessClasspaths().firstOrNull { file ->
246277
file.name.startsWith("kotlin-annotation-processing-embeddable")
247278
}
248279
?: throw IllegalStateException(
249280
"no kotlin-annotation-processing-embeddable jar on classpath:\n " +
250-
"${getHostProcessClasspathFiles().joinToString(separator = "\n ")}}")
281+
"${getHostProcessClasspaths().joinToString(separator = "\n ")}}")
251282
}
252283

253284
/**

0 commit comments

Comments
 (0)