nullaway
Skill này làm gì
Skill này hướng dẫn AI của bạn sửa lỗi từ NullAway, công cụ tìm các giá trị có thể là null trong mã Java. Skill dành cho lập trình viên thêm hoặc cập nhật chú thích null.
Khi nào nên dùng
- ✓Khi NullAway báo lỗi về một giá trị có thể là null.
- ✓Khi bạn thêm @NullMarked vào một file Java và cần sửa các cảnh báo mới.
- ✓Khi bạn cần truyền một supplier hoặc giá trị observable có thể null mà không làm hỏng mã.
Liên kết skill
https://github.com/chromium/chromium/blob/main/agents/skills/nullaway/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“NullAway đang báo lỗi trên file Java của tôi. Bạn có thể sửa nó mà không làm thay đổi cách ứng dụng hoạt động không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
AI xem xét cách mỗi giá trị có thể null được dùng, rồi thêm một kiểm tra an toàn hoặc khẳng định, chọn cách nhẹ nhất đáp ứng được công cụ kiểm tra.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Core Principles
1. Migration to @NullMarked vs New Code
The approach differs significantly depending on whether you are migrating
existing code to @NullMarked or writing new code in an already @NullMarked
context:
- Migration to
@NullMarked:- Goal: Satisfy the static analyzer while minimizing functional changes to avoid regressions.
- Practice: Use
NullUtil.assumeNonNull()when dereferencing immediately to satisfy the analyzer without runtime overhead. Useassertwhen storing or returning values. Useifguards occasionally if it helps avoid assertions, but avoid changing the logic.
- New Code (and modifying already
@NullMarkedcode):- Goal: Build robust, null-safe components from the ground up.
- Practice: Be more liberal with
asserts to enforce contracts. Rely on correct annotations (or their absence for implicit@NonNull) and only use null guards for@Nullablevalues.assumeNonNull()should generally not be used for new code.
2. No Functional Changes (For Migration)
- Rule: When migrating existing code, avoid adding new throwing runtime checks where they did not exist before, unless guided by the rules below.
3. assumeNonNull vs assert != null
The choice between assumeNonNull and Java assert depends on how the value is
used:
- Dereferencing
@Nullablevalues immediately: UseNullUtil.assumeNonNull(x).- Example:
var value = mSupplier.get(); assumeNonNull(value); value.doSomething(); - Why: If it is null, it will NPE on the dereference anyway.
assumeNonNullis a no-op that satisfies the analyzer without adding redundant runtime checks. - Style Rule:
assumeNonNull()should be on a separate line, almost always, rather than used inline. - Note: This rule is primarily for migrations to avoid functional
changes. For new code, rely on correct annotations and avoid
assumeNonNull().
- Example:
- Passing
@Nullablevalues to non-null parameters: First, look into the call tree of the receiver class and see if that parameter should be annotated as@Nullableor not. If it can natively handle null, update the method signature instead of adding an assertion. If it strictly requires a non-null value, THEN you MUST use a Javaassert x != null;on a preceding line before passing or storing it.[!CAUTION] NEVER use
assumeNonNull(x)to pass a@Nullablevalue to a non-null parameter or to return it from a non-nullable method. This is STRICTLY FORBIDDEN. You MUST use a Javaasserton a preceding line to add a runtime check.- Bad Example:
mReceiver.setSomething(assumeNonNull(nullableValue)); - Good Example:
assert nullableValue != null; mReceiver.setSomething(nullableValue);
- Bad Example:
- Returning
@Nullablevalues from non-nullable methods: First, consider if the method's return type can be safely updated to@Nullable. If it cannot (e.g., because it implements an interface or strictly enforces a non-null contract), you MUST use a Javaassert x != null;on a preceding line before returning the value. UsingassumeNonNull(x)inline within a return statement is strictly forbidden.- Bad Example:
return assumeNonNull(nullableValue); - Good Example:
assert nullableValue != null; return nullableValue;
- Bad Example:
- Why: Asserts add a runtime check (active in tests/debug) which is an
acceptable functional change. We rely on instrumentation tests to validate
these changes.
- Deep Investigation Rule: Before asserting or assuming non-null for a
passed value, investigate the call tree. If the method being called can be
updated to consider the parameter
@Nullable, prefer updating the method signature over adding an assertion. - Warning: Be careful with
assert value != nullonSupplier.get()during initialization. If the supplier value is set LATER (as is common with UI wiring), the assert might fail immediately during construction. In such cases, the getter should return@Nullableand callers should handle it, rather than asserting non-null immediately.
- Deep Investigation Rule: Before asserting or assuming non-null for a
passed value, investigate the call tree. If the method being called can be
updated to consider the parameter
4. Handling Suppliers and Generics
- Problem: Passing a
Supplier<@Nullable T>to a constructor that expectsSupplier<T>(non-nullable), or vice
Câu hỏi thường gặp
NullAway là gì?
NullAway là công cụ kiểm tra mã Java để tìm những chỗ một giá trị có thể trống, điều có thể gây crash.
Bản sửa có thay đổi cách ứng dụng hoạt động không?
Skill cố tránh thay đổi hành vi khi chuyển mã cũ. Nó ưu tiên các khẳng định nhỏ hơn là logic mới.
Điều này chỉ dành cho mã mới à?
Không. Nó bao gồm cả việc chuyển mã cũ sang mã có đánh dấu null và viết mã mới trong các file đã dùng cách này.
Chi tiết
Mô tả gốc của tác giả
Guide for resolving NullAway static analysis errors. Best practices for: - Passing ObservableSupplier/Supplier<@Nullable T> - Dereferencing potentially @Nullable values - Adding @NullMarked to Java code
repo: chromium/chromium
Độ phổ biến
- Sao GitHub
- 25k
- Vị trí xếp hạng
- #10062
- Giấy phép
- BSD-3-Clause
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
fuzzing
25kGiúp agent của bạn viết các bài kiểm thử fuzz, đưa nhiều đầu vào ngẫu nhiên vào mã để tìm lỗi crash và bug trong mã C++.
gn-deps-debugging
25kGiúp agent của bạn sửa lỗi build do phụ thuộc thiếu hoặc sai trong file build GN, mà không thêm các phụ thuộc rộng và không cần thiết.
java-memory-leaks
25kGiúp agent của bạn tìm và sửa rò rỉ bộ nhớ Java trong ứng dụng Android bằng cách đọc báo cáo rò rỉ từ LeakCanary.
jni-type-conversion
25kGiúp agent của bạn thay thế mã chuyển đổi Java sang C++ thủ công bằng các chú thích @JniType đơn giản hơn trong mã Android.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.