Java Code Review
Review Java source code systematically for correctness, security, performance,
and maintainability.
Input Handling
Determine the input type and gather code accordingly:
- Direct code -- Code provided in the conversation. Review as-is.
- File path(s) -- Read the specified
.java files.
- Directory path -- Find all
.java files recursively. For large
codebases (>50 files), ask the user which packages or files to focus on.
- Diff/patch content -- Review only the changed lines plus sufficient
surrounding context. Focus findings on the changed code.
Review Process
Execute these phases in order. For each finding, assign a severity.
Phase 1: Correctness and Bug Detection
Examine code for:
- Null safety -- Nullable parameters without checks, Optional misuse,
potential NullPointerException paths
- Resource leaks -- Unclosed streams, connections, or locks. Verify
try-with-resources usage for all AutoCloseable types
- Error handling -- Empty catch blocks, catching overly broad exceptions
(Exception, Throwable), swallowed exceptions, missing finally blocks
- Concurrency -- Race conditions, unsynchronized shared mutable state,
incorrect use of volatile, double-checked locking without volatile,
ConcurrentModificationException risks
- Logic errors -- Off-by-one, incorrect operator precedence, unreachable
code, broken equals/hashCode contracts
- API misuse -- Incorrect use of Collections, Streams, Date/Time API,
String comparison with == instead of .equals()
Phase 2: Security Review
Read references/security-checklist.md
before this phase.
Check for:
- Injection -- SQL injection (string concatenation in queries), command
injection, LDAP injection, XPath injection, log injection
- Authentication and authorization -- Hardcoded credentials, missing
access checks, insecure token handling
- Data exposure -- Sensitive data in logs, exceptions, or error messages.
Unmasked PII
- Cryptography -- Weak algorithms (MD5, SHA-1 for security), hardcoded
keys, insecure random (Math.random for security purposes)
- Deserialization -- Untrusted ObjectInputStream usage, missing type
validation
- Input validation -- Missing or insufficient validation of external input,
path traversal risks
Phase 3: Performance Analysis
Read references/performance-patterns.md
before this phase.
Check for:
- Algorithmic complexity -- O(n^2) or worse in hot paths, unnecessary
nested iterations
- Memory -- Excessive object creation in loops, large collections held
longer than needed, missing initial capacity for known-size collections
- String handling -- String concatenation in loops (use StringBuilder),
unnecessary String.format in hot paths
- I/O -- Unbuffered streams, N+1 query patterns, missing connection
pooling, synchronous I/O where async is appropriate
- Collections -- Wrong collection type for the access pattern, unnecessary
copying, missing pre-sizing
- JVM considerations -- Excessive autoboxing, finalizer usage, classloader
leaks
Phase 4: Code Quality and Maintainability
Check for:
- Design -- God classes, excessive coupling, Liskov substitution
violations, missing encapsulation
- Naming -- Unclear variable/method names, naming convention violations
(Java conventions: camelCase methods, PascalCase classes, UPPER_SNAKE
constants)
- Complexity -- Methods exceeding ~30 lines, cyclomatic complexity >10,
deeply nested conditionals (>3 levels)
- Duplication -- Repeated logic that should be extracted
- Documentation -- Missing Javadoc on public API, outdated comments that
contradict code
- Testing gaps -- Untested public methods, missing edge case tests,
no assertions in test methods, test methods that cannot fail
- AI-generated code smells -- Hallucinated or non-existent API calls,
overly verbose boilerplate that could use standard library methods,
unnecessary wrapper classes or abstractions that add indirection without
value, generic variable names (data, result, temp, info) that obscure
intent, contradictory or parroted comments that restate the code without
adding insight, TODO/FIXME/placeholder blocks left unimplemented,
inconsistent patterns within the same file (e.g., mixing builder and
constructor styles, mixing streams and loops for identical tasks),
dead code or unreachable branches that suggest generation artifacts
Phase 5: Dependency and License Review
If build files are available (pom.xml, build.gradle, build.gradle.kts),
or if import statements reference third-party libraries, check for:
- License compatibility -- Copyleft licenses (GPL, AGPL, LGPL) in
proprietary or permissively licensed projects. Flag any dependency whose
license is incompatible with the project's declared license
- License presence -- Dependencies with no discernible license (treat as
all-rights-reserved). Unlicensed code cannot be safely used
- Copyleft obligations -- LGPL dependencies linked statically (must be
dynamic), GPL dependencies in non-GPL projects, AGPL dependencies in
network services without source disclosure
- Transitive risk -- A permissively licensed library that itself depends
on a copyleft library. The copyleft obligation propagates
- Deprecated or unmaintained libraries -- Dependencies with known
end-of-life status, no updates in 2+ years, or archived repositories
- Duplicate functionality -- Multiple libraries solving the same problem
(e.g., both Guava and Apache Commons for the same utilities), increasing
attack surface and license exposure unnecessarily
Severity Levels
Assign one severity to each finding:
| Severity |
Label |
Meaning |
| S1 |
CRITICAL |
Will cause data loss, security breach, or production failure. Fix immediately. |
| S2 |
HIGH |
Likely to cause bugs, performance degradation, or security weakness in production. Fix before merge. |
| S3 |
MEDIUM |
Code smell, maintainability issue, or minor bug risk. Should be addressed. |
| S4 |
LOW |
Style issue, naming suggestion, or minor improvement. Address at discretion. |
Output Format
Structure the review as follows:
Summary
Provide a 2-3 sentence overview: what the code does, overall quality
assessment, and the most important finding.
Findings
List each finding with this structure:
[S{n}] {Category}: {Brief title}
- Location: File and line number (or method name if line unknown)
- Issue: What is wrong and why it matters
- Suggestion: Concrete fix, with a code snippet when helpful
Order findings by severity (S1 first), then by location within each severity.
Positive Observations
Note 1-3 things the code does well. Good patterns, clean design, or thorough
error handling deserve acknowledgment.
Summary Table
End with a count table:
| Severity |
Count |
| S1 CRITICAL |
n |
| S2 HIGH |
n |
| S3 MEDIUM |
n |
| S4 LOW |
n |
Guidelines
- Be specific. Reference exact line numbers, method names, and variable names.
- Provide concrete fix suggestions, not vague advice.
- When suggesting a fix, show a brief code snippet demonstrating the
improvement.
- Do not flag style preferences that have no correctness or readability
impact (e.g., brace placement style) unless the code mixes styles
inconsistently.
- For diffs, focus review on changed lines. Only flag pre-existing issues
if they interact with the changes.
- If the code is too large for a single review, divide it into logical
sections and review each, providing a consolidated summary.
- When uncertain about intent, state the assumption explicitly rather than
making a silent judgment.
1---2name: java-code-review3description: Review Java source code for bugs, security vulnerabilities, performance problems, concurrency issues, AI-generated code quality issues, dependency licensing risks, and best practice violations. Use when a user asks to review Java code, audit Java files, find bugs in Java, check Java code quality, detect AI slop, check library licenses, or perform a code review on .java files. Accepts code provided directly, as local file paths, as directory paths, or as unified diff output.4---56# Java Code Review78Review Java source code systematically for correctness, security, performance,9and maintainability.1011## Input Handling1213Determine the input type and gather code accordingly:14151. **Direct code** -- Code provided in the conversation. Review as-is.162. **File path(s)** -- Read the specified `.java` files.173. **Directory path** -- Find all `.java` files recursively. For large18 codebases (>50 files), ask the user which packages or files to focus on.194. **Diff/patch content** -- Review only the changed lines plus sufficient20 surrounding context. Focus findings on the changed code.2122## Review Process2324Execute these phases in order. For each finding, assign a severity.2526### Phase 1: Correctness and Bug Detection2728Examine code for:2930- **Null safety** -- Nullable parameters without checks, Optional misuse,31 potential NullPointerException paths32- **Resource leaks** -- Unclosed streams, connections, or locks. Verify33 try-with-resources usage for all AutoCloseable types34- **Error handling** -- Empty catch blocks, catching overly broad exceptions35 (Exception, Throwable), swallowed exceptions, missing finally blocks36- **Concurrency** -- Race conditions, unsynchronized shared mutable state,37 incorrect use of volatile, double-checked locking without volatile,38 ConcurrentModificationException risks39- **Logic errors** -- Off-by-one, incorrect operator precedence, unreachable40 code, broken equals/hashCode contracts41- **API misuse** -- Incorrect use of Collections, Streams, Date/Time API,42 String comparison with == instead of .equals()4344### Phase 2: Security Review4546Read [references/security-checklist.md](references/security-checklist.md)47before this phase.4849Check for:5051- **Injection** -- SQL injection (string concatenation in queries), command52 injection, LDAP injection, XPath injection, log injection53- **Authentication and authorization** -- Hardcoded credentials, missing54 access checks, insecure token handling55- **Data exposure** -- Sensitive data in logs, exceptions, or error messages.56 Unmasked PII57- **Cryptography** -- Weak algorithms (MD5, SHA-1 for security), hardcoded58 keys, insecure random (Math.random for security purposes)59- **Deserialization** -- Untrusted ObjectInputStream usage, missing type60 validation61- **Input validation** -- Missing or insufficient validation of external input,62 path traversal risks6364### Phase 3: Performance Analysis6566Read [references/performance-patterns.md](references/performance-patterns.md)67before this phase.6869Check for:7071- **Algorithmic complexity** -- O(n^2) or worse in hot paths, unnecessary72 nested iterations73- **Memory** -- Excessive object creation in loops, large collections held74 longer than needed, missing initial capacity for known-size collections75- **String handling** -- String concatenation in loops (use StringBuilder),76 unnecessary String.format in hot paths77- **I/O** -- Unbuffered streams, N+1 query patterns, missing connection78 pooling, synchronous I/O where async is appropriate79- **Collections** -- Wrong collection type for the access pattern, unnecessary80 copying, missing pre-sizing81- **JVM considerations** -- Excessive autoboxing, finalizer usage, classloader82 leaks8384### Phase 4: Code Quality and Maintainability8586Check for:8788- **Design** -- God classes, excessive coupling, Liskov substitution89 violations, missing encapsulation90- **Naming** -- Unclear variable/method names, naming convention violations91 (Java conventions: camelCase methods, PascalCase classes, UPPER_SNAKE92 constants)93- **Complexity** -- Methods exceeding ~30 lines, cyclomatic complexity >10,94 deeply nested conditionals (>3 levels)95- **Duplication** -- Repeated logic that should be extracted96- **Documentation** -- Missing Javadoc on public API, outdated comments that97 contradict code98- **Testing gaps** -- Untested public methods, missing edge case tests,99 no assertions in test methods, test methods that cannot fail100- **AI-generated code smells** -- Hallucinated or non-existent API calls,101 overly verbose boilerplate that could use standard library methods,102 unnecessary wrapper classes or abstractions that add indirection without103 value, generic variable names (data, result, temp, info) that obscure104 intent, contradictory or parroted comments that restate the code without105 adding insight, TODO/FIXME/placeholder blocks left unimplemented,106 inconsistent patterns within the same file (e.g., mixing builder and107 constructor styles, mixing streams and loops for identical tasks),108 dead code or unreachable branches that suggest generation artifacts109110### Phase 5: Dependency and License Review111112If build files are available (`pom.xml`, `build.gradle`, `build.gradle.kts`),113or if import statements reference third-party libraries, check for:114115- **License compatibility** -- Copyleft licenses (GPL, AGPL, LGPL) in116 proprietary or permissively licensed projects. Flag any dependency whose117 license is incompatible with the project's declared license118- **License presence** -- Dependencies with no discernible license (treat as119 all-rights-reserved). Unlicensed code cannot be safely used120- **Copyleft obligations** -- LGPL dependencies linked statically (must be121 dynamic), GPL dependencies in non-GPL projects, AGPL dependencies in122 network services without source disclosure123- **Transitive risk** -- A permissively licensed library that itself depends124 on a copyleft library. The copyleft obligation propagates125- **Deprecated or unmaintained libraries** -- Dependencies with known126 end-of-life status, no updates in 2+ years, or archived repositories127- **Duplicate functionality** -- Multiple libraries solving the same problem128 (e.g., both Guava and Apache Commons for the same utilities), increasing129 attack surface and license exposure unnecessarily130131## Severity Levels132133Assign one severity to each finding:134135| Severity | Label | Meaning |136|----------|-------|---------|137| S1 | CRITICAL | Will cause data loss, security breach, or production failure. Fix immediately. |138| S2 | HIGH | Likely to cause bugs, performance degradation, or security weakness in production. Fix before merge. |139| S3 | MEDIUM | Code smell, maintainability issue, or minor bug risk. Should be addressed. |140| S4 | LOW | Style issue, naming suggestion, or minor improvement. Address at discretion. |141142## Output Format143144Structure the review as follows:145146### Summary147148Provide a 2-3 sentence overview: what the code does, overall quality149assessment, and the most important finding.150151### Findings152153List each finding with this structure:154155**[S{n}] {Category}: {Brief title}**156- **Location**: File and line number (or method name if line unknown)157- **Issue**: What is wrong and why it matters158- **Suggestion**: Concrete fix, with a code snippet when helpful159160Order findings by severity (S1 first), then by location within each severity.161162### Positive Observations163164Note 1-3 things the code does well. Good patterns, clean design, or thorough165error handling deserve acknowledgment.166167### Summary Table168169End with a count table:170171| Severity | Count |172|----------|-------|173| S1 CRITICAL | n |174| S2 HIGH | n |175| S3 MEDIUM | n |176| S4 LOW | n |177178## Guidelines179180- Be specific. Reference exact line numbers, method names, and variable names.181- Provide concrete fix suggestions, not vague advice.182- When suggesting a fix, show a brief code snippet demonstrating the183 improvement.184- Do not flag style preferences that have no correctness or readability185 impact (e.g., brace placement style) unless the code mixes styles186 inconsistently.187- For diffs, focus review on changed lines. Only flag pre-existing issues188 if they interact with the changes.189- If the code is too large for a single review, divide it into logical190 sections and review each, providing a consolidated summary.191- When uncertain about intent, state the assumption explicitly rather than192 making a silent judgment.