review-java

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Skill: Review Java

Skill: Java代码审查

Purpose

目的

Review code in Java for language and runtime conventions only. Do not define scope (diff vs codebase) or perform security/architecture analysis; those are handled by scope and cognitive skills. Emit a findings list in the standard format for aggregation. Focus on concurrency and thread safety, exceptions and try-with-resources, API and version compatibility, collections and Streams, NIO and proper closing, modules (JPMS) where relevant, and testability.

仅审查Java代码是否符合语言及运行时规范。无需定义审查范围(差异对比 vs 整个代码库),也无需进行安全/架构分析;这些工作由范围技能和认知技能负责。输出符合标准格式的问题发现列表,以便进行汇总。重点关注并发与线程安全、异常与try-with-resources、API与版本兼容性、集合与Streams、NIO与资源正确关闭、相关模块(JPMS)以及可测试性。

Use Cases

使用场景

  • Orchestrated review: Used as the language step when review-code runs scope → language → framework → library → cognitive for Java projects.
  • Java-only review: When the user wants only language/runtime conventions checked.
  • Pre-PR Java checklist: Ensure concurrency, resource management, and API compatibility are correct.
When to use: When the code under review is Java and the task includes language/runtime quality. Scope is determined by the caller or user.

  • 编排式审查:在review-code针对Java项目执行「范围→语言→框架→库→认知」的审查流程时,作为语言环节的技能使用。
  • 仅Java代码审查:当用户仅需要检查语言/运行时规范时使用。
  • PR前Java检查清单:确保并发处理、资源管理和API兼容性符合要求。
适用时机:当待审查代码为Java,且任务包含语言/运行时质量检查时。审查范围由调用方或用户确定。

Behavior

行为规范

Scope of this skill

本技能的审查范围

  • Analyze: Java language and runtime conventions in the given code scope (files or diff provided by the caller). Do not decide scope; accept the code range as input.
  • Do not: Perform scope selection, security review, or architecture review; do not review non-Java files for Java rules unless explicitly in scope.
  • 分析内容:在调用方提供的代码范围(文件或差异内容)内,检查Java语言及运行时规范。无需自行决定审查范围,直接接受输入的代码范围即可。
  • 禁止操作:不得选择审查范围、进行安全审查或架构审查;除非明确纳入范围,否则不得针对非Java文件应用Java规则进行审查。

Review checklist (Java dimension only)

审查检查清单(仅Java维度)

  1. Concurrency and thread safety: Correct use of synchronized, volatile, locks, or concurrent APIs; visibility and happens-before; shared mutable state; executor usage and shutdown.
  2. Exceptions and resources: try-with-resources for Closeable/AutoCloseable; exception handling and suppression; avoiding empty catch or overly broad catch.
  3. API and version compatibility: Public API stability; backward compatibility; use of deprecated APIs and migration path; module boundaries (JPMS) if applicable.
  4. Collections and Streams: Appropriate use of Stream API; side effects in streams; allocation and boxing; immutable collections where appropriate.
  5. NIO and closing: Proper closing of streams, channels, and selectors; avoid resource leaks; use try-with-resources.
  6. Testability: Dependency injection; static and singleton usage; overridable vs final; test doubles and mocking.
  1. 并发与线程安全:synchronized、volatile、锁或并发API的正确使用;可见性与先行发生原则;共享可变状态;执行器的使用与关闭。
  2. 异常与资源管理:对Closeable/AutoCloseable使用try-with-resources;异常处理与抑制;避免空catch块或过于宽泛的catch范围。
  3. API与版本兼容性:公共API的稳定性;向后兼容性;已废弃API的使用与迁移路径;适用情况下的模块边界(JPMS)。
  4. 集合与Streams:Stream API的合理使用;流操作中的副作用;内存分配与装箱;适用场景下的不可变集合。
  5. NIO与资源关闭:流、通道和选择器的正确关闭;避免资源泄漏;使用try-with-resources。
  6. 可测试性:依赖注入;静态与单例的使用;可重写方法与final方法的设计;测试替身与模拟。

Tone and references

表述风格与引用要求

  • Professional and technical: Reference specific locations (file:line). Emit findings with Location, Category, Severity, Title, Description, Suggestion.

  • 专业且技术化:引用具体位置(文件:行号)。输出的问题发现需包含位置、分类、严重程度、标题、描述、建议。

Input & Output

输入与输出

Input

