Workbench / The TiffinBox Build
S34 · V3·33
The Language, Audited
Volume 3 begins: the server half of TiffinBox, in Java, chapter by chapter. Its first stage turns chapter 33's sharp edges (silent overflow, boxed equality, default locales, narrowing casts, catching everything) into a javac plugin that runs inside every build with full type information. On TiffinBox's own code it reported 17 places; reading each one found no live bug (Volume 1 had checked ranges before casting), so the stage is about what to do with a linter's findings on correct code: fix what can be made explicit, suppress the rest with a reason. One real gap turned up beside it: Integer.parseInt reads "٥" as 5, and the menu search accepted limit=٥.
Stage 34: the language, audited
The brief
What you'll build.
tools/lint/:LangLint, a javac plugin (the JDK's owncom.sun.sourceAPI, no dependencies) with five rules:locale(String.format,formatted,printf,toLowerCase,toUpperCasewithout aLocale),boxed-eq(==/!=between boxed numbers or Strings),narrowing(a non-constantint/longcast to a smaller type),broad-catch(Exception,Throwable,RuntimeException),float-money(afloatordoublenamed like money);@SuppressWarnings("langlint:RULE")switches one off for one declaration; a test file where every expected warning is marked// expect: RULE;- the builds:
core,serverandtbxcompile with-Xplugin:LangLintand-Werror;release-check.shbuilds the plugin first; - the fixes: checked casts made explicit (
Math.toIntExact), intentional bit casts and last-resort catches suppressed with a reason,Locale.ROOTon two formats, a static check thatOrderLine.MAX_QUANTITYstill fitsDayColumns'sbyte[], and ASCII-only digits for the searchlimit; - tests: the plugin's own, and one server test for
limit=٥.
Why a real company needs this. Code review catches language traps when the reviewer remembers them, on the day they remember them. A rule in the compiler catches them every time, in every module, for every new engineer. The cost is what to do with findings that aren't bugs, and that cost decides whether a team keeps a linter or switches it off.
Why now. Chapter 33 is Java's language foundations compared across languages: types and values, overflow, floating point, equality, strings, records, enums and errors. Volume 1 built TiffinBox's core carefully (records with validation, Money in whole paise, exact arithmetic); Volume 3 grows the server a lot, so this is when the rules should start running.
What you'll have at the end.
- a compiler that refuses five classes of language bug;
- every existing exception to a rule written down where it lives;
- ADR-0036: language rules for the backend.
Theory to own
- Why a compiler plugin and not a regex? (The chapter, "Reference types and value types"; Companion, Code walkthrough.)
- Overflow and narrowing. What does
(byte) 200give, and which Java operations throw instead of wrapping? (The chapter's lab, Experiment 1.) - Locales. Which formatting depends on the default locale, and what changes? (Companion, Gaps 1.)
- Broad catches. When is
catch (Exception e)right? (The chapter, "Error handling models".)
Build it
Step 1: the plugin and its test
Write LangLint as a com.sun.source.util.Plugin: register a TaskListener, and on each ANALYZE event scan the compilation unit with a TreePathScanner that looks at method invocations, binary ==/!=, casts, catch clauses and variable declarations, asking Trees for types and elements. Register it in META-INF/services/com.sun.source.util.Plugin. Write test/Cases.java with a line for each case, good and bad, and test.sh that compares the warnings javac prints with the // expect: marks.
Checkpoint. langlint: 1 test files, every expected warning and no others.
Step 2: run it on TiffinBox
Compile core, server and tbx with the plugin and read every warning.
Checkpoint. A list of findings, each classified as a bug, a missing check, or intended.
Step 3: what parseInt accepts
Check every Integer.parseInt and Long.parseLong on untrusted input. Write exp/34/Digits.java to see what they accept.
Checkpoint. ServerTest refuses limit=%D9%A5.
Senior and architect lens
1. A linter is a policy; its suppressions are the policy's exceptions, reviewed. Every @SuppressWarnings("langlint:…") carries a reason, and they're easy to list (grep -rn langlint: */src). A rule with too many suppressions is a rule to change.
2. ADR-0036: language rules for the backend. Write it with these points:
- Compiler:
-Xlint:all -WerrorandLangLinton every module; new rules arrive with a test file and a run over the whole codebase. - Numbers: money is
Money(whole paise, exact arithmetic); casts that narrow are checked (Math.toIntExact) or suppressed with a reason; untrusted digits are ASCII ([0-9]) before parsing. - Text: data for machines uses
Locale.ROOT; text for people uses the user's locale explicitly (Volume 3's locale work). - Errors: catch what you handle; last-resort catches at boundaries only, logged.
3. Zero findings is a good result. A linter that finds no bugs in careful code still pays: it keeps the code careful as the team and codebase grow.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| null | the rules say nothing about null |
stage 37 (generics: Optional and nullness), stage 46 (API validation) |
| generics and collections | unchecked casts, mutable shared lists | stages 36–37 |
| more rules wanted | the plugin grows ad hoc | stage 44 (build system: Error Prone or a rule registry) |
| locales for people | Locale.ROOT everywhere isn't localisation |
Volume 23 (internationalisation) |
Evidence
tools/lint/build.shprintsevery expected warning and no others.core,serverandtbxbuild with the plugin and-Werror: 44, 40 and 21 tests pass.- Every suppression has a reason;
exp/34/before.txtlists the 17 original findings. - ADR-0036 is committed.
Carry forward
- LangLint gains rules as later chapters find traps (stage 36: mutable collections exposed by records).
Math.toIntExactand ASCII digits are the house style for numbers from outside.- The release check builds the plugin first; every module after it is linted.