Skip to content

[java] Unqualified method call resolves to an enclosing class method that the inner class hides #7151

Closed

Description

@saryn17

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:

/**
* 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:

  1. javac Comb.java, also with --release 8, then javap -c -p 'Comb$Inner': the call is m:(J)V.
  2. Run PMD on Comb.java with the XPath rule //MethodCall[pmd-java:matchesSig("Comb#m(java.lang.Object)")]. It reports line 8.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. added this to the 7.29.0 milestone on Oct 23, 2026
  2. added
    in:type-resolution
    in:symbol-table
    and removed
    in:type-resolution
    on Oct 23, 2026
  3. added 2 commits that reference this issue on Oct 23, 2026
    b93f2a8
    1d042fa
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

    in:symbol-table

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions