From 6befcf797a04e741ead21825642e51f76e2e885f Mon Sep 17 00:00:00 2001 From: Dmitriy Bushin Date: Tue, 17 Nov 2026 02:07:00 +0700 Subject: [PATCH] Check the annotations of Facade by KSP --- design/pattern/README.md | 62 +++-- .../pattern/ksp/api/semantic-pattern-ksp.api | 5 + design/pattern/ksp/build.gradle.kts | 12 + .../structural/facade/FacadeAnnotation.kt | 30 +++ .../structural/facade/FacadeOnClassChecker.kt | 24 ++ .../facade/FacadeOutsideSubsystemsChecker.kt | 23 ++ .../facade/FacadeSubsystemsNotEmptyChecker.kt | 16 ++ .../facade/FacadeSymbolProcessor.kt | 53 +++++ .../facade/FacadeSymbolProcessorProvider.kt | 11 + .../SingleSubsystemMembershipChecker.kt | 28 +++ .../SubsystemAnnotationPresenceChecker.kt | 24 ++ .../structural/facade/SubsystemMembership.kt | 31 +++ ...ols.ksp.processing.SymbolProcessorProvider | 1 + .../facade/FacadeSymbolProcessorTests.kt | 220 ++++++++++++++++++ .../FacadeSymbolProcessorProviderTests.kt | 19 ++ settings.gradle.kts | 1 + 16 files changed, 544 insertions(+), 16 deletions(-) create mode 100644 design/pattern/ksp/api/semantic-pattern-ksp.api create mode 100644 design/pattern/ksp/build.gradle.kts create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeAnnotation.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOnClassChecker.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOutsideSubsystemsChecker.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSubsystemsNotEmptyChecker.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessor.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessorProvider.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SingleSubsystemMembershipChecker.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemAnnotationPresenceChecker.kt create mode 100644 design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemMembership.kt create mode 100644 design/pattern/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider create mode 100644 design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/integration/structural/facade/FacadeSymbolProcessorTests.kt create mode 100644 design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/unit/structural/facade/FacadeSymbolProcessorProviderTests.kt diff --git a/design/pattern/README.md b/design/pattern/README.md index fa693af7..32893898 100644 --- a/design/pattern/README.md +++ b/design/pattern/README.md @@ -4,7 +4,7 @@ Classic patterns of objects for Kotlin and Java, grouped as the Gang of Four gro them: creational, structural, behavioral, plus the patterns of Patterns of Enterprise Application Architecture by Fowler in a group of our own, enterprise. A package per group, a package per pattern inside it. Plain Kotlin, no framework; adapters come as modules of their own: -`semantic-pattern-spring` for Spring. +`semantic-pattern-spring` for Spring, `semantic-pattern-ksp` for the checks of KSP at compilation. In place so far: a registry with its store, a registrar and a factory in the creational group; the contracts of Composite, Decorator and Proxy and the annotations of Facade in the structural one; @@ -470,8 +470,8 @@ marker of its own. The rules of the two annotations: the database. - A type counts with its arguments in all the rules: a `List` or a `CrudRepository` is a type of the subsystem. -- `@Facade` goes on a class, not an interface, and names one subsystem at least; each annotation it names carries - `@Facade.Subsystem`. +- `@Facade` goes on a class, not an interface or an annotation, and names one subsystem at least; each annotation it + names carries `@Facade.Subsystem`. - A class carries the marker of one subsystem at most, and a facade carries none: a facade stands outside its subsystems. @@ -484,15 +484,15 @@ writes no name, a Java one writes `@Facade(subsystems = FileSubsystem.class)`. The way past the facade is a marker: a class that carries one joins its subsystem, and the rules take it at its word. A review sees the marker, and a search by it finds each class that joined a subsystem. -The annotations check nothing themselves: the processor of the plan checks the rules when a module is compiled, the -check of Spring the first one when the application starts. Kotlin has no visibility of a package, as Java has, and -`internal` covers a whole Gradle module: in one service every class of a subsystem is open to every client. The -compiler closes a class to those that do not declare `@OptIn` of its marker, if the marker is annotated with -`@RequiresOptIn` of the level `ERROR`, as `@InternalCoroutinesApi` closes the internals of kotlinx.coroutines. -Kotlin takes no other annotation for `@OptIn`, though: a facade would name its subsystem twice, in `@Facade` and in -`@OptIn`. The marker here is a plain annotation, and the checks of the plan read it from the classes; the compiler -still checks the bodies of functions for a project that adds `@RequiresOptIn` to its marker and `@OptIn` to its -facade. +The annotations check nothing themselves: `semantic-pattern-ksp` checks the rules of the annotations when a module +is compiled, and the plan adds the other rules to it and the check of Spring when the application starts. Kotlin has +no visibility of a package, as Java has, and `internal` covers a whole Gradle module: in one service every class of +a subsystem is open to every client. The compiler closes a class to those that do not declare `@OptIn` of its +marker, if the marker is annotated with `@RequiresOptIn` of the level `ERROR`, as `@InternalCoroutinesApi` closes +the internals of kotlinx.coroutines. Kotlin takes no other annotation for `@OptIn`, though: a facade would name its +subsystem twice, in `@Facade` and in `@OptIn`. The marker here is a plain annotation, and the checks read it from +the classes; the compiler still checks the bodies of functions for a project that adds `@RequiresOptIn` to its +marker and `@OptIn` to its facade. `@Facade` and the marker are annotations, not interfaces, as the layers of `semantic-explicit` are: a facade has no operation that every facade shares, and the annotations ask nothing of a class. `@Facade.Subsystem` marks an @@ -527,13 +527,43 @@ The samples: `@NamedInterface` opens one of them. `ApplicationModules.verify()` fails a test on an access to the internals of a module. +### KSP + +`semantic-pattern-ksp` is a processor of KSP that fails the build of a module on a broken rule, at the line that +breaks it, as Dagger fails a build on a broken graph of dependencies. Each module with facades or subsystems applies +it: + +```kotlin +plugins { + id("com.google.devtools.ksp") version "2.3.12" +} + +dependencies { + implementation("pro.vlprojects.semantic:semantic-pattern:") + ksp("pro.vlprojects.semantic:semantic-pattern-ksp:") +} +``` + +A facade that names no subsystem stops the build with: + +```text +e: [ksp] /…/FileFacade.kt:3: Facade FileFacade names no subsystem +``` + +- It checks the annotation of each facade and the markers each class of the module is annotated with, nested ones + too, in Kotlin and in Java, under a type alias as well. The markers of supertypes and the rules of members and of + clients come next, by the plan. +- `FacadeSymbolProcessorProvider` is listed in `META-INF/services`, where KSP finds its processors by `ServiceLoader`. + ### Plan of Facade 1. `Facade` and `Facade.Subsystem`. -2. `semantic-pattern-ksp`, a processor of KSP that fails the build of a module on a broken rule, at the line - that breaks it, as Dagger fails a build on a broken graph of dependencies. KSP sees the declarations, not the - bodies of functions: a class of a subsystem made in a body goes unseen. Each module with clients applies it. -3. A check of `semantic-pattern-spring` that stops the startup on an injection that breaks the first rule, as +2. `semantic-pattern-ksp`, a processor of KSP that checks the rules of the annotations at compilation. +3. The rule of public members of a facade in `semantic-pattern-ksp`. +4. The rules of clients in `semantic-pattern-ksp`: the constructors, properties and signatures of functions of each + class and each top-level declaration, and the markers of supertypes. KSP sees no bodies of functions: a class of + a subsystem made in a body goes unseen. +5. A check of `semantic-pattern-spring` that stops the startup on an injection that breaks the first rule, as `ApplicationModules.verify()` of Spring Modulith checks the modules in a test. It reads the dependencies Spring records between beans, as `getDependentBeans` of `ConfigurableBeanFactory` tells them, and catches beans that no processor saw. `@Facade` and the markers are looked up on the user class of a bean and its interfaces diff --git a/design/pattern/ksp/api/semantic-pattern-ksp.api b/design/pattern/ksp/api/semantic-pattern-ksp.api new file mode 100644 index 00000000..2f738ee8 --- /dev/null +++ b/design/pattern/ksp/api/semantic-pattern-ksp.api @@ -0,0 +1,5 @@ +public final class pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessorProvider : com/google/devtools/ksp/processing/SymbolProcessorProvider { + public fun ()V + public fun create (Lcom/google/devtools/ksp/processing/SymbolProcessorEnvironment;)Lcom/google/devtools/ksp/processing/SymbolProcessor; +} + diff --git a/design/pattern/ksp/build.gradle.kts b/design/pattern/ksp/build.gradle.kts new file mode 100644 index 00000000..914f3a62 --- /dev/null +++ b/design/pattern/ksp/build.gradle.kts @@ -0,0 +1,12 @@ +plugins { + id("semantic.kotlin-library") +} + +dependencies { + implementation(project(":semantic-pattern")) + implementation(project(":semantic-ksp-helper")) + compileOnly(libs.ksp.symbol.processing.api) + + testImplementation(libs.ksp.symbol.processing.api) + testImplementation(libs.kctfork.ksp) +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeAnnotation.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeAnnotation.kt new file mode 100644 index 00000000..76ef7f5e --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeAnnotation.kt @@ -0,0 +1,30 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSAnnotation +import com.google.devtools.ksp.symbol.KSType +import pro.vlprojects.semantic.ksp.helper.getFirstAnnotationOrNull +import pro.vlprojects.semantic.ksp.helper.getTypeValues +import pro.vlprojects.semantic.ksp.helper.resolveClassDeclaration + +internal class FacadeAnnotation private constructor(val symbol: KSAnnotation) { + + fun hasSubsystems() = this + .getSubsystemTypes() + .isNotEmpty() + + // An unresolved class is left to the compiler, which names it better. + fun getSubsystems() = this + .getSubsystemTypes() + .filterNot(KSType::isError) + .mapNotNull(KSType::resolveClassDeclaration) + + private fun getSubsystemTypes() = this.symbol.getTypeValues(Facade::subsystems.name) + + companion object Factory { + + fun find(declaration: KSAnnotated) = declaration + .getFirstAnnotationOrNull(Facade::class) + ?.let(::FacadeAnnotation) + } +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOnClassChecker.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOnClassChecker.kt new file mode 100644 index 00000000..ef2a387b --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOnClassChecker.kt @@ -0,0 +1,24 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.symbol.ClassKind +import com.google.devtools.ksp.symbol.KSClassDeclaration +import pro.vlprojects.semantic.ksp.helper.getShortName + +internal class FacadeOnClassChecker(private val logger: KSPLogger) { + + fun check(declaration: KSClassDeclaration, annotation: FacadeAnnotation) { + val facadeName = declaration.getShortName() + + when (declaration.classKind) { + ClassKind.INTERFACE -> this.reportMisplaced(annotation, "the interface $facadeName") + ClassKind.ANNOTATION_CLASS -> this.reportMisplaced(annotation, "the annotation $facadeName") + else -> Unit + } + } + + private fun reportMisplaced(annotation: FacadeAnnotation, target: String) = this.logger.error( + "@Facade goes on a class, not on $target", + annotation.symbol, + ) +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOutsideSubsystemsChecker.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOutsideSubsystemsChecker.kt new file mode 100644 index 00000000..fd83fdf8 --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeOutsideSubsystemsChecker.kt @@ -0,0 +1,23 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.symbol.KSClassDeclaration +import pro.vlprojects.semantic.ksp.helper.getShortName + +internal class FacadeOutsideSubsystemsChecker(private val logger: KSPLogger) { + + fun check(declaration: KSClassDeclaration) { + val facadeName = declaration.getShortName() + + SubsystemMembership + .findAll(declaration) + .forEach { membership -> + val subsystemName = membership.describe() + + this.logger.error( + "Facade $facadeName carries $subsystemName: a facade stands outside its subsystems", + membership.annotation, + ) + } + } +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSubsystemsNotEmptyChecker.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSubsystemsNotEmptyChecker.kt new file mode 100644 index 00000000..c94261c2 --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSubsystemsNotEmptyChecker.kt @@ -0,0 +1,16 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.symbol.KSClassDeclaration +import pro.vlprojects.semantic.ksp.helper.getShortName + +internal class FacadeSubsystemsNotEmptyChecker(private val logger: KSPLogger) { + + fun check(declaration: KSClassDeclaration, annotation: FacadeAnnotation) { + if (!annotation.hasSubsystems()) { + val facadeName = declaration.getShortName() + + this.logger.error("Facade $facadeName names no subsystem", annotation.symbol) + } + } +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessor.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessor.kt new file mode 100644 index 00000000..c67882ec --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessor.kt @@ -0,0 +1,53 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.google.devtools.ksp.symbol.KSFile +import pro.vlprojects.semantic.ksp.helper.getDeclaredClasses + +internal class FacadeSymbolProcessor(logger: KSPLogger) : SymbolProcessor { + + private val singleSubsystemMembershipChecker = SingleSubsystemMembershipChecker(logger) + + private val facadeOnClassChecker = FacadeOnClassChecker(logger) + + private val facadeSubsystemsNotEmptyChecker = FacadeSubsystemsNotEmptyChecker(logger) + + private val subsystemAnnotationPresenceChecker = SubsystemAnnotationPresenceChecker(logger) + + private val facadeOutsideSubsystemsChecker = FacadeOutsideSubsystemsChecker(logger) + + override fun process(resolver: Resolver): List { + resolver + .getNewFiles() + .flatMap(KSFile::declarations) + .filterIsInstance() + .flatMap(::flattenClasses) + .forEach(::checkClass) + + return emptyList() + } + + private fun checkClass(declaration: KSClassDeclaration) { + this.singleSubsystemMembershipChecker.check(declaration) + + FacadeAnnotation + .find(declaration) + ?.let { annotation -> this.checkFacade(declaration, annotation) } + } + + private fun checkFacade(declaration: KSClassDeclaration, annotation: FacadeAnnotation) { + this.facadeOnClassChecker.check(declaration, annotation) + this.facadeSubsystemsNotEmptyChecker.check(declaration, annotation) + this.subsystemAnnotationPresenceChecker.check(declaration, annotation) + this.facadeOutsideSubsystemsChecker.check(declaration) + } + + private fun flattenClasses(declaration: KSClassDeclaration): Sequence = declaration + .getDeclaredClasses() + .flatMap(::flattenClasses) + .let { nested -> sequenceOf(declaration) + nested } +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessorProvider.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessorProvider.kt new file mode 100644 index 00000000..0a6b85b9 --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/FacadeSymbolProcessorProvider.kt @@ -0,0 +1,11 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.processing.SymbolProcessorEnvironment +import com.google.devtools.ksp.processing.SymbolProcessorProvider + +public class FacadeSymbolProcessorProvider : SymbolProcessorProvider { + + override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = + FacadeSymbolProcessor(environment.logger) +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SingleSubsystemMembershipChecker.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SingleSubsystemMembershipChecker.kt new file mode 100644 index 00000000..ed90c280 --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SingleSubsystemMembershipChecker.kt @@ -0,0 +1,28 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.symbol.KSClassDeclaration +import pro.vlprojects.semantic.ksp.helper.getShortName +import pro.vlprojects.semantic.ksp.helper.hasAnnotation + +internal class SingleSubsystemMembershipChecker(private val logger: KSPLogger) { + + // A marker on a facade is reported as a facade inside a subsystem. + fun check(declaration: KSClassDeclaration) { + if (!declaration.hasAnnotation(Facade::class)) { + this.checkMemberships(declaration) + } + } + + private fun checkMemberships(declaration: KSClassDeclaration) { + val memberships = SubsystemMembership.findAll(declaration) + + if (memberships.size > 1) { + val name = declaration.getShortName() + val subsystemNames = memberships.joinToString(transform = SubsystemMembership::describe) + val symbol = memberships.last().annotation + + this.logger.error("$name carries the markers of several subsystems: $subsystemNames", symbol) + } + } +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemAnnotationPresenceChecker.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemAnnotationPresenceChecker.kt new file mode 100644 index 00000000..7e4fd12f --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemAnnotationPresenceChecker.kt @@ -0,0 +1,24 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.symbol.KSClassDeclaration +import pro.vlprojects.semantic.ksp.helper.getShortName +import pro.vlprojects.semantic.ksp.helper.hasAnnotation + +internal class SubsystemAnnotationPresenceChecker(private val logger: KSPLogger) { + + fun check(declaration: KSClassDeclaration, annotation: FacadeAnnotation) { + val facadeName = declaration.getShortName() + + annotation + .getSubsystems() + .filterNot { subsystem -> subsystem.hasAnnotation(Facade.Subsystem::class) } + .map(KSClassDeclaration::getShortName) + .forEach { subsystemName -> + this.logger.error( + "Facade $facadeName names @$subsystemName, which is not marked with @Facade.Subsystem", + annotation.symbol, + ) + } + } +} diff --git a/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemMembership.kt b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemMembership.kt new file mode 100644 index 00000000..c4b953c3 --- /dev/null +++ b/design/pattern/ksp/src/main/kotlin/pro/vlprojects/semantic/pattern/structural/facade/SubsystemMembership.kt @@ -0,0 +1,31 @@ +package pro.vlprojects.semantic.pattern.structural.facade + +import com.google.devtools.ksp.symbol.KSAnnotation +import com.google.devtools.ksp.symbol.KSClassDeclaration +import pro.vlprojects.semantic.ksp.helper.getQualifiedName +import pro.vlprojects.semantic.ksp.helper.getShortName +import pro.vlprojects.semantic.ksp.helper.hasAnnotation +import pro.vlprojects.semantic.ksp.helper.resolveClassDeclaration + +internal class SubsystemMembership private constructor( + val annotation: KSAnnotation, + private val subsystem: KSClassDeclaration, +) { + + fun getSubsystemName() = this.subsystem.getQualifiedName() + + fun describe() = "@${this.subsystem.getShortName()}" + + companion object Factory { + + fun findAll(declaration: KSClassDeclaration) = declaration.annotations + .mapNotNull(::createOrNull) + .distinctBy(SubsystemMembership::getSubsystemName) + .toList() + + private fun createOrNull(annotation: KSAnnotation) = annotation + .resolveClassDeclaration() + ?.takeIf { subsystem -> subsystem.hasAnnotation(Facade.Subsystem::class) } + ?.let { subsystem -> SubsystemMembership(annotation, subsystem) } + } +} diff --git a/design/pattern/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider b/design/pattern/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider new file mode 100644 index 00000000..f1a4f6b0 --- /dev/null +++ b/design/pattern/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider @@ -0,0 +1 @@ +pro.vlprojects.semantic.pattern.structural.facade.FacadeSymbolProcessorProvider diff --git a/design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/integration/structural/facade/FacadeSymbolProcessorTests.kt b/design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/integration/structural/facade/FacadeSymbolProcessorTests.kt new file mode 100644 index 00000000..e00c88b7 --- /dev/null +++ b/design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/integration/structural/facade/FacadeSymbolProcessorTests.kt @@ -0,0 +1,220 @@ +package pro.vlprojects.semantic.pattern.integration.structural.facade + +import com.tschuchort.compiletesting.JvmCompilationResult +import com.tschuchort.compiletesting.KotlinCompilation +import com.tschuchort.compiletesting.KotlinCompilation.ExitCode +import com.tschuchort.compiletesting.SourceFile +import com.tschuchort.compiletesting.configureKsp +import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import pro.vlprojects.semantic.pattern.structural.facade.FacadeSymbolProcessorProvider + +@OptIn(ExperimentalCompilerApi::class) +class FacadeSymbolProcessorTests { + + @Test + fun `facades and subsystems of Kotlin and of Java compile`() { + val javaFile = SourceFile.java( + "JavaArchive.java", + """ + import pro.vlprojects.semantic.pattern.structural.facade.Facade; + + @Facade.Subsystem + @interface ArchiveSubsystem {} + @ArchiveSubsystem + class ArchiveEntity {} + @Facade(subsystems = ArchiveSubsystem.class) + class ArchiveFacade {} + @Facade(subsystems = {FileSubsystem.class, BillingSubsystem.class}) + class ReportFacade {} + """, + ) + + val result = this.compileSources(javaFile) + + assertEquals(ExitCode.OK, result.exitCode, result.messages) + } + + @Test + fun `classes of Java follow the same rules`() { + val javaFile = SourceFile.java( + "JavaFacades.java", + """ + import pro.vlprojects.semantic.pattern.structural.facade.Facade; + + @Facade(subsystems = Plain.class) + class JavaPlainFacade {} + @Facade(subsystems = {FileSubsystem.class, Plain.class}) + class JavaFileFacade {} + @Facade(subsystems = FileSubsystem.class) + interface JavaFileOperations {} + @Facade(subsystems = FileSubsystem.class) + @FileSubsystem + class JavaMarkedFacade {} + class JavaFileModule { + @FileSubsystem + @BillingSubsystem + static class SharedClient {} + } + """, + ) + + SourceFile + .kotlin("Plain.kt", "annotation class Plain") + .let { kotlinFile -> this.compileSources(kotlinFile, javaFile) } + .let { result -> + this.assertCompilationFails( + result, + "JavaFacades.java:3: Facade JavaPlainFacade names @Plain, which is not marked", + "JavaFacades.java:5: Facade JavaFileFacade names @Plain, which is not marked", + "JavaFacades.java:7: @Facade goes on a class, not on the interface JavaFileOperations", + "JavaFacades.java:10: Facade JavaMarkedFacade carries @FileSubsystem: a facade stands outside", + "JavaFacades.java:14: JavaFileModule.SharedClient carries the markers of several subsystems", + ) + } + } + + @Test + fun `a facade names one subsystem at least`() = this.assertCompilationFails( + "@Facade\nclass EmptyFacade", + 2, + "Facade EmptyFacade names no subsystem", + ) + + @Test + fun `a facade under a type alias names one subsystem at least`() = this.assertCompilationFails( + "typealias Entry = Facade\n@Entry\nclass EmptyFacade", + 3, + "Facade EmptyFacade names no subsystem", + ) + + @Test + fun `an unresolved subsystem is left to the compiler`() { + val result = this.compile("@Facade(Unknown::class)\nclass UnknownFacade") + + assertEquals(ExitCode.COMPILATION_ERROR, result.exitCode, result.messages) + assertTrue("Unknown" in result.messages, result.messages) + assertFalse(PROCESSOR_FAILURE in result.messages, result.messages) + } + + @Test + fun `a facade names markers of subsystems only`() = this.assertCompilationFails( + "annotation class Plain\n@Facade(Plain::class)\nclass PlainFacade", + 3, + "Facade PlainFacade names @Plain, which is not marked with @Facade.Subsystem", + ) + + @Test + fun `a facade goes on a class, not on an interface`() = this.assertCompilationFails( + "@Facade(FileSubsystem::class)\ninterface FileOperations", + 2, + "@Facade goes on a class, not on the interface FileOperations", + ) + + @Test + fun `a facade goes on a class, not on an annotation`() = this.assertCompilationFails( + "@Facade(FileSubsystem::class)\nannotation class FileOperation", + 2, + "@Facade goes on a class, not on the annotation FileOperation", + ) + + @Test + fun `a nested class carries the marker of one subsystem at most`() = this.assertCompilationFails( + "class FileModule {\n@FileSubsystem\n@BillingSubsystem\nclass SharedClient\n}", + 4, + "FileModule.SharedClient carries the markers of several subsystems: @FileSubsystem, @BillingSubsystem", + ) + + @Test + fun `a marker under a type alias counts as the marker itself`() = this.assertCompilationFails( + "@FileMarker\n@BillingSubsystem\nclass SharedClient", + 3, + "SharedClient carries the markers of several subsystems: @FileSubsystem, @BillingSubsystem", + ) + + @Test + fun `a repeated marker names one subsystem`() { + val result = this.compile( + """ + @Facade.Subsystem + @Repeatable + annotation class ArchiveSubsystem + + @ArchiveSubsystem + @ArchiveSubsystem + class ArchiveEntity + """, + ) + + assertEquals(ExitCode.OK, result.exitCode, result.messages) + } + + @Test + fun `a facade carries no marker`() = this.assertCompilationFails( + "@Facade(FileSubsystem::class)\n@FileSubsystem\nclass MarkedFacade", + 3, + "Facade MarkedFacade carries @FileSubsystem: a facade stands outside its subsystems", + ) + + private fun assertCompilationFails(source: String, line: Int, message: String) = this + .compile(source) + .let { result -> this.assertCompilationFails(result, "$VIOLATION_FILE:$line: $message") } + + private fun assertCompilationFails(result: JvmCompilationResult, vararg errors: String) { + assertEquals(ExitCode.COMPILATION_ERROR, result.exitCode, result.messages) + + errors.forEach { error -> + assertTrue(error in result.messages) { "No \"$error\" in ${result.messages}" } + } + } + + // The import takes the first line of the file, so the source starts at the second one. + private fun compile(source: String) = SourceFile + .kotlin(VIOLATION_FILE, "$FACADE_IMPORT\n$source") + .let(::compileSources) + + private fun compileSources(vararg files: SourceFile) = KotlinCompilation() + .apply { + sources = files.toList() + SUBSYSTEMS_FILE + inheritClassPath = true + configureKsp { symbolProcessorProviders += FacadeSymbolProcessorProvider() } + } + .compile() + + private companion object { + + const val VIOLATION_FILE = "Violation.kt" + + const val FACADE_IMPORT = "import pro.vlprojects.semantic.pattern.structural.facade.Facade" + + // kctfork prints it once a processor reports an error or throws. + const val PROCESSOR_FAILURE = "Error occurred in KSP" + + val SUBSYSTEMS_FILE = SourceFile.kotlin( + "Subsystems.kt", + """ + $FACADE_IMPORT + + @Facade.Subsystem + annotation class FileSubsystem + + @Facade.Subsystem + annotation class BillingSubsystem + + typealias FileMarker = FileSubsystem + + @FileSubsystem + class FileEntity + + @FileMarker + class FileStorageClient + + @Facade(FileMarker::class) + class FileFacade(private val storage: FileStorageClient, private val file: FileEntity) + """, + ) + } +} diff --git a/design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/unit/structural/facade/FacadeSymbolProcessorProviderTests.kt b/design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/unit/structural/facade/FacadeSymbolProcessorProviderTests.kt new file mode 100644 index 00000000..24054cb3 --- /dev/null +++ b/design/pattern/ksp/src/test/kotlin/pro/vlprojects/semantic/pattern/unit/structural/facade/FacadeSymbolProcessorProviderTests.kt @@ -0,0 +1,19 @@ +package pro.vlprojects.semantic.pattern.unit.structural.facade + +import com.google.devtools.ksp.processing.SymbolProcessorProvider +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import pro.vlprojects.semantic.pattern.structural.facade.FacadeSymbolProcessorProvider +import java.util.ServiceLoader + +class FacadeSymbolProcessorProviderTests { + + @Test + fun `ServiceLoader finds the provider`() { + val providers = ServiceLoader + .load(SymbolProcessorProvider::class.java) + .map { provider -> provider::class } + + assertTrue(FacadeSymbolProcessorProvider::class in providers) { "No provider in $providers" } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 81559823..fd9233a4 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -52,6 +52,7 @@ module("semantic-dbc", "design/dbc/core") module("semantic-algebra", "design/algebra/core") module("semantic-pattern", "design/pattern/core") module("semantic-pattern-spring", "design/pattern/spring/core") +module("semantic-pattern-ksp", "design/pattern/ksp") module("semantic-layered", "architecture/style/layered/core") module("semantic-onion", "architecture/style/onion/core")