GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Fix #7134, #7135: UnnecessaryBoxing: check whether another overload is applicable without the unboxing by saryn17 · Pull Request #7136 · pmd/pmd

Conversation

@saryn17

@saryn17 saryn17 commented Oct 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Describe the PR

The overload check from #5971 looks only at the type that other overloads have at the converted argument. A reference type there always suppresses the warning, even if the boxed argument cannot be passed to it (#7134). A primitive type never does, even if that overload would be selected without the conversion (#7135).

This PR reruns the overload resolution of the call (Infer.getCompileTimeDecl) with the boxed expression in place of the unboxing call, with mirrors built as in UseDiamondOperatorRule. The rule reports the unboxing unless another method is selected or the call becomes ambiguous. It also reruns the call as written, because the result stored on the AST can lose the ambiguity flag of the overload selection. When that rerun fails, the rule has no selected method to compare with and stays silent.

In October, the rule reported 1,793 violations on the JDK sources and main reported 1,773. The earlier version of this PR reported the same 1,793 (my earlier comment counted 1,808 output lines, which include 15 lines of error output for 3 files that PMD could not process). I compiled the 24 calls that differ from main with and without the conversion. In 22 of them the conversion can be removed and main does not report it. The other 2 are in BCEL's PUSH(ConstantPoolGen, Boolean) and PUSH(ConstantPoolGen, Character), which main reports, where this(cp, value.booleanValue()) and this(cp, value.charValue()) would call their own constructor without the conversion.

Where PMD's overload resolution differs from javac, the rule follows PMD. Ten such differences are filed as #7144, #7145, #7146, #7147, #7148, #7149, #7150, #7151, #7152 and #7313.

Tests

Twenty cases are added to UnnecessaryBoxing.xml. Each expected violation was checked by compiling the test class with and without the conversion and comparing the invoked method descriptors shown by javap. The block that imports org.example.Lib and org.example.Task was compiled against stub classes. In the PMD test these types stay unresolved, and the lines that use them expect no report, except t(value.intValue(), Lib.count() + 1). PMD types Lib.count() + 1 as int, and since #7163 it selects t(int, long) with and without intValue(), as javac does when Lib.count() returns int. If Lib.count() returns long, the call without intValue() does not compile.

Two cases whose expected result depends on a difference between PMD's resolution and javac are disabled until #7147 and #7152 are fixed. Seven more were enabled after #7144, #7145, #7146, #7149, #7150, #7151 and #7313 were fixed. Two loop conditions of the #7144 case, (boolean) true and ON == true, moved to a new case for #7313. The protected constructor case uses a helper class in src/test/java-resources, as in #6993. Thirteen of the eighteen enabled cases fail with the rule as on main (b27f97a).

At 15327e0 I ran the commands of the CI jobs on Ubuntu 24.04 (WSL) with JDK 21 and the extra homes for 8, 11, 17 and 25.

  • UnnecessaryBoxingTest: 60 tests, 0 failures, 8 disabled
  • verify-unittests: 19,932 tests, 0 failures
  • verify: BUILD SUCCESS, 0 Checkstyle violations
  • dogfood: BUILD SUCCESS, 0 violations
  • regressiontester: 0 changed violations, 0 new violations

At 14cedf4 I ran verify for pmd-java on Windows 11 with JDK 21. It passed with 10,838 tests and 0 Checkstyle violations. UnnecessaryBoxingTest has 61 tests, 0 failures and 2 disabled.

docs/pages/release_notes.md lists #7134 and #7135 under Fixed Issues.

Related issues

  • Fix [java] UnnecessaryBoxing: False negative when another overload takes an unrelated reference type #7134
  • Fix [java] UnnecessaryBoxing: False positive when removing the conversion selects a more specific primitive overload #7135

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@saryn17
[java] Fix pmd#7134, pmd#7135: UnnecessaryBoxing: check whether anoth…
aa6e67f
…er overload is applicable without the unboxing
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-11-16 12:24:45+00:00 for 14cedf4)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@saryn17
[java] UnnecessaryBoxing: check protected access of other overloads
fde5aa5
A protected method of a class in another package can be called from
classes nested in a subclass, but only on an expression of that
subclass type (JLS 6.6.2.1). A protected constructor of such a class
is not a candidate for a plain new (JLS 6.6.2.2).
@UncleOwen

UncleOwen commented Oct 4, 2026

Copy link
Copy Markdown
Member

That's a lot of code. I haven't looked at it in detail, just skimmed it to get a high-level overview.

There's a call to our existing overload resolution code in line 251. And the next almost-500-lines is another overload resolution algorithm. Is there any way to use the existing code for the what-if-we-remove-this-boxing-call-case?

Failing that: You mentioned that UnnecessaryVarargsArrayCreation does something similar. Is there some code that can be shared?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@saryn17

saryn17 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen I tried both.