输入

  • Code scope: Files or directories (or diff) already selected by the user or by the scope skill. This skill does not decide scope; it reviews the provided Java code for language conventions only.
  • 代码范围:由用户或范围技能已选择的文件、目录(或差异内容)。本技能不负责确定审查范围,仅针对提供的Java代码检查语言规范。

Output

输出

  • Emit zero or more findings in the format defined in Appendix: Output contract.
  • Category for this skill is language-java.

  • 输出零个或多个问题发现,格式需符合附录:输出约定中的定义。
  • 本技能对应的分类为
    language-java

Restrictions

限制条件

  • Do not perform security, architecture, or scope selection. Stay within Java language and runtime conventions.
  • Do not give conclusions without specific locations or actionable suggestions.
  • Do not review non-Java code for Java-specific rules unless explicitly in scope.

  • 禁止进行安全审查、架构审查或范围选择。仅聚焦于Java语言及运行时规范。
  • 禁止在未提供具体位置或可操作建议的情况下给出结论。
  • 禁止针对非Java文件应用Java特定规则进行审查,除非明确将其纳入范围。

Self-Check

自我检查

  • Was only the Java language/runtime dimension reviewed (no scope/security/architecture)?
  • Are concurrency, exceptions, resources, collections/Streams, NIO, and testability covered where relevant?
  • Is each finding emitted with Location, Category=language-java, Severity, Title, Description, and optional Suggestion?
  • Are issues referenced with file:line?

  • 是否仅审查了Java语言/运行时维度(未涉及范围/安全/架构)?
  • 是否涵盖了相关的并发、异常、资源、集合/Streams、NIO以及可测试性内容?
  • 每个问题发现是否都包含位置、分类=language-java、严重程度、标题、描述及可选的建议?
  • 是否引用了文件:行号来标记问题?

Examples

示例

Example 1: Resource and exception

示例1:资源与异常处理

  • Input: Java method that opens an InputStream and does not use try-with-resources.
  • Expected: Emit a finding for resource management; suggest try-with-resources. Category = language-java.
  • 输入:打开InputStream但未使用try-with-resources的Java方法。
  • 预期输出:输出一条关于资源管理的问题发现;建议使用try-with-resources。分类 = language-java。

Example 2: Concurrency

示例2:并发处理

  • Input: Shared mutable list accessed from multiple threads without synchronization or concurrent collection.
  • Expected: Emit finding(s) for thread safety (e.g. use CopyOnWriteArrayList or synchronize); reference the field and usage. Category = language-java.
  • 输入:多个线程访问共享可变列表,但未使用同步机制或并发集合。
  • 预期输出:输出关于线程安全的问题发现(例如,建议使用CopyOnWriteArrayList或添加同步机制);引用对应的字段和使用位置。分类 = language-java。

Edge case: Mixed Java and SQL

边缘案例:Java与SQL混合代码

  • Input: File with JDBC or JPA and Java logic.
  • Expected: Review only Java conventions (resources, exceptions, concurrency). Do not emit SQL-injection findings here; that is for review-security or review-sql.

  • 输入:包含JDBC或JPA与Java逻辑的文件。
  • 预期输出:仅审查Java规范(资源、异常、并发)。不得在此处输出SQL注入相关的发现;该类问题由review-security或review-sql技能负责。

Appendix: Output contract

附录:输出约定

Each finding MUST follow the standard findings format:
ElementRequirement
Location
path/to/file.ext
(optional line or range).
Category
language-java
.
Severity
critical
|
major
|
minor
|
suggestion
.
TitleShort one-line summary.
Description1–3 sentences.
SuggestionConcrete fix or improvement (optional).
Example:
markdown
- **Location**: `src/main/java/com/example/Loader.java:45`
- **Category**: language-java
- **Severity**: major
- **Title**: InputStream not closed in all paths
- **Description**: Leak possible if an exception is thrown before close.
- **Suggestion**: Use try-with-resources for the InputStream.
每个问题发现必须遵循以下标准格式:
元素要求
Location
path/to/file.ext
(可选行号或范围)。
Category
language-java
Severity
critical
|
major
|
minor
|
suggestion
Title简短的单行摘要。
Description1–3句话。
Suggestion具体的修复或改进建议(可选)。
示例:
markdown
- **Location**: `src/main/java/com/example/Loader.java:45`
- **Category**: language-java
- **Severity**: major
- **Title**: InputStream未在所有执行路径中关闭
- **Description**: 若在close()调用前抛出异常,可能会导致资源泄漏。
- **Suggestion**: 为InputStream使用try-with-resources。