jade-recipe-1.7-try-with-resources
Converts manual open/close resource management to Java 7 try-with-resources where — and only where — the resource genuinely implements Closeable/AutoCloseable, is closed unconditionally on every path,
SKILL.md.claude/skills/java-migration-skill-registry/1.7/jade-recipe-1.7-try-with-resources/SKILL.md
GitHubjade-recipe-1.7-try-with-resources — manual close() → try-with-resources
Java 7 lets a try declare resources in its own parentheses; anything
declared there that implements java.lang.AutoCloseable (or the older
java.io.Closeable) is closed automatically, in reverse declaration order,
when the block exits — normally or via exception. This recipe converts the
pre-Java-7 manual equivalent (X x = ...; try { ...; x.close(); } finally { ... } and its many variants) to that form, where it is safe to.
Why this rule needs a narrower pattern before it needs a recipe
The manifest's rule pattern is \btry\s*\{ — it matches every try block
in JADE. Measured on the full source tree: 1832 flags, ~11 real candidates,
0.6% precision (D2). Blindly dispatching every flag to a human or an agent
would be almost all noise.
scripts/scan.py narrows this by requiring, inside one try block (nested
try blocks masked out so they aren't double-counted against their
container):
- a variable declared with a type whose name looks Closeable-shaped
(
CLOSEABLE_TYPE_SUFFIXES— java.io streams/readers/writers, java.sqlStatement/ResultSet, java.net sockets, anything literally namedConnection/Closeable/AutoCloseable, etc.), and - a
.close()call on that same variable, either in the try's ownfinallyor, absent afinally, inline in the try's own body.
Measured on migration-runs/jade-1.6-to-1.7/workspace/src/jade/src
(1015 .java files):
| count | |
|---|---|
raw \btry\s*\{ hits |
1880 |
scan.py narrow hits |
47 |
| manually read (all 47) | 47 |
| genuine try-with-resources candidates | 33 |
| precision | 33/47 ≈ 70.2% |
That clears the 70% target the pattern was asked to hit — but barely, and
precision-of-the-shortlist is not the same question as safety-of-a-script.
See the next two sections for why this stays mode: agent regardless.
What scan.py cannot decide — read this before trusting a hit
scan.py is a shortlist, not a verdict. Of the 14 shortlisted hits (30% of
the 47) that were not genuine candidates, read during precision
measurement, most fail for reasons a regex fundamentally cannot see:
6 of the 14 flag a variable whose type merely contains the word
Connectionand has a.close()method, but does not implementCloseable/AutoCloseableat all:jade.imtp.leap.JICP.Connection(BIFEDispatcher.java:625,663,HTTPFEDispatcher.java:457,462) — an abstract class,close()declaredabstract, noimplements Closeable/AutoCloseableanywhere in its hierarchy.jade.imtp.leap.JICP.JICPConnection extends Connection(NATUtils.java:24,79) — inherits the same gap.jade.imtp.leap.nio.NIOJICPConnection extends Connection(BEManagementService.java:1114) — same.KAConnection, a private nested class inKeepAlive.java:203with noimplementsclause at all —close()here is pure naming convention, not a real resource-management contract.
Resolving this requires reading the type's own declaration (and its superclass chain) — exactly the kind of static-type resolution the
string-isemptyrecipe's rationale already established a regex cannot do. Always open the declaration of the flagged type before converting. A name match is not proof.The rest fail on scope/control-flow grounds covered by name in "When NOT to transform" below (resource used outside this try's scope, or close() that is conditional on a branch the resource is still needed in).
Given that ~43% of this shortlist's false positives (6 of 14) come from a
class of error a script cannot detect at all, and the true positives
themselves split across structurally different rewrite shapes (see
"Transform" below), this recipe is mode: agent even though the narrowing
pattern alone clears 70%.
Scope of one task
You receive a shard contract. Edit only the files listed in
editable_files. read_only_context is there so you can resolve a
resource's type or check a declaration outside the edited file — never
modify it. For each entry in entry_points, optionally run
scripts/scan.py --file <that file> first to see which try blocks in it
the narrow pattern already flagged — treat its output as a hint about where
to look, never as a decision.
Transform
For each flagged try at an entry point:
- Confirm it is live code. Use
java_source.is_live_code(source, line)(shared/lib/java_source.py, imported the same way scan.py does) — a flag sitting inside a comment or dead code gets zero edits (D1). - Identify every resource tied to this try: variable name(s), whether declared inside the try or in the statement(s) immediately before it, and their declared type.
- Resolve each resource's real type. Open its declaration (and,
for a non-JDK type, that class's own source) and confirm it actually
implements java.io.Closeableorjava.lang.AutoCloseable— directly, or via a superclass/superinterface. A name like...Connectionor aclose()method proves nothing on its own (see the six real counterexamples above). If you cannot find and confirm the implementation, do not convert — reportSKIPPED. - Confirm unconditional close on every exit path, and that the resource is not touched again after the try/finally construct. See "When NOT to transform" for the shapes that fail this.
- Rewrite: move the resource declaration(s) into the try-with-resources
header, in the same order they were originally declared (try-with-resources
closes them in reverse of that order automatically — matching the manual
close order most of this codebase already uses, e.g.
ACLTracePanel.java:376closesbwthenf, exactly the order a try-with-resources overf, bwwould produce). Remove the manual.close()call(s). Remove any nestedtry { x.close(); } catch (...) {}that existed solely to swallow a checked exception from that oneclose()call — see the exception-swallowing note below before doing this. - Remove the
// JADE-FLAG:TRY_WITH_RESOURCES(orJADE-MODERNIZATION-DEFERRED:TRY_WITH_RESOURCES) comment for that site.
A swallowed close()-exception is a behavior change, not just cleanup
Several genuine candidates wrap only the close() call in its own
try/catch purely to ignore whatever it throws (e.g.
HTTPFEDispatcher.java:461-466: finally { try { c.close(); } catch (Exception e) {} }). Converting to try-with-resources removes that
swallow: if close() throws and the try body did not already throw, that
exception now propagates (or, if the body did throw, is attached as a
suppressed exception on the propagating one) instead of being silently
discarded.
- If the enclosing method already declares/catches that exception type
broadly enough to still handle it (true for every DFDBKB.java
Statementcase in this corpus — its owncatch (SQLException e)on the same try also catches an exception from the implicitstmt.close()), the conversion is safe and the inner swallow-only try can simply go away. - If it does not (the method would now need a new
catchorthrowsclause it did not have before, or the original swallow was deliberately preserving a return value that a propagating close()-exception would now discard), keep an explicitcatchon the try-with-resources statement that reproduces the original swallow, or reportNEEDS_REVIEWrather than silently changing what the method does when a physical resource fails to close.
When NOT to transform
Report SKIPPED (or NEEDS_REVIEW if you already started editing and are
not sure) and leave the code as-is for any of these — each has a real
citation from this corpus:
- The resource's declaration or use spans outside this try's own
scope (before it, after it, or across a sibling try at the same
nesting level). Try-with-resources scopes the resource to exactly one
block; moving a declaration that is used before or after that block
either won't compile or silently narrows the resource's real lifetime.
Sniffer.java:564-590—BufferedReader inis opened and read across an earlier, separate try; the flagged try at line 584 exists only to close it. Converting just that try would leaveinundeclared where it's actually used.FileMessageStorage.java:150—inis read in the try before this one too (line 139, before either try even starts) and only closed in this one'sfinally.PhoneBasedSMSManager.java:86—serveris declared in the enclosing method, then closed from inside an anonymous inner class'srun(). The try that closes it isn't the scope that owns it.
close()is conditional on a branch where the resource is still needed in the other branch(es).BIFEDispatcher.java:625-646(connectInp) —c.close()fires only on theERROR_TYPEbranch; the success branch instead callshandleInpReconnection(c)—cescapes the try when nothing went wrong. (connectOut, line 663, is the same shape.)FileMessageStorage.java:250-276(decrementCounter) —in.close()appears at three separate conditional sites (thecounter==0branch, the nestedfinally, and thecatch(NumberFormatException)) — too much branch-dependent control flow to fold into one auto-close point without changing which path closes what.
- Multiple resources with a closing order that must be verified, not
assumed. This corpus's real multi-resource cases happen to close in
the same order try-with-resources would produce automatically —
ACLTracePanel.java:376(bwwrapsf; manual order isbw.close(); f.close();, matching reverse-declaration order) andLEAPFrameCodec.java:114-137(inpStreamwrapsinpBuffer, same shape). Converting these is safe because the order was checked. Don't assume it always lines up — verify declaration order against manual close order before folding two-or-more resources into one try-with-resources header. - The flagged type does not actually implement
Closeable/AutoCloseable. See "What scan.py cannot decide" above for six real, verified examples in this corpus (jade.imtp.leap.JICP.Connectionand everything that extends it, plusKeepAlive.KAConnection). Having aclose()method is not the same thing as implementing the marker interface — try-with-resources will not compile against a type that only has the former.
Invariants
- No edit outside
editable_files. - No public signature changes — this rule is
body-local; every edit stays inside a method body. - No new imports needed for the conversion itself (the resource types are
already imported wherever they were declared); if you add a
catchto preserve swallow behavior (see above), its exception type must already be resolvable in the file.
Status to report
| status | when |
|---|---|
FIXED |
resource's Closeable-ness confirmed, single unconditional close, no post-try use — rewritten |
SKIPPED |
any "When NOT to transform" condition applies, or the type's Closeable-ness could not be confirmed |
NEEDS_REVIEW |
rewritten, but a swallowed close()-exception's new propagation is not clearly safe |
FAILED |
file unreadable, or the flagged try is not present at the given line |
Uncertainty is a SKIPPED, never a guess.