Rerunning the inference with value instead of value.intValue(), like UseDiamondOperatorRule does, is 23 lines instead of ~500. On the JDK sources it reports the same 1,808 lines as this PR. On the 3,477 javac-checked calls I collected while working on this, most of them constructed corner cases, it is wrong on 242 where this PR is wrong on 106. 126 of those are false positives that main doesn't have.

Most of them come from the resolver itself. For example, () -> { while (true) { } } isn't treated as value-compatible (LambdaMirrorImpl.java:141-148), and d(value, 5) with d(int, int) and d(long, Integer) resolves although javac reports it as ambiguous (PhaseOverloadSet.java:168-186). I can open issues for these.

Sharing with UnnecessaryVarargsArrayCreation works. With the overload code moved to a helper under rule.internal, UnnecessaryBoxing gives the same results. UnnecessaryVarargsArrayCreation on top of the helper goes from 24 false positives and 7 false negatives to 1 and 1 on 51 calls I checked with javac, and 2 of its 99 reports on the JDK change.

I haven't changed the PR yet. I'd move the current logic into the helper here and switch UnnecessaryVarargsArrayCreation in a separate PR. If you'd rather have the inference version, I can do that instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen

UncleOwen commented Oct 5, 2026

Copy link
Copy Markdown
Member

In that case, please use the existing overload resolution code. If there are bugs in there, please file additional issues/PRs.

oowekyala reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala

oowekyala commented Oct 5, 2026

Copy link
Copy Markdown
Member

In that case, please use the existing overload resolution code. If there are bugs in there, please file additional issues/PRs.

+1 ---depending on the core type resolution allows us to expose and fix bugs with it and improve all rules that use it at once.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Oct 6, 2026
[java] Lambda block body that cannot complete normally is not treated as value-compatible #7144
Closed
[java] Lambda with a parenthesized expression body is treated as void-compatible #7145
Closed
[java] Overload resolution prefers int over Integer where javac reports an ambiguity #7146
Closed
[java] Protected access check ignores enclosing classes, the qualifier type and the constructor form #7147
Open
[java] Strict phase rejects lambda and method reference results that need boxing #7148
Closed
[java] Loose invocation conversion accepts int to Long and null to int #7149
Closed
[java] Numeric conditional with a char operand and an int constant is typed as int #7150
Closed
[java] Unqualified method call resolves to an enclosing class method that the inner class hides #7151
Closed
[java] Type-name-qualified invocation filters out instance methods before overload resolution #7152
Open
@saryn17
[java] UnnecessaryBoxing: rerun the overload resolution without the u…
15327e0
…nboxing

The rule now reruns PMD's overload resolution on the call with the
boxed expression in place of the unboxing call. The mirrors are built
as in UseDiamondOperatorRule. The unboxing is reported unless another
method is selected or the call becomes ambiguous. This replaces the
applicability check written for this rule.

Eight of the new test cases are disabled until pmd#7144, pmd#7145, pmd#7146,
pmd#7147, pmd#7149, pmd#7150, pmd#7151 and pmd#7152 are fixed. Their expected results
follow javac where PMD's overload resolution differs from it.
@saryn17

saryn17 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen @oowekyala The rule now reruns the existing overload resolution with the boxed expression in place of the unboxing call and compares the selected methods. UnnecessaryBoxingRule.java is +24/-66 against main. I filed #7144, #7145, #7146, #7147, #7148, #7149, #7150, #7151 and #7152 for differences from javac that this exposed. Eight of them have a test case in UnnecessaryBoxing.xml, disabled until the issue is fixed.

oowekyala reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

saryn17 added 3 commits October 6, 2026 22:19
@saryn17
Merge branch 'main' into unnecessaryboxing-overload-fn
7ac7285
@saryn17
Merge remote-tracking branch 'upstream/main' into unnecessaryboxing-o…
dafe7be
…verload-fn
@saryn17
[java] UnnecessaryBoxing: enable the test case for pmd#7145
1aac0ac
pmd#7145 was fixed by pmd#7158.
@saryn17

saryn17 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen could you take another look at this? It now uses the existing overload resolution as you suggested.

The test for #7145 is enabled now that #7158 is merged. The fixes for #7146, #7149, #7150 and #7151 are in #7161, #7162 and #7163. Once those are merged, I can enable the corresponding tests here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@noor-ahmadi noor-ahmadi mentioned this pull request Oct 21, 2026
[java] Fix #7144: Fix lambda block body compatibility #7218
Merged
3 of 4 tasks
@adangel

adangel commented Oct 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

