How is our code loaded into the JVM for execution? (a)

Java -> 2. Javac compilation -> 3. XXX.Class -> 4.ClassLoader -> 5. Bytecode interpreter, JIT -> 6. Execution engine execution

Java files are compiled into class files

Java files are familiar, but class files are unfamiliar. The JVM runs class files, and any file that can be interpreted as a class file can run on the JVM, separating the language from the executing code (the adapter pattern idea), which is where “compile once, run everywhere” comes from. We can take a look at the structure of the class file to help us understand the following process.

You can use the jclasslib Bytecode Viewer plugin in IDEA to help you look at this.

The plugin will automatically translate the description into Chinese, so look at the following file structure and what it means:

ClassFileFormat

  • Magic Number: this file is a. Class file — cafe Babe
  • Minor Version: Indicates the Minor Version number, which is backward compatible
  • Major Version — Major Version number, backward compatibility
  • Constant_pool_count — Number of constant pools
  • Constant_pool (table with length constant_pool-conunt-1) — constant pool
  • Access_flags — Access flags (bitable)
  • This_class — current class
  • Super_class – parent class
  • Interfaces_count – Implements several interfaces
  • Interfaces — Concrete interfaces
  • Fields_count — Number of attributes
  • Fields — Concrete attributes
  • Methods_count — Number of methods
  • Methods — specific methods
  • Attributes_count – u2 — What attributes are attached
  • Attributes – Specific attached attributes

For a more in-depth look, see this article: Explaining the structure of Class files

This is the reading of the class file, and the contents of step 4 ClassLoader will be described.

ClassLoader ClassLoader

Bootstrap ClassLoader, Extension ClassLoader, App ClassLoader, Custom ClassLoader…

The working steps of the class loader

Loading — Loading

Class loader loading mechanism: parent delegate mechanism (figure below). When A class file is loaded, the class object is returned if the custom class loader (A) has already been loaded, the parent property of the class loader (A) is used if it has not, and the class object is returned if the parent class loader (B) has already loaded. If no parent class loader (B) has been loaded, the parent class loader (C) has not been loaded until the Bootstap(top level) has not been loaded, then the path of the class file will be asked if it is loaded, if it is loaded, if it is not loaded, then it will be loaded. It is loaded by the custom class loader.

Advantages of parent delegation mechanism: avoid class reloading, core class tampering and other security situations, mainly for security reasons.

Disadvantages of parent delegation: If a class has already been loaded by a class loader (especially Bootstrap), no other class loader can modify it, and no other interface implementation is reloaded, but the developer really needs to modify the interface implementation, then the SPI(Service provider Interface) mechanism needs to be used. Implement the policy mode and hot swap effect.

Public class JVMTest {public static void main(String[] args) {ClassLoader classLoader1 = JVMTest.class.getClassLoader(); ClassLoader classLoader2 = classLoader1.getParent(); ClassLoader classLoader3 = classLoader2.getParent(); System.out.println("classLoader1 == " + classLoader1); // classLoader1 == sun.misc.Launcher$AppClassLoader@14dad5dc System.out.println("classLoader2 == " + classLoader2); // classLoader2 == sun.misc.Launcher$ExtClassLoader@19469ea2 System.out.println("classLoader3 == " + classLoader3); // classLoader3 == null --> because Bootstrap is implemented by C++, Java does not have this class, so return null}}Copy the code

ClassLoader source code parsing

So this is understanding what ClassLoader is to help you understand the context. LoadClass: loadClass: loadClass: loadClass: loadClass: loadClass: loadClass

When loading a class, the entry method is loadClass. Here is the pseudocode for loadClass.

protected Class<? > loadClass(String name, boolean resolve){ synchronized (getClassLoadingLock(name)) { // First, Check if the class has already been loaded. > c = findLoadedClass(name); If (c == null) {if (c == null) { = null) { c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); } if (c == null) { // If still not found, C = findClass(name); c = findClass(name); ResolveClass (c); resolveClass(c); } return c; }}Copy the code

ResolveClass (): Link the specified class. This method is used by the Classloader to link a class. If the class has already been linked, it simply returns. Otherwise, the class will be linked according to the Execution description in the Java™ specification.

FindClass () implementation in the URLClassLoader class

protected Class<? > findClass(final String name){ final Class<? > result; result = AccessController.doPrivileged( new PrivilegedExceptionAction<Class<? >>() { public Class<? > run() throws ClassNotFoundException { String path = name.replace('.', '/').concat(".class"); Resource res = ucp.getResource(path, false); if (res ! Return defineClass(name, res); } else { return null; } } }, acc); return result; }Copy the code

2. Linking – link

  1. Verification, validation

Verify that the class file meets the requirements of the vm (for example, “cafe Babe” at the beginning of the class file) to ensure that the class file is loaded correctly.

  1. Preparation, Preparation

Static variables are assigned default values, static fields in the class are allocated memory, base types are assigned default values (such as long=0L,int=0), and reference types are assigned null.

  1. Resolution – parse

The class file contains a lot of references to a class’s information in the constant pool. It would be inefficient to use the constant pool every time. Therefore, the parsing process will locate the direct references for symbolic references to classes, interfaces, methods, and member variables.

3.Initializing — Initializing

Static variable assigned to initial value

Next up

  • The custom this
  • Very special context classloader
  • Use a custom ClassLoader to implement the hot swap function

JVM custom ClassLoader(2)