Repository navigation
Conversation
|
Compared to main: (comment created at 2026-11-16 12:24:45+00:00 for 14cedf4) |
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).
|
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? |
|
@UncleOwen I tried both. Rerunning the inference with Most of them come from the resolver itself. For example, Sharing with UnnecessaryVarargsArrayCreation works. With the overload code moved to a helper under 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. |
|
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. |
…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.
|
@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. |
|
@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. |
Co-authored-by: Andreas Dangel <[email protected]>
…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.
|
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 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. |
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)andPUSH(ConstantPoolGen, Character), which main reports, wherethis(cp, value.booleanValue())andthis(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 byjavap. The block that importsorg.example.Libandorg.example.Taskwas compiled against stub classes. In the PMD test these types stay unresolved, and the lines that use them expect no report, exceptt(value.intValue(), Lib.count() + 1). PMD typesLib.count() + 1asint, and since #7163 it selectst(int, long)with and withoutintValue(), as javac does whenLib.count()returnsint. IfLib.count()returnslong, the call withoutintValue()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) trueandON == true, moved to a new case for #7313. The protected constructor case uses a helper class insrc/test/java-resources, as in #6993. Thirteen of the eighteen enabled cases fail with the rule as onmain(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 disabledAt 14cedf4 I ran
verifyforpmd-javaon Windows 11 with JDK 21. It passed with 10,838 tests and 0 Checkstyle violations.UnnecessaryBoxingTesthas 61 tests, 0 failures and 2 disabled.docs/pages/release_notes.mdlists #7134 and #7135 under Fixed Issues.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)