So, if I understand this PR correctly, this PR depends on:

  • [java] Lambda with a parenthesized expression body is treated as void-compatible #7145
    • [java] Fix #7145: void compatibility of parenthesized lambda bodies #7158
  • [java] Unqualified method call resolves to an enclosing class method that the inner class hides #7151
    • [java] Fix #7151: Apply the comb rule to unqualified method calls #7161
  • [java] Numeric conditional with a char operand and an int constant is typed as int #7150
    • [java] Fix #7150: type of numeric conditionals with byte, short or char operands #7162
  • [java] Overload resolution prefers int over Integer where javac reports an ambiguity #7146
    • [java] Fix #7146, #7149: Loose invocation conversion and the most specific check for boxing #7163
  • [java] Loose invocation conversion accepts int to Long and null to int #7149
    • [java] Fix #7146, #7149: Loose invocation conversion and the most specific check for boxing #7163
  • [java] Strict phase rejects lambda and method reference results that need boxing #7148
    • [java] Fix #7148: Allow boxing of lambda and method reference results in the strict phase #7164
  • [java] Lambda block body that cannot complete normally is not treated as value-compatible #7144
    • [java] Fix #7144: Fix lambda block body compatibility #7218
  • [java] Protected access check ignores enclosing classes, the qualifier type and the constructor form #7147
  • [java] Type-name-qualified invocation filters out instance methods before overload resolution #7152

I'll put this PR then in draft mode.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel
adangel marked this pull request as draft October 26, 2026 14:33
adangel
adangel reviewed Oct 26, 2026
View reviewed changes
Comment thread ...va/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryBoxing.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@adangel
Enable test case after pmd#7151 has been fixed
1d042fa
adangel
adangel reviewed Oct 26, 2026
View reviewed changes
Comment thread ...va/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryBoxing.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel and others added 2 commits October 26, 2026 17:02
@adangel
Enable test case for pmd#7150
e0b56be
@adangel
Merge branch 'main' into unnecessaryboxing-overload-fn
6faf4f1
adangel
adangel reviewed Oct 27, 2026
View reviewed changes
Comment thread ...va/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryBoxing.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel reviewed Oct 27, 2026
View reviewed changes
Comment thread ...va/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryBoxing.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel and others added 3 commits October 27, 2026 10:19
@adangel
Enable test cases (pmd#7146, pmd#7149)
6727f3a
Co-authored-by: Andreas Dangel <[email protected]>
@saryn17
Merge branch 'main' into unnecessaryboxing-overload-fn
8db7d94
@saryn17
[java] UnnecessaryBoxing: update the expected reports after the fix of …
6124cdd
…pmd#7149

Since pmd#7163, the test case "Other arguments whose type is inferred or
unknown" reports t(value.intValue(), Lib.count() + 1) with t(int, long)
and t(Integer, Long). Lib is not on the auxclasspath and Lib.count() + 1
is typed as int, which cannot be passed to t(Integer, Long). The
overload resolution selects t(int, long) with and without intValue().
Before pmd#7163 it accepted int for the Long parameter, so the call without
intValue() was ambiguous and the unboxing was not reported.
@saryn17

saryn17 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

The unit tests failed on "Other arguments whose type is inferred or unknown" (12 problems instead of 11) after #7163 was merged. The extra report is t(value.intValue(), Lib.count() + 1) with t(int, long) and t(Integer, Long). Lib is unresolved, and PMD types Lib.count() + 1 as int. Before #7163, the overload resolution accepted int for the Long parameter (#7149), so the call without intValue() was ambiguous. Now it selects t(int, long) with and without intValue(), as javac does when Lib.count() returns int. If Lib.count() returns long, the call without intValue() does not compile, so this report depends on PMD typing Lib.count() + 1 as int.

6124cdd updates the expected lines. I also merged main to resolve the conflict in the release notes, and updated the description for the disabled cases and this line.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@saryn17
Merge branch 'main' into unnecessaryboxing-overload-fn
6365759
This was referenced Nov 11, 2026
[java] Casts to boolean and boolean equality are not folded as constant expressions #7313
Closed
[java] Fix #7313: Fold casts to boolean and boolean equality #7314
Merged
[java] Fix #7147: Check protected access as in JLS 6.6.2 #7315
Open
@saryn17
[java] UnnecessaryBoxing: enable the test case for pmd#7144
8647b35
pmd#7144 was fixed by pmd#7218. The loop conditions (boolean) true and
ON == true move to a new disabled case, because ConstantFolder does
not fold them (pmd#7313).
@saryn17

saryn17 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Merged main (6365759) and enabled the #7144 case in 8647b35.

The two calls with (boolean) true and ON == true are now in a separate disabled test for #7313. #7314 fixes the constant folding issue.

#7299 fixes #7152, and #7315 builds on #7299 to fix #7147.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

saryn17 added 3 commits November 14, 2026 11:33
@saryn17
Merge branch 'main' into unnecessaryboxing-overload-fn
3f7dc7e
@saryn17
Merge branch 'main' into unnecessaryboxing-overload-fn
1b84182
@saryn17
[java] UnnecessaryBoxing: enable the test case for pmd#7313
14cedf4
pmd#7313 was fixed by pmd#7314.
@saryn17

saryn17 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Merged main (1b84182) and enabled the #7313 case in 14cedf4.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This branch has not been deployed

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

Reviewers

@adangel adangel adangel left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[java] UnnecessaryBoxing: False positive when removing the conversion selects a more specific primitive overload [java] UnnecessaryBoxing: False negative when another overload takes an unrelated reference type

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@saryn17 @UncleOwen @oowekyala @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.