1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
<!--
Copyright 2006-2024 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
<head>
<title>Twenty Second Tutorial</title>
</head>
<body>
<p>There are two main interfaces in Objenesis:</p>
<ul>
<li><strong><a href="${javadoc}/org/objenesis/instantiator/ObjectInstantiator.html">ObjectInstantiator</a></strong>
- Instantiates multiple instances of a single class.
<pre>interface ObjectInstantiator {
Object newInstance();
}</pre>
</li>
<li><strong><a href="${javadoc}/org/objenesis/strategy/InstantiatorStrategy.html">InstantiatorStrategy</a></strong>
- A particular strategy for how to instantiate a class (as this differs for different types of classes).
<pre>interface InstantiatorStrategy {
ObjectInstantiator newInstantiatorOf(Class type);
}</pre>
</li>
</ul>
<p><strong>Note:</strong> All Objenesis classes are in the
<strong><code>org.objenesis</code></strong> package.</p>
<h1>Step By Step</h1>
<p>There are many different strategies that Objenesis uses for instantiating objects based on the JVM vendor,
JVM version, SecurityManager and type of class being instantiated.</p>
<p>We have defined that two different kinds of instantiation are required:</p>
<ul>
<li><strong>Stardard</strong> - No constructor will be called</li>
<li><strong>Serializable compliant</strong> - Acts like an object instantiated by java standard
serialization. It means that the constructor of the first non-serializable parent class will
be called. However, specific implementation methods (e.g. <code>readResolve()</code>) are not called and we never check if the object is serializable.</li>
</ul>
<p>The simplest way to use Objenesis is by using <a href="${javadoc}/org/objenesis/ObjenesisStd.html">ObjenesisStd</a> (Standard) and
<a href="${javadoc}/org/objenesis/ObjenesisSerializer.html">ObjenesisSerializer</a> (Serializable compliant). By default,
automatically determines the best strategy - so you don't have to.</p>
<pre>Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer</pre>
<p>Once you have the Objenesis implementation, you can then create an <code>ObjectInstantiator</code>, for a specific type.</p>
<pre>ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(MyThingy.class);</pre>
<p>Finally, you can use this to instantiate new instances of this type.</p>
<pre>MyThingy thingy1 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy2 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy3 = (MyThingy)thingyInstantiator.newInstance();</pre>
<h1>Performance and Threading</h1>
<p>To improve performance, it is best to reuse the <code>ObjectInstantiator</code>
objects as much as possible. For example, if you are instantiating multiple instances of a specific class,
do it from the same <code>ObjectInstantiator</code>.
</p>
<p>Both <code>InstantiatorStrategy</code> and <code>ObjectInstantiator</code> can be shared between multiple
threads and used concurrently. They are thread safe.</p>
<h1>That Code Again</h1>
<p>(For the impatient)</p>
<pre>
Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer
MyThingy thingy1 = (MyThingy) objenesis.newInstance(MyThingy.class);
// or (a little bit more efficient if you need to create many objects)
Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer
ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(MyThingy.class);
MyThingy thingy2 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy3 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy4 = (MyThingy)thingyInstantiator.newInstance();
</pre>
</body>
</html>
|