Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to debug Lambdas in Java 8

I just started playing with Java 8 Lambdas and I noticed that I can't debug them in the NetBeans IDE. If I try to attach a breakpoint to the following code I get a variable breakpoint which is definately not what I wanted:

private EventListener myListener (Event event) ->
{
  command1;
  command2; // Set Breakpoint here
  command3;
};

NetBeans attaches the debugger at the "myListener" variable but I can't step into the EventListener itself so I can't see what is happening inside it.

Is there debugging information missing, is this a missing feature in NetBeans or is it not at all possible to debug Lambdas in Java?

like image 311
mw88 Avatar asked Mar 20 '14 11:03

mw88


2 Answers

It works for me in Eclipse. For example:

public class Foo {

    private static final Runnable r1 = () -> {
        System.out.println("r1a");
        System.out.println("r1b");
    };

    public static void main(String[] args) {
        Runnable r2 = () -> {
            System.out.println("r2a");
            System.out.println("r2b");
        };

        r1.run();
        r2.run();
    }
}

I can add breakpoints to individual lines within both r1 and r2, and they get hit appropriately, with stepping etc.

If I put a breakpoint on just the run() calls, I can step into the relevant lambda expression too.

So it sounds like all the debug information at least can be available.

EDIT: Apparently the above works in Netbeans too - I suggest you try it to check that you can get that working.

like image 159
Jon Skeet Avatar answered Sep 19 '22 10:09

Jon Skeet


With the following sample code in Netbeans 8 Release:

private void init() {
    List<Map<Integer, String>> mapList = new ArrayList<>();

    Map<Integer, String> map1 = new HashMap<>();
    map1.put(1, "String1");
    mapList.add(map1);

    Map<Integer, String> map2 = new HashMap<>();
    map2.put(2, "String2");
    mapList.add(map2);

    Map<Integer, String> map3 = new HashMap<>();
    map3.put(1, "String3");
    mapList.add(map3);

    Map<Integer, String> map4 = new HashMap<>();
    map4.put(2, "String4");
    mapList.add(map4);

    Map<Integer, List<String>> response = mapList.stream()
            .flatMap(map -> map.entrySet().stream())
            .collect(
                    Collectors.groupingBy(
                            Map.Entry::getKey, 
                            Collectors.mapping(
                                    Map.Entry::getValue, 
                                    Collectors.toList()
                            )
                    )
            );
    response.forEach((i, l) -> {
        System.out.println("Integer: " + i + " / List: " + l);
    });
}

I can set a breakpoint on System.out.println("Integer: " + i + " / List: " + l); and inspect the values (i, l) perfectly fine.

So I'm inclined to say that it is working.

like image 32
skiwi Avatar answered Sep 19 '22 10:09

skiwi