Skip to content

Treat Class#getCanonicalName() on class literals as non-null - #2190

Open
vitinh0z wants to merge 1 commit into
apache:masterfrom
vitinh0z:class-literal-canonical-name
Open

vitinh0z wants to merge 1 commit into
apache:masterfrom
vitinh0z:class-literal-canonical-name

Conversation

@vitinh0z

Copy link
Copy Markdown
Contributor

Fixes #2128.

A class literal denotes a type with a canonical name unless the type (or an array's element type) is local or a member of a local class (JLS 6.7; anonymous classes have no class literal). In that case Class#getCanonicalName() can never return null, so the @Nullable return from the JSpecify JDK model does not apply.

Following the suggestion in the issue, this adds the logic to LibraryModelsHandler rather than a separate handler. A helper, isCanonicalNameOfClassLiteral, is used both in onOverrideMayBeNullExpr (dereferences, returns, arguments) and in onDataflowVisitMethodInvocation (FORCE_NONNULL, so the result stays non-null through local variables). Invocations on receivers that are not class literals, such as clazz.getCanonicalName() or obj.getClass().getCanonicalName(), remain @Nullable.

The new test covers:

  • non-null: top-level, member, member-of-member, primitive, void, and array class literals, including via a local variable, a return, and an argument
  • still @Nullable: local classes, records, enums and interfaces, a member of a local class, an array of a local class, and a local class inside a lambda
  • still @Nullable: parameter, field, getClass() and local-variable receivers

I used Claude Code to analyze the codebase and draft an implementation plan, to write the regression test, and to draft the helper's Javadoc and a fix for a NullAway self-check error. I wired the helper into the handler myself, reviewed every change, ran ./gradlew :nullaway:test and ./gradlew :nullaway:buildWithNullAway locally, and I understand all the changes in this PR.

Assisted-by: Claude Code (claude-sonnet-5-5, claude-opus-5-5)

Copilot AI balanced review requested due to automatic review settings November 25, 2026 21:18
@cla-assistant

cla-assistant Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cla-assistant

cla-assistant Bot commented Nov 25, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Victor Gabriel seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

Walkthrough

NullAway treated every Class.getCanonicalName() result as nullable, including calls on class literals whose types have canonical names. The handler now treats those calls as non-null and keeps local types and non-literal receivers nullable. JSpecify tests cover both cases.

Severity of issue fixed: Low

Merge Risk: Low · previous review 4ab21

Calling getCanonicalName() on a parenthesized class literal such as (String.class) will still be reported as nullable. This is a rare, safe false positive and is not merge-blocking.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

private static boolean isCanonicalNameOfClassLiteral(
MethodInvocationTree invocation, Symbol.MethodSymbol callee, VisitorState state) {
if (!callee.name.contentEquals("getCanonicalName")
|| !(ASTHelpers.getReceiver(invocation) instanceof MemberSelectTree literal)

@coderabbitai coderabbitai Bot Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Remove receiver parentheses before matching the class literal.

For (String.class).getCanonicalName().length(), ASTHelpers.getReceiver() preserves the parentheses. This check rejects the receiver, so the nullable warning remains. The pinned Error Prone implementation returns the selected expression unchanged. (raw.githubusercontent.com)

Strip parentheses before the MemberSelectTree check. Add positive coverage for (String.class) and negative coverage for (Local.class).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msridhar apply this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, why not, it's easy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Fixes apache#2128.

A class literal denotes a type with a canonical name unless the type
(or an array's element type) is local or a member of a local class.
Handle this in LibraryModelsHandler, both for expression checks and for
dataflow. Invocations on non-literal receivers stay @nullable.
@vitinh0z
vitinh0z force-pushed the class-literal-canonical-name branch from 4ab216e to 78be1a6 Compare November 25, 2026 21:26

@msridhar msridhar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM! I think it's simple enough that we don't need a separate handler and this strategy is the right one. Just address the coderabbit feedback and then we should be good to go.

private static boolean isCanonicalNameOfClassLiteral(
MethodInvocationTree invocation, Symbol.MethodSymbol callee, VisitorState state) {
if (!callee.name.contentEquals("getCanonicalName")
|| !(ASTHelpers.getReceiver(invocation) instanceof MemberSelectTree literal)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, why not, it's easy

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.54%. Comparing base (594cf3d) to head (78be1a6).

Files with missing lines Patch % Lines
...m/uber/nullaway/handlers/LibraryModelsHandler.java 92.85% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2190      +/-   ##
============================================
+ Coverage     89.52%   89.54%   +0.02%     
- Complexity     4019     4029      +10     
============================================
  Files           113      113              
  Lines         13179    13192      +13     
  Branches       2724     2730       +6     
============================================
+ Hits          11798    11813      +15     
+ Misses          622      621       -1     
+ Partials        759      758       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msridhar

msridhar commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@vitinh0z do you have cycles to address the coderabbit feedback here?

@vitinh0z

vitinh0z commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@vitinh0z do you have cycles to address the coderabbit feedback here?

@msridhar Sorry, I’ve been very busy with my internship and the collage :/

but I’ll implement this on Friday, I promise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handler: treat Class#getCanonicalName() on class literals as @NonNull

3 participants