8

I think 5 years has passed since Lambda has been released for Java.

public static void main(String[] args) {
    go();
}

private static void go() {
    Set<String> set = new HashSet<>();
    set.stream().forEach((s) -> {
        inner(s);
    });
}

private static void inner(String s) {
    inner1(s);
}

private static void inner1(String s) {

}

When I press CTRL-ALT-H - (open call hierarchy) on inner1 method, I expect to see a whole stack trace from in inner1 to main method. Instead, my staktrace is trimmed on inner method. I've just downloaded the newest Eclipse, I think it 2018-12, previously I was using Mars.

enter image description here Intellij can show me the expected call-hierarchy, and I don't really understand why Eclipse still can't do it. Not sure if anyone else is using Eclipse in 2019, but maybe you can advise a plugin or something.

Switching to Intellij is not an option, I tried couple of times, but the habit is hard to overcome.

UPDATE

There is similar - SO question

At run time, evaluation of a lambda expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. Evaluation of a lambda expression is distinct from execution of the lambda body.

and

Just note, that for lambdas implementing library types like Consumer, the number of callers into accept(T) in a workspace may easily become unmanageable, similar to any call hierarchy through, e.g, Runnable.run() - but that doesn't question the general usefulness of call hierarchies through lambdas.

I don't really care about lambda internals, somehow other IDE is able to show expected stacktrace

Anton
  • 5,831
  • 3
  • 35
  • 45
  • 1
    `go()` is *not* invoking `inner(String)`. All it does, is passing a lambda expression to a method entirely outside your code base. That method, the actual `forEach` implementation *may* invoke the method through the lambda expression, but that’s knowledge not connected with your code. The fact that another IDE shows something, is not a proof of correctness. In this specific case, it’s just speculating. – Holger Mar 11 '19 at 11:47
  • @Hoger I think you are trolling – Anton Mar 11 '19 at 15:15
  • I don’t know why you think that. In your example code, `inner` will actually be never invoked. So Eclipse’s answer is even correct in this case. You may dispute the result because `forEach` *could* invoke `inner` if the `HashSet` had elements, but what about, e.g. `Executors .newSingleThreadExecutor() .submit( () -> inner("foo") )` which will definitely not invoke the method (in this call chain)? Or `entirelyUnknownMethod( s -> inner(s) )`? Do you expect Eclipse to know all contracts or to peek in every implementation? Or just assume it is invoked, even if not? There is no right answer here. – Holger Mar 11 '19 at 15:42
  • Eclipse sets a precedent with anonymous-classes, showing 2 sub trees for [constructors] vs [callers] of the class. In fact you can Ctrl-1 over the `->`, select "Convert to anonymous class creation" and the call hierarchy then shows a path back to `main` via [constructors]. *Could call* is what I expect. If the method holding the lambda is somewhere in the view then I can browse to the code & decide whether to keep following the call chain, without discarding the other results of the call-hierarchy view. – Luke Usherwood Mar 12 '19 at 13:08

2 Answers2

3

There's an existing old bug for eclipse, reported in 2016, still in NEW status

Bug 498498 - [1.8][search][call hierarchy] No usage for lambdas

Call hierarchy on bar method correctly shows usage in accept, and for accept there is no usage shown. This issue was already present in mars.

There are 3 votes to fix it, you can vote too

From your edit's links there's another relevant old bug in NEW status

Bug 468561 - [search]Call Hierarchy stops searching in doubly-nested lambda chain

with 3 votes too...

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • It sounds promising – Anton Mar 10 '19 at 11:29
  • I just linked those two bug reports to each other. One is marked as "helpwanted" the other actually has a proposed fix, which just fell between the cracks when its author left the team. So, yes there definitely is hope, one way or other :) – Stephan Herrmann Mar 12 '19 at 10:48
  • @StephanHerrmann thank you Stephan. I'm pretty sure I'm not the only one, who is suffer from this issue. Another SO questions has 18 upvotes. Even google has "eclipse call hierarchy lambda" search suggestion – Anton Mar 12 '19 at 11:58
2

Eclipse 4.22 (Q4 2021) should help:

Improved lambda support in the Call Hierarchy view

The Call Hierarchy view is enhanced with showing not only the callers of the lambda function, but the callers of the declaring function too.

For the following code:

Sample code

Checking the callers of the function() will show this:

CallHierarchy result

The [declaration] node in the tree is the new addition, which shows the callers of the definer() function, in this case, only the main() function.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250