Skip to content

</> logotype Kotlin Analysis API

Introduction

The Kotlin Analysis API is a library for analyzing Kotlin code at the semantic level. It provides a structured way to query information about Kotlin code, including symbols, types, and semantic relationships between them. Built on top of Kotlin's PSI syntax trees, the API offers a clean abstraction over the compiler's internal representations, making it accessible both for IDE plugin makers and command-line tool developers.

While the Analysis API uses the Kotlin compiler internally, the API layer itself does not expose any compiler internals. The library offers both source and binary backward compatibility for its stable parts.

The Analysis API encapsulates all challenging parts needed for efficient Kotlin code analysis, including lazy resolution and cache invalidation, so you can focus on the actual code handling logic.

Quick Example

The hasStringType() function below checks if the given expression has a non-null kotlin.String type.

@RequiresReadLock
fun KtExpression.hasStringType(): Boolean {
    analyze(this) {
        return expressionType == builtinTypes.string
    }
}

Most of the Analysis API surface can be accessed from inside analyze {} blocks. Such is the expressionType extension property – for the given KtExpression, it returns the resolved type.

builtinTypes is also a part of the Analysis API available inside the analysis block. It provides access to basic Kotlin types, including number types, Any, Nothing, and String.

The type of builtinTypes.string is a KaType. Ka is a common prefix for all Analysis API components and abstractions which stands for "Kotlin Analysis".

Using in IntelliJ IDEA

The Analysis API is not just some external API. It serves as the foundation for many features within the Kotlin plugin for IntelliJ IDEA, including code completion, navigation across declarations, refactorings, inspections, the debugger, and many more. By using the Analysis API, you get the same tools as the developers of the Kotlin plugin itself.

Using in Command-Line Tools

The Analysis API can also be used in command-line tools that perform Kotlin code analysis, including linters, documentation generators, and code-generating utilities. This functionality is provided through the Standalone mode.

Note

While developers are welcome to experiment with the Standalone mode, it is currently in active development and subject to incompatible changes.

Read about Analysis API

Projects using Analysis API

Have a project using Analysis API? Contact us to be listed here!

Report an Issue

Kotlin Analysis API works with JetBrains YouTrack task management system. To report an issue you've encountered, please select KT project as well as a suitable Analysis API subsystem when creating a ticket.