> For the complete documentation index, see [llms.txt](https://docs.konsist.lemonappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.konsist.lemonappdev.com/features/declaration.md).

# Declaration

What is declaration?

The declaration (`KoBaseDeclaration`) represents a code entity, a piece of Kotlin code. Every parsed Kotlin File (`KoFileDeclaration`) contains one or more declarations. The declaration can be a package (`KoPackageDeclaration`), property (`KoPropertyDeclaration`), annotation (`KoAnnotationDeclaration`), class (`KoClassDeclaration`), etc.

Consider this Kotlin code snippet file:

```kotlin
private const val logLevel = "debug"

@Entity
open class Logger(val level: String) {
   fun log(message: String) {
   
   } 
}
```

The above snippet is represented by the `KoFileDeclaration` class. It contains two declarations - property declaration (`KoPropertyDeclaration`) and class declaration (`KoClassDeclaration`). The `Logger` class declaration contains a single function declaration (`KoFunctionDeclaration` ):

```mermaid
%%{init: {'theme':'forest'}}%%
flowchart TD
    KoFile
    KoFile---KoProperty
    KoFile---KoClass
    KoClass---KoFunction
```

Declarations mimic the Kotlin file structure. Konsist API provides a way to retrieve every element. To get all functions in all classes inside the file use `.classes().functions()`:

```kotlin
koFile // KoFileDeclaration
    .classes()  // List<KoClassDeclaration>
    .functions() // List<KoFunctionDeclaration>
```

{% hint style="info" %}
To print declaration content use `koDeclaration.print()` method.
{% endhint %}

## Declaration Properties

Each declaration contains a set of properties to facilitate filtering and verification e.g. `KoClassDeclaration` has `name`, `modifiers`, `annotations`, `declarations` (containing `KoFunctionDeclaration`) etc. Here is how the `name` of the function can be retrieved.

```kotlin
val name = koFile // KoFileDeclaration
    .classes()  // List<KoClassDeclaration>
    .functions() // List<KoFunctionDeclaration>
    .first() // KoFunctionDeclaration
    .name // String
    
println(name) // prints: log
```

Although it is possible to retrieve a property of a single declaration usually verification is performed on a collection of declarations matching certain criteria eg. methods annotated with specific annotations or classes residing within a single package. See the [Declaration Filtering](/writing-tests/declaration-query-and-filter.md) page.

## Debugging Declaration Properties

Each declaration exposes a few additional properties to help with debugging:

* `text` - provides declaration text e.g. `val role = "Developer"`
* `location` - provides file path with file name, line, and column e.g. `~\Dev\IdeaProject\SampleApp\src\kotlin\com\sample\Logger.kt:10:5`
* `locationWithText` - provides `location` together with the declaration `text`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.konsist.lemonappdev.com/features/declaration.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
