Fix Java resource leaks flagged by static analysis
Feed leak warnings from a static analyzer to a model that rewrites the code with try-with-resources, then test before review.
The use case
Unclosed files, streams and connections cause slow failures in long-running Java services. Uber described FixrLeak, which takes resource-leak findings from static analysis, checks that the leak is local to one function, and asks a model to rewrite it using try-with-resources. Each patch is built and tested, then sent as a pull request. Limiting the scope to simple, function-local leaks kept the success rate high. Engineers still review each PR, especially where the resource is passed to or returned from other methods, since closing it early would break callers.
Example workflow
- Run static analysis to list resource-leak warnings
- Filter to leaks contained in a single function
- Ask the model for a try-with-resources rewrite
- Build and run tests on each patch
- Open a PR for owner review
Tools
- SonarQube
- LLM API
- Java
Risks and guardrails
- Closing a resource that escapes the function breaks callers
- Tests may not cover the cleanup path
- Keep each PR to one leak for easy review
Sources
Written by Agents Lookup. Credits and official pages for the tools this use case names:
- Uber engineering blog: FixrLeak: www.uber.com/blog/fixrleak-fixing-java-resource-leaks-with-genai
- SonarQube MCP server (official page): github.com/SonarSource/sonarqube-mcp-server (checked 2026-11-09)