ServiceLoaderLite: Support paths with spaces (KT-28527)
This commit is contained in:
@@ -11,7 +11,10 @@ import java.lang.Character.isJavaIdentifierPart
|
|||||||
import java.lang.Character.isJavaIdentifierStart
|
import java.lang.Character.isJavaIdentifierStart
|
||||||
import java.lang.IllegalArgumentException
|
import java.lang.IllegalArgumentException
|
||||||
import java.lang.RuntimeException
|
import java.lang.RuntimeException
|
||||||
|
import java.lang.UnsupportedOperationException
|
||||||
import java.net.URLClassLoader
|
import java.net.URLClassLoader
|
||||||
|
import java.nio.file.FileSystemNotFoundException
|
||||||
|
import java.nio.file.Paths
|
||||||
import java.util.zip.ZipFile
|
import java.util.zip.ZipFile
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,9 +35,13 @@ object ServiceLoaderLite {
|
|||||||
*/
|
*/
|
||||||
fun <Service> loadImplementations(service: Class<out Service>, classLoader: URLClassLoader): List<Service> {
|
fun <Service> loadImplementations(service: Class<out Service>, classLoader: URLClassLoader): List<Service> {
|
||||||
val files = classLoader.urLs.map { url ->
|
val files = classLoader.urLs.map { url ->
|
||||||
if (url.protocol.toLowerCase() != "file") throw IllegalArgumentException("Only local files are supported, got $url")
|
try {
|
||||||
val path = url.path.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("Path is empty for $url")
|
Paths.get(url.toURI()).toFile()
|
||||||
File(path)
|
} catch (e: FileSystemNotFoundException) {
|
||||||
|
throw IllegalArgumentException("Only local URLs are supported, got ${url.protocol}")
|
||||||
|
} catch (e: UnsupportedOperationException) {
|
||||||
|
throw IllegalArgumentException("Only local URLs are supported, got ${url.protocol}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val implementations = mutableListOf<Service>()
|
val implementations = mutableListOf<Service>()
|
||||||
|
|||||||
+7
@@ -31,6 +31,13 @@ class ServiceLoaderLiteTestWithClassLoader : AbstractServiceLoaderLiteTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testDirWithSpaces() {
|
||||||
|
classLoaderTest("test dir", impls<Intf>(NestedComponent::class), clazz<NestedComponent>()) { classLoader ->
|
||||||
|
val impls = ServiceLoaderLite.loadImplementations<Intf>(classLoader)
|
||||||
|
assertTrue(impls.single() is NestedComponent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun testNestedComponent() {
|
fun testNestedComponent() {
|
||||||
classLoaderTest("test", impls<Intf>(NestedComponent::class), clazz<NestedComponent>()) { classLoader ->
|
classLoaderTest("test", impls<Intf>(NestedComponent::class), clazz<NestedComponent>()) { classLoader ->
|
||||||
val impls = ServiceLoaderLite.loadImplementations<Intf>(classLoader)
|
val impls = ServiceLoaderLite.loadImplementations<Intf>(classLoader)
|
||||||
|
|||||||
Reference in New Issue
Block a user