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

spi.KotlinCodeGenerationStrategy.kt Maven / Gradle / Ivy

The newest version!
package io.toolisticon.kotlin.generation.spi

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import kotlin.reflect.KClass

/**
 * Root marker interface for all strategies.
 *
 * Strategies are building plans for a spec. They return a kotlin poet spec that is generated by the logic defined in the strategy using an input (the concrete
 * data the spec is generated for) and a context (the overall generator state containing additional data and also the registry to trigger other strategies and processors).
 *
 * Hint: for implementing a concrete strategy, use the [io.toolisticon.kotlin.generation.spi.strategy.KotlinCodeGenerationStrategyBase].
 */
@ExperimentalKotlinPoetApi
interface KotlinCodeGenerationStrategy, INPUT : Any, SPEC : Any> : KotlinCodeGenerationSpi {

  override val contextType: KClass
  override val inputType: KClass
  override val name: String
  override val order: Int

  /**
   * The type of the created SPEC, used to filter relevant instances for execution.
   */
  val specType: KClass

  /**
   * Implements the actual strategy logic to generate a spec from a given input, using context information.
   *
   * @param context the context we are operating in
   * @param input the concrete work item
   * @return the generated spec
   */
  operator fun invoke(context: CONTEXT, input: INPUT): SPEC
  override fun test(context: CONTEXT, input: Any): Boolean = super.test(context, input)

  /**
   * Checks if this strategy should be applied (using [test]) and then runs [invoke].
   *
   * @see [invoke] the executed strategy logic
   * @see [test] to check if the strategy should be applied.
   * @param context the context we are operating in
   * @param input the concrete work item
   * @return generated spec (or `null` if test was `false`).
   */
  fun execute(context: CONTEXT, input: INPUT): SPEC? = if (test(context, input)) {
    invoke(context, input)
  } else {
    null
  }
}

/**
 * Convenience alias to reference unbound strategies without repeating the `<*,*,*>`.
 */
@ExperimentalKotlinPoetApi
typealias UnboundKotlinCodeGenerationStrategy = KotlinCodeGenerationStrategy<*, *, *>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy