Skip to content

[java] Fix #7151: Apply the comb rule to unqualified method calls - #7161

Merged
adangel merged 1 commit into
pmd:mainfrom
saryn17:fix-7151
Oct 23, 2026
Merged

adangel merged 1 commit into
pmd:mainfrom
saryn17:fix-7151

Conversation

@saryn17

@saryn17 saryn17 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

For an unqualified call, PMD merged the methods of the enclosing classes into the scope of a nested class and dropped only the override-equivalent ones. With Inner.m(long) and Comb.m(Object), both reached overload resolution and m(Object) won. JLS 15.12.1 searches the innermost enclosing class or interface that has a member method with that name. The method scope of the class now shadows the enclosing methods by name, with the same shadow barrier the scopes of fields and types use. The custom merger is gone. Two details follow from the rule. Supertype methods that are not inherited are not members of the subclass (JLS 8.4.8), so they must not stop the search. Accessibility alone does not decide that: private methods are never inherited, and a package-private method is not inherited by a subclass in another package, so a class in the original package that extends that subclass does not have it either. javac selects the enclosing method in that case. The old merger also dropped the instance methods of an enclosing class from the scope of a static nested class that has no method of that name. Those methods are in scope under JLS 6.3. If overload resolution selects one for an unqualified call, JLS 15.12.3 then checks whether the compile-time declaration is appropriate.

New cases in MemberInheritanceTest:

  • the sample of the issue
  • an inherited method, and a private method of the superclass that is not inherited
  • a package-private method whose inheritance stops at a subclass in another package, with test classes in javasymbols.testdata
  • a static nested class, with and without a method of that name
  • a static import hidden by a method of the class
  • an inner class without a method of that name, which still sees the enclosing method

Three existing expectations in that file encoded the old merging and now follow the rule.

Ran ./mvnw -B -ntp -pl pmd-java verify with Java 21.

Related issues

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)

The method scope of a class merged the methods of the enclosing
classes and dropped only the override-equivalent ones. JLS 15.12.1
searches the innermost enclosing class or interface that has a member
method with the name of the call. The method scope now shadows the
enclosing methods by name, as the scopes of fields and types do, and
the custom merger is removed. Supertype methods that are not inherited
no longer count as members of the subclass for this lookup. That
covers private methods and package-private methods whose inheritance
stops at a subclass in another package.
@pmd-actions-helper

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, 0 errors and 0 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-10-07 15:42:12+00:00 for 7a1591c)

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, thank you

@oowekyala oowekyala added this to the 7.29.0 milestone Oct 23, 2026
@adangel adangel changed the title [java] Apply the comb rule to unqualified method calls [java] Fix #7151: Apply the comb rule to unqualified method calls Oct 23, 2026
@adangel
adangel merged commit b93f2a8 into pmd:main Oct 23, 2026
13 checks passed
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.

[java] Unqualified method call resolves to an enclosing class method that the inner class hides

3 participants