问题
我们在查看JDK
源码时,可能会遇到这种情况,步入底层查看JDK
源码时,出现一堆var变量,可读性非常之差,例如笔者最近想看到nio
包下的SocketChannelImpl
的write
方法,结果看到这样一番景象:
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;}}}
所以本文就带大家演示如何添加OpenJDK
的zip
包确保可以阅读到源码。
配置步骤
下载OpenJDK的zip包
首先我们步入官网,地址:https://hg.openjdk.org/,然后选择所需的JDK
版本进行下载,以笔者为例选择JDK8
:
然后选择JDK
进入下一个界面:
直接点击zip
即可开始下载:
完成卸载后我们会得到这样一个zip包。
添加到IDEA中
基于IDEA
查看我们要查看源码的项目,点击Project Structure
,选择Sourcepath
,点击添加,将刚刚的zip
包添加进来。
随后会弹出这样一个页面,要我们选择需要导入的classes,笔者这里直接选择全部:
测试
随后我们再次查看对应源码,所有的变量都语义化了:
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