1 Star 11 Fork 0

LMW/JavaSource

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

JDK1.8源码阅读环境搭建

源码的学习者,与大家共勉

参考

  1. 曹工力荐:调试 jdk 中 rt.jar 包部分的源码 可自由增加注释,修改代码并debug
  2. idea中搭建jdk1.8源码阅读环境

1、背景

我们都知道,rt.jar包是java主要功能的实现包,但是在各个IDE中,打开java是无法进行源码编辑的,这样在阅读JDK源码带来许多的不便。那么怎样才可以实现在IDE中对JDK源码进行注释呢?

2、需求

首先讨论一下我们需要达成什么样的目标吧。

2.1、运行时关联代码

  1. 可以在IDEA中关联我们的源码,确保运行时,源码不会左右横跳到Oracle定义官方jar包中。

2.2、顶部注释

  1. 可以在源码中在代码顶部写下自己的注释,这点很重要。

顶部注释类似于这种

//比较的是两个对象的地址。
return (this == obj);

而尾行注释则是这种。

return (this == obj);//比较的是两个对象的地址。

我查询了许多搭建JDK源码的相关博客,但是他们都告诉我,只能写在只能在尾部注释,否则debug时,就会出现代码错位

debug错位演示1

此时我们 F7进行下一步时,就会出现下面的这种情况。

debug错位演示2

WTF?竟然没有跳到concat的源码,而是跳到了注释上。这就是JDK源码阅读时,常蹚的坑——debug代码错位。

很明显,尾行注释的方法即不美观,也影响了代码的可读性,所以我们不推荐这种方法。除了这些博主介绍的尾行注释法外,下面要介绍的就是Debug时加参数的方法。

3、基础环境

操作系统: Windows 8.1 Java版本: 1.8.0_152 IDEA: IntelliJ IDEA 2019.1.1

4、环境搭建步骤

大多数博主在搭建JDK阅读环境时都推荐使用简单Java项目的方式进行建项目。而我们这里则推荐建立Maven或者Gradle项目的方式进行源码环境的搭建。

4.1、为什么要使用Maven的方式建立项目?

一开始,我也是使用普通Java项目的方式搭建源码阅读环境的。直到我也出现了Debug错位的现象,我开始找各种解决办法。找了许多种办法,解决这种问题的思路就是重新编译一次,并且让IDEA识别修改后的代码。但是无论是Javac编译还是IDEA的Rebuild projecct都没法解决问题。当然也有大佬写了脚本解决了这个问题 [1]。但是到我这就水土不服了。

读完曹工的文章了,我找到原因了,原来是Java的类加载机制搞的鬼。由于编译后,Java会优先加载JDK中rt.jar中的源码,之后才会“看到”我们注释的同包同名的类,由于已经存在过了该类,根本就不会加载我们的类。所以使用maven的目的有两个;其一、引用junit的相关类运行注释后的代码。其二、将注释后的源码打包成jar,通过我们的配置,让BootStrapClassloader优先加载它。

4.2、新建一个Maven项目

File-> New -> project... -> Maven 新建maven项目1

新建maven项目2

确定了Maven项目存放的地方后,FINISH即可。

POM文件的相应配置如下 新建maven项目3

4.3、导入源码

在资源管理器中打开自己项目路径下的src/main/java,并且进入到java目录。在资源管理器中复制java的源码不会卡死IDEA。

导入源码1

打开自己的Java安装路径。找到src.zip,这个压缩文件就是Java的源码了。

导入源码2

导入源码3

并不需要导入太多源码,只需要导入java包下的io、lang、math、net、nio、text、time、util等八个包即可。下面是在这两年打算阅读的包。

导入源码4

4.4、IDEA设置

为了防止内存溢出,我们可以将堆内存加大,按下Ctrl+Alt+S,弹出Settings页面

IDEA设置1

之后点击下面的OK就好了。

4.5、关联源代码

同时按下Ctrl+Alt+Shift+S打开项目设置页面。

增加一个新的SDK,并将我们src/main/java下的源码关联到这个SDK中

关联源代码1

关联源代码2

关联源代码3

将原来关联到压缩包的sourcePath去除掉,并且改成我们自己项目的src/main/java目录。

关联源代码4

关联源代码5

为项目关联新增的SDK,并且设置语言特性。

关联源代码6

关联源代码7

4.6、测试

在src/main下建立一个Main类,

public class Main {

    public static void main(String[] args) {
        String hello = "Hello ";
        String world = "world";
        String helloworld = hello.concat(world);
        System.out.println(helloworld);
    }
}

运行这个类,第一次运行,由于需要将源码打包到target/目录下,所以会编译很久,甚至会运行失败。别灰心,运行maven菜单中的Reimport重新运行即可。

但是,此时debug发现,源码标记有Source code does not match the bytecode的提示,这就出现了前面所说的debug错位现象。

那么该如何解决呢?

4.7、解决方法

解决方法就是配置运行时的虚拟机参数—— -Dsun.boot.class.path

该参数的意思是优先加载指定目录的文件。只要将我们的jar包放在系统JDK的加载顺序之前,debug时,就可以正确地跳到源代码,而不会跳到注释行了。

可以通过以下代码获取该参数的值

String property = System.getProperty("sun.boot.class.path");
String targetClassesPath="D:\\IntelliJIDEAResposity\\JavaSource\\target\\classes";
System.setProperty("sun.boot.class.path",targetClassesPath +";"+ property);
System.out.println(System.getProperty("sun.boot.class.path"));

比如,我的参数是长这样的:

-Dsun.boot.class.path="D:\IntelliJIDEAResposity\Java8Source\target\classes;D:\Java\jdk1.8\jre\lib\resources.jar; D:\Java\jdk1.8\jre\lib\rt.jar;D:\Java\jdk1.8\jre\lib\sunrsasign.jar;D:\Java\jdk1.8\jre\lib\jsse.jar; D:\Java\jdk1.8\jre\lib\jce.jar;D:\Java\jdk1.8\jre\lib\charsets.jar;D:\Java\jdk1.8\jre\lib\jfr.jar; D:\Java\jdk1.8\jre\classes"

配置运行参数1 配置运行参数2

这里需要配置自己的targetClassesPath。然后将运行结果复制到该方法的虚拟机运行参数即可。

targetclasses路径

为了方便测试,我们可以将上面的参数复制到Junit的模板中。大家日常开发时,不要忘记将该参数暂时删除掉。

junit模板配置1

junit模板配置2

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

JDK8源码学习。 对JDK源码进行注释,并添加到思维导图中,归纳总结。 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者 (1)

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/lmw0726/java-source.git
git@gitee.com:lmw0726/java-source.git
lmw0726
java-source
JavaSource
master

搜索帮助