Select audit¶
Incubating
The select audit (model-query-select-audit, UnselectedColumnRead and SelectAuditReport) is annotated
@Incubating (see API stability).
A model is only as complete as its select set: an unselected record component reads null and an unselected class
field keeps its initialiser. Code that reads a property the query did not select gets a silent wrong value, often
several method calls away from the query. The opposite mistake — a select set wider than any reader needs — costs
joins and bytes without failing anything.
model-query-select-audit is a test-scope Java agent that watches your existing test suite with no test code changes:
- Fails an unselected read with
UnselectedColumnRead(AssertionError) at the line that reads the property, naming the query call site that built the model and its selection. - Writes a report when the JUnit session closes, listing unselected reads, serializer warnings, and selected columns that no test ever read per query call site and child load.
Setup¶
Add model-query-select-audit in test scope and attach its jar as a -javaagent at JVM startup. The module implements
a non-API hand-off observer in model-query-jpa and shades Byte Buddy under a relocated package, so it must resolve at
model-query-jpa's exact version from model-query-bom.
<dependencies>
<dependency>
<groupId>io.github.rey5137</groupId>
<artifactId>model-query-select-audit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Populates ${io.github.rey5137:model-query-select-audit:jar} -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} -javaagent:${io.github.rey5137:model-query-select-audit:jar}</argLine>
</configuration>
</plugin>
</plugins>
</build>
val selectAuditAgent by configurations.creating {
isTransitive = false
}
dependencies {
testImplementation(platform("io.github.rey5137:model-query-bom:0.16.0"))
testImplementation("io.github.rey5137:model-query-select-audit")
selectAuditAgent("io.github.rey5137:model-query-select-audit:0.16.0")
}
tasks.test {
systemProperty(
"modelquery.selectAudit.dir",
layout.buildDirectory.dir("model-query").get().asFile.absolutePath
)
jvmArgumentProviders.add(CommandLineArgumentProvider {
listOf("-javaagent:${selectAuditAgent.singleFile.absolutePath}")
})
}
No Spring bean or test configuration is needed: model-query-jpa discovers the hand-off observer via ServiceLoader
and JUnit Platform discovers the session listener automatically, so plain JPA tests, Spring tests, and repositories are
audited alike.
- Startup
-javaagentrequired. Model classes are recognised from their class-file annotations and hooked as they load (and JDK 21+ warns on dynamic agent loading under JEP 451). With the jar on the test classpath and no-javaagentattached, the JUnitLauncherSessionListenerfails the test session immediately when it opens, naming theargLineto add, so a test suite never passes silently unaudited. The agent must be attached at JVM start: a late attach, after the first query ran, leaves the audit off for that class loader. - Test scope only. Without
model-query-select-auditon the classpath,model-query-jpauses a no-op observer that changes neither SQL nor results. If the audit jar is accidentally placed on the runtime classpath outside JUnit, it logs one warning and does nothing, and the executor keeps no rows for it. - Newer JDKs. Because Byte Buddy is shaded and relocated inside the agent jar so it never clashes with Mockito's or
your application's version, running on a newer JDK than the release was built with may require upgrading
model-query-select-audit.
Failing an unselected read¶
When application code reads a mapped model property (via a record accessor, a getX/isX getter, or a direct field
read inside a method on the model itself) that was not selected in the row that built the model, the hook throws
UnselectedColumnRead (a subclass of AssertionError, so catch (Exception e) in application code does not swallow
it):
UnselectedColumnRead: OrderView.customerEmail was not selected
read at InvoiceMailer.compose(InvoiceMailer.java:57)
model of OrderService.search:142 list, select INVOICE_SET
add QOrderView.CUSTOMER_EMAIL to the select set, or ignore this reader (modelquery.selectAudit.ignore)
@Joinand@Childproperties. A@Joinproperty is selected when its presence key is selected; a presentOptionaltags the nested model from the scoped row, and an emptyOptionaltags nothing. A@Childproperty is selected only when the query's fetch plan loads it; reading an unloaded@Childfails at the read, and loaded child models are tagged from the child query's rows and reported under the parent call site aschild <name> select <set>.- Engine and
Objectreads do not count. Reads made by the mapper, generatedwithmethods, fetch plans, enrichers,@Selectedaccessors, or inside a model's ownequals,hashCodeandtoString(including Lombok's) neither fail nor count as application reads. - Mapping callbacks.
Row.getandRow.rawreads insideafterMapor a finisher count as reads of those columns (so they are not reported unread) and never fail. - Report-only mode. Set
-Dmodelquery.selectAudit.failOn=none(defaultunselected) to record unselected reads in the report without throwingUnselectedColumnRead.
Serializers and ignore rules¶
- Serializers warn, never fail. The audit classifies a read by the first frame outside the model, skipping JDK,
hidden and generated accessor frames (including Jackson Blackbird and Afterburner). Reads from Jackson 2
(
com.fasterxml.jackson.), Jackson 3 (tools.jackson.) and JSON-B (jakarta.json.,javax.json.) getter paths log a warning and are listed in the report without failing the test: a partial?fields=response (FieldIndex) reads unselected getters by design when@JsonInclude(NON_NULL)dropsnullvalues. Serializer reads of selected columns still count as reads. - Ignoring a reader (
modelquery.selectAudit.ignore). Pass comma-separated package prefixes orClass#methodentries in-Dmodelquery.selectAudit.ignore=...for code that intentionally reads an unselected property and handlesnull(or a test asserting that an unselected field isnull). An ignored reader never fails, but its reads of selected columns still count as reads, so the report never advises dropping a column an ignored reader uses.- A rule matches only the immediate reading frame (after skipping JDK, hidden, generated and audit frames), never a caller further up the stack.
- A
Class#methodrule (com.example.OrderTest#shouldLeaveNotesNull) also matches synthetic lambda frames declared in that method (lambda$shouldLeaveNotesNull$0). - A method reference invoked by a library (for example,
assertThat(order).extracting(OrderView::notes)) has the library as its reading frame and therefore requires a package rule for that library (org.assertj.). - A read guarded by
order.selected().contains(QOrderView.NOTES)needs no ignore entry, because it runs only when the column was selected.
The report¶
When the JUnit session closes, the listener writes a report to target/model-query/select-audit-fork-<n>-<pid>.txt (override
the directory with -Dmodelquery.selectAudit.dir=...) and logs a one-line summary:
UNSELECTED READS (2)
OrderView.customerEmail read at InvoiceMailer.compose:57 model of OrderService.search:142 list x4
OrderView.total read at ReportJob.run:88 (may not have failed a test) model of ReportService.load:31 stream
SERIALIZER READS OF UNSELECTED COLUMNS (warning)
OrderView.internalNote read by Jackson model of OrderController.list:40 page x12
UNREAD COLUMNS (warning)
OrderService.search:142 list, select INVOICE_SET: OrderView.createdBy, OrderView.updatedAt
never read in 37 calls across 12 tests
OrderService.search:142 child lines select LINE_SET: LineView.sku
READS OF UNMAPPED INHERITED FIELDS (warning)
OrderView.auditedBy (BaseView.getAuditedBy) read at AuditLog.write:19 model of OrderService.search:142 list
MODELS NOT OBSERVED (hook missing)
LegacyView model of LegacyService.load:12 list x3: loaded before the agent; reads not recorded
- Other-thread reads (
may not have failed a test). Each tag records the thread that handed the model off. An unselected read on a different thread (such as an@Asynctask or a worker pool whose future was not awaited) is labelled(may not have failed a test)because the audit cannot know whether the test joined that thread. Because the check compares against the tag's creation thread, the label is conservative and also appears when a test spawns and joins a worker thread. - Tests counted. "across N tests" counts the distinct test methods that reached the call site, taken from the JUnit launcher on the calling thread.
- Unread columns. Counted per query call site and
SelectSet(and per child load): any column in the query's ownselect()that no test read on any model returned from that call site and select set, and no mapping callback read throughRow. Primary keys or foreign keys added internally by the engine for paging or child matching, and queries replayed byPortabilityCheck, are excluded. Unread columns are always a warning, never a failure, because they are bounded by what your tests exercise. - Unmapped inherited fields. Only a model's own
@Columnfields are mapped by the processor; a getter inherited from a superclass reading an unmapped superclass field is reported as a warning rather than an unselected failure. -
Merging multiple forks. Each JVM writes a unique fork file (
select-audit-fork-<n>-<pid>.txtunder Surefire or Failsafe, orselect-audit-<pid>.txtotherwise) soreuseForks=falseand Surefire + Failsafe never overwrite each other. Each file starts with a run header: the run id is-Dmodelquery.selectAudit.runwhen set, else the parent process and its start instant, which all forks of one Maven build (Surefire and Failsafe) share. The merge keeps only the newest run's files and logs one warning naming how many older files it skipped, so a build withoutcleannever reports from an earlier run. A fork whose parent or start time is unknown is never skipped. A long-lived parent, such as a Gradle daemon or an IDE, keeps one run id across builds (not verified): set-Dmodelquery.selectAudit.runper build, or clean the directory (build/model-queryvia-Dmodelquery.selectAudit.dirfor Gradle). When a build runs more than one test JVM, merge all fork files intoselect-audit.txtafter the test phase (a column is reported unread only if no fork read it):./mvnw exec:java -Dexec.classpathScope=test \ -Dexec.mainClass=com.rey.modelquery.audit.SelectAuditReporttasks.register<JavaExec>("mergeSelectAuditReport") { classpath = sourceSets.test.get().runtimeClasspath mainClass.set("com.rey.modelquery.audit.SelectAuditReport") args(layout.buildDirectory.dir("model-query").get().asFile.absolutePath) }
Mockito, JaCoCo, and JDK 17/21 records¶
Mockito agent ordering and the JDK 17/21 record ClassFormatError¶
Either -javaagent may come first on the command line when combining model-query-select-audit with Mockito's agent:
<argLine>
-javaagent:${io.github.rey5137:model-query-select-audit:jar}
-javaagent:${org.mockito:mockito-core:jar}
</argLine>
On JDK 17 and 21, HotSpot has a defect when retransforming a record whose record component carries a
CLASS-retention annotation (such as @PrimaryKey, @Column, @Join or @Child): the class file rebuilt by the JVM
for retransformation has a malformed Record attribute and fails with ClassFormatError. Furthermore, Mockito 5.23
marks a class as instrumented before retransforming it and clears that mark only on an Exception, not an Error;
after the first ClassFormatError on a spy or mock of an annotated record, a subsequent mock(...) of the same record
silently returns an uninstrumented instance that is not a mock. (JDK 25 fixes the rebuilt Record attribute.)
With model-query-select-audit attached at startup, the audit agent transforms model records as they load using a
retransform-capable transformer (RedefinitionStrategy.RETRANSFORMATION, TypeStrategy.DECORATE,
disableClassFormatChanges()). Because the class was modified at load time by a retransform-capable agent, HotSpot
caches the original class file bytes and hands them to later retransforms (such as Mockito's inline mock maker) instead
of rebuilding the class file from memory. As a result, Mockito spies and mocks of annotated model records work on JDK
17, 21 and 25 when the audit agent is attached, and the audit hooks survive Mockito re-instrumentation.
JaCoCo agent ordering¶
The audit agent works whether JaCoCo's agent is placed before or after model-query-select-audit on the argLine. In
Maven, let jacoco-maven-plugin's prepare-agent goal populate @{argLine} and append the audit agent after it (or
pass both -javaagent flags explicitly in maven-surefire-plugin / maven-failsafe-plugin):
<argLine>
@{argLine}
-javaagent:${io.github.rey5137:model-query-select-audit:jar}
-javaagent:${org.mockito:mockito-core:jar}
</argLine>
Because the audit agent recognises models from the class file's constant pool and annotations rather than reflection,
and restricts its changes to method bodies (disableClassFormatChanges()), JaCoCo coverage probes and audit hooks
coexist in either order.
Surefire useSystemClassLoader=false¶
Surefire's isolated class loader (useSystemClassLoader=false) loads the audit classes a second time, apart from the
copy the agent attached to. The session then fails with "agent not installed" although the agent is attached. Keep
the default useSystemClassLoader=true (not reproduced in this build).
What the audit does not see¶
- Branches no test runs, just like any dynamic coverage tool.
- Models built outside an executor hand-off: direct
newcalls, copies built by a custom user wither, direct calls toBuiltQuery.map(Tuple), or a customModelQueryExecutorimplementation. - Direct reads of a
publicfield from another class, which would require transforming every caller class. Field reads inside the model's own methods are hooked and attributed to the caller of that model method. - Reflective field reads, including Gson and Jackson or JSON-B configured for field visibility rather than getters. (JSON-B and Jackson getter reads are seen and classified as serializer reads; generated bean mappers such as MapStruct call ordinary accessors and are seen as application reads.)
- Reads on a model whose hook a later agent overwrote by completely replacing accessor bytecode without preserving prior instructions.
- Columns read and then discarded: if application code calls an accessor and ignores the return value, the column counts as read.