IDEA设置查看JDK源码

问题

我们在查看JDK源码时,可能会遇到这种情况,步入底层查看JDK源码时,出现一堆var变量,可读性非常之差,例如笔者最近想看到nio包下的SocketChannelImplwrite方法,结果看到这样一番景象:

public int write(ByteBuffer var1) throws IOException {
        if (var1 == null) {
            throw new NullPointerException();
        } else {
            Object var2 = this.writeLock;
            synchronized(this.writeLock) {
                this.ensureWriteOpen();
                int var3 = 0;
                boolean var20 = false;

                byte var5;
                label310: {
                    int var27;
                    try {
                        var20 = true;
                        this.begin();
                        Object var4 = this.stateLock;
                        synchronized(this.stateLock) {
                            if (!this.isOpen()) {
                                var5 = 0;
                                var20 = false;
                                break label310;
                            }

                            this.writerThread = NativeThread.current();
                        }

                        do {
                            var3 = IOUtil.write(this.fd, var1, -1L, nd);
                        } while(var3 == -3 && this.isOpen());

                        var27 = IOStatus.normalize(var3);
                        var20 = false;
                    } finally {
                        if (var20) {
                            this.writerCleanup();
                            this.end(var3 > 0 || var3 == -2);
                            Object var11 = this.stateLock;
                            synchronized(this.stateLock) {
                                if (var3 <= 0 && !this.isOutputOpen) {
                                    throw new AsynchronousCloseException();
                                }
                            }

                            assert IOStatus.check(var3);

                        }
                    }

                    this.writerCleanup();
                    this.end(var3 > 0 || var3 == -2);
                    Object var28 = this.stateLock;
                    synchronized(this.stateLock) {
                        if (var3 <= 0 && !this.isOutputOpen) {
                            throw new AsynchronousCloseException();
                        }
                    }

                    assert IOStatus.check(var3);

                    return var27;
                }

                this.writerCleanup();
                this.end(var3 > 0 || var3 == -2);
                Object var6 = this.stateLock;
                synchronized(this.stateLock) {
                    if (var3 <= 0 && !this.isOutputOpen) {
                        throw new AsynchronousCloseException();
                    }
                }

                assert IOStatus.check(var3);

                return var5;
            }
        }
    }

所以本文就带大家演示如何添加OpenJDKzip包确保可以阅读到源码。

配置步骤

下载OpenJDK的zip包

首先我们步入官网,地址:https://hg.openjdk.org/,然后选择所需的JDK版本进行下载,以笔者为例选择JDK8

IDEA设置查看JDK源码_第1张图片

然后选择JDK进入下一个界面:

IDEA设置查看JDK源码_第2张图片

直接点击zip即可开始下载:

IDEA设置查看JDK源码_第3张图片

完成卸载后我们会得到这样一个zip包。

在这里插入图片描述

添加到IDEA中

基于IDEA查看我们要查看源码的项目,点击Project Structure,选择Sourcepath,点击添加,将刚刚的zip包添加进来。

IDEA设置查看JDK源码_第4张图片

随后会弹出这样一个页面,要我们选择需要导入的classes,笔者这里直接选择全部:

IDEA设置查看JDK源码_第5张图片

测试

随后我们再次查看对应源码,所有的变量都语义化了:

public int write(ByteBuffer buf) throws IOException {
        if (buf == null)
            throw new NullPointerException();
        synchronized (writeLock) {
            ensureWriteOpen();
            int n = 0;
            try {
                begin();
                synchronized (stateLock) {
                    if (!isOpen())
                        return 0;
                    writerThread = NativeThread.current();
                }
                for (;;) {
                    n = IOUtil.write(fd, buf, -1, nd);
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
                        continue;
                    return IOStatus.normalize(n);
                }
            } finally {
                writerCleanup();
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
                synchronized (stateLock) {
                    if ((n <= 0) && (!isOutputOpen))
                        throw new AsynchronousCloseException();
                }
                assert IOStatus.check(n);
            }
        }
    }

参考

idea查看 rt.jar源码:https://blog.csdn.net/yangyangrenren/article/details/117554745

你可能感兴趣的:(日常配置,intellij-idea)