eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Wrong call hierarchy for method reference

Open
#847 1 comment 2 reactions 0 assignees View on GitHub
bug java search
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=539602

To reproduce, compute the call hierarchy of `test.Test.bar(Test)` in the following snippet:

```
public class Test {

private Listener listener = null;

public interface Listener {
void process(Test source);
}

public void addListener(Listener l) {
listener = l;
}

public void foo() {
addListener(this::bar);
}

private void bar(Test test) {
System.out.println("hello world: " + test);
}

public void other() {
listener.process(this);
}

public static void main(String[] args) {
Test t = new Test();
t.foo();
t.other();
}
}
```

The call hierarchy shows `foo()`, but there only a listener is registered - `foo()` doesn't actually call `bar()`. We expect `process(Test)`, callers of which should be `other()`.

Changing the `foo()` to use a lambda, the resulting call hierarchy is closer to what would be correct, but still wrong:

```
public void foo() {
addListener(t -> bar(t));
}

```

The call hierarchy now shows `process(Test)`, but the callers of `process(Test)` are empty (`foo()` is listed with a label `declaration`).

Changing `foo()` to use an anonymous class, the call hierarchy is as expected and contains `process(Test)` and `other()` (`foo()` is listed with a label `constructor`):

```
public void foo() {
addListener(new Listener() {
@Override
public void process(Test source) {
bar(source);
}
});
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the call hierarchy for the provided Java example, then compare the method-reference, lambda, and anonymous-class variants. Done means the method-reference and lambda cases identify process(Test) correctly and show other() as its caller, matching the anonymous-class behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.