Repository navigation
Conversation
…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.
Contributor
|
Compared to main: (comment created at 2026-11-14 03:04:21+00:00 for de2fe32) |
3 of 4 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe the PR
TypeOps.isAccessibleaccepted 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,mainand javac select different overloads forbase.m(value)andnew Base(value)inSub, and forsub.m(value)inSub.Inner.Base::mandBase::newfrom 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 toJavaResolvers.isMemberMethodfrom that PR.The candidates of qualified method calls,
newexpressions and method references now go throughJavaResolvers.isAccessibleMethodandisAccessibleConstructor. They follow JLS 6.6.2 andResolve.isProtectedAccessiblein javac 21, with the exemption that javac gives tosuper.Sof its declaring class, which includes the classes nested inS. The enclosing classes are searched outwards. An interface never counts asS, so in a top-level interfaceimpl.clone()finds no accessibleclone(), as with javac.Sor a subclass ofS(6.6.2.1). Static methods and thesuperforms skip this check.super(..), which keeps its own check.new Base(value)andBase::newselectBase(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 protectedr(Object),base::ris now exact, andrun(base::r)selectsrun(Function)overrun(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 toObject.clone(). Withp.Base extends p.Missingand noMissing.class,base.m(value)in a subclass resolves tom(int).LambdaCanBeMethodReferenceRulechecks a method reference that it builds from the call. An unqualified call of an instance method is checked asthis::m, without the qualifier check, sox -> m(x)keeps its suggestion for an inherited protectedm. In a class nested in the subclass, the rule now suggestsSub.this::m. When the first lambda parameter is the receiver, the rule suggests the raw declaring type, as inBase::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 anObjectargument and a protectedm(Object), javac rejectsBase::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 asList::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, andInternalApiBridgegets no new method. PMD no longer callsTypeOps.accessibleMethodFilter,lazyFilterAccessibleandfilterAccessible, so this PR deprecates them, as #7299 does forgetMethodsOf.javac drops a method that the qualifier type overrides before it checks access (
Resolve.notOverriddenIn). Ifr.Sub3 extends q.Suboverrides the protectedm(Object)ofp.Basewith a protected method, javac resolvessub3.m(value)inq.Subtom(int), and PMD still selectsBase.m(Object).X.supergets the typeX(LazyTypeResolver.visit(ASTSuperExpression)), so whenSubOoverridesm(Object),SubO.super.m(v)resolves toSubO.m(Object), where javac selectsBase.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 asObject, the declaring class, a sibling subclass, the subclass and a type variable with an intersection bound, andthis,superandSub.superOverloadResolutionTest: 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 classOverloadResolutionTest: protected static methods,t.clone()with<T extends K & J>where the classKdeclaresclone(Object...), qualifiers whose type or superclass is unresolved, private and package-private members, and conflicting access modifiersCtorInferenceTest:super(..),new C(..)and anonymous classes, in the subclass and in an unrelated classMethodRefInferenceTest: bound, unbound,superand constructor references with exact and inexact candidates,run(base::r)andmake(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 boundTen 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.xmlin #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 asJEditorPane.this.firePropertyChange(..)atJEditorPane.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 verifywith Java 21 on Windows 11. Of 10,835 tests, none failed or had errors, and 21 were skipped.Related issues
Ready?
pmd-java./mvnw clean verifypasses (checked automatically by github actions)