After nearly a year, my first book, Understanding JVM Bytecode In Depth, is finally published.

The idea for this book came from the gold-digger booklet JVM Bytecode from Beginner to Master, which was my first technical column, and it was also in the spirit of summing up my knowledge of JVM bytecode. After the launch of Nuggets, although the sales volume was average, I received a lot of opinions and saw many problems with incomplete knowledge system. It was a coincidence that the machine Press came up with this book.

The process of writing a book is quite dark. Notion gives itself a deliverable plan.

Just like writing code, I was too optimistic when I estimated it. When I started to write, I realized that the water was really deep. Polishing content, typesetting and proofreading were all physical work. After writing the complete version, I spent nearly two months revising and perfecting the manuscript. After the launch of the small volume, I spent nearly half a year to complete the whole manuscript day and night, and handed it to the publishing house.

Why write this book

About four or five years ago, adhering to the concept of “code untouched, monitoring first”, I started to build a service quality monitoring platform in the platform department of the company. At the very beginning, the human body burying point was used to report monitoring information. The access cost of the business side was very high, and it took half a day to one day to bury the point before going online, which was unbearable.

Later, the company promoted DevOps, containerization, and microservices, which improved the efficiency of development and operation and maintenance. However, at the same time, problems caused by the distributed deployment architecture quickly became apparent, such as unclear service topologies, complex service dependencies, and scattered logs in various microservices. When a problem occurs online, the developer does not know how to troubleshoot and cannot quickly locate the problem. Later, I learned that javaAgent + ASM technology can automatically inject embedded code, spent about a month to learn bytecode, ASM related knowledge, ASM that official English manual read more than 5 times, well aware of the difficulties. In addition, APM, a set of performance monitoring tools, requires extremely high reliability and very low performance loss, forcing me to have a deeper study on bytecode execution principle and high-performance Java.

I spent about three months more than a month of time a person from zero to a implementation of a complete set of not invade the bytecode injection of APM system, on the whole the back-end service call stack for the monitoring, implements business buried the zero point, expanding process of heterogeneous system call link tracking, performance problems code level real-time positioning, business topology discovery, SLA real-time statistics, and other functions, Online operation for several years without any problems.

Usually like to toss pojie some things, at the beginning are directly modified class file way to deal with, more cumbersome. Later, AFTER mastering javaAgent, ASM, JVMTI these tools, one can’t get out of hand, holding the purpose of learning painless Pojie Censum, GCEay, FastThread, HeapHero, ELK and other commercial software, feel quite a sense of achievement.

He moved from the platform group to the business group with a larger team, bringing in Kotlin early on to do back-end development. Due to the lack of knowledge behind grammatical sugar, students with poor foundation often misunderstand the meaning of some grammatical sugar. In view of this situation, I made a detailed analysis of the implementation behind a large number of grammars in Kotlin, so that they could know more about what happened behind them when using them.

Along the way, I feel that the lower-level knowledge is more valuable, so I want to write a book like this, so that more people can know more about the low-level details of JVM bytecode, and can start to analyze problems and have their own independent judgment.

The reader object

By reading this book, I hope you can go from knowing why to knowing why, understand the implementation principles behind cool syntax candy and advanced frameworks, implement various JVM technologies, and really understand the implementation details behind reflection, Lambda expressions, AOP, hot loading, software Pojie, and more.

This book is suitable for the following readers.

  • Readers who have a rudimentary understanding of Java and want to learn more about the inner workings
  • Readers of high-performance frameworks that learn bytecode rewriting techniques
  • Readers who are interested in APM and want to know the realization principle of APM and build APM system to do distributed system call chain tracking
  • Want to learn Java software common POjie and anti-POjie method, improve their own software reverse and pojie threshold readers

How to read this book

The book consists of 12 chapters, which are mainly divided into two parts: bytecode principle and application.

Chapter 1 dissects the inner structure of a class file in detail to make the bytecode principles introduced later in the book easier to understand.

Chapter 2 begins with an introduction to what bytecode is, followed by an introduction to Java virtual machine stacks and stack frames, followed by an introduction to bytecode instructions using syntax such as for loops, switch-case, and try-catch-finally.

In chapter 3, we introduce the advanced knowledge of bytecode. The main purpose is to let the reader master the basic implementation principles of method call instructions, generic erase, synchronized keyword, and reflection.

Chapter 4 introduces the principle of Javac compiler. The principle of compiler is the pearl in the crown of computer science. Only by understanding Javac can we better understand the principle of bytecode generation. This chapter begins with an introduction to Javac source debugging, followed by a detailed description of the seven phases of Javac compilation and the role of each phase.

Chapter 5 introduces the Kotlin language from the perspective of bytecode, introduces the common syntax sugar, coroutine and other principles, hope readers can learn from other JVM languages can use similar ideas.

Chapter 6 introduces the two bytecode manipulation tools ASM and Javassist, which are very important and are used by a large number of middleware frameworks. The APM and Software POjie sections cover the use of these two tools.

Chapter 7 introduces the principle of Java Instrumentation, divided into two ways to explain the use of Instrumentation, and finally introduces the underlying Unix domain socket communication principle of Attach API.

Chapter 8 introduces the principles of JSR 269 plug-in annotation processing. We hope you can learn how to modify the generated code during compilation and understand the implementation principles behind Lombok and ButterKnife tools.

Chapter 9 focuses on the application of bytecode in cglib, Fastjson, Dubbo, Jacoco, and Mock frameworks, which will expose readers to more bytecode usage scenarios.

Chapter 10 mainly introduces the content related to decompilation, pojie, anti-Pojie and reverse engineering. The content of this chapter is not to teach how to pojie other people’s paid software, but to better protect your software products by understanding common POjie and reverse engineering methods.

Chapter 11 introduces an overview of APM, the basic principles of distributed tracing, the basic concepts of OpenTracing, and the code implementation of unburied bytecode petting. If you are interested in APM, you can read this chapter as a starting point to implement your own APM products.

Chapter 12 introduces in detail the structure of Android DEX file, the difference between Android bytecode instructions and Java bytecode instructions, and finally introduces the Gradle bytecode rewriting to achieve non-intrusive petting method.

JVM bytecode technology is huge, and this book only scratches the surface of the iceberg. I hope I can use this book to teach them to fish. If readers can master some tools and methods through reading this book to solve the practical problems in the development process, I will be very satisfied.

I drew a mind map of the book for you to understand, as shown below.

Thank you

Finally, I would like to thank fuchuan and Li Yi from the publishing house, as well as Rong Ge, Old Qian, Cheng Ge and Zhou Zhiming who wrote the recommendation for me.