Declaration Query And Filter

Query and filter declarations using Konsist API

Declaration Querying

Declaration querying allows to retrieval of declarations of a given type. It is the middle step of the Konsist config preceded by scope retrieval (Create The Scope) and followed by the verification (Declaration Assertion) step.

Typically, verification has performed a collection of declarations such as methods marked with particular annotations or classes located within a single package.

Every Create The Scope contains a set of declarations (Declaration) such as classes (KoClass), properties (KoProperty), functions (KoFunction), etc. The KoScope class provides a set of methods to access Kotlin declarations. Each method returns a list representing a declaration subset:

Method

Description

files()

returns all files present in the scope

packages()

returns all packages present in the scope

imports()

returns all imports present in the scope

classes()

returns all classes present in the scope

interfaces()

returns all interfaces present in the scope

objects()

returns all objects present in the scope

functions()

returns all functions present in the scope

properties()

returns all properties present in the scope

companionObjects()

returns all companion objects present in the scope

typeAliases()

returns all type aliases present in the scope

declarations()

returns all declarations present in the scope

To get all classes from the given scope use KoScope.classes() method:

koScope
    .classes()

Here is an example of querying all properties defined inside classes:

    koScope
        .classes()
        .properties()
        .assertTrue { 
            //...
        }

Filter Declarations

More granular filtering can be applied to additionally filter classes annotated with certain attributes like classes annotated with UseCase annotation.

Konsist is compatible with Kotlin Collection processing API, so the filter method can be used to filter the content of the List<KoClass>: Here filter return classes annotated with UseCase annotation:

koScope
    .classes()
    .filter { it.hasAnnotationOf<UseCase>() }
    .assertTrue { 
        //... 
    }

Konsist provides a set of with... extensions to simplify the filtering syntax. The above snippet can be improved:

koScope
    .classes()
    .withAllAnnotationsOf(UseCase::class)
    .assertTrue { 
        //...
    }

The.withAllAnnotationsOf(Annotation1::class, Annotation2::class) filter classes heaving all annotations present (Annotation1 and Annotation2).

The.withSomeAnnotationsOf(Annotation1::class, Annotation2::class) filterclasses heavingat least one annotation (Annotation1 or Annotation2).

Multiple conditions can be chained to perform more specific filtering. The below snippet filters classes with the BaseUseCase parent class that resides in the usecase package:

koScope
    .classes()
    .withAllAnnotationsOf(UseCase::class)
    .resideInPackage("..usecase")
    .assertTrue { 
        //...
    }

It is also possible to filter declarations by using certain aspects e.g. visibility modifiers. Usage of providers allows verifying the visibility of different declaration types such as classes, functions, properties, etc:

koScope
    .declarations()
    .declarations<KoVisibilityModifierProvider>()
    .assertTrue { it.hasInternalModifier() }

Query And Filter Declaration

Queuing and filtering stages can be mixed to perform more specific checks. The below snippet filters classes reside in the controller package retrieves all properties, and filters properties with Inject annotation:

koScope
    .classes() // query all classes
    .resideInPackage("..controller") // filter classes in 'controller' package
    .properties()  // query all properties
    .withAnnotationOf<Inject>() // filter classes in 'controller' package
    .assertTrue { 
        //...
    }

To print all declarations within use the print() method:

koScope
    .classes()
    .properties()
    .print()

Last updated