I just did a quick experiment at Eclipse.
public class StackTractTest { static class Nasty { public Integer toInt() { if (1 == 1) throw new RuntimeException(); return 1; } } @Test public void methodReference() { Stream.of(new Nasty()) .map(Nasty::toInt) .findFirst(); } @Test public void lambda() { Stream.of(new Nasty()) .map(n -> n.toInt()) .findFirst(); } }
When the help method test fails, the trace begins
java.lang.RuntimeException at com.example.StackTractTest$Nasty.toInt(StackTractTest.java:11) at com.example.StackTractTest$$Lambda$1/1681433494.apply(Unknown Source) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
There is no reference to the line where the method reference is used, although the end of the trace (not shown) refers to the line with findFirst on.
While stackdrace lamdba begins
java.lang.RuntimeException at com.example.StackTractTest$Nasty.toInt(StackTractTest.java:11) at com.example.StackTractTest.lambda$0(StackTractTest.java:26) at com.example.StackTractTest$$Lambda$1/1681433494.apply(Unknown Source) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
What correctly identifies the lambda was used in line 26.
Is this a feature of the Eclipse compiler or is it a common disadvantage of using method references that should be considered when choosing between them and the lambda?
java stack-trace lambda java-8 method-reference
henry
source share