Describes a tool class that bypasses constructors to create Java instances:
If a class does not have an empty constructor method, you will throw an exception if you call the Newinstance method directly to try to get an instance object. Can there be a way to instantiate an object by bypassing the construction method?
Objenesis provides it with solutions on four different JVMs.
Sun Hotspot VM, versions 1.3, 1.4, 1.5 and 1.6
GCJ version 3.4.4 (tested on Windows/cygwin)
BEA JRockit versions 7.0 (1.3.1), 1.4.2 and 1.5
Aonix PERC (no serialization support), tested on version 5.0.0667 protobuf The objenesis construct instance when deserializing (version 2.1)
<dependency>
<groupId>org.objenesis</groupId>
<artifactid>objenesis</ Artifactid>
</dependency>
Source
Package org.wit.ff.util;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import java.util.List;
Import Com.dyuproject.protostuff.LinkedBuffer;
Import Com.dyuproject.protostuff.ProtostuffIOUtil;
Import Com.dyuproject.protostuff.Schema;
Import Com.dyuproject.protostuff.runtime.RuntimeSchema; /** * * <pre> * serial Number tool * </pre> * @author F.fang/public class Protostuffserializerutil {Publ IC static <T> byte[] Serialize (T obj) {if (obj = = null) {throw new RuntimeException ("Serialization Object (" +
obj + ")!"); } @SuppressWarnings ("Unchecked") schema<t> Schema = (schema<t>) Runtimeschema.getschema (obj.ge
Tclass ());
Linkedbuffer buffer = linkedbuffer.allocate (1024 * 1024);
byte[] Protostuff = null;
try {protostuff = Protostuffioutil.tobytearray (obj, schema, buffer); catch (Exception e) {throw new RuntimeeXception ("+ obj.getclass () +") object ("+ obj +") An exception occurred! ", e);
finally {buffer.clear ();
return protostuff; public static <T> T deserialize (byte[] paramarrayofbyte, class<t> targetclass) {if (ParamArray Ofbyte = = NULL | |
Paramarrayofbyte.length = = 0) {throw new RuntimeException ("an exception occurred in deserialization object, byte sequence is empty!");
} T instance = null;
try {// t message = objenesis.newinstance (CLS);
Instance = Targetclass.newinstance (); catch (Instantiationexception |
Illegalaccessexception e) {throw new RuntimeException (failed to create object on type during deserialization! ", e);
} schema<t> Schema = Runtimeschema.getschema (Targetclass);
Protostuffioutil.mergefrom (Paramarrayofbyte, instance, schema);
return instance; public static <T> byte[] Serializelist (list<t> objlist) {if (objlist = null | | objlist.is
Empty ()) { throw new RuntimeException ("The Serialized object list (" + objlist + ") parameter exception!");} @SuppressWarnings ("unchecked") schema<t> Schema = (schema<t>) runtimeschema.getschema (objlist.get (0). g
Etclass ());
Linkedbuffer buffer = linkedbuffer.allocate (1024 * 1024);
byte[] Protostuff = null;
Bytearrayoutputstream BOS = NULL;
try {bos = new Bytearrayoutputstream ();
Protostuffioutil.writelistto (Bos, objlist, schema, buffer);
Protostuff = Bos.tobytearray ();
An exception occurred in the catch (Exception e) {throw new RuntimeException ("The list of serialized objects (" + objlist + ")!", e);
finally {buffer.clear ();
try {if (bos!=null) {bos.close ();
} catch (IOException e) {e.printstacktrace ();
} return Protostuff; } public static <T> list<t> DeserializelIST (byte[] paramarrayofbyte, class<t> targetclass) {if (Paramarrayofbyte = null | | paramarrayofbyte.length
= = 0) {throw new RuntimeException ("The deserialization object has an exception, the byte sequence is empty!");
} schema<t> Schema = Runtimeschema.getschema (Targetclass);
list<t> result = null;
try {result = Protostuffioutil.parselistfrom (new Bytearrayinputstream (paramarrayofbyte), schema);
The catch (IOException e) {throw new RuntimeException ("an exception occurred in the deserialized list of objects!", e);
return result; }
}
Test code
public class Protostuffserializerutiltest {public
static class person{
int id;
String name;
Public person () {
} public person
(int id, String name) {
this.id = ID;
this.name = name;
public int getId () {return
ID;
}
Public String GetName () {return
name;
}
}
@Test Public
Void Demo () {person
p = new person (1, "FF");
byte[] arr = protostuffserializerutil.serialize (p);
Person result = Protostuffserializerutil.deserialize (arr, person.class);
Assertequals (P.getid (), Result.getid ());
Assertequals (P.getname (), Result.getname ());
}