java - 如何使用 jdb 调试 lambda 表达式

标签 java debugging lambda jdb

我在集群生产服务器中遇到问题,我设法将单个实例与用户隔离开来,因此我可以使用它进行调试,我正在使用 jdb 执行此操作。长介绍结束。我的问题是我需要将代码调试为 lambda 表达式

public void method(){
    this.privateField = Util.methodCall(); // Here the breakpoint works
    Clazz.staticMethod(() -> {
       Integer x = 1;
       Long y = 2; 
       y = x * y; // I need a Break point here
       /* 
         And a lot of non related code
       */
       });
}

当我在该行的 jdb 中创建断点时,它会忽略我的断点。我很确定调用该方法是因为断点到达了该方法的第一行,但只是忽略了另一行,我正在使用 nextstep 命令,但没有工作。那么我的过程可能有什么问题呢?如何使用 jdb 调试 lambda 表达式?

最佳答案

这里是对登陆这里并感兴趣的人的回答。

创建类

import java.util.concurrent.Callable;
public class Clazz {

    public static void main(String[] args) throws Exception {
        new Clazz().method();
    }

    public void method() throws Exception {
        Clazz.staticMethod(() -> {
            Integer x = 1;
            Long y = 2L;
            y = x * y; // I need a Break point here
            return y;
        });
    }

    private static void staticMethod(Callable i) throws Exception {
        System.out.println("i = " + i.call());
    }
}

编译它

javac Clazz.java

为这个类启动jdb

jdb Clazz
Initializing jdb ...

使用 stop 在 main 方法中设置断点

> stop in Clazz.main
Deferring breakpoint Clazz.main.
It will be set after the class is loaded.

使用run开始调试 session

> run
run Clazz
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
> 
VM Started: Set deferred breakpoint Clazz.main

Breakpoint hit: "thread=main", Clazz.main(), line=6 bci=0
6            new Clazz().method();

现在调试器在 main 方法中停止,就在调用 new Clazz().method(); 之前。

为了找到我们感兴趣的行,我们列出

main[1] list
2    
3    public class Clazz {
4    
5        public static void main(String[] args) throws Exception {
6 =>         new Clazz().method();
7        }
8        
9        public void method() throws Exception {
10            Clazz.staticMethod(() -> {
11                Integer x = 1;
main[1] list 12
8        
9        public void method() throws Exception {
10            Clazz.staticMethod(() -> {
11                Integer x = 1;
12 =>             Long y = 2L;
13                y = x * y; // I need a Break point here
14                return y;
15            });
16        }
17    

需要命令 list 12 来列出以下行。在输出中我们可以看到我们想要在 13 行停止。因此,让我们使用 stop 命令在此处设置一个新断点

main[1] stop at Clazz:13
Set breakpoint Clazz:13

继续执行直到下一个断点发出命令cont

main[1] cont
> 
Breakpoint hit: "thread=main", Clazz.lambda$method$0(), line=13 bci=12
13                y = x * y; // I need a Break point here

我们不在 13 行,例如可以dump xy 的值。

main[1] dump x
 x = {
    MIN_VALUE: -2147483648
    MAX_VALUE: 2147483647
    TYPE: instance of java.lang.Class(reflected class=int, id=568)
    digits: instance of char[36] (id=569)
    DigitTens: instance of char[100] (id=570)
    DigitOnes: instance of char[100] (id=571)
    sizeTable: instance of int[10] (id=572)
    value: 1
    SIZE: 32
    BYTES: 4
    serialVersionUID: 1360826667806852920
    java.lang.Number.serialVersionUID: -8742448824652078965
}
main[1] dump y
 y = {
    MIN_VALUE: -9223372036854775808
    MAX_VALUE: 9223372036854775807
    TYPE: instance of java.lang.Class(reflected class=long, id=574)
    value: 2
    SIZE: 64
    BYTES: 8
    serialVersionUID: 4290774380558885855
    java.lang.Number.serialVersionUID: -8742448824652078965
}

进一步一步

main[1] step
> 
Step completed: "thread=main", Clazz.lambda$method$0(), line=14 bci=26
14    

我们现在可以再次dump y

的值
main[1] dump y
 y = {
    MIN_VALUE: -9223372036854775808
    MAX_VALUE: 9223372036854775807
    TYPE: instance of java.lang.Class(reflected class=long, id=574)
    value: 2
    SIZE: 64
    BYTES: 8
    serialVersionUID: 4290774380558885855
    java.lang.Number.serialVersionUID: -8742448824652078965
}

关于java - 如何使用 jdb 调试 lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39111803/

相关文章:

java - 有没有更好的方法在java中组合两个字符串集?

java - 将 Jsoup 元素转换为字符串

debugging - 调试器如何从断点恢复?

ios - 我怎样才能准确地知道哪个物体导致了我的崩溃?

java - 将匿名类转换为 lambda 后代码表现不同

c++ - 在 lambdas 的上下文中,捕获这个词是什么意思?

c# - 使用 LINQ 时从字符串访问 lambda 表达式中的属性

java - Keys.SHIFT 在 Mac 中工作正常,但在使用 Selenium 的 Windows 中不行

java - Java 中的正则表达式分割线

java - 在我的 HTC Evo 上使用 Eclipse 进行 Android 调试 : Augh!