Skip to content

[java] Fix #7152: Consider instance methods for type-name-qualified calls - #7299

Open
saryn17 wants to merge 5 commits into
pmd:mainfrom
saryn17:fix-7152
Open

saryn17 wants to merge 5 commits into
pmd:mainfrom
saryn17:fix-7152

Conversation

@saryn17

@saryn17 saryn17 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

For a call like Foo.m(value), MethodInvocMirror asked TypeOps.getMethodsOf for static methods only. JLS 15.12.2.1 searches all member methods, and 15.12.3 checks the static modifier after the most specific method is chosen. With static m(int) and m(Object), javac selects m(Object) for an Integer argument and reports that it is not static. PMD resolved the call to m(int). With an interface as qualifier, the abstract and default methods of the interface were skipped the same way.

The candidates of a qualified call are now the accessible methods that are members of the qualifier type, for a type name and for an expression. For an expression, main checked accessibility only, which also admits methods that are not members. These calls compile, and main resolved them differently from javac:

  • child.m(value), where Child declares static m(int) and its superclass in the same nest has a private m(Object): javac resolves the call to Child.m(int), main to Parent.m(Object)
  • the same call through a type variable U extends T with T extends Child, or through T extends Child & Marker
  • j.clone(), where the interface J declares Object clone(): javac resolves the call to J.clone(), main to Object.clone()
  • t.clone() with <T extends I & J>, where I declares void clone(Object...): javac resolves the call to I.clone(Object...), main to Object.clone()

JavaResolvers.isMemberMethod decides membership. It takes the checks of the method scope of a class for private and package-private methods of supertypes and for static interface methods, and adds that an interface has only the public methods of Object (JLS 9.2). The method scope now calls it. On main, Object.clone() in the method scope of an interface also hides the methods named clone of an enclosing class: in an interface nested in a class that declares static void clone(int), clone(1) is not resolved. javac calls the method of the enclosing class, and PMD now does too.

An intersection has the members of its induced class or interface (JLS 4.9), which inherits neither private methods nor static interface methods, even those declared by a component. If the class component is Object, the induced type is an interface and has only the public methods of Object. For <T extends I & J>, javac finds Object.clone() and reports that t.clone() has protected access in Object. When I declares clone(Object...), javac resolves t.clone() to that method, and PMD now does too. With a single bound <T extends C>, javac selects a private method of C and reports that it has private access, and PMD keeps that choice.

For a type name qualifier, a protected method of another package is checked against JLS 6.6.2 instead of the accessibility check of PMD, which looks only at the innermost enclosing class and not at the qualifier type (#7147). Such a method is accessible in the body of a subclass S of its declaring class, including the classes nested in S, and an instance method also needs the type to be S or a subclass of S (6.6.2.1). Without the qualifier check, Base.m(value) in a subclass in another package would select a protected m(Object) instead of static m(int), which javac calls and main selects. Sub.m(value) in that subclass selects m(Object), as javac does before it reports that the method is not static. In a class nested in that subclass, Base.s(value) with a protected static s(Object) and a public static s(int) compiles and javac calls s(Object). main resolves it to s(int).

When an instance method is chosen for a type name qualifier, PMD keeps it as the compile-time declaration. Unqualified calls in a static context behave the same way: the method scope contains the instance methods and the static context is not checked after the choice.

MethodInvocMirror no longer calls TypeOps.getMethodsOf, and nothing else in PMD does, so it is deprecated.

New cases in OverloadResolutionTest, each checked against javac 21:

  • the sample of the issue, an inherited instance method, and a static method that stays selected, with a type name qualifier
  • an interface qualifier with an abstract method and a static method
  • static interface methods of a superinterface, of an interface implemented by the qualifier class, and through an expression
  • a private method of a superclass in the same nest, with a type name, an expression, a type variable bounded by a type variable and an intersection bound
  • a private method and a static interface method declared by a component of an intersection, and a private method of a single bound
  • a package-private method whose inheritance stops at a subclass in another package
  • protected and public methods of Object for an interface and for an intersection of interfaces, qualified and in an interface body, and an interface nested in a class with a clone method
  • protected instance and static methods of another package through a type name, from a subclass, from classes nested in a subclass, from the same package and from an unrelated class, with two new test classes in javasymbols.testdata

For an expression qualifier, the qualifier type of a protected method is still not checked. With <T extends K & J>, where the class K declares clone(Object...), PMD resolves t.clone() to Object.clone() on main and with this PR, while javac calls K.clone(Object...). That is #7147.

Ran ./mvnw -B -ntp -pl pmd-java verify with Java 21. Eighteen of the 25 new tests fail on main. The other seven cover cases that main resolves as javac does.

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)

…d calls

For a call like Foo.m(value), MethodInvocMirror asked
TypeOps.getMethodsOf for static methods only. JLS 15.12.2.1 searches
all member methods and 15.12.3 checks the static modifier after the
most specific one is chosen, so javac selects an instance method there
and reports an error.

The candidates of a qualified call are now the accessible methods that
are members of the qualifier type. Accessibility alone admitted methods
that are not members, such as a private method of a superclass in the
same nest or a protected method of Object for an interface type. The
member check of the method scope of a class moves to
JavaResolvers.isMemberMethod, which both use. An intersection has the
members of its induced class or interface (JLS 4.9), which inherits
neither private methods nor static interface methods of its components.
The induced interface, used when the class component is Object, has
only public members.

For a type name qualifier, a protected method of another package is
checked against JLS 6.6.2 instead of the accessibility filter, which
only looks at the innermost enclosing class and not at the qualifier.
Such a method is accessible in the body of a subclass S of its
declaring class, including the classes nested in S. An instance
method also needs the type to be S or a subclass of S.
@pmd-actions-helper

pmd-actions-helper Bot commented Nov 7, 2026 •

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-11-14 03:01:37+00:00 for f4fb2d9)

@oowekyala

Copy link
Copy Markdown
Member

TypeOps.getMethodsOf keeps its signature and behaviour. MethodInvocMirror no longer calls it.

So does this method still have a use? If it's not meant to be kept, it should be deprecated.

Since the fix of pmd#7152, PMD does not call this method.
@saryn17

saryn17 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

No, nothing in PMD calls it after this PR. I've deprecated it in 16906bc and added it to the release notes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] Type-name-qualified invocation filters out instance methods before overload resolution

2 participants