Skip to content

feat: Kotlin support - #18

Merged
floriankraemer merged 9 commits into
mainfrom
feat/kotlin
Nov 12, 2026
Merged

floriankraemer merged 9 commits into
mainfrom
feat/kotlin

Conversation

@floriankraemer

Copy link
Copy Markdown
Owner

Summary

The analyser now measures Kotlin (.kt) as well as Java, including projects that mix both.
Only parsing forks by file extension.
The metrics model, scoring, reports, baselines, cache, churn and coverage are shared.

  • Parser: kotlin-compiler-embeddable 2.4.21 PSI, without type resolution. The environment is created lazily, once per process, and only when a Kotlin file is found. Compiler messages are suppressed.
  • New collector KotlinMetricsCollector and visitors for the 8 cognitive metrics, cyclomatic, Halstead and understandability. Their rules mirror the Java visitors.
  • Units: functions of classes, data and enum classes, interfaces (with a body), objects and companion objects (pkg.Outer.Companion). Top-level and extension functions are keyed under the JVM facade class (pkg.FileNameKt, which respects @file:JvmName). Secondary constructors are named after their class. Overloads are keyed name(Type,...), with an extension receiver as the first type. init blocks, accessors, enum entry bodies, anonymous objects and local classes are skipped. Lambdas and local functions belong to their function. @cca-ignore works in KDoc and in comments.
  • Mapping: when counts like switch, and an if expression like if. Elvis is +1 cyclomatic only. ?. and !! add no cognitive counts. Labeled returns count. Implicit it counts when it is used. Implicit this property reads follow the Java heuristic. The full table is in the README and in docs/Cognitive-Complexity-Analysis.md.
  • FileFinder finds .java and .kt files. .kts is excluded and documented.
  • Parse errors print Parser failed for file <path>: (line L,col C) <message> and the run continues.
  • Cache: the key already contains the path, so the extension (language) is part of the key. The payload is unchanged, so CACHE_FORMAT_VERSION stays the same.
  • Coverage: JaCoCo, Cobertura and Clover class names map onto the Kotlin keys as they are. Internal functions (audit$module) now match by their source name.
  • The pom description and the CLI help say "Java and Kotlin".

Size and startup

before after
shaded jar 5.9 MB 71.9 MB
--help 0.23 s 0.27 s
Java-only analyse src/main/java ~1.7 s ~1.7 s
first Kotlin file - about 0.5 s to create the environment

The shaded jar works without any relocation or transformer.

Test plan

  • ./mvnw -B verify: 847 tests, JaCoCo line 97.7% and branch 94.9%, PIT 99% (1459/1469). The Kotlin classes are covered by the existing domain.* target.
  • Per-construct Kotlin fixtures with exact expected values for all metrics: ifs, returns, units and keys, variables, ignore, cyclomatic, understandability, Halstead and @file:JvmName.
  • Java/Kotlin cross-check: 12 equivalent methods give identical metrics, lines and keys.
  • Facade tests: mixed dispatch, .kts skipped, Kotlin parse error, exclusion of .kt files, and a Kotlin cache round trip.
  • E2E goldens: a mixed project in the console and in JSON, a Kotlin parse error, and Kotlin coverage from a JaCoCo report recorded from the compiled fixture.
  • Smoke test with java -jar target/cognitive-analysis.jar on mixed sources.

Mixed Java and Kotlin projects share one metrics model, so scores, reports,
baselines, cache and churn work unchanged; only parsing forks by extension.
Top-level functions are keyed by their JVM facade class, so keys match what
the JVM and coverage tools report.
JaCoCo names Kotlin facade classes and companions like the analysis keys
already do, but mangles internal functions as name$module; match those by
their source name. Tested against a report recorded from a compiled fixture.
…d reporter loading

Replace the offset comparison of the Kotlin ignore check with a structural one,
drop visits of children that name nothing to count, and test the negated
type and range checks, the XML entity resolver and the context class loader
of reporter constructors.
Editors on Windows write one, and the Java and Kotlin parsers reject it.
The cache key still hashes the raw bytes.
… mangling

Truncating every name at the first dollar sign let synthetic methods such as
run$lambda$0 take the coverage of run. Only a single non-synthetic suffix,
the module name of an internal function, now falls back to the source name.
The shaded jar kept one arbitrary LICENSE and NOTICE and nothing for the
libraries that ship none. It now merges the NOTICE files, lists every bundled
library with its license in META-INF/THIRD-PARTY-LICENSES.txt and keeps the
extra license files of Jackson.
@floriankraemer
floriankraemer merged commit 1b709b1 into main Nov 12, 2026
1 check passed
@floriankraemer
floriankraemer deleted the feat/kotlin branch November 12, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant