Verify Classes
Konsist enables development teams to enforce structural rules for classes, ensuring code consistency across projects.
To verify classes start by querying all classes present in the project:
Konsist
.scopeFromProject()
.classes()
...Konsist allows you to verify multiple aspects of a class. For the complete list of available APIs, explore KoClassDeclaration in your IDE (code completion and KDoc).
Let's look at a few examples.
Verify Name
Class names can be validated to ensure they follow project naming conventions and patterns.
Check if class name ends with Repository:
...
.assertTrue {
it.hasNameEndingWith("Repository")
}Verify Modifiers
Class modifiers can be validated to ensure proper encapsulation and access control.
Check if class has internal modifier:
Verify Annotations
Class-level and member annotations can be verified for presence, correct usage, and required attribute values.
Check if class is annotated with Service annotation:
Verify Package
Package declarations can be validated to ensure classes are located in the correct package structure according to architectural guidelines.
Check if class has model package or sub-packages (.. means include sub-packages):
Verify Methods
Methods can be validated for their signatures, modifiers, annotations, naming patterns, return types, and parameter structures.
Check if methods (functions defined inside class) have no annotations:
See Verify Functions.
Verify Properties
Properties can be checked for proper access modifiers, type declarations, and initialization patterns.
Check if all properties (defined inside class) have val modifier:
See Verify Properties.
Verify Constructors
Primary and secondary constructors can be validated for parameter count, types, and proper initialization.
Check if class has explicit primary constructor:
Check if primary constructor is annotated with Inject annotation:
Verify Generic Type Parameters
Generic type parameters and constraints can be checked for correct usage and bounds declarations.
Check if class has no type parameters:
Verify Generic Type Arguments
Generic type arguments can be checked for correct usage.
Check if parent has no type arguments:
Verify Parents
Inheritance hierarchies, interface implementations, and superclass relationships can be validated.
Check if class extends CrudRepository:
Verify Companion Objects
Companion object declarations, their contents, and usage patterns can be verified for compliance.
Check if class has companion object:
Verify Members Order
The sequential arrangement of class members can be enforced according to defined organizational rules.
Check if class properties are defined before functions:
Last updated