The technical principle analysis and use guide of the ObjNESIS framework in the Java class library
Objeneis is a Java class library that is used to instantiate objects by bypassing the constructor.Its main purpose is to provide a way to create objects at runtime without calling the constructor.This article will explore the technical principles of the Objenes frame and provide guidelines.
Technical principle:
OBJNESIS uses Java bytecode to operate technology, bypassed the constructor of the class to directly instantiate the object.It is based on a simple principle, that is, the constructive function is only used to complete the initialization work when the memory distribution object instance is used.Objeneis assigned it to the object by direct allocating memory and jumping through the process of calling the constructor.This method is more efficient than using reflected or traditional constructor calls.
user's guidance:
1. Add dependencies: First of all, you need to add an ObjinSis dependencies to the Java project.You can introduce the following Maven dependencies in the construction configuration file of the project:
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.2</version>
</dependency>
2. Import class library: By importing the ObjneSis class library in the code, you can use its function.You can use the following import statements:
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
3. Experimental object: It is very simple to use ObjenesIS instances.The following is an example:
Objenes objenes = new objenersstd (); // Create Objenesis objects
Myclass myclass = objects.newinstance (myclass.class); // instantiated object
In the above example, we first created the ObjinSis object, and then used the `newInstance` method to instantiate a class called MyClass.At this time, Objeneis will skip the direct instance of the constructor.
4. Configuration instantiated strategy: Objeneis provides different instantiated strategies to meet different needs.For example, you can use the `Instantiatorsstrategy` interface to define the customized strategy of customized.The following is an example:
Objenesis objenesis = new ObjenesisStd(new YourInstantiatorStrategy());
In this example, we configure the customized instantiated strategy by passing the `YourinstantiatorsStrategy` to the constructor function of the` ObjeneSISSTDD "constructor.
Summarize:
Objeneis is a powerful Java class library that can greatly simplify the instance of the object.By bypassed the constructor, Objenesis realizes the instantiated object of rapid and efficient objects.By following the guidelines of the use of this article, you can easily use Objeneis in your Java project and improve the performance of the code.
Please note that although Objeneis can bypass the constructor, it does not mean that it can bypass the object.When using Objeneis, you still need to pay attention to ensure that the object status is properly initialized and there is no potential security loophole.