Class 1 recording mechanism

JVM class loaders from the top down mainly have BootStrapClassLoader, ExtClassLoader AppClassLoader these in the Android source code can not find the specific definition of the data JDK package inside

BootStrapClassLoader loads core classes, implemented by C++, such as String. ExtClassLoader AppClassLoader is located under the security package and is related to security.

The parent delegation mode is used for class loading. That is, the parent loader is used to load the class first. If the parent loader is not finished, the parent loader is used to load the class. Ensure that the class is unique. The process of passing up and looking down. Prevents multiple copies of the same bytecode from appearing in memory.

The Android class loading mechanism has three main classes: PathClassLoader (DelegateLastClassLoader), DexClassLoader and InMemoryClassLoader inherit from BaseClassLoader. Load a file into memory with the corresponding class

From the source comments to see the role of three classes:

The PathClassLoader loads internal application files, not external files. Android uses it to load in-app files

DexClassLoader can load external dex JAR APK files and load files that are not installed on the system. It is recommended that the file be stored in context.getCodecachedir.

InMemoryDexClassLoader loads dexFile in memory and executes code that has not been written to the local file system

Class loading is to instantiate an object from a.class file or a dex file. The external system obtains a binary stream based on the class name, gives it to the JVM, and then instantiates it into a class instance. ClassLoader only applies to class loading. Each classLoader constructor passes a parent class, which is loaded using the parent classLoader in preference. When the parent classLoader fails to load, the child classLoader is used to load the parent classLoader

The three subclasses implement a single constructor that calls the methods of the parent class and eventually calls the logic in findClass in BaseDexClassLoader:

Android 10:

1. If sharelibaryLoader! = null, use this classloader for the shelf

Pathlist.findclass (name);

3. PathList is responsible for iterating through a collection of dexElements with methods that contain Elements.

4. Use Element to hold a reference to DexFile. Classes are logged by the loadClassBinaryName of DexFile. DefineClass –>defineClassNative () to load the class.

Android 9: createClass —-> modifyClass —-> defineClass —-> classForName

2 the reflection

Reflection is an advanced use of class loading, and the reflection related classes are located under the java.lang.Reflect package in the JDK. The main classes are Constructor Field Method Proxy Type InvocationHandler. There is also a class class under the java.lang package

Initialize a Class using the new keyword, Class (newInstance, constructure.newinstance), and clone.

Reflection application scenarios

1. Create an object. Display in place of new.

GetDeclareConstructure setAccessible(true) getDeclareConstructure setAccessible(true)

Get the private property getDeclaredField

4. Get the private getDecalredMethod

5, reflection system method. Modify some values of runtime variables

3 Dynamic Proxy

The difference between dynamic proxy and static proxy is that the definition of proxy class is reduced, and the process control of proxy method is unified.

Dynamic proxies are implemented with the help of Proxy ClassLoader InvocationHandler Class (passing real objects through the constructor) classes and the newProxyInstance() method. When the object calls its own method, the invoke method of InvocationHandler will be executed. Invoke the method.invoke (real object) method to execute the method by reflection. You can control both before and after invoke execution.

Look at the source code look a little confused. Behind newProxyInstance method will be called Constructure – > ConstructorAccessorImpl – > DelegatingConstructorAccess

Because the interface cannot create objects through new, it cannot get method calls. If the interface does not have an implementation class, but wants to call methods in the interface and encapsulate its own implementation. Dynamic proxies can be used.

Retrofi uses a method of creating objects using dynamic proxies.

Call Retrofit’s create method.