Skip to content

[java] Overload resolution prefers int over Integer where javac reports an ambiguity #7146

Closed

Description

@saryn17

Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)

Description:

With d(int, int), d(long, Integer) and an Integer value, javac 21 rejects d(value, 5):

Example.java:6: error: reference to d is ambiguous
  both method d(int,int) in Example and method d(long,Integer) in Example match

Both methods are applicable by loose invocation. For the second argument, int and Integer are not subtypes of each other, so neither method is more specific (JLS 15.12.2.5).

PMD resolves the call to d(int, int) and getOverloadSelectionInfo().isFailed() is false. PhaseOverloadSet.doInfer, called from isInferredMoreSpecific, compares the parameter types with checkConvertibleOrDefer:

JMethodSig sfun = TypeOps.findFunctionalInterfaceMethod(si);
JMethodSig tfun = TypeOps.findFunctionalInterfaceMethod(ti);
if (sfun == null || tfun == null) {
if (phase.canBox()) {
JTypeMirror stdExprTy = ei.getStandaloneType();
if (stdExprTy != null
// there is a boxing or unboxing conversion happening
&& stdExprTy.isPrimitive() != si.isPrimitive()
&& stdExprTy.isPrimitive() != ti.isPrimitive()) {
// si or ti is more specific if it only involves
// the boxing/unboxing conversion, without widening
// afterwards.
if (stdExprTy.box().equals(si.box())) {
continue;
} else if (stdExprTy.box().equals(ti.box())) {
return false;
}
}
}
infer.checkConvertibleOrDefer(ctx, si, ti, ei, phase, site);
continue;

In the loose phase, when one of the two types is primitive, Infer.isConvertible boxes both before comparing them (Infer.java:1004-1012). The pair e(long, long) and e(Object, Object), two of the overloads of JUnit 4 Assert.assertEquals, behaves the same way. assertEquals(value, 5) is ambiguous for javac and resolves to assertEquals(long, long) in PMD.

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 d(value.intValue(), 5), which does not compile without it.

Code Sample demonstrating the issue:

class Example {
    static void d(int x, int y) { }
    static void d(long x, Integer y) { }

    void test(Integer value) {
        d(value, 5); // javac: reference to d is ambiguous
    }
}

Steps to reproduce:

  1. javac Example.java, also with --release 8, fails with the error above.
  2. Run PMD on Example.java with the XPath rule //MethodCall[pmd-java:matchesSig("Example#d(int,int)")]. It reports line 6.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. added
    in:type-resolution
    on Oct 27, 2026
  2. added this to the 7.29.0 milestone on Oct 27, 2026
  3. added 2 commits that reference this issue on Oct 27, 2026
    d26fe78
    6727f3a
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:type-resolution

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions