Repository navigation
Description
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:
pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java
Lines 2106 to 2122 in 5c9271c
| 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:
javac -d classes p/Base.java q/Sub.java, also with--release 8, thenjavap -c -p -cp classes q.Sub 'q.Sub$Inner':Sub.testcallsp/Base.m:(I)Vandp/Base."<init>":(I)V,Inner.testcallsq/Sub.access$000, which invokesm:(Ljava/lang/Object;)V.- Run PMD on
q/Sub.javawithclasseson 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
[java] UnnecessaryBoxing: rerun the overload resolution without the u…
saryn17 commentedon Oct 8, 2026
I looked into how this could be fixed.
The check in
TypeOpsonly 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 withgetAsSuperso that intersection bounds work, whilesuper.m(..)is exempt from that condition. Protected constructors would need separate handling by form (outside the package onlysuper(..)and anonymous classes).The public helpers in
TypeOpscould stay as they are. The currentgetMethodsOffilters beforecollectMostSpecific, so the internal callers would need a site-aware variant, for example throughtypes/InternalApiBridgelikeisSameTypeInInference. Method references would also need to know whether they are super-qualified, both inMethodRefMirrorImpland in the mirror used byLambdaCanBeMethodReferenceRule(x -> super.m(x)becomessuper::m).Two things this does not cover:
X.super.m()starts the search atXinstead 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
InternalApiBridgebe acceptable, or would you prefer public overloads inTypeOps?[java] Deprecate TypeOps helpers that PMD no longer calls