mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-08 10:49:59 +00:00
java: changes Template interface: unpack(Unpacker, Object to = null), convert(MessagePackObject from, Object to = null)
This commit is contained in:
parent
e9d44b90bc
commit
76679d33df
36 changed files with 473 additions and 313 deletions
|
|
@ -20,8 +20,8 @@ package org.msgpack;
|
|||
import java.io.IOException;
|
||||
|
||||
public abstract class AbstractTemplate implements Template {
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
return convert(pac.unpackObject());
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return convert(pac.unpackObject(), to);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,6 @@
|
|||
package org.msgpack;
|
||||
|
||||
public interface MessageConverter {
|
||||
Object convert(MessagePackObject from) throws MessageTypeException;
|
||||
Object convert(MessagePackObject from, Object to) throws MessageTypeException;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -76,6 +76,16 @@ public class MessagePack {
|
|||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(byte[] buffer, Template tmpl, T to) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(tmpl, to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(byte[] buffer, Class<T> klass) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
|
|
@ -86,6 +96,16 @@ public class MessagePack {
|
|||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(byte[] buffer, T to) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static MessagePackObject unpack(InputStream in) throws IOException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
return pac.unpackObject();
|
||||
|
|
@ -100,6 +120,15 @@ public class MessagePack {
|
|||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(InputStream in, Template tmpl, T to) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(tmpl, to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(InputStream in, Class<T> klass) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
|
|
@ -107,7 +136,15 @@ public class MessagePack {
|
|||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(InputStream in, T to) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void register(Class<?> target) { // auto-detect
|
||||
|
|
|
|||
|
|
@ -140,12 +140,24 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable {
|
|||
abstract public Object clone();
|
||||
|
||||
public Object convert(Template tmpl) throws MessageTypeException {
|
||||
return tmpl.convert(this);
|
||||
return convert(tmpl, null);
|
||||
}
|
||||
|
||||
public Object convert(Template tmpl, Object to) throws MessageTypeException {
|
||||
return tmpl.convert(this, to);
|
||||
}
|
||||
|
||||
public <T> T convert(Class<T> klass) throws MessageTypeException {
|
||||
return convert(klass, null);
|
||||
}
|
||||
|
||||
public <T> T convert(T to) throws MessageTypeException {
|
||||
return convert((Class<T>)to.getClass(), to);
|
||||
}
|
||||
|
||||
public <T> T convert(Class<T> klass, Object to) throws MessageTypeException {
|
||||
// FIXME nullable?
|
||||
return (T)convert(new NullableTemplate(new ClassTemplate(klass)));
|
||||
return (T)convert(new NullableTemplate(new ClassTemplate(klass)), to);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,6 @@ package org.msgpack;
|
|||
import java.io.IOException;
|
||||
|
||||
public interface MessageUnpacker {
|
||||
Object unpack(Unpacker pac) throws IOException, MessageTypeException;
|
||||
Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -579,12 +579,24 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
|||
//}
|
||||
|
||||
final public Object unpack(Template tmpl) throws IOException, MessageTypeException {
|
||||
return tmpl.unpack(this);
|
||||
return unpack(tmpl, null);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Template tmpl, T to) throws IOException, MessageTypeException {
|
||||
return (T)tmpl.unpack(this, to);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass) throws IOException, MessageTypeException {
|
||||
return unpack(klass, null);
|
||||
}
|
||||
|
||||
final public <T> T unpack(T to) throws IOException, MessageTypeException {
|
||||
return unpack((Class<T>)to.getClass(), to);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass, T to) throws IOException, MessageTypeException {
|
||||
// FIXME nullable?
|
||||
return (T)unpack(new NullableTemplate(new ClassTemplate(klass)));
|
||||
return (T)unpack(new NullableTemplate(new ClassTemplate(klass)), to);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,11 +31,11 @@ public class AnyTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackObject();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,11 +28,11 @@ public class BigIntegerTemplate implements Template {
|
|||
pk.packBigInteger((BigInteger)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackBigInteger();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asBigInteger();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class BooleanTemplate implements Template {
|
|||
pk.packBoolean((Boolean)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackBoolean();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asBoolean();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class ByteArrayTemplate implements Template {
|
|||
pk.packByteArray((byte[])target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackByteArray();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asByteArray();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,12 +41,12 @@ public class ByteBufferTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
byte[] bytes = pac.unpackByteArray();
|
||||
return ByteBuffer.wrap(bytes);
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
byte[] bytes = from.asByteArray();
|
||||
return ByteBuffer.wrap(bytes);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class ByteTemplate implements Template {
|
|||
pk.packByte((Byte)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackByte();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asByte();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -149,16 +149,22 @@ public class ClassTemplate implements Template {
|
|||
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
@Override
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
try {
|
||||
MessageUnpacker unpacker = CustomUnpacker.get(klass);
|
||||
if(unpacker != null) {
|
||||
return unpacker.unpack(pac);
|
||||
return unpacker.unpack(pac, to);
|
||||
}
|
||||
|
||||
if(MessageUnpackable.class.isAssignableFrom(klass)) {
|
||||
Object obj = klass.newInstance();
|
||||
((MessageUnpackable)obj).messageUnpack(pac);
|
||||
MessageUnpackable obj;
|
||||
if(to == null) {
|
||||
obj = (MessageUnpackable)klass.newInstance();
|
||||
} else {
|
||||
obj = (MessageUnpackable)to;
|
||||
}
|
||||
obj.messageUnpack(pac);
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
|
@ -174,7 +180,7 @@ public class ClassTemplate implements Template {
|
|||
|
||||
if (unpacker != null) {
|
||||
CustomUnpacker.register(klass, unpacker);
|
||||
return unpacker.unpack(pac);
|
||||
return unpacker.unpack(pac, to);
|
||||
}
|
||||
|
||||
// fallback
|
||||
|
|
@ -193,7 +199,7 @@ public class ClassTemplate implements Template {
|
|||
|
||||
if (converter != null) {
|
||||
CustomConverter.register(klass, converter);
|
||||
return converter.convert(pac.unpackObject());
|
||||
return converter.convert(pac.unpackObject(), to);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -206,16 +212,22 @@ public class ClassTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
@Override
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
try {
|
||||
MessageConverter converter = CustomConverter.get(klass);
|
||||
if(converter != null) {
|
||||
return converter.convert(from);
|
||||
return converter.convert(from, to);
|
||||
}
|
||||
|
||||
if(MessageConvertable.class.isAssignableFrom(klass)) {
|
||||
Object obj = klass.newInstance();
|
||||
((MessageConvertable)obj).messageConvert(from);
|
||||
MessageConvertable obj;
|
||||
if(to == null) {
|
||||
obj = (MessageConvertable)klass.newInstance();
|
||||
} else {
|
||||
obj = (MessageConvertable)to;
|
||||
}
|
||||
obj.messageConvert(from);
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
|
@ -231,7 +243,7 @@ public class ClassTemplate implements Template {
|
|||
|
||||
if (converter != null) {
|
||||
CustomConverter.register(klass, converter);
|
||||
return converter.convert(from);
|
||||
return converter.convert(from, to);
|
||||
}
|
||||
|
||||
throw new MessageTypeException();
|
||||
|
|
|
|||
|
|
@ -41,20 +41,34 @@ public class CollectionTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
List<Object> list = new LinkedList<Object>();
|
||||
List<Object> list;
|
||||
if(to == null) {
|
||||
list = new LinkedList<Object>();
|
||||
} else {
|
||||
// TODO: optimize if list is instanceof ArrayList
|
||||
list = (List<Object>)to;
|
||||
list.clear();
|
||||
}
|
||||
for(; length > 0; length--) {
|
||||
list.add( elementTemplate.unpack(pac) );
|
||||
list.add( elementTemplate.unpack(pac, null) );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] array = from.asArray();
|
||||
List<Object> list = new LinkedList<Object>();
|
||||
List<Object> list;
|
||||
if(to == null) {
|
||||
list = new LinkedList<Object>();
|
||||
} else {
|
||||
// TODO: optimize if list is instanceof ArrayList
|
||||
list = (List<Object>)to;
|
||||
list.clear();
|
||||
}
|
||||
for(MessagePackObject element : array) {
|
||||
list.add( elementTemplate.convert(element) );
|
||||
list.add( elementTemplate.convert(element, null) );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class DoubleTemplate implements Template {
|
|||
pk.packDouble(((Double)target));
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackDouble();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asDouble();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class FloatTemplate implements Template {
|
|||
pk.packFloat((Float)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackFloat();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asFloat();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class IntegerTemplate implements Template {
|
|||
pk.packInt((Integer)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackInt();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asInt();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,20 +45,33 @@ public class ListTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
List<Object> list = new ArrayList<Object>(length);
|
||||
List<Object> list;
|
||||
if(to == null) {
|
||||
list = new ArrayList<Object>(length);
|
||||
} else {
|
||||
list = (List<Object>)to;
|
||||
list.clear();
|
||||
}
|
||||
for(; length > 0; length--) {
|
||||
list.add( elementTemplate.unpack(pac) );
|
||||
list.add( elementTemplate.unpack(pac, null) );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] array = from.asArray();
|
||||
List<Object> list = new ArrayList<Object>(array.length);
|
||||
List<Object> list;
|
||||
if(to == null) {
|
||||
list = new ArrayList<Object>(array.length);
|
||||
} else {
|
||||
// TODO: optimize if list is instanceof ArrayList
|
||||
list = (List<Object>)to;
|
||||
list.clear();
|
||||
}
|
||||
for(MessagePackObject element : array) {
|
||||
list.add( elementTemplate.convert(element) );
|
||||
list.add( elementTemplate.convert(element, null) );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class LongTemplate implements Template {
|
|||
pk.packLong((Long)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackLong();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asLong();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -52,24 +52,36 @@ public class MapTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackMap();
|
||||
Map<Object,Object> map = new HashMap<Object,Object>(length);
|
||||
Map<Object,Object> map;
|
||||
if(to == null) {
|
||||
map = new HashMap<Object,Object>(length);
|
||||
} else {
|
||||
map = (Map<Object,Object>)to;
|
||||
map.clear();
|
||||
}
|
||||
for(; length > 0; length--) {
|
||||
Object key = keyTemplate.unpack(pac);
|
||||
Object value = valueTemplate.unpack(pac);
|
||||
Object key = keyTemplate.unpack(pac, null);
|
||||
Object value = valueTemplate.unpack(pac, null);
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
Map<MessagePackObject,MessagePackObject> src = from.asMap();
|
||||
Map<Object,Object> map = new HashMap();
|
||||
Map<Object,Object> map;
|
||||
if(to == null) {
|
||||
map = new HashMap<Object,Object>(src.size());
|
||||
} else {
|
||||
map = (Map<Object,Object>)to;
|
||||
map.clear();
|
||||
}
|
||||
for(Map.Entry<MessagePackObject,MessagePackObject> pair : src.entrySet()) {
|
||||
Object key = keyTemplate.convert(pair.getKey());
|
||||
Object value = valueTemplate.convert(pair.getValue());
|
||||
Object key = keyTemplate.convert(pair.getKey(), null);
|
||||
Object value = valueTemplate.convert(pair.getValue(), null);
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
|
|
|
|||
|
|
@ -39,18 +39,18 @@ public class NullableTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
if(pac.tryUnpackNull()) {
|
||||
return null;
|
||||
}
|
||||
return elementTemplate.unpack(pac);
|
||||
return elementTemplate.unpack(pac, to);
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
if(from.isNil()) {
|
||||
return null;
|
||||
}
|
||||
return elementTemplate.convert(from);
|
||||
return elementTemplate.convert(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,18 +45,18 @@ public class OptionalTemplate implements Template {
|
|||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
if(pac.tryUnpackNull()) {
|
||||
return defaultObject;
|
||||
return defaultObject; // FIXME return to?
|
||||
}
|
||||
return elementTemplate.unpack(pac);
|
||||
return elementTemplate.unpack(pac, to);
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
if(from.isNil()) {
|
||||
return defaultObject;
|
||||
return defaultObject; // FIXME return to?
|
||||
}
|
||||
return elementTemplate.convert(from);
|
||||
return elementTemplate.convert(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class ShortTemplate implements Template {
|
|||
pk.packShort((Short)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackShort();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asShort();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ public class StringTemplate implements Template {
|
|||
pk.packString((String)target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackString();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asString();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -76,11 +76,12 @@ public interface Constants {
|
|||
|
||||
String STATEMENT_PACKER_PACKERMETHODBODY_04 = "$1.pack(((java.lang.Enum)_$$_t).ordinal()); ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_01 = "%s _$$_t = new %s(); ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_01 = "%s _$$_t; if($2 == null) { _$$_t = new %s(); } else { _$$_t = (%s)$2; } ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_02 = "int _$$_len = $1.unpackArray(); ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_03 = "_$$_t.%s = %s(%s)_$$_templates[%d].unpack($1)%s; ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_03_NULL = "_$$_t.%s = %s(%s)_$$_templates[%d].unpack($1, null)%s; ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_03 = "_$$_t.%s = %s(%s)_$$_templates[%d].unpack($1, _$$_t.%s)%s; ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_04 = "return _$$_t; ";
|
||||
|
||||
|
|
@ -96,7 +97,8 @@ public interface Constants {
|
|||
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_01 = "%s _$$_ary = $1.asArray(); ";
|
||||
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_02 = "_$$_t.%s = %s(%s)_$$_templates[%d].convert(_$$_ary[%d])%s; ";
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_02_NULL = "_$$_t.%s = %s(%s)_$$_templates[%d].convert(_$$_ary[%d], null)%s; ";
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_02 = "_$$_t.%s = %s(%s)_$$_templates[%d].convert(_$$_ary[%d], _$$_t.%s)%s; ";
|
||||
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_03 = "int i = _$$_ary[0].asInt(); ";
|
||||
|
||||
|
|
|
|||
|
|
@ -395,7 +395,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||
int mod = javassist.Modifier.PUBLIC;
|
||||
CtClass returnType = classToCtClass(Object.class);
|
||||
String mname = METHOD_NAME_UNPACK;
|
||||
CtClass[] paramTypes = new CtClass[] { classToCtClass(Unpacker.class) };
|
||||
CtClass[] paramTypes = new CtClass[] { classToCtClass(Unpacker.class), classToCtClass(Object.class) };
|
||||
CtClass[] exceptTypes = new CtClass[] {
|
||||
classToCtClass(IOException.class),
|
||||
classToCtClass(MessageTypeException.class) };
|
||||
|
|
@ -424,7 +424,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||
sb.append(CHAR_NAME_SPACE);
|
||||
// Foo _$$_t = new Foo();
|
||||
String typeName = classToString(type);
|
||||
Object[] args0 = new Object[] { typeName, typeName };
|
||||
Object[] args0 = new Object[] { typeName, typeName, typeName };
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_01, args0));
|
||||
// int _$$_L = $1.unpackArray();
|
||||
Object[] args1 = new Object[0];
|
||||
|
|
@ -449,15 +449,25 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||
// target.fi = ((Integer)_$$_tmpls[i].unpack(_$$_pk)).intValue();
|
||||
Class<?> returnType = field.getType();
|
||||
boolean isPrim = returnType.isPrimitive();
|
||||
Object[] args = new Object[] {
|
||||
String callExpr;
|
||||
if(isPrim) {
|
||||
Object[] args = new Object[] {
|
||||
field.getName(),
|
||||
isPrim ? "(" : "",
|
||||
isPrim ? getPrimToWrapperType(returnType).getName()
|
||||
: classToString(returnType),
|
||||
i,
|
||||
isPrim ? ")." + getPrimTypeValueMethodName(returnType) + "()"
|
||||
: "" };
|
||||
String callExpr = String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_03, args);
|
||||
"(",
|
||||
getPrimToWrapperType(returnType).getName(),
|
||||
i,
|
||||
")." + getPrimTypeValueMethodName(returnType) + "()" };
|
||||
callExpr = String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_03_NULL, args);
|
||||
} else {
|
||||
Object[] args = new Object[] {
|
||||
field.getName(),
|
||||
"",
|
||||
classToString(returnType),
|
||||
i,
|
||||
field.getName(),
|
||||
"" };
|
||||
callExpr = String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_03, args);
|
||||
}
|
||||
if (tmpl instanceof OptionalTemplate) {
|
||||
Object[] args0 = new Object[] { i, callExpr };
|
||||
// if (_$$_len > i && !unpacker.tryUnpackNull()) { ... }
|
||||
|
|
@ -507,7 +517,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||
int mod = javassist.Modifier.PUBLIC;
|
||||
CtClass returnType = classToCtClass(Object.class);
|
||||
String mname = METHOD_NAME_CONVERT;
|
||||
CtClass[] paramTypes = new CtClass[] { classToCtClass(MessagePackObject.class) };
|
||||
CtClass[] paramTypes = new CtClass[] { classToCtClass(MessagePackObject.class), classToCtClass(Object.class) };
|
||||
CtClass[] exceptTypes = new CtClass[] { classToCtClass(MessageTypeException.class) };
|
||||
CtMethod newCtMethod = CtNewMethod.make(mod, returnType, mname,
|
||||
paramTypes, exceptTypes, sb.toString(), tmplCtClass);
|
||||
|
|
@ -534,7 +544,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||
sb.append(CHAR_NAME_SPACE);
|
||||
// Foo _$$_t = new Foo();
|
||||
String typeName = classToString(type);
|
||||
Object[] args0 = new Object[] { typeName, typeName };
|
||||
Object[] args0 = new Object[] { typeName, typeName, typeName };
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_01, args0));
|
||||
// MessagePackObject[] _$$_ary = $1.asArray();
|
||||
Object[] args1 = new Object[] { classToString(MessagePackObject[].class) };
|
||||
|
|
@ -560,16 +570,27 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||
// target.fi = ((Object)_$$_tmpls[i].convert(_$$_ary[i])).intValue();
|
||||
Class<?> returnType = field.getType();
|
||||
boolean isPrim = returnType.isPrimitive();
|
||||
Object[] args = new Object[] {
|
||||
String callExpr;
|
||||
if(isPrim) {
|
||||
Object[] args = new Object[] {
|
||||
field.getName(),
|
||||
isPrim ? "(" : "",
|
||||
isPrim ? getPrimToWrapperType(returnType).getName()
|
||||
: classToString(returnType),
|
||||
i,
|
||||
i,
|
||||
isPrim ? ")." + getPrimTypeValueMethodName(returnType) + "()"
|
||||
: "" };
|
||||
String callExpr = String.format(STATEMENT_TMPL_CONVERTMETHODBODY_02, args);
|
||||
"(",
|
||||
getPrimToWrapperType(returnType).getName(),
|
||||
i,
|
||||
i,
|
||||
")." + getPrimTypeValueMethodName(returnType) + "()" };
|
||||
callExpr = String.format(STATEMENT_TMPL_CONVERTMETHODBODY_02_NULL, args);
|
||||
} else {
|
||||
Object[] args = new Object[] {
|
||||
field.getName(),
|
||||
"",
|
||||
classToString(returnType),
|
||||
i,
|
||||
i,
|
||||
field.getName(),
|
||||
"" };
|
||||
callExpr = String.format(STATEMENT_TMPL_CONVERTMETHODBODY_02, args);
|
||||
}
|
||||
if (tmpl instanceof OptionalTemplate) {
|
||||
Object[] args0 = new Object[] { i, i, callExpr };
|
||||
// if (_$$_len > i && !_$$_ary[i].isNull()) { ... }
|
||||
|
|
|
|||
|
|
@ -76,10 +76,15 @@ public class DynamicCodeGenBase implements Constants {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object unpack(Unpacker unpacker) throws IOException,
|
||||
public Object unpack(Unpacker unpacker, Object to) throws IOException,
|
||||
MessageTypeException {
|
||||
try {
|
||||
MessageUnpackable obj = (MessageUnpackable) type.newInstance();
|
||||
MessageUnpackable obj;
|
||||
if(to == null) {
|
||||
obj = (MessageUnpackable) type.newInstance();
|
||||
} else {
|
||||
obj = (MessageUnpackable) to;
|
||||
}
|
||||
obj.messageUnpack(unpacker);
|
||||
return obj;
|
||||
} catch (ClassCastException e) {
|
||||
|
|
@ -92,11 +97,15 @@ public class DynamicCodeGenBase implements Constants {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object convert(MessagePackObject from)
|
||||
public Object convert(MessagePackObject from, Object to)
|
||||
throws MessageTypeException {
|
||||
try {
|
||||
MessageConvertable obj = (MessageConvertable) type
|
||||
.newInstance();
|
||||
MessageConvertable obj;
|
||||
if(to == null) {
|
||||
obj = (MessageConvertable) type.newInstance();
|
||||
} else {
|
||||
obj = (MessageConvertable) to;
|
||||
}
|
||||
obj.messageConvert(from);
|
||||
return obj;
|
||||
} catch (ClassCastException e) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue