The technical principles of ObjenesIS framework in the Java library detailed explanation
Objeneis is an open source Java library that is used for instantiated objects without calling the constructor.It provides a simple API that can bypass the constructor of the class to directly create a new instance.In this article, we will introduce the technical principles of the ObjenesIS framework in detail.
1. Why do Objeneisis?
In Java, to create a new object, the constructor of the object is usually required.However, the constructor of some classes may be private or restricted and cannot be directly called.In addition, sometimes we may want to avoid some side effects or initialization logic in the constructor.This is where the ObjenesIS library works, which can bypass the direct instantiated object of the constructor.
2. Objeneis's working principle
Objenesis uses Java's bytecode operation and reflection mechanism to implement the object instance.It bypasses the constructor by generating byte code and creates a new object instance.
Specifically, Objeneis is instantiated by the object through the following steps:
-E analyzing the class structure of an object that is instantiated, including information such as fields, methods, and constructors.
-Ad the byte code operating library (such as ASM, byte Buddy, etc.) to generate a new class, which will inherit the class that will be instantiated.
-In newly generated classes, rewriting the constructor that to be instantiated to make it without actual operation or return NULL directly.
-In use Java's reflection mechanism, load and instantiate newly generated classes, and return a new object example.
Below is an example of using ObjinSis instantiated objects:
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
public class ExampleClass {
private String name;
public ExampleClass() {
this.name = "Default";
}
public String getName() {
return name;
}
public static void main(String[] args) {
Objenesis objenesis = new ObjenesisStd();
ExampleClass instance = (ExampleClass) objenesis.newInstance(ExampleClass.class);
System.out.println (Instance.getName ()); // Output: default
}
}
In the above example, we created a class called ExampleClass, which contains a private field name and a constructor.We use the ObjeneSISSTD class to create an ObjneSis instance, and then call its Newinstance method to instantiated ExampleClass objects.Since the constructor of the ExampleClass class is private, Objeneis can bypass the constructor to directly create an object instance.Finally, the name field value of our printed object, the verification object is instantiated successfully.
Summarize:
Objeneis is a very useful Java library that can bypass the direct instantiation object of the constructor.It uses byte code operation and reflection mechanism, bypasses the limitation of the constructor by generating byte code, and creates a new object instance.Whether it is to avoid access restrictions on private constructors or to improve performance, Objeneis is a very valuable tool.