Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions design/pattern/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ marker of its own. The rules of the two annotations:
only if it carries the marker of that subsystem or is a facade that names it; a top-level function or property
names none. `FileController` keeps the rules, `AdminFileController` breaks the first: it reaches the storage past
the facade, and a deletion of its own forgets the quota.
- A class of a subsystem takes no facade that names its own subsystem: the subsystem knows nothing of its facade.
- A class of a subsystem takes no facade that names its own subsystem, in a parameter or a property: the subsystem
knows nothing of its facade. A configuration of the subsystem still returns the facade from a `@Bean` method.
- A function or property of a facade that a client sees, public or internal, anything but private in Java, its
companion's too, its constructor aside, neither takes nor returns a type of its subsystems: the facade answers
with a type of its own, `FileView`, and a client stays apart from the schema of the database.
Expand All @@ -485,15 +486,14 @@ The way past the facade is a marker: a class that carries one joins its subsyste
word. A review sees the marker, and a search by it finds each class annotated with it; a class that takes a marker
from a supertype is named in the errors with the class the marker comes from: `@FileSubsystem through FileEntity`.

The annotations check nothing themselves: `semantic-pattern-ksp` checks the rules of a facade and of the markers
when a module is compiled, and the plan adds the rules of clients 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.
The annotations check nothing themselves: `semantic-pattern-ksp` checks the rules when a module is compiled, and the
plan adds the check of Spring for the first rule 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
Expand Down Expand Up @@ -545,20 +545,22 @@ dependencies {
}
```

A `find` of the facade above that returned `FileEntity` instead of `FileView` stops the build at its line, in a file
that starts with the facade:
`AdminFileController` above stops the build at its line, in a file of its own:

```text
e: [ksp] /…/FileFacade.kt:18: Facade FileFacade exposes FileEntity of @FileSubsystem
e: [ksp] /…/AdminFileController.kt:3: AdminFileController names FileStorageClient of @FileSubsystem past a facade
```

- It checks the annotation of each facade and the members a client sees of it, and the markers of each class of the
module, nested ones too, and of its supertypes, in Kotlin and in Java, under a type alias as well. The rules of
clients come next, by the plan.
- It checks the declarations of the module, in Kotlin and in Java: the annotation of each facade and the members a
client sees of it, the markers of each class and of its supertypes, under a type alias as well, the constructors,
properties and signatures of functions of each class, nested ones too, and each top-level function and property. A
class of a subsystem named in the body of a function, an initializer or a default value goes unseen: KSP sees no
bodies.
- A marker of a class of another module is read from its class file, as Kotlin keeps an annotation there by
default, and so are the supertypes that pass their markers on.
- A type alias counts as the type it stands for, a function type as its parameters and result, a parameter of a
type as its bounds.
- A narrow interface of a facade carries no annotation, so a class of a subsystem that takes one goes unseen.
- `FacadeSymbolProcessorProvider` is listed in `META-INF/services`, where KSP finds its processors by `ServiceLoader`.

### Plan of Facade
Expand All @@ -568,8 +570,8 @@ e: [ksp] /…/FileFacade.kt:18: Facade FileFacade exposes FileEntity of @FileSub
3. The rule of members a client sees of a facade in `semantic-pattern-ksp`.
4. The markers of supertypes in `semantic-pattern-ksp`.
5. The rules of clients in `semantic-pattern-ksp`: the constructors, properties and signatures of functions of each
class and each top-level declaration. KSP sees no bodies of functions: a class of a subsystem made in a body goes
unseen.
class and each top-level declaration. KSP sees no bodies: a class of a subsystem named in the body of a function,
an initializer or a default value goes unseen.
6. 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ 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.KSDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import pro.vlprojects.semantic.ksp.helper.getDeclaredClasses

internal class FacadeSymbolProcessor(logger: KSPLogger) : SymbolProcessor {

private val singleSubsystemMembershipChecker = SingleSubsystemMembershipChecker(logger)

private val subsystemAccessThroughFacadeChecker = SubsystemAccessThroughFacadeChecker(logger)

private val subsystemIndependentOfFacadeChecker = SubsystemIndependentOfFacadeChecker(logger)

private val facadeOnClassChecker = FacadeOnClassChecker(logger)

private val facadeSubsystemsNotEmptyChecker = FacadeSubsystemsNotEmptyChecker(logger)
Expand All @@ -26,15 +33,24 @@ internal class FacadeSymbolProcessor(logger: KSPLogger) : SymbolProcessor {
resolver
.getNewFiles()
.flatMap(KSFile::declarations)
.filterIsInstance<KSClassDeclaration>()
.flatMap(::flattenClasses)
.forEach(::checkClass)
.forEach(::checkDeclaration)

return emptyList()
}

private fun checkDeclaration(declaration: KSDeclaration) {
when (declaration) {
is KSClassDeclaration -> this.flattenClasses(declaration).forEach(::checkClass)
is KSFunctionDeclaration -> this.subsystemAccessThroughFacadeChecker.check(declaration)
is KSPropertyDeclaration -> this.subsystemAccessThroughFacadeChecker.check(declaration)
else -> Unit
}
}

private fun checkClass(declaration: KSClassDeclaration) {
this.singleSubsystemMembershipChecker.check(declaration)
this.subsystemAccessThroughFacadeChecker.check(declaration)
this.subsystemIndependentOfFacadeChecker.check(declaration)

FacadeAnnotation
.find(declaration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.google.devtools.ksp.symbol.KSValueParameter
import com.google.devtools.ksp.symbol.Origin
import pro.vlprojects.semantic.ksp.helper.getDeclaredClasses
import pro.vlprojects.semantic.ksp.helper.hasAnnotation
import pro.vlprojects.semantic.ksp.helper.isSynthetic

internal class MemberSignatureTypes private constructor(private val declaration: KSClassDeclaration) {

Expand All @@ -38,13 +39,61 @@ internal class MemberSignatureTypes private constructor(private val declaration:
return functionTypes + propertyTypes + companionTypes
}

private fun findFunctionTypes(function: KSFunctionDeclaration) = function.parameters
.map(KSValueParameter::type)
fun findAll() = this.findTaken() + this.findReturned()

// Generated members repeat declarations checked elsewhere: properties of a data class, members of a delegate.
fun findTaken(): Sequence<KSTypeReference> {
val functionTypes = this.declaration
.getDeclaredFunctions()
.filterNot(KSFunctionDeclaration::isSynthetic)
.flatMap(::findParameterTypes)

val propertyTypes = this.declaration
.getDeclaredProperties()
.flatMap(::findPropertyTypes)

val companionTypes = this
.findCompanions()
.map(Factory::of)
.flatMap(MemberSignatureTypes::findTaken)

return functionTypes + propertyTypes + companionTypes
}

private fun findReturned(): Sequence<KSTypeReference> {
val functionTypes = this.declaration
.getDeclaredFunctions()
.filterNot(KSFunctionDeclaration::isSynthetic)
.filterNot(KSFunctionDeclaration::isConstructor)
.mapNotNull(KSFunctionDeclaration::returnType)

val companionTypes = this
.findCompanions()
.map(Factory::of)
.flatMap(MemberSignatureTypes::findReturned)

return functionTypes + companionTypes
}

private fun findFunctionTypes(function: KSFunctionDeclaration) = this
.findParameterTypes(function)
.plus(function.returnType)
.filterNotNull()

// A parameter that declares a property is checked once, as the property.
private fun findParameterTypes(function: KSFunctionDeclaration) = function.parameters
.filterNot(::isProperty)
.map(KSValueParameter::type)
.plus(function.extensionReceiver)
.filterNotNull()
.asSequence()

private fun isProperty(parameter: KSValueParameter) = when {
parameter.isVal -> true
parameter.isVar -> true
else -> false
}

private fun findPropertyTypes(property: KSPropertyDeclaration) =
sequenceOf(property.type, property.extensionReceiver).filterNotNull()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package pro.vlprojects.semantic.pattern.structural.facade

import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.KSTypeReference
import com.google.devtools.ksp.symbol.KSValueParameter
import pro.vlprojects.semantic.ksp.helper.getReferencedClasses
import pro.vlprojects.semantic.ksp.helper.getShortName

internal class SubsystemAccessThroughFacadeChecker(private val logger: KSPLogger) {

fun check(declaration: KSClassDeclaration) {
when (val annotation = FacadeAnnotation.find(declaration)) {
null -> this.checkClass(declaration)
else -> this.checkFacade(declaration, annotation)
}
}

fun check(declaration: KSFunctionDeclaration) = this
.findFunctionTypes(declaration)
.let { types -> this.checkTopLevel(declaration, types) }

fun check(declaration: KSPropertyDeclaration) = this
.findPropertyTypes(declaration)
.let { types -> this.checkTopLevel(declaration, types) }

private fun checkFacade(declaration: KSClassDeclaration, annotation: FacadeAnnotation) {
val facadeName = declaration.getShortName()
val subsystemNames = annotation.getSubsystemNames()

MemberSignatureTypes
.of(declaration)
.findAll()
.forEach { type -> this.checkType("Facade $facadeName", type, subsystemNames) }
}

// A companion is checked with the class it belongs to, as a part of it.
private fun checkClass(declaration: KSClassDeclaration) {
if (!declaration.isCompanionObject) {
this.checkMemberTypes(declaration)
}
}

private fun checkMemberTypes(declaration: KSClassDeclaration) {
val className = declaration.getShortName()

val subsystemNames = SubsystemMembership
.findAll(declaration)
.map(SubsystemMembership::getSubsystemName)
.toSet()

MemberSignatureTypes
.of(declaration)
.findAll()
.forEach { type -> this.checkType(className, type, subsystemNames) }
}

private fun checkTopLevel(declaration: KSDeclaration, types: Sequence<KSTypeReference>) {
val name = declaration.getShortName()

types.forEach { type -> this.checkType(name, type, emptySet()) }
}

private fun checkType(subject: String, type: KSTypeReference, allowedNames: Set<String>) = type
.getReferencedClasses()
.forEach { dependency ->
val dependencyName = dependency.getShortName()

SubsystemMembership
.findAll(dependency)
.filterNot { membership -> membership.getSubsystemName() in allowedNames }
.map(SubsystemMembership::describe)
.forEach { subsystemName ->
this.logger.error("$subject names $dependencyName of $subsystemName past a facade", type)
}
}

private fun findFunctionTypes(function: KSFunctionDeclaration) = function.parameters
.map(KSValueParameter::type)
.plus(function.returnType)
.plus(function.extensionReceiver)
.filterNotNull()
.asSequence()

private fun findPropertyTypes(property: KSPropertyDeclaration) =
sequenceOf(property.type, property.extensionReceiver).filterNotNull()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pro.vlprojects.semantic.pattern.structural.facade

import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSTypeReference
import pro.vlprojects.semantic.ksp.helper.getReferencedClasses
import pro.vlprojects.semantic.ksp.helper.getShortName
import pro.vlprojects.semantic.ksp.helper.hasAnnotation

internal class SubsystemIndependentOfFacadeChecker(private val logger: KSPLogger) {

// A companion is checked with the class it belongs to, as a part of it.
fun check(declaration: KSClassDeclaration) {
when {
declaration.isCompanionObject -> Unit
declaration.hasAnnotation(Facade::class) -> Unit
else -> this.checkTakenTypes(declaration)
}
}

private fun checkTakenTypes(declaration: KSClassDeclaration) {
val className = declaration.getShortName()
val memberships = SubsystemMembership.findAll(declaration)

MemberSignatureTypes
.of(declaration)
.findTaken()
.forEach { type -> this.checkTakenType(className, type, memberships) }
}

private fun checkTakenType(className: String, type: KSTypeReference, memberships: List<SubsystemMembership>) = type
.getReferencedClasses()
.forEach { dependency ->
val dependencyName = dependency.getShortName()

val facadeSubsystemNames = FacadeAnnotation
.find(dependency)
?.getSubsystemNames()
.orEmpty()

memberships
.filter { membership -> membership.getSubsystemName() in facadeSubsystemNames }
.map(SubsystemMembership::describe)
.forEach { subsystemName ->
this.logger.error("$className of $subsystemName takes its facade $dependencyName", type)
}
}
}
Loading
Loading