Skip to content

abuchanan-airbyte/kotlin-inspector

Repository files navigation

Kotlin Inspector

A precise Kotlin code parser and call graph analyzer built on the official Kotlin compiler. Analyze function calls and understand code dependencies within your Kotlin projects.

Features

  • Precise Parsing: Uses the official Kotlin compiler's PSI (Program Structure Interface) for accurate code analysis
  • Function Discovery: List all functions in a Kotlin project with their fully qualified names
  • Call Analysis: Analyze which functions are called by a given function
  • Call Tree Visualization: View the complete call graph as a hierarchical tree, with configurable depth
  • Interactive HTML Export: Generate beautiful, interactive HTML pages with collapsible call trees and searchable function lists
  • Class Method Support: Analyze both top-level functions and class methods

Building

./gradlew build

Usage

The tool recursively parses all .kt files under the specified path, automatically excluding build, target, .gradle, and node_modules directories.

Tip: For multi-module projects, point to the root src directory or project root to analyze calls across all modules.

List all functions in a project

./gradlew run --args="list <project-path>"

Example:

./gradlew run --args="list examples/sample-project"

# For larger projects, use the src directory or project root
./gradlew run --args="list /path/to/project/src"

Output:

All functions in project:
  com.example.main (Main.kt:3)
  com.example.initializeApp (Main.kt:10)
  com.example.DataProcessor.process (Utils.kt:4)
  ...

Analyze call chain

./gradlew run --args="calls <project-path> <function-name> [max-depth]"

Examples:

# Full call chain (depth 5)
./gradlew run --args="calls examples/sample-project main 5"

# Direct calls only (depth 1)
./gradlew run --args="calls examples/sample-project main 1"

# Analyze across multiple modules - use broader path
./gradlew run --args="calls /path/to/project/src getCheckInput 15"

Output (depth 5):

Call tree for: main (max depth: 5)

main (Main.kt:3)
├─ cleanup (Main.kt:67)
│  └─ closeConnections (Main.kt:71)
├─ initializeApp (Main.kt:10)
│  ├─ loadConfiguration (Main.kt:15)
│  │  └─ validateConfig (Main.kt:20)
│  └─ setupDatabase (Main.kt:24)
│     └─ connectToDatabase (Main.kt:29)
└─ processData (Main.kt:33)
   ├─ fetchData (Main.kt:39)
   ├─ saveResults (Main.kt:58)
   │  └─ writeToDatabase (Main.kt:63)
   └─ transformData (Main.kt:44)
      ├─ applyFilters (Main.kt:50)
      └─ applyTransformations (Main.kt:54)

Total unique functions in call tree: 15

Generate interactive HTML call tree

./gradlew run --args="html-calls <project-path> <function-name> <output-file> [max-depth]"

Examples:

# Generate HTML call tree with depth 5
./gradlew run --args="html-calls examples/sample-project main call-graph.html 5"

# Generate HTML for a specific function
./gradlew run --args="html-calls /path/to/project/src processData analysis.html 10"

Features:

  • Interactive collapsible tree - Click arrows to expand/collapse branches
  • Expand/Collapse controls - Buttons to expand all, collapse all, or expand to specific levels
  • Beautiful styling - Modern gradient design with hover effects
  • Cycle detection - Functions already shown are marked with "[already shown]"
  • Source location - Each function shows its file and line number
  • Self-contained - Single HTML file with embedded CSS and JavaScript

Generate interactive HTML function list

./gradlew run --args="html-list <project-path> <output-file>"

Examples:

# Generate HTML function list
./gradlew run --args="html-list examples/sample-project functions.html"

# For larger projects
./gradlew run --args="html-list /path/to/project/src all-functions.html"

Features:

  • Searchable table - Real-time filtering of functions
  • Sortable columns - Click headers to sort by name, file, or line number
  • Complete function inventory - Shows fully qualified names with source locations
  • Responsive design - Works on all screen sizes

Architecture

The project consists of four main components:

  1. KotlinParser (src/main/kotlin/com/inspector/KotlinParser.kt)

    • Wraps the Kotlin compiler's parsing capabilities
    • Provides methods to parse individual files or entire projects
    • Uses KotlinCoreEnvironment for accurate compilation context
  2. CallGraphAnalyzer (src/main/kotlin/com/inspector/CallGraphAnalyzer.kt)

    • Builds an index of all functions in the project
    • Resolves function calls within the project scope
    • Constructs call graphs and tracks call chains
  3. HtmlGenerator (src/main/kotlin/com/inspector/HtmlGenerator.kt)

    • Generates interactive HTML pages from analysis results
    • Creates collapsible call trees with JavaScript controls
    • Produces searchable function list tables
    • Self-contained HTML with embedded CSS and JavaScript
  4. CLI Interface (src/main/kotlin/com/inspector/Main.kt)

    • Provides command-line interface for the inspector
    • Implements list, calls, html-calls, and html-list commands

How It Works

  1. Parsing: The Kotlin compiler's PSI is used to parse Kotlin source files into abstract syntax trees
  2. Indexing: All functions are indexed with their fully qualified names
  3. Call Resolution: Function calls are resolved by traversing the AST and matching call expressions to function declarations
  4. Graph Construction: Call relationships are tracked and can be traversed to arbitrary depth

Call Resolution Strategy

The analyzer uses intelligent heuristics to resolve function calls within your project:

  1. Same file - Prioritizes functions in the same file
  2. Same package - Then looks for functions in the same package
  3. Project-wide - Finally searches across the entire project
  4. Test file filtering - Excludes test files when resolving calls from production code
  5. Standard library filtering - Filters out common stdlib methods (e.g., contains, map, filter) to avoid false matches

Limitations

  • Symbol resolution is heuristic-based within the project scope (not full semantic analysis)
  • External library calls are not tracked
  • Overloaded functions are resolved by name and context only
  • Method calls with receivers use simple filtering to avoid stdlib matches
  • Requires Java 21 or higher

Requirements

  • Java 21+
  • Gradle 8.5+

Example Project

An example Kotlin project is included in examples/sample-project/ for testing the inspector.

License

This project uses the Kotlin compiler which is licensed under Apache License 2.0.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published