通过IntelliJ IDEA单步调试JDK源码

通过IntelliJ IDEA单步调试JDK

0 使用环境:

宿主机操作系统:Windows 10 专业版

虚拟机软件:VMware® Workstation 16 Pro

虚拟机操作系统:ubuntu-22.04.1-desktop-amd64.iso

IntelliJ IDEA:2020.1.4

1 创建一个Java项目

在这里插入图片描述

2. 设置项目JDK环境

2.1 新增JDK

这里要选择我们自己编译后的JDK版本:jdk/build/linux-x86_64-server-fastdebug/images/jdk

在这里插入图片描述

在这里插入图片描述

2.2 修改源文件路径

在右上角有一个Project Structure入口,点击进入之后,找到SDKs位置,将Sourcepath全部清除(如果有数据),然后导入openjdk的源码文件中的src目录

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3. 开始Debug

配置完成上面的步骤,就可以开始调试JDK的源码了,在工程下面新建一个Java类,如下:

public class Demo {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

通过println方法跟踪进去,可以看到如下代码:

/**
     * Prints a String and then terminate the line.  This method behaves as
     * though it invokes {@link #print(String)} and then
     * {@link #println()}.
     *
     * @param x  The {@code String} to be printed.
     */
    public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

我们试着修改一下PrintStream类的println方法

/**
     * Prints a String and then terminate the line.  This method behaves as
     * though it invokes {@link #print(String)} and then
     * {@link #println()}.
     *
     * @param x  The {@code String} to be printed.
     */
    public void println(String x) {
        synchronized (this) {
            print("have a try ============");
            newLine();

            print(x);
            newLine();

            print("have a try try ============");
        }
    }

此时执行该main方法,会发现我们新添加的代码并没有出现在控制台中,这是因为修改JDK源码后并没有重新编译。只有重新编译后,才会使最新的代码生效。

进入到openjdk12源码根目录,重新编译make images,然后回到IntelliJ IDEA中继续运行该Demo的main方法,发现我们新添加的代码已经生效。

have a try ============
Hello World!
have a try try ============
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值