You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] Type-name-qualified invocation filters out instance methods before overload resolution #7152
In the sample, m(Object) is applicable to Foo.m(value) by strict invocation and is the compile-time declaration, so javac 21 rejects the call:
Foo.java:6: error: non-static method m(Object) cannot be referenced from a static context
javac searches all member methods (JLS 15.12.2.1) and checks the modifier after choosing the most specific one: "the compile-time declaration must be static, or a compile-time error occurs" (JLS 15.12.3).
For TypeName.m(args), MethodInvocMirror.getAccessibleCandidates passes staticOnly = true and TypeOps.getMethodsOf drops the instance methods:
PMD does not see m(Object). It resolves the call to m(int) and getOverloadSelectionInfo().isFailed() is false. With an interface as qualifier, only the static methods declared in the interface are candidates (TypeOps.java:2077-2080), so an abstract or default method is skipped the same way.
Found while working on #7136, where UnnecessaryBoxing reruns the overload resolution without the unboxing. It resolves the call without intValue() and reports value.intValue() in Foo.m(value.intValue()), which does not compile without it.
Code Sample demonstrating the issue:
publicclassFoo {
staticvoidm(intx) { }
voidm(Objecto) { }
voidt(Integervalue) {
Foo.m(value); // javac: non-static method m(Object) cannot be referenced from a static context
}
}
Steps to reproduce:
javac Foo.java, also with --release 8, fails with the error above.
Run PMD on Foo.java with the XPath rule //MethodCall[pmd-java:matchesSig("Foo#m(int)")]. It reports line 6.
Activity
[java] UnnecessaryBoxing: rerun the overload resolution without the u…
[java] Deprecate TypeOps.getMethodsOf