All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spi.registry.KotlinCodeGenerationServiceRepository.kt Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
package io.toolisticon.kotlin.generation.spi.registry

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.spi.*
import io.toolisticon.kotlin.generation.spi.processor.KotlinCodeGenerationProcessorList
import io.toolisticon.kotlin.generation.spi.strategy.KotlinCodeGenerationStrategyList
import mu.KLogging
import java.util.*
import kotlin.reflect.KClass

/**
 * Holds all implementation instances of [KotlinCodeGenerationStrategy] and [KotlinCodeGenerationProcessor].
 *
 * Main Use-Case is loading these instances via [ServiceLoader] (implemented in [KotlinCodeGenerationServiceLoader],
 * or short by [io.toolisticon.kotlin.generation.KotlinCodeGeneration.spi.registry].
 */
@ExperimentalKotlinPoetApi
class KotlinCodeGenerationServiceRepository(
  override val contextTypeUpperBound: KClass<*>,
  override val processors: KotlinCodeGenerationProcessorList = KotlinCodeGenerationProcessorList(),
  override val strategies: KotlinCodeGenerationStrategyList = KotlinCodeGenerationStrategyList(),
) : KotlinCodeGenerationSpiRegistry {
  companion object : KLogging() {

    fun load(contextTypeUpperBound: KClass<*>,
             classLoader: ClassLoader = KotlinCodeGeneration.spi.defaultClassLoader(),
             exclusions: Set = emptySet()
    ): KotlinCodeGenerationSpiRegistry {
      return KotlinCodeGenerationServiceLoader(contextTypeUpperBound, classLoader, exclusions)()
    }
  }

  init {
    require(strategies.isNotEmpty()) { "At least one strategy is required." }
    if (processors.isEmpty()) {
      logger.info { "No processors have been registered." }
    }
  }

  override fun toString(): String = "${this::class.simpleName}(" +
    "contextType=$contextTypeUpperBound, " +
    "strategies=${strategies}, " +
    "processors=${processors})"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy