HOOK · deterministic-policy

PostToolUse hook: Checkstyle gate

Runs Checkstyle after every Java edit and feeds violations back to Claude as a blocking error, so style findings are fixed in-session rather than in CI.

✓ verified 2026-06-10 · stack: java, claude-code, checkstyle · source

Checkstyle reports; it cannot fix. That makes it the report-side complement to the auto-fixing format-on-edit hook: instead of silently repairing the file, this hook runs the Checkstyle source set after each Java edit and, on violations, exits 2 with the findings on stderr. For a PostToolUse hook the edit has already happened: exit 2 does not undo it, but the stderr text is delivered to Claude as a blocking error it must address, so violations get fixed in the session that caused them.

.claude/hooks/checkstyle-gate.sh
#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
case "$FILE_PATH" in
*/src/main/java/*.java) TASK="checkstyleMain" ;;
*/src/test/java/*.java) TASK="checkstyleTest" ;;
*) exit 0 ;;
esac
OUTPUT=$("${CLAUDE_PROJECT_DIR}/gradlew" -p "${CLAUDE_PROJECT_DIR}" "$TASK" 2>&1)
if [ $? -ne 0 ]; then
echo "Checkstyle violations after editing ${FILE_PATH}:" >&2
echo "$OUTPUT" | grep -E '\.java:[0-9]+' | head -30 >&2
echo "Fix the violations. Do not edit checkstyle.xml or add suppressions." >&2
exit 2
fi
exit 0
.claude/settings.json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "\"${CLAUDE_PROJECT_DIR}\"/.claude/hooks/checkstyle-gate.sh"
}
]
}
]
}
}

Make the script executable (chmod +x .claude/hooks/checkstyle-gate.sh) and commit both files. Gradle’s Checkstyle plugin prints violations to the console by default (showViolations), which is what the grep extracts.

Maven variant. Replace the Gradle invocation with a single-file-scoped check (the include pattern is relative to the source directory):

Terminal window
REL="${FILE_PATH#*src/main/java/}"
OUTPUT=$(mvn -q -f "${CLAUDE_PROJECT_DIR}/pom.xml" checkstyle:check \
-Dcheckstyle.includes="${REL}" 2>&1)

Either way, mind the bundled-version trap: the Maven Checkstyle Plugin 3.6.0 ships Checkstyle 9.3 by default and needs a dependency override (and Gradle needs toolVersion) to run the current 13.x line: exact configuration in linters as ground truth.

What it does

  • Fires after every Edit or Write; non-Java paths and Java files outside src/main/java / src/test/java (generated sources, build output) fall through silently at the case statement.
  • On violations, Claude receives the violation lines (File.java:23:5: Missing a Javadoc comment) as a blocking error: the same feedback loop a failing test provides, but per-edit instead of per-build.
  • The final stderr line pre-empts the cheapest “fix”: telling the agent not to weaken checkstyle.xml or sprinkle suppressions. That instruction arrives at 100% reliability because the hook delivers it, not CLAUDE.md.
  • Pairs with the format-on-edit hook: Spotless owns layout and fixes it silently; Checkstyle owns naming, Javadoc, and magic numbers, and reports. Keep formatting modules out of checkstyle.xml so the two never disagree.

Failure modes

  • It runs the whole source set per edit. checkstyleMain checks every main-source file, so violations in files the agent did not touch also surface, and on a large module the per-edit latency (JVM plus Gradle startup, mitigated by the Gradle daemon) may be noticeable. For big modules, prefer the Maven single-file variant or accept the gate at CI granularity instead.
  • Any task failure exits 2, not just style failures. If checkstyleMain fails because the code does not compile, the grep finds no violation lines and Claude sees only the header. Harmless in practice (the agent re-runs the build and sees the compile error), but be aware the gate is “task failed”, not strictly “style violated”.
  • jq or gradlew absent. The script errors non-zero but not 2, which Claude Code treats as a non-blocking error: the edit stands unchecked. Assert command -v jq in onboarding or a SessionStart hook if that is unacceptable.
  • Hook ordering is not guaranteed. If you register this alongside the Spotless hook on the same event, do not assume the formatter ran first. Safe as long as Checkstyle carries no formatting modules; if yours does, chain both commands in one script instead.
  • It is repo-trust. The script lives in the repo; anyone with write access can weaken it. For organisation-grade enforcement replicate it in managed settings. See hooks as hard gates.

Verified against the hooks guide’s PostToolUse feedback pattern and hook input/exit-code schema, 2026-06-10.