How to Read Java Source Code
Often there is a lot of trivial code in open-source projects that we don’t need to read. To read a Java open-source project, the most important thing is to understand the core functionalities of the project.
We need to understand the core functionalities one by one. First, we read the code for the most important functionality in the project. Then read the code of the next core functionality.
The process of reading the source code
Understand the core functionality implementation process: A map of the source code
1. First, we need to find the starting point of the functionality implementation.
2. Use the debugger to find out the process of functionality implementation.
3. Draw a sequence diagram of the functionality implementation process.
4. Continuously debug the program and improve the sequence diagram to make it more complete and detailed.
Understand the hierarchy of Java classes and their functions: The Basics
1. Understand the hierarchy of all important Java classes encountered during the functionality implementation.
2. Write a functional description of each core Java class.
Use your own words to simply describe the top-level class of each Java class hierarchy.
Write a description for the rest of the classes of each Java class hierarchy. Or copy the class description from the source code.
3. Draw class diagrams.
4. Understanding the design of class hierarchies.
Understand the details of function implementation
1. Understand each key step in the process of functionality implementation.
2. Explain the code of each key step.
Other Tips
Understand the class by its name
Many design patterns are often used in open-source projects. When you read the code of the project, you can know its functionality through the class name, such as xxxFactory, and xxxAdapter.
How to find the method call chain?
Finding the method call chain means finding the most important code in each method.
There is generally pre-processing and post-processing code in a method. The key code is usually in the middle and back position of the method.
How to know which specific implementation class an interface or abstract class variable is?
It’s best to use the debugger (Step into) to find out the specific implementation class an interface or abstract class variable is.
How to know when a certain field of an object has been processed?
Set breakpoints at the line of the assignment statements of the field. And see the call stacks on the frame of the debugger tool window.
Or set field watchpoints to monitor the field initialization, read, and write.