Comparison of the technical principles of Objeneis frame and Java library
Comparison of the technical principles of Objeneis frame and Java library
Summary: Objeneis is a Java library, which aims to provide a way to instantiate objects by skipping the constructor.This article will compare the technical principles of ObjinSis framework and Java class library and provide relevant Java code examples.
1 Introduction
In Java, it is necessary to instantiate a structure function.But in some cases, we hope to skip the execution of the constructor and create a object directly.This demand may be due to performance optimization, testing, derivativeization, or other special application scenarios.The ObjenesIS framework provides a solution that does not need to call the constructor to instantiate the object without calling the constructor.
2. Objenesis framework principle
The core idea of the ObjenesIS framework is to create objects directly in memory by bypass the constructor.It uses the technique of Java bytecode manipulation to dynamically generate a subclass or agent class, thereby achieving instance of the target class.In this way, the ObjenesIS framework can create an object that does not call the constructor.
Below is a basic example, demonstrating the process of creating objects of ObjNESIS framework:
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
import org.objenesis.instantiator.ObjectInstantiator;
public class ObjenesisExample {
public static void main(String[] args) {
Objenesis objenesis = new ObjenesisStd();
ObjectInstantiator<ExampleClass> instantiator = objenesis.getInstantiatorOf(ExampleClass.class);
ExampleClass instance = instantiator.newInstance();
// Use an object that does not need to call the constructor
instance.doSomething();
}
}
class ExampleClass {
public ExampleClass() {
// The constructor will not be called
System.out.println("Constructor called");
}
public void doSomething() {
// After the object is created, you can call the method after successful creation
System.out.println("Doing something");
}
}
In this example, we use the ObjenesIS framework to instantiated an `ExampleClass` object.It can be seen that in the process of creating objects, there is no calling constructor, but it directly creates an instance.This method can bring performance improvement in some special scenarios.
3. The instance of the Java library
Compared with the Objeneis frame, the instantiated process in the Java class library creates an object by calling the constructor.The Java class library provides a series of methods of `newInstance (), which is used to instantiate the constructor with reflection calls.
Below is a simple example, showing how to use the reflection call constructor in the Java class library to instantiate the object:
import java.lang.reflect.Constructor;
public class JavaLibraryExample {
public static void main(String[] args) {
try {
Class<?> clazz = ExampleClass.class;
Constructor<?> constructor = clazz.getConstructor();
ExampleClass instance = (ExampleClass) constructor.newInstance();
// Use an object that is instantiated by calling the constructor
instance.doSomething();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this example, we use the Java class library to institimate a `ExampleClass` object by reflecting the constructor.First, we get the constructor of the target class, and then create the object through the `newInstance () method.It is necessary to call the constructor's process, which ensures the correct initialization of the object.
4. Objenesis framework comparison with the Java class library
-State principle: Objeneis is instantiated with the object that does not require the constructor through bytecode manipulation, and the Java class library creates an object by reflecting the constructor.
-Profage difference: Since Objeneis creates an object directly in memory to avoid calling the constructive function, its performance may be better in some specific scenarios.The instance of the Java library needs to reflect the constructor in the process of reflecting, which will bring additional overhead.
-Factor difference: OBJNENSIS only provides an instantiated function, and the reflection mechanism of the Java class library can not only instantly instance objects, but also call and operate the methods of class and members.
in conclusion:
ObjenesIS frameworks are instantiated with bytecode manipulation that do not need to call an object of the constructor, thereby providing performance advantages in some scenarios.In contrast, the Java library can create objects by reflecting the constructor to achieve more functions.When choosing a framework, developers should choose according to specific needs and scenes.
Reference link:
-OBJNESIS official website: https://objestsis.org/
-JAVA reflection mechanism: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/package-summmmlml