From 84ea5c68420a75c7b8401000dbe6dbf3ff2cd6d0 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 4 Nov 2015 16:26:07 +0100 Subject: @Inject all the things --- src/Utilities/DokkaModule.kt | 73 +++++++++++++++++++++++++++++++++++++++++ src/Utilities/GuiceModule.kt | 65 ------------------------------------ src/Utilities/ServiceLocator.kt | 27 ++------------- 3 files changed, 76 insertions(+), 89 deletions(-) create mode 100644 src/Utilities/DokkaModule.kt delete mode 100644 src/Utilities/GuiceModule.kt (limited to 'src/Utilities') diff --git a/src/Utilities/DokkaModule.kt b/src/Utilities/DokkaModule.kt new file mode 100644 index 00000000..3b2d26f1 --- /dev/null +++ b/src/Utilities/DokkaModule.kt @@ -0,0 +1,73 @@ +package org.jetbrains.dokka.Utilities + +import com.google.inject.Binder +import com.google.inject.Module +import com.google.inject.Provider +import com.google.inject.name.Names +import org.jetbrains.dokka.* +import org.jetbrains.dokka.Formats.FormatDescriptor +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import java.io.File + +class DokkaModule(val environment: AnalysisEnvironment, + val options: DocumentationOptions, + val logger: DokkaLogger) : Module { + override fun configure(binder: Binder) { + binder.bind(File::class.java).annotatedWith(Names.named("outputDir")).toInstance(File(options.outputDir)) + + binder.bindNameAnnotated("singleFolder") + binder.bindNameAnnotated("singleFolder") + binder.bindNameAnnotated("folders") + binder.bindNameAnnotated("folders") + + // defaults + binder.bind(LocationService::class.java).to(FoldersLocationService::class.java) + binder.bind(FileLocationService::class.java).to(FoldersLocationService::class.java) + binder.bind(LanguageService::class.java).to(KotlinLanguageService::class.java) + + binder.bind(HtmlTemplateService::class.java).toProvider(object : Provider { + override fun get(): HtmlTemplateService = HtmlTemplateService.default("/dokka/styles/style.css") + }) + + binder.registerCategory("language") + binder.registerCategory("outline") + binder.registerCategory("format") + binder.registerCategory("generator") + + val descriptor = ServiceLocator.lookup("format", options.outputFormat) + + descriptor.outlineServiceClass?.let { clazz -> + binder.bind(OutlineFormatService::class.java).to(clazz.java) + } + descriptor.formatServiceClass?.let { clazz -> + binder.bind(FormatService::class.java).to(clazz.java) + } + binder.bind().to(descriptor.packageDocumentationBuilderClass.java) + binder.bind().to(descriptor.javaDocumentationBuilderClass.java) + + binder.bind().to(descriptor.generatorServiceClass.java) + + val coreEnvironment = environment.createCoreEnvironment() + binder.bind().toInstance(coreEnvironment) + + val dokkaResolutionFacade = environment.createResolutionFacade(coreEnvironment) + binder.bind().toInstance(dokkaResolutionFacade) + + binder.bind().toInstance(options) + binder.bind().toInstance(logger) + } +} + +private inline fun Binder.registerCategory(category: String) { + ServiceLocator.allServices(category).forEach { + @Suppress("UNCHECKED_CAST") + bind(T::class.java).annotatedWith(Names.named(it.name)).to(T::class.java.classLoader.loadClass(it.className) as Class) + } +} + +private inline fun Binder.bindNameAnnotated(name: String) { + bind(Base::class.java).annotatedWith(Names.named(name)).to(T::class.java) +} + + +inline fun Binder.bind() = bind(T::class.java) diff --git a/src/Utilities/GuiceModule.kt b/src/Utilities/GuiceModule.kt deleted file mode 100644 index e852ae19..00000000 --- a/src/Utilities/GuiceModule.kt +++ /dev/null @@ -1,65 +0,0 @@ -package org.jetbrains.dokka.Utilities - -import com.google.inject.Binder -import com.google.inject.Module -import com.google.inject.Provider -import com.google.inject.name.Names -import com.google.inject.util.Providers -import org.jetbrains.dokka.* -import org.jetbrains.dokka.Formats.FormatDescriptor -import java.io.File - -class GuiceModule(val config: DokkaGenerator) : Module { - override fun configure(binder: Binder) { - binder.bind(DokkaGenerator::class.java).toInstance(config) - binder.bind(File::class.java).annotatedWith(Names.named("outputDir")).toInstance(File(config.outputDir)) - - binder.bindNameAnnotated("singleFolder") - binder.bindNameAnnotated("singleFolder") - binder.bindNameAnnotated("folders") - binder.bindNameAnnotated("folders") - - // defaults - binder.bind(LocationService::class.java).to(FoldersLocationService::class.java) - binder.bind(FileLocationService::class.java).to(FoldersLocationService::class.java) - binder.bind(LanguageService::class.java).to(KotlinLanguageService::class.java) - - binder.bind(HtmlTemplateService::class.java).toProvider(object : Provider { - override fun get(): HtmlTemplateService = HtmlTemplateService.default("/dokka/styles/style.css") - }) - - binder.registerCategory("language") - binder.registerCategory("outline") - binder.registerCategory("format") - binder.registerCategory("generator") - - val descriptor = ServiceLocator.lookup("format", config.outputFormat, config) - - descriptor.outlineServiceClass?.let { clazz -> - binder.bind(OutlineFormatService::class.java).to(clazz) - } - descriptor.formatServiceClass?.let { clazz -> - binder.bind(FormatService::class.java).to(clazz) - } - if (descriptor.packageDocumentationBuilderServiceClass != null) { - binder.bind(PackageDocumentationBuilder::class.java).to(descriptor.packageDocumentationBuilderServiceClass) - } else { - binder.bind(PackageDocumentationBuilder::class.java).toProvider(Providers.of(null)) - } - - binder.bind(Generator::class.java).to(descriptor.generatorServiceClass) - } - -} - -private inline fun Binder.registerCategory(category: String) { - ServiceLocator.allServices(category).forEach { - @Suppress("UNCHECKED_CAST") - bind(T::class.java).annotatedWith(Names.named(it.name)).to(T::class.java.classLoader.loadClass(it.className) as Class) - } -} - -private inline fun Binder.bindNameAnnotated(name: String) { - bind(Base::class.java).annotatedWith(Names.named(name)).to(T::class.java) -} - diff --git a/src/Utilities/ServiceLocator.kt b/src/Utilities/ServiceLocator.kt index 57d2f47b..92aa9ba4 100644 --- a/src/Utilities/ServiceLocator.kt +++ b/src/Utilities/ServiceLocator.kt @@ -1,6 +1,5 @@ package org.jetbrains.dokka.Utilities -import org.jetbrains.dokka.DokkaGenerator import java.io.File import java.util.* import java.util.jar.JarFile @@ -11,12 +10,11 @@ data class ServiceDescriptor(val name: String, val category: String, val descrip class ServiceLookupException(message: String) : Exception(message) public object ServiceLocator { - public fun lookup(clazz: Class, category: String, implementationName: String, conf: DokkaGenerator): T { + public fun lookup(clazz: Class, category: String, implementationName: String): T { val descriptor = lookupDescriptor(category, implementationName) val loadedClass = javaClass.classLoader.loadClass(descriptor.className) val constructor = loadedClass.constructors - .filter { it.parameterTypes.isEmpty() || (it.parameterTypes.size == 1 && conf.javaClass.isInstance(it.parameterTypes[0])) } - .sortedByDescending { it.parameterTypes.size } + .filter { it.parameterTypes.isEmpty() } .firstOrNull() ?: throw ServiceLookupException("Class ${descriptor.className} has no corresponding constructor") val implementationRawType: Any = if (constructor.parameterTypes.isEmpty()) constructor.newInstance() else constructor.newInstance(constructor) @@ -29,19 +27,6 @@ public object ServiceLocator { return implementationRawType as T } - public fun lookupClass(clazz: Class, category: String, implementationName: String): Class = lookupDescriptor(category, implementationName).className.let { className -> - javaClass.classLoader.loadClass(className).let { loaded -> - if (!clazz.isAssignableFrom(loaded)) { - throw ServiceLookupException("Class $className is not a subtype of ${clazz.name}") - } - - @Suppress("UNCHECKED_CAST") - val casted = loaded as Class - - casted - } - } - private fun lookupDescriptor(category: String, implementationName: String): ServiceDescriptor { val properties = javaClass.classLoader.getResourceAsStream("dokka/$category/$implementationName.properties")?.use { stream -> Properties().let { properties -> @@ -81,13 +66,7 @@ public object ServiceLocator { } ?: emptyList() } -public inline fun ServiceLocator.lookup(category: String, implementationName: String, conf: DokkaGenerator): T = lookup(T::class.java, category, implementationName, conf) -public inline fun ServiceLocator.lookupClass(category: String, implementationName: String): Class = lookupClass(T::class.java, category, implementationName) -public inline fun ServiceLocator.lookupOrNull(category: String, implementationName: String, conf: DokkaGenerator): T? = try { - lookup(T::class.java, category, implementationName, conf) -} catch (any: Throwable) { - null -} +public inline fun ServiceLocator.lookup(category: String, implementationName: String): T = lookup(T::class.java, category, implementationName) private val ZipEntry.fileName: String get() = name.substringAfterLast("/", name) -- cgit