# Java and Kotlin

> How trimtest runs Gradle and Maven tests, records coverage with JaCoCo, and which files stay attached.

| | |
|---|---|
| Runners | Gradle (or `./gradlew`), Maven (or `./mvnw`) |
| Static graph | `import`, wildcard imports, same-package references |
| Recorded tier | yes, with JaCoCo (fetched by the build); function-level (by method signature) |

## Recording

Each test class runs alone under JaCoCo and the executed source files are kept.

- **Gradle:** an init script (`-I`, so nothing in your build files changes) applies the `jacoco` plugin to every
  `java` project and makes the report cover **all modules' classes**, since a test may exercise its dependencies.
- **Maven:** the `jacoco-maven-plugin` goals are invoked directly. For multi-module builds one shared exec file
  collects the whole reactor (`-pl <module> -am`) and a report is produced per module, so a test executing code in a
  dependency module is still seen.

**Vacuous passes.** A Maven reactor build (`-pl <module> -am`, or a root pom with `<modules>`) must let modules
without a matching test pass, so surefire exits 0 even when the selected test ran nowhere (an abstract class, a
renamed class). After such a run trimtest looks for a fresh `surefire-reports/TEST-<class>.xml` (or
`failsafe-reports`) with at least one test for every selected class, and prints
`trimtest: uncertain: <n> selected test(s) ran no tests: ...` with exit code 3 when it is missing. A suite whose
tests were all skipped still counts as having run. Gradle needs no check: `--tests` matching nothing fails the build.

This catches reflection, `Class.forName`, service loaders and DI-created classes that the import graph cannot see.
Verified with a multi-module Gradle 8 build (Java and Kotlin) and Maven builds on JDK 17.

## Function-level selection

With a JaCoCo recording, an edit to a method or function that the test never ran does not select it. Units are
keyed by signature (`Svc.used(int)`, Kotlin `Svc.used(Int)`), so overloads stay apart. Everything else in a type
(fields and property initializers, `init` and static blocks, constructor parameters, supertypes, enum entries, and
the set of members) is its header: a change there selects the tests that ran any method of the type or that mention
the changed name.

For Kotlin, a function's range is its whole declaration, so default-argument expressions and expression bodies
count. Secondary constructors and custom property accessors are functions. Top-level properties, imports and every
`inline` or `reified` function always count, because JaCoCo credits inlined code to the caller's file. A Kotlin
file whose class body starts on the declaration line (`class A { val x = 1 }`) does not parse cleanly with the
grammar trimtest uses and stays at file level.

## What stays attached

JaCoCo cannot vouch for compile-time constants (`static final`, `const val`), `inline` code, statement-less classes
(interfaces, annotations, abstract and sealed types, enums) or test helpers (only main classes are measured). Those
files stay attached to every test that statically depends on them, as do classes a test mocks by name.

Build files (`pom.xml`, `build.gradle*`, `settings.gradle*`, version catalogs, `buildSrc/`, `.mvn/`, wrapper
properties) force a full run of the project.

## Gradle file watching

trimtest passes `-Dorg.gradle.vfs.watch=false` to every Gradle command it builds. Gradle's file watcher can miss an
edit made shortly after a build, so `compileJava` comes back `UP-TO-DATE` and the selected test runs against stale
classes and passes. Gradle before 6.5 ignores the property.

## Limits

- Android modules (no `java` plugin) fall back to the static graph.
- Only the default `test` source set is recorded for Gradle; other source sets (`integrationTest`, `jvmTest`) use
  the static graph.
- Tests that use `ProcessBuilder` or `Runtime.exec` run on any change.
