Skip to content

[Bug][javasrc2cpg]TypeInferencePass fails to match method calls when argument types are subclasses of declared parameter types #5679

@jjzdxmd

Description

@jjzdxmd

Bug Description

The TypeInferencePass fails to match method calls when the argument types are subclasses of the declared parameter types. The type matching logic in doArgumentTypesMatch() only performs exact type matching and does not consider inheritance hierarchies.
Root Cause: Java's Liskov Substitution Principle allows subclass instances to be passed where parent class instances are expected. The current implementation violates this by requiring exact type matches.

Reproduce

Test case:Method call with subclass argument

// Class hierarchy
class Animal { }
class Dog extends Animal { }

// Method declaration
public void process(Animal animal) { }

// Method call
Dog dog = new Dog();
process(dog);

Expected CPG result:

  • The call should be matched to the process(Animal) method
  • call.methodFullName should be updated to the actual method's full name
  • call.signature should be updated to void(com.example.Animal)

Actual CPG result:

  • The call remains unmatched
  • call.methodFullName stays as <unresolvedNamespace>.process:<unresolvedSignature>(1)
  • call.signature remains <unresolvedSignature>(1)

Debug output:

Checking method: com.example.MyClass.process:void(com.example.Animal)
  parameterSizesMatch: true (1 == 1)
  argTypesMatch: false
    [0] Param: animal (com.example.Animal) vs Arg: dog (com.example.Dog) -> Match: false
  hasDifferingArg: true
  RESULT: false

Expected behavior

The TypeInferencePass should correctly match method calls where argument types are subclasses of the declared parameter types. Specifically:

  1. The type matching should consider the class hierarchy (inheritance relationships)
  2. If argument type A is a subclass of parameter type B, they should be considered compatible
  3. Method calls with subclass arguments should be successfully resolved when a matching parent-class method exists

Desktop

OS: macOS
Joern Version: latest
Java version: 21

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions