Skip to content

[java] Fix #7147: Check protected access as in JLS 6.6.2 - #7315

Open
saryn17 wants to merge 9 commits into
pmd:mainfrom
saryn17:fix-7147
Open

saryn17 wants to merge 9 commits into
pmd:mainfrom
saryn17:fix-7147

Conversation

@saryn17

@saryn17 saryn17 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

TypeOps.isAccessible accepted a protected method or constructor of another package when the innermost class around the access was a subclass of its declaring class. In the sample of #7147, main and javac select different overloads for base.m(value) and new Base(value) in Sub, and for sub.m(value) in Sub.Inner. Base::m and Base::new from the issue differ the same way.

This builds on #7299, whose commits come first here. It replaces the type name check that #7299 adds to MethodInvocMirror, and it puts the new checks next to JavaResolvers.isMemberMethod from that PR.

The candidates of qualified method calls, new expressions and method references now go through JavaResolvers.isAccessibleMethod and isAccessibleConstructor. They follow JLS 6.6.2 and Resolve.isProtectedAccessible in javac 21, with the exemption that javac gives to super.

  • A protected method of another package is accessible in the body of a subclass S of its declaring class, which includes the classes nested in S. The enclosing classes are searched outwards. An interface never counts as S, so in a top-level interface impl.clone() finds no accessible clone(), as with javac.
  • An instance method accessed through a qualifier also needs the qualifying type to be S or a subclass of S (6.6.2.1). Static methods and the super forms skip this check.
  • A protected constructor of another package is accessible to the creation of an anonymous class (6.6.2.2) and to super(..), which keeps its own check. new Base(value) and Base::new select Base(int), as javac does.

Whether a method reference is exact depends on which methods are accessible (JLS 15.13.1). In a subclass, with a public r(int) and a protected r(Object), base::r is now exact, and run(base::r) selects run(Function) over run(Consumer), as javac does.

If the qualifying type, or one of its superclasses below the declaring class, is unresolved, the qualifier check passes. With an incomplete auxclasspath, unknown.clone() still resolves to Object.clone(). With p.Base extends p.Missing and no Missing.class, base.m(value) in a subclass resolves to m(int).

LambdaCanBeMethodReferenceRule checks a method reference that it builds from the call. An unqualified call of an instance method is checked as this::m, without the qualifier check, so x -> m(x) keeps its suggestion for an inherited protected m. In a class nested in the subclass, the rule now suggests Sub.this::m. When the first lambda parameter is the receiver, the rule suggests the raw declaring type, as in Base::m. The reference built for the check now searches that raw type instead of the type of the parameter. For (s, x) -> s.m(x) with an Object argument and a protected m(Object), javac rejects Base::m, and the rule no longer suggests it. A lambda whose receiver type has a wildcard, such as (List<? extends Number> l, Integer i) -> l.get(i), is now reported as List::get, which javac accepts.

On the question in my comment on #7147: the checks use only public API, so they are in the internal JavaResolvers, and InternalApiBridge gets no new method. PMD no longer calls TypeOps.accessibleMethodFilter, lazyFilterAccessible and filterAccessible, so this PR deprecates them, as #7299 does for getMethodsOf.

javac drops a method that the qualifier type overrides before it checks access (Resolve.notOverriddenIn). If r.Sub3 extends q.Sub overrides the protected m(Object) of p.Base with a protected method, javac resolves sub3.m(value) in q.Sub to m(int), and PMD still selects Base.m(Object). X.super gets the type X (LazyTypeResolver.visit(ASTSuperExpression)), so when SubO overrides m(Object), SubO.super.m(v) resolves to SubO.m(Object), where javac selects Base.m(Object). This PR does not change the check for protected fields, the second gap in my comment on #7147.

New cases, each checked against javac 21 except the one with unresolved types:

  • OverloadResolutionTest: qualifiers typed as Object, the declaring class, a sibling subclass, the subclass and a type variable with an intersection bound, and this, super and Sub.super
  • OverloadResolutionTest: calls in an inner class, a static nested class, a nested interface, an anonymous class and a subclass nested in the subclass, and in a subclass and an anonymous subclass in an unrelated class
  • OverloadResolutionTest: protected static methods, t.clone() with <T extends K & J> where the class K declares clone(Object...), qualifiers whose type or superclass is unresolved, private and package-private members, and conflicting access modifiers
  • CtorInferenceTest: super(..), new C(..) and anonymous classes, in the subclass and in an unrelated class
  • MethodRefInferenceTest: bound, unbound, super and constructor references with exact and inexact candidates, run(base::r) and make(ProtectedMembers::new)
  • LambdaCanBeMethodReference.xml: this::m, super::m, Sub.this::m, Sub.super::m, a static method, a receiver parameter, and a receiver typed by a type variable with a wildcard bound

Ten of the fourteen new tests fail on 868b415, the head of #7299. The other four pass there too and check that this PR keeps those results. The disabled case of UnnecessaryBoxing.xml in #7136 that waits for #7147 passes with this change.

Outside the PR, I compared PMD with javac 21 on 444 method calls, constructor calls and method references in 14 scenarios. 292 of them resolve as with javac on 868b415, and 382 with this PR. None that matched before differs now. On 2,145 files of the JDK 21 sources (java.base, java.desktop, java.xml), 10 of 162,950 calls and references change, all from unresolved to resolved, such as JEditorPane.this.firePropertyChange(..) at JEditorPane.java:729. The reports of all Java rules except the documentation category do not change on these files, nor on 914 files of the PMD modules other than pmd-core and pmd-java, analyzed without auxclasspath.

Ran mvnw.cmd -B -ntp -pl :pmd-java verify with Java 21 on Windows 11. Of 10,835 tests, none failed or had errors, and 21 were skipped.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests in pmd-java
  • 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.
The accessibility check of overload resolution accepted a protected
method or constructor of another package whenever the innermost class
around the access was a subclass of its declaring class.

JavaResolvers.isAccessibleMethod follows JLS 6.6.2 for a protected
method of another package. The method is accessible in the body of a
subclass S of its declaring class, which includes the classes nested
in S, so the enclosing classes are searched outwards. An instance
method accessed through a qualifier also needs the qualifying type to
be S or a subclass of S. Static methods and the super forms skip this
check. If the qualifying type, or one of its superclasses below the
declaring class, is unresolved, the check passes. For a protected
constructor of another package, JavaResolvers.isAccessibleConstructor
accepts only the creation of an anonymous class, and super(..) keeps
its own check.

Qualified method calls, new expressions and method references use
these checks. They replace the check for type name qualifiers that the
fix of pmd#7152 added. The mirror of a method reference tells whether it
is qualified by super.

In LambdaCanBeMethodReference, the method reference built to check a
suggestion treats an unqualified call of an instance method as this::m.
When the first lambda parameter is the receiver, it now searches the
raw declaring type of the method, which is the type the suggestion
names. Before, it searched the type of the parameter.

TypeOps.accessibleMethodFilter, lazyFilterAccessible and
filterAccessible keep their signature and behaviour. PMD no longer
calls them.
@pmd-actions-helper

pmd-actions-helper Bot commented Nov 11, 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:04:21+00:00 for de2fe32)

Since the fix of pmd#7152, PMD does not call this method.
accessibleMethodFilter, lazyFilterAccessible and filterAccessible check
protected access with TypeOps.isAccessible, which does not follow
JLS 6.6.2. Since the fix of pmd#7147, PMD does not call them.

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] Protected access check ignores enclosing classes, the qualifier type and the constructor form

1 participant