Konsist
GitHubSlack (kotlinlang)Twitter
  • 🚀GETTING STARTED
    • What is Konsist?
    • Getting Started
      • Add Konsist Dependency
      • Create First Konsist Test - Declaration Check
      • Create Secound Konsist Test - Architectural Check
    • Articles & Videos
  • ✅WRITING TESTS
    • Create The Scope
    • Declaration Filtering
    • Declaration Assertion
    • Architecture Assertion
    • Suppress Konsist Test
  • ✏️VERYFYING CODEBASE
    • Verify Classes
    • Verify Interfaces
    • Verify Functions
    • Verify Properties
    • Verify Generics
    • Verify Source Declarations
  • 📗FEATURES
    • Add Konsist Existing To Project (Baseline)
    • Debug Konsist Test
    • Declaration
    • Declaration Vs Property
    • Compiler Type Inference
    • Package Wildcard
    • Declaration References
    • Indirect Parents
    • Kotest Support
  • 🔍INSPIRATION
    • Starter Projects
    • Snippets
      • General Snippets
      • Android Snippets
      • Spring Snippets
      • Test Snippets
      • JUnit Snippets
      • Kotest Snippets
      • Architecture Snippets
      • Clean Architecture Snippets
      • Kotlin Serialization Snippets
      • Library Snippets
      • Generic Types Snippets
  • 🎓ADVANCED
    • Isolate Konsist Tests
    • Enable Full Command Line Logging
    • Dynamic Konsist Tests
      • Explicit Test Names
    • When Konsist API Is Not Enough
    • Additional JUnit5 Setup
    • Why There Are No Pre-defined Rules?
    • Konsist Snapshots
  • ❓HELP
    • Getting Help
    • Known Issues
      • java.lang.OutOfMemoryError: Java heap space
    • Compatibility
  • ℹ️OTHER
    • Changelog
    • Project Status
    • Contributing
    • Contributors
    • Assets And Logos
    • Open Source Licenses
    • Sponsor Konsist
Powered by GitBook
On this page
  • Verify Name
  • Verify Modifiers
  • Verify Annotations
  • Verify Package
  • Verify Methods
  • Verify Properties
  • Verify Constructors
  • Verify Generic Type Parameters
  • Verify Generic Type Arguments
  • Verify Parents
  • Verify Companion Objects
  • Verify Members Order
Edit on GitHub
Export as PDF
  1. VERYFYING CODEBASE

Verify Classes

PreviousSuppress Konsist TestNextVerify Interfaces

Last updated 5 months ago

Konsist enables development teams to enforce structural rules for class ensuring code consistency across projects.

To verify classes start by querying all classes present in the project:

Konsist
.scopeFromProject()
.classes()
...

The above code selects all classes present in the project codebase. While this demonstrates Konsist's API capabilities, in practical scenarios you'll typically want to verify a specific subset of classes - such as those with a particular name suffix or classes within a given package. See Create The Scope and Declaration Filtering.

Konsist allows you to verify multiple aspects of a class. For a complete understanding of the available APIs, refer to the language reference documentation for .

Let's look at 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:

...
.assertTrue {
   it.hasInternalModifier
}

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:

...
.assertTrue {
   it.hasAnnotationOf(Service::class)
}

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):

...
.assertTrue {
   it.resideInPackage("com.lemonappdev.model..")
}

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:

...
.functions()
.assertTrue {
   it.annotations.isEmpty()
}

See .

Verify Properties

Properties can be checked for proper access modifiers, type declarations, and initialization patterns.

Check if all properties (defined inside class) has val modifiers:

...
.properties()
.assertTrue {
   it.isVal
}

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:

...
.assertTrue {
   it.hasPrimaryConstructor
}

Check if primary constructor is annotated with Inject annotation:

...
.primaryConstructors
.assertTrue {
    it.hasAnnotation(Inject::class)
}

Verify Generic Type Parameters

Generic type parameters and constraints can be checked for correct usage and bounds declarations.

Check if class has not type parameters:

...
.assertFalse {
    it.hasTypeParameters()
}

Verify Generic Type Arguments

Generic type arguments can be checked for correct usage.

Check if parent has no type arguments:

...
.parents()
.assertFalse {
    it.hasTypeArguments()
}

Verify Parents

Inheritance hierarchies, interfaces implementations, and superclass relationships can be validated.

Check if class extends CrudRepository:

...
.assertTrue {
   it.hasParentOf(CrudRepository::class)
}

Verify Companion Objects

Companion object declarations, their contents, and usage patterns can be verified for compliance.

Check if class has companion object:

...
.assertTrue { declaration ->
    declaration.hasObject { it.hasCompanionModifier }
}

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:

...
.assertTrue {
    val lastKoPropertyDeclarationIndex = it
        .declarations(includeNested = false, includeLocal = false)
        .indexOfLastInstance<KoPropertyDeclaration>()
    
    val firstKoFunctionDeclarationIndex = it
        .declarations(includeNested = false, includeLocal = false)
        .indexOfFirstInstance<KoFunctionDeclaration>()
    
    if (lastKoPropertyDeclarationIndex != -1 && firstKoFunctionDeclarationIndex != -1) {
        lastKoPropertyDeclarationIndex < firstKoFunctionDeclarationIndex
    } else {
        true
    }
}

✏️
KoClassDeclaration