Skip to content

[java] Protected access check ignores enclosing classes, the qualifier type and the constructor form #7147

Open

Description

@saryn17

Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)

Description:

TypeOps.isAccessible accepts a protected member of another package when the class passed as access site is a subclass of the declaring class:

private static boolean isAccessible(JExecutableSymbol method, JClassSymbol ctx) {
Objects.requireNonNull(ctx, "Cannot check a null symbol");
int mods = method.getModifiers();
if (Modifier.isPublic(mods)) {
return true;
}
JClassSymbol owner = method.getEnclosingClass();
if (Modifier.isPrivate(mods)) {
return ctx.getNestRoot().equals(owner.getNestRoot());
}
return ctx.getPackageName().equals(owner.getPackageName())
// we can exclude interfaces because their members are all public
|| Modifier.isProtected(mods) && isSubClassOfNoInterface(ctx, owner);

For a qualified method call and for new C(...) without a class body, the access site is the innermost enclosing class of the call (MethodInvocMirror.java:72, CtorInvocMirror.java:103).

JLS 6.6.2 permits the access in the body of a subclass S, and that body includes the classes declared in S. From Sub.Inner in the sample, sub.m(value) does not see protected m(Object) and resolves to m(int). javac calls m(Object) through the synthetic accessor Sub.access$000.

A protected instance method may be accessed through a qualifier only if the type of the qualifier is S or a subclass of S (JLS 6.6.2.1). base.m(value) with a Base base resolves to m(Object) in PMD. javac selects m(int). For base.m((Object) value) it reports no suitable method found for m(Object) with the reason m(Object) has protected access in Base.

A protected constructor is accessible to super(...) and to an anonymous class, but not to new C(...) outside its package (JLS 6.6.2.2). new Base(value) resolves to Base(Object) in PMD. javac selects Base(int).

Method references go through the same filter (ExprOps.java:506 and :537). Base::m resolves to m(Object) and Base::new to Base(Object) in PMD, where javac binds the int overloads.

Found while working on #7136.

Code Sample demonstrating the issue:

package p;

public class Base {
    public Base(int value) { }
    protected Base(Object value) { }
    public void m(int value) { }
    protected void m(Object value) { }
}
package q;

import p.Base;

public class Sub extends Base {
    Sub() { super(0); }

    void test(Base base, Integer value) {
        base.m(value);   // javac: m(int)      PMD: m(Object)
        new Base(value); // javac: Base(int)   PMD: Base(Object)
    }

    class Inner {
        void test(Sub sub, Integer value) {
            sub.m(value); // javac: m(Object)  PMD: m(int)
        }
    }
}

Steps to reproduce:

  1. javac -d classes p/Base.java q/Sub.java, also with --release 8, then javap -c -p -cp classes q.Sub 'q.Sub$Inner': Sub.test calls p/Base.m:(I)V and p/Base."<init>":(I)V, Inner.test calls q/Sub.access$000, which invokes m:(Ljava/lang/Object;)V.
  2. Run PMD on q/Sub.java with classes on the auxiliary classpath and the XPath rules //MethodCall[pmd-java:matchesSig("p.Base#m(java.lang.Object)")] and //ConstructorCall[pmd-java:matchesSig("p.Base#new(java.lang.Object)")]. They report lines 9 and 10, and not line 15.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. saryn17 commented on Oct 8, 2026

    @saryn17
    ContributorAuthor

    I looked into how this could be fixed.

    The check in TypeOps only looks at the innermost enclosing class, and it never sees the type of the qualifier. It could take the qualifier type and walk the enclosing classes. The qualifier can be checked with getAsSuper so that intersection bounds work, while super.m(..) is exempt from that condition. Protected constructors would need separate handling by form (outside the package only super(..) and anonymous classes).

    The public helpers in TypeOps could stay as they are. The current getMethodsOf filters before collectMostSpecific, so the internal callers would need a site-aware variant, for example through types/InternalApiBridge like isSameTypeInInference. Method references would also need to know whether they are super-qualified, both in MethodRefMirrorImpl and in the mirror used by LambdaCanBeMethodReferenceRule (x -> super.m(x) becomes super::m).

    Two things this does not cover: X.super.m() starts the search at X instead of its superclass, and field access has the same protected-access problem.

    Does this direction look right? If so, I can put together a PR. For the site-aware variant, would InternalApiBridge be acceptable, or would you prefer public overloads in TypeOps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions