Repository navigation
Description
Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)
Description:
In the sample, Inner declares m(long) and the enclosing class Comb declares m(Object). For m(value) with an Integer value, javac searches only the innermost class that has a method named m (JLS 15.12.1), so Inner.m(long) is the only candidate and the call compiles to m:(J)V. PMD resolves the call to Comb.m(Object).
The unqualified lookup merges the methods of the enclosing class into the scope of the inner class and shadows only override-equivalent ones:
pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/JavaResolvers.java
Lines 291 to 329 in 5c9271c
| /** | |
| * Merges two method scopes, the otherResult is the one of an enclosing class, | |
| * the inner result is the one inherited from supertypes (which take precedence | |
| * in case of override equivalence). | |
| * | |
| * <p>Non-static methods of the outer result are excluded if the inner scope is static. | |
| */ | |
| private static List<JMethodSig> methodMerger(boolean inStaticType, List<JMethodSig> myResult, List<JMethodSig> otherResult) { | |
| if (otherResult.isEmpty()) { | |
| return myResult; | |
| } // don't check myResult for emptiness, we might need to remove static methods | |
| // For both the input lists, their elements are pairwise non-equivalent. | |
| // If any element of myResult is override-equivalent to | |
| // another in otherResult, then we must exclude the otherResult | |
| BitSet isShadowed = new BitSet(otherResult.size()); | |
| for (JMethodSig m1 : myResult) { | |
| int i = 0; | |
| for (JMethodSig m2 : otherResult) { | |
| boolean isAlreadyShadowed = isShadowed.get(i); | |
| if (!isAlreadyShadowed && TypeOps.areOverrideEquivalent(m1, m2) | |
| || inStaticType && !m2.isStatic()) { | |
| isShadowed.set(i); // we'll remove it later | |
| } | |
| i++; | |
| } | |
| } | |
| if (isShadowed.isEmpty()) { | |
| return CollectionUtil.concatView(myResult, otherResult); | |
| } else { | |
| List<JMethodSig> result = new ArrayList<>(myResult.size() + otherResult.size() - 1); | |
| result.addAll(myResult); | |
| copyIntoWithMask(otherResult, isShadowed, result); | |
| return Collections.unmodifiableList(result); | |
| } | |
| } |
The merger is installed in SymTableFactory (lines 410-412). Both methods then reach the overload resolution, m(Object) is applicable by strict invocation and wins.
Found while working on #7136.
Code Sample demonstrating the issue:
class Comb {
void m(Object o) { }
class Inner {
void m(long x) { }
void t(Integer value) {
m(value);
}
}
}Steps to reproduce:
javac Comb.java, also with--release 8, thenjavap -c -p 'Comb$Inner': the call ism:(J)V.- Run PMD on
Comb.javawith the XPath rule//MethodCall[pmd-java:matchesSig("Comb#m(java.lang.Object)")]. It reports line 8.
Running PMD through: Other (PmdAnalysis API)
Activity
[java] UnnecessaryBoxing: rerun the overload resolution without the u…
[java] Fix pmd#7151: Apply the comb rule to unqualified method calls (p…
Enable test case after pmd#7151 has been fixed