Declaration References
Declaration reference represents a link between codebase declarations. Konsist allows to precisely verify properties of linked type. This type can be used in function or property declaration or child/parent class or interface. For example
1. Verify if all types of function parameters are interfaces:
2. Access properties of parents (parent classes and child interfaces). Below snippet checks if parent class has internal
modifier:
3. Access properties of children (child classes and child interfaces). Below snippet checks if all interfaces have children that resided in ..somepackage..
package:
Type Representation
Kotlin types can defined in multiple ways. Consider foo
property with Foo
type:
The Foo
type can be defined by:
class
interface
object
type alias
import alias
kotlin types (Kotlin basic type or Kotlin collections type)
function type
external library (type defined outside project codebase) represents declaration which is not defined in the project
The Foo
type can be represented by one of KoXDeclaration
classes:
KoClassDeclaration
KoInterfaceDeclaration
KoObjectDeclaration
KoTypeAliasDeclaration
KoImportAliasDeclaration
KoKotlinTypeDeclaration
KoFunctionDeclaration
KoExternalDeclaration
Each of these types possesses a largely distinct set of characteristics; for instance, classes and interfaces can include annotations, whereas import aliases cannot.
To access properties the specific declaration type, the declaration cast to more specific type is required (from generic KoTypeDeclaration
). Example below assumes that Foo
is represented by the Foo
class:
To facilitate testing Konsist API provides set of dedicated casting extensions. The above code can be simplified:
Here is the list of all casting extensions:
KoClassDeclaration
asClassDeclaration
isClass
KoInterfaceDeclaration
asObjectDeclaration
isObject
KoObjectDeclaration
asInterfaceDeclaration
isInterface
KoTypeAliasDeclaration
asTypeAliasDeclaration
isTypeAlias
KoImportAliasDeclaration
asImportAliasDeclaration
isImportAlias
KoKotlinTypeDeclaration
asKotlinTypeDeclaration
isKotlinType
KoFunctionDeclaration
asFunctionTypeDeclaration
isFunctionType
KoExternalDeclaration
asExternalTypeDeclaration
isExternalType
Type Represented By Class
Source code:
Usage:
Konsist test:
Type Represented By Interface
Source code:
Usage:
Konsist test:
Type Represented By Object
This scenario is uncommon, but still possible.
Source code:
Usage:
Konsist test:
Type Represented By Type Alias
Source code:
Usage:
Konsist test:
Type Represented By Import Alias
Source code:
Usage:
Konsist test:
Type Represented By Kotlin Type
Source code:
Usage:
Konsist test:
Type Represented Function Type
Source code:
Usage:
Konsist test:
Type Represented By External Type
External type represents the type defined outside of the project codebase, usually by external library. Konsist is not able to parse this type, so type information is limited (Konsist is not able to parse the compiled file).
For Example:
The Android ViewModel
class is provided by androidx.lifecycle:lifecycle-viewmodel-ktx
dependency, so Konsist has limited information.
Last updated