ObjenesIS framework in the Java library's application and technical principles in the Java library
Objeneis is a framework for instantiated Java objects. It can directly instance a object that can bypass the constructor of the class.In this article, we will explore the application of the ObjinSis framework in the Java class library and the technical principles behind it.
Objenesis has three main uses: to avoid using traditional constructor instance, efficiency improvement, and serialization.Let's discuss each purpose separately.
First of all, the traditional instantiated object needs to call the constructor of the class, and sometimes this constructor may be private, or it executes some other complex logic.Using Objeneis, we can directly instantiate the object and bypass the call of the constructor.
Secondly, the Objeneis frame can provide higher instantiation efficiency.In some scenarios, if we need to frequently instantiated objects, the traditional constructor method may consume a lot of time and resources.Objeneis will bypass some unnecessary steps when the object instances, thereby improving the instantiated efficiency.
Finally, Objeneis can also support some serialized scenes.Sometimes, we need to restore the state of an object at a certain point in the future, rather than perform the constructor when creating.Objenesis can support this demand by skipping the constructor when instantiated objects.
So how does Objeneis realize these functions?
The implementation principle of Objeneis is to skip the call of the constructor through the bytecode of the class.It uses an interface called "Instantiator" to complete the instance of the object.This interface has multiple default implementation, and each implementation is suitable for different scenes and requirements.
Below is an example code that uses the ObjinSis framework to instantiate an object:
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
public class ObjenesisExample {
public static void main(String[] args) {
// Create ObjinSis instance
Objenesis objenesis = new ObjenesisStd();
// Get an Instant;
Instantiator<MyClass> instantiator = objenesis.getInstantiatorOf(MyClass.class);
// Use Instant;
MyClass obj = instantiator.newInstance();
// Output object information
System.out.println(obj);
}
static class MyClass {
private String name;
public MyClass() {
this.name = "Objenesis";
}
@Override
public String toString() {
return "MyClass{" +
"name='" + name + '\'' +
'}';
}
}
}
In the above sample code, we use the ObjeneSSTD class as an Objestsis implementation, which is a standard implementation of Objeneis.We first created an ObjinSis instance, and then obtained an Instantiator object by calling the `Getinstant;Finally, we used Instantiator's `newInstance` method to instantiate a MyClass object and output it to the console.
In short, ObjinSis is a powerful Java instantiated framework that can achieve the needs of the direct instantiated object of the constructor and provide an efficient and flexible instantiated solution.Whether it is to avoid the calls of complex constructor, improving instantiated efficiency or supporting serialization scenarios, Objeneis can play a huge role.