Skip to content

Commit 17a371f

Browse files
committed
add exclusion filter and toString, fixes #40
1 parent db3df87 commit 17a371f

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

_itest/spi-itest/src/test/kotlin/SpiITest.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,34 @@ import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
66
import com.tschuchort.compiletesting.KotlinCompilation
77
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
88
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.className
9+
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.spi.registry
910
import io.toolisticon.kotlin.generation.spi.strategy.executeSingle
1011
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
1112
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
1213
import org.assertj.core.api.Assertions.assertThat
14+
import org.assertj.core.api.Assertions.assertThatThrownBy
1315
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
1416
import org.junit.jupiter.api.Test
1517

1618
internal class SpiITest {
1719

20+
@Test
21+
fun `init fails when only strategy is excluded`() {
22+
assertThatThrownBy {
23+
registry(
24+
contextTypeUpperBound = TestContext::class,
25+
exclusions = setOf("io.toolisticon.kotlin.generation.itest.spi.TestDataClassStrategy")
26+
)
27+
}.isInstanceOf(IllegalStateException::class.java)
28+
.hasMessage("No serviceInstances found, configure `META-INF/services/io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationSpi`, and/or check your exclusions filter.")
29+
}
30+
1831
@Test
1932
fun `use spi defined strategies and processors to generate code`() {
20-
val registry = KotlinCodeGeneration.spi.registry(TestContext::class)
33+
val registry = registry(contextTypeUpperBound = TestContext::class)
2134
val context = TestContext(registry)
2235

36+
2337
val input = MapInput(
2438
className = className(packageName = "foo.bar", simpleName = "ExampleDataClass"),
2539
fields = mapOf(

kotlin-code-generation/src/main/kotlin/KotlinCodeGeneration.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,9 @@ object KotlinCodeGeneration : KLogging() {
396396
*/
397397
fun registry(
398398
contextTypeUpperBound: KClass<*> = Any::class,
399-
classLoader: ClassLoader = defaultClassLoader()
400-
): KotlinCodeGenerationSpiRegistry = KotlinCodeGenerationServiceLoader(contextTypeUpperBound = contextTypeUpperBound, classLoader = classLoader).invoke()
399+
classLoader: ClassLoader = defaultClassLoader(),
400+
exclusions: Set<String> = emptySet()
401+
): KotlinCodeGenerationSpiRegistry = KotlinCodeGenerationServiceLoader(contextTypeUpperBound = contextTypeUpperBound, classLoader = classLoader, exclusions = exclusions).invoke()
401402
}
402403

403404
/**

kotlin-code-generation/src/main/kotlin/spi/processor/KotlinCodeGenerationProcessorList.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ value class KotlinCodeGenerationProcessorList(val list: List<UnboundKotlinCodeGe
2727
): List<PROCESSOR> {
2828
return list.filterIsInstance(processorType.java)
2929
}
30+
31+
override fun toString(): String = "KotlinCodeGenerationProcessorList(processors=${list.map { it.name }})"
3032
}
3133

3234

kotlin-code-generation/src/main/kotlin/spi/registry/KotlinCodeGenerationServiceLoader.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import kotlin.reflect.full.isSubclassOf
2121
@ExperimentalKotlinPoetApi
2222
class KotlinCodeGenerationServiceLoader(
2323
val contextTypeUpperBound: KClass<*> = Any::class,
24-
val classLoader: ClassLoader = KotlinCodeGeneration.spi.defaultClassLoader()
24+
val classLoader: ClassLoader = KotlinCodeGeneration.spi.defaultClassLoader(),
25+
val exclusions: Set<String> = emptySet()
2526
) : () -> KotlinCodeGenerationSpiRegistry {
2627

2728
override fun invoke(): KotlinCodeGenerationSpiRegistry {
2829
val serviceInstances: List<KotlinCodeGenerationSpi<*, *>> = ServiceLoader.load(KotlinCodeGenerationSpi::class.java, classLoader).toList()
29-
require(serviceInstances.isNotEmpty()) { "No serviceInstances found, configure `${KotlinCodeGenerationSpi.metaInfServices}`." }
30+
.filterNot { exclusions.contains(it::class.java.name) }
31+
check(serviceInstances.isNotEmpty()) { "No serviceInstances found, configure `${KotlinCodeGenerationSpi.metaInfServices}`, and/or check your exclusions filter." }
3032

3133
val withIllegalContextType = serviceInstances.filterNot { it.contextType.isSubclassOf(contextTypeUpperBound) }
3234

kotlin-code-generation/src/main/kotlin/spi/registry/KotlinCodeGenerationServiceRepository.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ class KotlinCodeGenerationServiceRepository(
2323
) : KotlinCodeGenerationSpiRegistry {
2424
companion object : KLogging() {
2525

26-
fun load(contextTypeUpperBound: KClass<*>, classLoader: ClassLoader = KotlinCodeGeneration.spi.defaultClassLoader()): KotlinCodeGenerationSpiRegistry {
27-
return KotlinCodeGenerationServiceLoader(contextTypeUpperBound, classLoader)()
26+
fun load(contextTypeUpperBound: KClass<*>,
27+
classLoader: ClassLoader = KotlinCodeGeneration.spi.defaultClassLoader(),
28+
exclusions: Set<String> = emptySet()
29+
): KotlinCodeGenerationSpiRegistry {
30+
return KotlinCodeGenerationServiceLoader(contextTypeUpperBound, classLoader, exclusions)()
2831
}
29-
3032
}
3133

3234
init {

kotlin-code-generation/src/main/kotlin/spi/strategy/KotlinCodeGenerationStrategyList.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ value class KotlinCodeGenerationStrategyList(private val list: List<UnboundKotli
2626
): List<STRATEGY> {
2727
return list.filterIsInstance(strategyType.java)
2828
}
29+
30+
override fun toString(): String = "KotlinCodeGenerationStrategyList(strategies=${list.map { it.name }})"
2931
}
3032

3133
@ExperimentalKotlinPoetApi

0 commit comments

Comments
 (0)