mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-22 16:40:22 +02:00
java: refactor org.msgpack.util.codegen.*.java
This commit is contained in:
parent
dbb28d9a8f
commit
ad5ebd007e
@ -25,6 +25,7 @@ public class CustomMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void registerTemplate(Class<?> target, Template tmpl) {
|
public static void registerTemplate(Class<?> target, Template tmpl) {
|
||||||
|
CustomPacker.register(target, tmpl);
|
||||||
CustomUnpacker.register(target, tmpl);
|
CustomUnpacker.register(target, tmpl);
|
||||||
CustomConverter.register(target, tmpl);
|
CustomConverter.register(target, tmpl);
|
||||||
}
|
}
|
||||||
|
@ -33,11 +33,13 @@ public class ListTemplate implements Template {
|
|||||||
return elementTemplate;
|
return elementTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
if(target instanceof List) {
|
if(!(target instanceof List)) {
|
||||||
throw new MessageTypeException();
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
List<Object> list = (List<Object>)target;
|
List<Object> list = (List<Object>)target;
|
||||||
|
pk.packArray(list.size());
|
||||||
for(Object element : list) {
|
for(Object element : list) {
|
||||||
elementTemplate.pack(pk, element);
|
elementTemplate.pack(pk, element);
|
||||||
}
|
}
|
||||||
|
@ -39,11 +39,13 @@ public class MapTemplate implements Template {
|
|||||||
return valueTemplate;
|
return valueTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
if(target instanceof Map) {
|
if(!(target instanceof Map)) {
|
||||||
throw new MessageTypeException();
|
throw new MessageTypeException();
|
||||||
}
|
}
|
||||||
Map<Object,Object> map = (Map<Object,Object>)target;
|
Map<Object,Object> map = (Map<Object,Object>)target;
|
||||||
|
pk.packMap(map.size());
|
||||||
for(Map.Entry<Object,Object> pair : map.entrySet()) {
|
for(Map.Entry<Object,Object> pair : map.entrySet()) {
|
||||||
keyTemplate.pack(pk, pair.getKey());
|
keyTemplate.pack(pk, pair.getKey());
|
||||||
valueTemplate.pack(pk, pair.getValue());
|
valueTemplate.pack(pk, pair.getValue());
|
||||||
|
@ -72,7 +72,7 @@ public interface Constants {
|
|||||||
|
|
||||||
String STATEMENT_PACKER_PACKERMETHODBODY_02 = "$1.packArray(%d); ";
|
String STATEMENT_PACKER_PACKERMETHODBODY_02 = "$1.packArray(%d); ";
|
||||||
|
|
||||||
String STATEMENT_PACKER_PACKERMETHODBODY_03 = "_$$_packers[%d].pack($1, %s_$$_t.%s%s); ";
|
String STATEMENT_PACKER_PACKERMETHODBODY_03 = "_$$_templates[%d].pack($1, %s_$$_t.%s%s); ";
|
||||||
|
|
||||||
String STATEMENT_PACKER_PACKERMETHODBODY_04 = "$1.pack(((java.lang.Enum)_$$_t).ordinal()); ";
|
String STATEMENT_PACKER_PACKERMETHODBODY_04 = "$1.pack(((java.lang.Enum)_$$_t).ordinal()); ";
|
||||||
|
|
||||||
|
@ -34,13 +34,11 @@ import javassist.CtNewMethod;
|
|||||||
import javassist.NotFoundException;
|
import javassist.NotFoundException;
|
||||||
|
|
||||||
import org.msgpack.MessagePackObject;
|
import org.msgpack.MessagePackObject;
|
||||||
import org.msgpack.MessagePacker;
|
|
||||||
import org.msgpack.MessageTypeException;
|
import org.msgpack.MessageTypeException;
|
||||||
import org.msgpack.Packer;
|
import org.msgpack.Packer;
|
||||||
import org.msgpack.Template;
|
import org.msgpack.Template;
|
||||||
import org.msgpack.Unpacker;
|
import org.msgpack.Unpacker;
|
||||||
import org.msgpack.annotation.MessagePackOptional;
|
import org.msgpack.annotation.MessagePackOptional;
|
||||||
import org.msgpack.packer.OptionalPacker;
|
|
||||||
import org.msgpack.template.OptionalTemplate;
|
import org.msgpack.template.OptionalTemplate;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -62,12 +60,9 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
|
|
||||||
private ConcurrentHashMap<String, Template[]> tmplCache;
|
private ConcurrentHashMap<String, Template[]> tmplCache;
|
||||||
|
|
||||||
private ConcurrentHashMap<String, MessagePacker[]> pkCache;
|
|
||||||
|
|
||||||
DynamicCodeGen() {
|
DynamicCodeGen() {
|
||||||
super();
|
super();
|
||||||
tmplCache = new ConcurrentHashMap<String, Template[]>();
|
tmplCache = new ConcurrentHashMap<String, Template[]>();
|
||||||
pkCache = new ConcurrentHashMap<String, MessagePacker[]>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTemplates(Class<?> type, Template[] tmpls) {
|
public void setTemplates(Class<?> type, Template[] tmpls) {
|
||||||
@ -78,73 +73,6 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
return tmplCache.get(type.getName());
|
return tmplCache.get(type.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMessagePackers(Class<?> type, MessagePacker[] pks) {
|
|
||||||
pkCache.put(type.getName(), pks);
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessagePacker[] getMessagePackers(Class<?> type) {
|
|
||||||
return pkCache.get(type.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class<?> generateMessagePackerClass(Class<?> origClass,
|
|
||||||
List<FieldOption> fieldOpts) {
|
|
||||||
try {
|
|
||||||
LOG.debug("start generating a packer class for "
|
|
||||||
+ origClass.getName());
|
|
||||||
String origName = origClass.getName();
|
|
||||||
String packerName = origName + POSTFIX_TYPE_NAME_PACKER + inc();
|
|
||||||
checkTypeValidation(origClass);
|
|
||||||
checkDefaultConstructorValidation(origClass);
|
|
||||||
CtClass packerCtClass = pool.makeClass(packerName);
|
|
||||||
setSuperclass(packerCtClass, MessagePackerAccessorImpl.class);
|
|
||||||
setInterface(packerCtClass, MessagePacker.class);
|
|
||||||
addClassTypeConstructor(packerCtClass);
|
|
||||||
Field[] fields = getDeclaredFields(origClass);
|
|
||||||
MessagePacker[] packers = null;
|
|
||||||
if (fieldOpts != null) {
|
|
||||||
fields = sortFields(fields, fieldOpts);
|
|
||||||
packers = createMessagePackers(fieldOpts);
|
|
||||||
} else {
|
|
||||||
packers = createMessagePackers(fields);
|
|
||||||
}
|
|
||||||
setMessagePackers(origClass, packers);
|
|
||||||
addPackMethod(packerCtClass, origClass, fields, false);
|
|
||||||
Class<?> packerClass = createClass(packerCtClass);
|
|
||||||
LOG.debug("generated a packer class for " + origClass.getName());
|
|
||||||
return packerClass;
|
|
||||||
} catch (NotFoundException e) {
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (CannotCompileException e) {
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class<?> generateOrdinalEnumPackerClass(Class<?> origClass) {
|
|
||||||
try {
|
|
||||||
LOG.debug("start generating an enum packer for "
|
|
||||||
+ origClass.getName());
|
|
||||||
String origName = origClass.getName();
|
|
||||||
String packerName = origName + POSTFIX_TYPE_NAME_PACKER + inc();
|
|
||||||
checkTypeValidation(origClass);
|
|
||||||
CtClass packerCtClass = pool.makeClass(packerName);
|
|
||||||
setSuperclass(packerCtClass, MessagePackerAccessorImpl.class);
|
|
||||||
setInterface(packerCtClass, MessagePacker.class);
|
|
||||||
addClassTypeConstructor(packerCtClass);
|
|
||||||
addPackMethod(packerCtClass, origClass, null, true);
|
|
||||||
Class<?> packerClass = createClass(packerCtClass);
|
|
||||||
LOG.debug("generated an enum class for " + origClass.getName());
|
|
||||||
return packerClass;
|
|
||||||
} catch (NotFoundException e) {
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (CannotCompileException e) {
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class<?> generateTemplateClass(Class<?> origClass,
|
public Class<?> generateTemplateClass(Class<?> origClass,
|
||||||
List<FieldOption> fieldOpts) {
|
List<FieldOption> fieldOpts) {
|
||||||
try {
|
try {
|
||||||
@ -167,19 +95,18 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
tmpls = createTemplates(fields);
|
tmpls = createTemplates(fields);
|
||||||
}
|
}
|
||||||
setTemplates(origClass, tmpls);
|
setTemplates(origClass, tmpls);
|
||||||
|
addPackMethod(tmplCtClass, origClass, fields, false);
|
||||||
addUnpackMethod(tmplCtClass, origClass, fields, false);
|
addUnpackMethod(tmplCtClass, origClass, fields, false);
|
||||||
addConvertMethod(tmplCtClass, origClass, fields, false);
|
addConvertMethod(tmplCtClass, origClass, fields, false);
|
||||||
Class<?> tmplClass = createClass(tmplCtClass);
|
Class<?> tmplClass = createClass(tmplCtClass);
|
||||||
LOG.debug("generated a template class for " + origClass.getName());
|
LOG.debug("generated a template class for " + origClass.getName());
|
||||||
return tmplClass;
|
return tmplClass;
|
||||||
} catch (NotFoundException e) {
|
} catch (NotFoundException e) {
|
||||||
DynamicCodeGenException ex = new DynamicCodeGenException(e
|
DynamicCodeGenException ex = new DynamicCodeGenException(e.getMessage(), e);
|
||||||
.getMessage(), e);
|
|
||||||
LOG.error(ex.getMessage(), ex);
|
LOG.error(ex.getMessage(), ex);
|
||||||
throw ex;
|
throw ex;
|
||||||
} catch (CannotCompileException e) {
|
} catch (CannotCompileException e) {
|
||||||
DynamicCodeGenException ex = new DynamicCodeGenException(e
|
DynamicCodeGenException ex = new DynamicCodeGenException(e.getMessage(), e);
|
||||||
.getMessage(), e);
|
|
||||||
LOG.error(ex.getMessage(), ex);
|
LOG.error(ex.getMessage(), ex);
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
@ -196,6 +123,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
setSuperclass(tmplCtClass, TemplateAccessorImpl.class);
|
setSuperclass(tmplCtClass, TemplateAccessorImpl.class);
|
||||||
setInterface(tmplCtClass, Template.class);
|
setInterface(tmplCtClass, Template.class);
|
||||||
addClassTypeConstructor(tmplCtClass);
|
addClassTypeConstructor(tmplCtClass);
|
||||||
|
addPackMethod(tmplCtClass, origClass, null, true);
|
||||||
addUnpackMethod(tmplCtClass, origClass, null, true);
|
addUnpackMethod(tmplCtClass, origClass, null, true);
|
||||||
addConvertMethod(tmplCtClass, origClass, null, true);
|
addConvertMethod(tmplCtClass, origClass, null, true);
|
||||||
Class<?> tmplClass = createClass(tmplCtClass);
|
Class<?> tmplClass = createClass(tmplCtClass);
|
||||||
@ -308,38 +236,6 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
return sorted;
|
return sorted;
|
||||||
}
|
}
|
||||||
|
|
||||||
MessagePacker[] createMessagePackers(List<FieldOption> fieldOpts) {
|
|
||||||
MessagePacker[] pks = new MessagePacker[fieldOpts.size()];
|
|
||||||
for (int i = 0; i < pks.length; ++i) {
|
|
||||||
pks[i] = toMessagePacker(fieldOpts.get(i).tmpl);
|
|
||||||
}
|
|
||||||
return pks;
|
|
||||||
}
|
|
||||||
|
|
||||||
MessagePacker[] createMessagePackers(Field[] fields) {
|
|
||||||
MessagePacker[] pks = new MessagePacker[fields.length];
|
|
||||||
for (int i = 0; i < pks.length; ++i) {
|
|
||||||
pks[i] = createMessagePacker(fields[i]);
|
|
||||||
}
|
|
||||||
return pks;
|
|
||||||
}
|
|
||||||
|
|
||||||
MessagePacker createMessagePacker(Field field) {
|
|
||||||
boolean isOptional = isAnnotated(field, MessagePackOptional.class);
|
|
||||||
Class<?> c = field.getType();
|
|
||||||
MessagePacker pk = null;
|
|
||||||
if (List.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c)) {
|
|
||||||
pk = createMessagePacker(field.getGenericType());
|
|
||||||
} else {
|
|
||||||
pk = createMessagePacker(c);
|
|
||||||
}
|
|
||||||
if (isOptional) {
|
|
||||||
return new OptionalPacker(pk);
|
|
||||||
} else {
|
|
||||||
return pk;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Template[] createTemplates(List<FieldOption> fieldOpts) {
|
Template[] createTemplates(List<FieldOption> fieldOpts) {
|
||||||
Template[] tmpls = new Template[fieldOpts.size()];
|
Template[] tmpls = new Template[fieldOpts.size()];
|
||||||
for (int i = 0; i < tmpls.length; ++i) {
|
for (int i = 0; i < tmpls.length; ++i) {
|
||||||
|
@ -24,7 +24,6 @@ import java.lang.reflect.Method;
|
|||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
@ -41,11 +40,9 @@ import javassist.NotFoundException;
|
|||||||
|
|
||||||
import org.msgpack.CustomConverter;
|
import org.msgpack.CustomConverter;
|
||||||
import org.msgpack.CustomMessage;
|
import org.msgpack.CustomMessage;
|
||||||
import org.msgpack.CustomPacker;
|
|
||||||
import org.msgpack.MessageConvertable;
|
import org.msgpack.MessageConvertable;
|
||||||
import org.msgpack.MessagePackObject;
|
import org.msgpack.MessagePackObject;
|
||||||
import org.msgpack.MessagePackable;
|
import org.msgpack.MessagePackable;
|
||||||
import org.msgpack.MessagePacker;
|
|
||||||
import org.msgpack.MessageTypeException;
|
import org.msgpack.MessageTypeException;
|
||||||
import org.msgpack.MessageUnpackable;
|
import org.msgpack.MessageUnpackable;
|
||||||
import org.msgpack.Packer;
|
import org.msgpack.Packer;
|
||||||
@ -55,32 +52,6 @@ import org.msgpack.Unpacker;
|
|||||||
import org.msgpack.annotation.MessagePackDelegate;
|
import org.msgpack.annotation.MessagePackDelegate;
|
||||||
import org.msgpack.annotation.MessagePackMessage;
|
import org.msgpack.annotation.MessagePackMessage;
|
||||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||||
import org.msgpack.packer.BigIntegerPacker;
|
|
||||||
import org.msgpack.packer.BooleanPacker;
|
|
||||||
import org.msgpack.packer.ByteArrayPacker;
|
|
||||||
import org.msgpack.packer.BytePacker;
|
|
||||||
import org.msgpack.packer.DoublePacker;
|
|
||||||
import org.msgpack.packer.FloatPacker;
|
|
||||||
import org.msgpack.packer.IntegerPacker;
|
|
||||||
import org.msgpack.packer.LongPacker;
|
|
||||||
import org.msgpack.packer.OptionalPacker;
|
|
||||||
import org.msgpack.packer.ShortPacker;
|
|
||||||
import org.msgpack.packer.StringPacker;
|
|
||||||
import org.msgpack.template.BigIntegerTemplate;
|
|
||||||
import org.msgpack.template.BooleanTemplate;
|
|
||||||
import org.msgpack.template.ByteArrayTemplate;
|
|
||||||
import org.msgpack.template.ByteTemplate;
|
|
||||||
import org.msgpack.template.ClassTemplate;
|
|
||||||
import org.msgpack.template.CollectionTemplate;
|
|
||||||
import org.msgpack.template.DoubleTemplate;
|
|
||||||
import org.msgpack.template.FloatTemplate;
|
|
||||||
import org.msgpack.template.IntegerTemplate;
|
|
||||||
import org.msgpack.template.ListTemplate;
|
|
||||||
import org.msgpack.template.LongTemplate;
|
|
||||||
import org.msgpack.template.MapTemplate;
|
|
||||||
import org.msgpack.template.OptionalTemplate;
|
|
||||||
import org.msgpack.template.ShortTemplate;
|
|
||||||
import org.msgpack.template.StringTemplate;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -89,11 +60,10 @@ public class DynamicCodeGenBase implements Constants {
|
|||||||
private static Logger LOG = LoggerFactory
|
private static Logger LOG = LoggerFactory
|
||||||
.getLogger(DynamicCodeGenBase.class);
|
.getLogger(DynamicCodeGenBase.class);
|
||||||
|
|
||||||
static class MessagePackablePacker implements MessagePacker {
|
static class MessagePackUnpackConvertableTemplate implements Template {
|
||||||
@SuppressWarnings("unused")
|
|
||||||
private Class<?> type;
|
private Class<?> type;
|
||||||
|
|
||||||
MessagePackablePacker(Class<?> type) {
|
MessagePackUnpackConvertableTemplate(Class<?> type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,61 +72,6 @@ public class DynamicCodeGenBase implements Constants {
|
|||||||
MessagePackable mp = MessagePackable.class.cast(target);
|
MessagePackable mp = MessagePackable.class.cast(target);
|
||||||
mp.messagePack(packer);
|
mp.messagePack(packer);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static class ListPacker implements MessagePacker {
|
|
||||||
private MessagePacker elementPacker;
|
|
||||||
|
|
||||||
ListPacker(MessagePacker elementPacker) {
|
|
||||||
this.elementPacker = elementPacker;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public void pack(Packer packer, Object target) throws IOException {
|
|
||||||
List<Object> list = (List<Object>) target;
|
|
||||||
packer.packArray(list.size());
|
|
||||||
for (Iterator<Object> iter = list.iterator(); iter.hasNext();) {
|
|
||||||
elementPacker.pack(packer, iter.next());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class MapPacker implements MessagePacker {
|
|
||||||
private MessagePacker keyPacker;
|
|
||||||
private MessagePacker valPacker;
|
|
||||||
|
|
||||||
MapPacker(MessagePacker keyPacker, MessagePacker valPacker) {
|
|
||||||
this.keyPacker = keyPacker;
|
|
||||||
this.valPacker = valPacker;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public void pack(Packer packer, Object target) throws IOException {
|
|
||||||
Map<Object, Object> map = (Map<Object, Object>) target;
|
|
||||||
packer.packMap(map.size());
|
|
||||||
for (Map.Entry<Object, Object> e : ((Map<Object, Object>) map)
|
|
||||||
.entrySet()) {
|
|
||||||
keyPacker.pack(packer, e.getKey());
|
|
||||||
valPacker.pack(packer, e.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static class MessageUnpackableConvertableTemplate implements Template {
|
|
||||||
private Class<?> type;
|
|
||||||
|
|
||||||
MessageUnpackableConvertableTemplate(Class<?> type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
|
||||||
// FIXME
|
|
||||||
pk.pack(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object unpack(Unpacker unpacker) throws IOException,
|
public Object unpack(Unpacker unpacker) throws IOException,
|
||||||
@ -192,27 +107,6 @@ public class DynamicCodeGenBase implements Constants {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static interface MessagePackerAccessor {
|
|
||||||
void setMessagePackers(MessagePacker[] packers);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class MessagePackerAccessorImpl implements MessagePackerAccessor {
|
|
||||||
public Class<?> type;
|
|
||||||
|
|
||||||
public MessagePacker[] _$$_packers;
|
|
||||||
|
|
||||||
public MessagePackerAccessorImpl() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessagePackerAccessorImpl(Class<?> type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMessagePackers(MessagePacker[] _$$_pks) {
|
|
||||||
_$$_packers = _$$_pks;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static interface TemplateAccessor {
|
public static interface TemplateAccessor {
|
||||||
void setTemplates(Template[] templates);
|
void setTemplates(Template[] templates);
|
||||||
}
|
}
|
||||||
@ -342,26 +236,6 @@ public class DynamicCodeGenBase implements Constants {
|
|||||||
newCtClass.addConstructor(newCtCons);
|
newCtClass.addConstructor(newCtCons);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addMessagePackerArrayField(CtClass newCtClass)
|
|
||||||
throws NotFoundException, CannotCompileException {
|
|
||||||
CtClass acsCtClass = pool
|
|
||||||
.get(MessagePackerAccessorImpl.class.getName());
|
|
||||||
CtField pksField = acsCtClass.getDeclaredField(VARIABLE_NAME_PACKERS);
|
|
||||||
CtField pksField2 = new CtField(pksField.getType(), pksField.getName(),
|
|
||||||
newCtClass);
|
|
||||||
newCtClass.addField(pksField2);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void addSetMessagePackersMethod(CtClass newCtClass)
|
|
||||||
throws NotFoundException, CannotCompileException {
|
|
||||||
CtClass acsCtClass = pool.get(TemplateAccessorImpl.class.getName());
|
|
||||||
CtMethod setPksMethod = acsCtClass
|
|
||||||
.getDeclaredMethod(METHOD_NAME_SETMESSAGEPACKERS);
|
|
||||||
CtMethod setPksMethod2 = CtNewMethod.copy(setPksMethod, newCtClass,
|
|
||||||
null);
|
|
||||||
newCtClass.addMethod(setPksMethod2);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void addTemplateArrayField(CtClass newCtClass)
|
protected void addTemplateArrayField(CtClass newCtClass)
|
||||||
throws NotFoundException, CannotCompileException {
|
throws NotFoundException, CannotCompileException {
|
||||||
CtClass acsCtClass = pool.get(TemplateAccessorImpl.class.getName());
|
CtClass acsCtClass = pool.get(TemplateAccessorImpl.class.getName());
|
||||||
@ -422,139 +296,6 @@ public class DynamicCodeGenBase implements Constants {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MessagePacker toMessagePacker(Template tmpl) {
|
|
||||||
if (tmpl instanceof BigIntegerTemplate) {
|
|
||||||
return BigIntegerPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof BooleanTemplate) {
|
|
||||||
return BooleanPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof ByteArrayTemplate) {
|
|
||||||
return ByteArrayPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof ByteTemplate) {
|
|
||||||
return BytePacker.getInstance();
|
|
||||||
} else if (tmpl instanceof ClassTemplate) {
|
|
||||||
UnsupportedOperationException e = new UnsupportedOperationException(
|
|
||||||
"not supported yet.");
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw e;
|
|
||||||
} else if (tmpl instanceof CollectionTemplate) {
|
|
||||||
UnsupportedOperationException e = new UnsupportedOperationException(
|
|
||||||
"not supported yet.");
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw e;
|
|
||||||
} else if (tmpl instanceof DoubleTemplate) {
|
|
||||||
return DoublePacker.getInstance();
|
|
||||||
} else if (tmpl instanceof FloatTemplate) {
|
|
||||||
return FloatPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof IntegerTemplate) {
|
|
||||||
return IntegerPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof ListTemplate) {
|
|
||||||
ListTemplate t = (ListTemplate) tmpl;
|
|
||||||
return new ListPacker(toMessagePacker(t.getElementTemplate()));
|
|
||||||
} else if (tmpl instanceof LongTemplate) {
|
|
||||||
return LongPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof MapTemplate) {
|
|
||||||
MapTemplate t = (MapTemplate) tmpl;
|
|
||||||
return new MapPacker(toMessagePacker(t.getKeyTemplate()),
|
|
||||||
toMessagePacker(t.getValueTemplate()));
|
|
||||||
} else if (tmpl instanceof OptionalTemplate) {
|
|
||||||
OptionalTemplate t = (OptionalTemplate) tmpl;
|
|
||||||
return new OptionalPacker(toMessagePacker(t.getElementTemplate()));
|
|
||||||
} else if (tmpl instanceof ShortTemplate) {
|
|
||||||
return ShortPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof StringTemplate) {
|
|
||||||
return StringPacker.getInstance();
|
|
||||||
} else if (tmpl instanceof TemplateAccessorImpl) {
|
|
||||||
Class<?> c = ((TemplateAccessorImpl) tmpl).type;
|
|
||||||
if (CustomPacker.isRegistered(c)) {
|
|
||||||
return CustomPacker.get(c);
|
|
||||||
} else {
|
|
||||||
MessagePacker packer = DynamicPacker.create(c);
|
|
||||||
CustomMessage.registerPacker(c, packer);
|
|
||||||
return packer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
UnsupportedOperationException e = new UnsupportedOperationException(
|
|
||||||
"not supported yet.");
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MessagePacker createMessagePacker(Type t) {
|
|
||||||
if (t.getClass().equals(Class.class)) {
|
|
||||||
Class<?> c = (Class<?>) t;
|
|
||||||
if (c.equals(boolean.class) || c.equals(Boolean.class)) {
|
|
||||||
return BooleanPacker.getInstance();
|
|
||||||
} else if (c.equals(byte.class) || c.equals(Byte.class)) {
|
|
||||||
return BytePacker.getInstance();
|
|
||||||
} else if (c.equals(short.class) || c.equals(Short.class)) {
|
|
||||||
return ShortPacker.getInstance();
|
|
||||||
} else if (c.equals(int.class) || c.equals(Integer.class)) {
|
|
||||||
return IntegerPacker.getInstance();
|
|
||||||
} else if (c.equals(float.class) || c.equals(Float.class)) {
|
|
||||||
return FloatPacker.getInstance();
|
|
||||||
} else if (c.equals(long.class) || c.equals(Long.class)) {
|
|
||||||
return LongPacker.getInstance();
|
|
||||||
} else if (c.equals(double.class) || c.equals(Double.class)) {
|
|
||||||
return DoublePacker.getInstance();
|
|
||||||
} else if (c.equals(String.class)) {
|
|
||||||
return StringPacker.getInstance();
|
|
||||||
} else if (c.equals(BigInteger.class)) {
|
|
||||||
return BigIntegerPacker.getInstance();
|
|
||||||
} else if (CustomPacker.isRegistered(c)) {
|
|
||||||
return CustomPacker.get(c);
|
|
||||||
} else if (CustomMessage.isAnnotated(c, MessagePackMessage.class)) {
|
|
||||||
// @MessagePackMessage
|
|
||||||
MessagePacker packer = DynamicPacker.create(c);
|
|
||||||
CustomMessage.registerPacker(c, packer);
|
|
||||||
return packer;
|
|
||||||
} else if (CustomMessage.isAnnotated(c, MessagePackDelegate.class)) {
|
|
||||||
// FIXME DelegatePacker
|
|
||||||
UnsupportedOperationException e = new UnsupportedOperationException(
|
|
||||||
"not supported yet. : " + c.getName());
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw e;
|
|
||||||
} else if (CustomMessage.isAnnotated(c,
|
|
||||||
MessagePackOrdinalEnum.class)) {
|
|
||||||
// @MessagePackOrdinalEnum
|
|
||||||
MessagePacker packer = DynamicOrdinalEnumPacker.create(c);
|
|
||||||
CustomMessage.registerPacker(c, packer);
|
|
||||||
return packer;
|
|
||||||
} else if (MessagePackable.class.isAssignableFrom(c)) {
|
|
||||||
MessagePacker packer = new MessagePackablePacker(c);
|
|
||||||
CustomMessage.registerPacker(c, packer);
|
|
||||||
return packer;
|
|
||||||
} else {
|
|
||||||
throw new MessageTypeException("Type error: "
|
|
||||||
+ ((Class<?>) t).getName());
|
|
||||||
}
|
|
||||||
} else if (t instanceof GenericArrayType) {
|
|
||||||
GenericArrayType gat = (GenericArrayType) t;
|
|
||||||
Type gct = gat.getGenericComponentType();
|
|
||||||
if (gct.equals(byte.class)) {
|
|
||||||
return ByteArrayPacker.getInstance();
|
|
||||||
} else {
|
|
||||||
throw new DynamicCodeGenException("Not supported yet: " + gat);
|
|
||||||
}
|
|
||||||
} else if (t instanceof ParameterizedType) {
|
|
||||||
ParameterizedType pt = (ParameterizedType) t;
|
|
||||||
Class<?> rawType = (Class<?>) pt.getRawType();
|
|
||||||
if (rawType.equals(List.class)) {
|
|
||||||
Type[] ats = pt.getActualTypeArguments();
|
|
||||||
return new ListPacker(createMessagePacker(ats[0]));
|
|
||||||
} else if (rawType.equals(Map.class)) {
|
|
||||||
Type[] ats = pt.getActualTypeArguments();
|
|
||||||
return new MapPacker(createMessagePacker(ats[0]),
|
|
||||||
createMessagePacker(ats[1]));
|
|
||||||
} else {
|
|
||||||
throw new DynamicCodeGenException("Type error: "
|
|
||||||
+ t.getClass().getName());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new DynamicCodeGenException("Type error: "
|
|
||||||
+ t.getClass().getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Template createTemplate(Type t) {
|
public static Template createTemplate(Type t) {
|
||||||
if (t.getClass().equals(Class.class)) {
|
if (t.getClass().equals(Class.class)) {
|
||||||
Class<?> c = (Class<?>) t;
|
Class<?> c = (Class<?>) t;
|
||||||
@ -576,7 +317,7 @@ public class DynamicCodeGenBase implements Constants {
|
|||||||
return Templates.tString();
|
return Templates.tString();
|
||||||
} else if (c.equals(BigInteger.class)) {
|
} else if (c.equals(BigInteger.class)) {
|
||||||
return Templates.tBigInteger();
|
return Templates.tBigInteger();
|
||||||
} else if (CustomConverter.isRegistered(c)) {
|
} else if (CustomConverter.isRegistered(c)) {// FIXME
|
||||||
return (Template) CustomConverter.get(c);
|
return (Template) CustomConverter.get(c);
|
||||||
} else if (CustomMessage.isAnnotated(c, MessagePackMessage.class)) {
|
} else if (CustomMessage.isAnnotated(c, MessagePackMessage.class)) {
|
||||||
// @MessagePackMessage
|
// @MessagePackMessage
|
||||||
@ -597,7 +338,7 @@ public class DynamicCodeGenBase implements Constants {
|
|||||||
return tmpl;
|
return tmpl;
|
||||||
} else if (MessageConvertable.class.isAssignableFrom(c)
|
} else if (MessageConvertable.class.isAssignableFrom(c)
|
||||||
|| MessageUnpackable.class.isAssignableFrom(c)) {
|
|| MessageUnpackable.class.isAssignableFrom(c)) {
|
||||||
Template tmpl = new MessageUnpackableConvertableTemplate(c);
|
Template tmpl = new MessagePackUnpackConvertableTemplate(c);
|
||||||
CustomMessage.registerTemplate(c, tmpl);
|
CustomMessage.registerTemplate(c, tmpl);
|
||||||
return tmpl;
|
return tmpl;
|
||||||
} else {
|
} else {
|
||||||
|
@ -17,35 +17,10 @@
|
|||||||
//
|
//
|
||||||
package org.msgpack.util.codegen;
|
package org.msgpack.util.codegen;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
|
|
||||||
import org.msgpack.MessagePacker;
|
import org.msgpack.MessagePacker;
|
||||||
import org.msgpack.util.codegen.DynamicCodeGenBase.MessagePackerAccessor;
|
|
||||||
|
|
||||||
public class DynamicOrdinalEnumPacker {
|
public class DynamicOrdinalEnumPacker {
|
||||||
public static MessagePacker create(Class<?> c) {
|
public static MessagePacker create(Class<?> c) {
|
||||||
try {
|
return DynamicOrdinalEnumTemplate.create(c);
|
||||||
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
|
||||||
Class<?> packerClass = gen.generateOrdinalEnumPackerClass(c);
|
|
||||||
Constructor<?> cons = packerClass
|
|
||||||
.getDeclaredConstructor(new Class[] { Class.class });
|
|
||||||
Object obj = cons.newInstance(new Object[] { c });
|
|
||||||
((MessagePackerAccessor) obj).setMessagePackers(gen
|
|
||||||
.getMessagePackers(c));
|
|
||||||
return (MessagePacker) obj;
|
|
||||||
} catch (InstantiationException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (SecurityException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,9 @@
|
|||||||
//
|
//
|
||||||
package org.msgpack.util.codegen;
|
package org.msgpack.util.codegen;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.msgpack.MessagePacker;
|
import org.msgpack.MessagePacker;
|
||||||
import org.msgpack.util.codegen.DynamicCodeGenBase.MessagePackerAccessor;
|
|
||||||
|
|
||||||
public class DynamicPacker {
|
public class DynamicPacker {
|
||||||
|
|
||||||
@ -31,27 +28,6 @@ public class DynamicPacker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static MessagePacker create(Class<?> c, List<FieldOption> fieldOpts) {
|
public static MessagePacker create(Class<?> c, List<FieldOption> fieldOpts) {
|
||||||
try {
|
return DynamicTemplate.create(c, fieldOpts);
|
||||||
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
|
||||||
Class<?> packerClass = gen.generateMessagePackerClass(c, fieldOpts);
|
|
||||||
Constructor<?> cons = packerClass
|
|
||||||
.getDeclaredConstructor(new Class[] { Class.class });
|
|
||||||
Object obj = cons.newInstance(new Object[] { c });
|
|
||||||
MessagePacker[] packers = gen.getMessagePackers(c);
|
|
||||||
((MessagePackerAccessor) obj).setMessagePackers(packers);
|
|
||||||
return (MessagePacker) obj;
|
|
||||||
} catch (InstantiationException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (SecurityException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import junit.framework.TestCase;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.msgpack.CustomConverter;
|
import org.msgpack.CustomConverter;
|
||||||
|
import org.msgpack.CustomMessage;
|
||||||
import org.msgpack.CustomPacker;
|
import org.msgpack.CustomPacker;
|
||||||
import org.msgpack.CustomUnpacker;
|
import org.msgpack.CustomUnpacker;
|
||||||
import org.msgpack.MessageConvertable;
|
import org.msgpack.MessageConvertable;
|
||||||
@ -1233,29 +1234,23 @@ public class TestPackConvert extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNestedFieldClass00() throws Exception {
|
public void testNestedFieldClass00() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
|
||||||
CustomPacker.register(NestedClass.class, packer2);
|
|
||||||
MessagePacker packer1 = DynamicPacker.create(BaseClass.class);
|
|
||||||
CustomPacker.register(BaseClass.class, packer1);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||||
CustomConverter.register(NestedClass.class, tmpl2);
|
Template tmpl = DynamicTemplate.create(BaseClass.class);
|
||||||
Template tmpl1 = DynamicTemplate.create(BaseClass.class);
|
CustomMessage.registerTemplate(BaseClass.class, tmpl);
|
||||||
CustomUnpacker.register(BaseClass.class, tmpl1);
|
|
||||||
CustomConverter.register(BaseClass.class, tmpl1);
|
|
||||||
BaseClass src = new BaseClass();
|
BaseClass src = new BaseClass();
|
||||||
NestedClass src2 = new NestedClass();
|
NestedClass src2 = new NestedClass();
|
||||||
src.f0 = 0;
|
src.f0 = 0;
|
||||||
src2.f2 = 2;
|
src2.f2 = 2;
|
||||||
src.f1 = src2;
|
src.f1 = src2;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer1.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
Unpacker pac = new Unpacker(in);
|
Unpacker pac = new Unpacker(in);
|
||||||
Iterator<MessagePackObject> it = pac.iterator();
|
Iterator<MessagePackObject> it = pac.iterator();
|
||||||
assertTrue(it.hasNext());
|
assertTrue(it.hasNext());
|
||||||
MessagePackObject mpo = it.next();
|
MessagePackObject mpo = it.next();
|
||||||
BaseClass dst = (BaseClass) tmpl1.convert(mpo);
|
BaseClass dst = (BaseClass) tmpl.convert(mpo);
|
||||||
assertTrue(src.f0 == dst.f0);
|
assertTrue(src.f0 == dst.f0);
|
||||||
assertTrue(src.f1.f2 == dst.f1.f2);
|
assertTrue(src.f1.f2 == dst.f1.f2);
|
||||||
assertFalse(it.hasNext());
|
assertFalse(it.hasNext());
|
||||||
@ -1263,21 +1258,13 @@ public class TestPackConvert extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNestedFieldClass02() throws Exception {
|
public void testNestedFieldClass02() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
|
||||||
CustomPacker.register(NestedClass.class, packer2);
|
|
||||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
|
||||||
.create(BaseClass.class));
|
|
||||||
CustomPacker.register(BaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||||
CustomConverter.register(NestedClass.class, tmpl2);
|
Template tmpl = new OptionalTemplate(DynamicTemplate.create(BaseClass.class));
|
||||||
Template tmpl = new OptionalTemplate(DynamicTemplate
|
CustomMessage.registerTemplate(BaseClass.class, tmpl);
|
||||||
.create(BaseClass.class));
|
|
||||||
CustomUnpacker.register(BaseClass.class, tmpl);
|
|
||||||
CustomConverter.register(BaseClass.class, tmpl);
|
|
||||||
BaseClass src = null;
|
BaseClass src = null;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
Unpacker pac = new Unpacker(in);
|
Unpacker pac = new Unpacker(in);
|
||||||
Iterator<MessagePackObject> it = pac.iterator();
|
Iterator<MessagePackObject> it = pac.iterator();
|
||||||
@ -1305,23 +1292,17 @@ public class TestPackConvert extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOptionalNestedFieldClass00() throws Exception {
|
public void testOptionalNestedFieldClass00() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(OptionalNestedClass.class);
|
|
||||||
CustomPacker.register(OptionalNestedClass.class, packer2);
|
|
||||||
MessagePacker packer = DynamicPacker.create(OptionalBaseClass.class);
|
|
||||||
CustomPacker.register(OptionalBaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||||
CustomUnpacker.register(OptionalNestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||||
CustomConverter.register(OptionalNestedClass.class, tmpl2);
|
|
||||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||||
CustomUnpacker.register(OptionalBaseClass.class, tmpl);
|
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||||
CustomConverter.register(OptionalBaseClass.class, tmpl);
|
|
||||||
OptionalBaseClass src = new OptionalBaseClass();
|
OptionalBaseClass src = new OptionalBaseClass();
|
||||||
OptionalNestedClass src2 = new OptionalNestedClass();
|
OptionalNestedClass src2 = new OptionalNestedClass();
|
||||||
src.f0 = 0;
|
src.f0 = 0;
|
||||||
src2.f2 = 2;
|
src2.f2 = 2;
|
||||||
src.f1 = src2;
|
src.f1 = src2;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
Unpacker pac = new Unpacker(in);
|
Unpacker pac = new Unpacker(in);
|
||||||
Iterator<MessagePackObject> it = pac.iterator();
|
Iterator<MessagePackObject> it = pac.iterator();
|
||||||
@ -1335,19 +1316,13 @@ public class TestPackConvert extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOptionalNestedFieldClass01() throws Exception {
|
public void testOptionalNestedFieldClass01() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(OptionalNestedClass.class);
|
|
||||||
CustomPacker.register(OptionalNestedClass.class, packer2);
|
|
||||||
MessagePacker packer = DynamicPacker.create(OptionalBaseClass.class);
|
|
||||||
CustomPacker.register(OptionalBaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||||
CustomUnpacker.register(OptionalNestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||||
CustomConverter.register(OptionalNestedClass.class, tmpl2);
|
|
||||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||||
CustomUnpacker.register(OptionalBaseClass.class, tmpl);
|
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||||
CustomConverter.register(OptionalBaseClass.class, tmpl);
|
|
||||||
OptionalBaseClass src = new OptionalBaseClass();
|
OptionalBaseClass src = new OptionalBaseClass();
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
Unpacker pac = new Unpacker(in);
|
Unpacker pac = new Unpacker(in);
|
||||||
Iterator<MessagePackObject> it = pac.iterator();
|
Iterator<MessagePackObject> it = pac.iterator();
|
||||||
|
@ -11,9 +11,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.msgpack.CustomConverter;
|
import org.msgpack.CustomMessage;
|
||||||
import org.msgpack.CustomPacker;
|
|
||||||
import org.msgpack.CustomUnpacker;
|
|
||||||
import org.msgpack.MessagePackable;
|
import org.msgpack.MessagePackable;
|
||||||
import org.msgpack.MessagePacker;
|
import org.msgpack.MessagePacker;
|
||||||
import org.msgpack.MessageTypeException;
|
import org.msgpack.MessageTypeException;
|
||||||
@ -1117,23 +1115,17 @@ public class TestPackUnpack extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNestedFieldClass00() throws Exception {
|
public void testNestedFieldClass00() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
|
||||||
CustomPacker.register(NestedClass.class, packer2);
|
|
||||||
MessagePacker packer = DynamicPacker.create(BaseClass.class);
|
|
||||||
CustomPacker.register(BaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||||
CustomConverter.register(NestedClass.class, tmpl2);
|
|
||||||
Template tmpl = DynamicTemplate.create(BaseClass.class);
|
Template tmpl = DynamicTemplate.create(BaseClass.class);
|
||||||
CustomUnpacker.register(BaseClass.class, tmpl);
|
CustomMessage.registerTemplate(BaseClass.class, tmpl);
|
||||||
CustomConverter.register(BaseClass.class, tmpl);
|
|
||||||
BaseClass src = new BaseClass();
|
BaseClass src = new BaseClass();
|
||||||
NestedClass src2 = new NestedClass();
|
NestedClass src2 = new NestedClass();
|
||||||
src.f0 = 0;
|
src.f0 = 0;
|
||||||
src2.f2 = 2;
|
src2.f2 = 2;
|
||||||
src.f1 = src2;
|
src.f1 = src2;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
BaseClass dst = (BaseClass) tmpl.unpack(new Unpacker(in));
|
BaseClass dst = (BaseClass) tmpl.unpack(new Unpacker(in));
|
||||||
assertTrue(src.f0 == dst.f0);
|
assertTrue(src.f0 == dst.f0);
|
||||||
@ -1142,21 +1134,12 @@ public class TestPackUnpack extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNestedFieldClass01() throws Exception {
|
public void testNestedFieldClass01() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
|
||||||
CustomPacker.register(NestedClass.class, packer2);
|
|
||||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
|
||||||
.create(BaseClass.class));
|
|
||||||
CustomPacker.register(BaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||||
CustomConverter.register(NestedClass.class, tmpl2);
|
Template tmpl = new OptionalTemplate(DynamicTemplate.create(BaseClass.class));
|
||||||
Template tmpl = new OptionalTemplate(DynamicTemplate
|
|
||||||
.create(BaseClass.class));
|
|
||||||
CustomUnpacker.register(BaseClass.class, tmpl);
|
|
||||||
CustomConverter.register(BaseClass.class, tmpl);
|
|
||||||
BaseClass src = null;
|
BaseClass src = null;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
BaseClass dst = (BaseClass) tmpl.unpack(new Unpacker(in));
|
BaseClass dst = (BaseClass) tmpl.unpack(new Unpacker(in));
|
||||||
assertEquals(src, dst);
|
assertEquals(src, dst);
|
||||||
@ -1179,70 +1162,47 @@ public class TestPackUnpack extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOptionalNestedFieldClass00() throws Exception {
|
public void testOptionalNestedFieldClass00() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(OptionalNestedClass.class);
|
|
||||||
CustomPacker.register(OptionalNestedClass.class, packer2);
|
|
||||||
MessagePacker packer = DynamicPacker.create(OptionalBaseClass.class);
|
|
||||||
CustomPacker.register(OptionalBaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||||
CustomUnpacker.register(OptionalNestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||||
CustomConverter.register(OptionalNestedClass.class, tmpl2);
|
|
||||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||||
CustomUnpacker.register(OptionalBaseClass.class, tmpl);
|
|
||||||
CustomConverter.register(OptionalBaseClass.class, tmpl);
|
|
||||||
OptionalBaseClass src = new OptionalBaseClass();
|
OptionalBaseClass src = new OptionalBaseClass();
|
||||||
OptionalNestedClass src2 = new OptionalNestedClass();
|
OptionalNestedClass src2 = new OptionalNestedClass();
|
||||||
src.f0 = 0;
|
src.f0 = 0;
|
||||||
src2.f2 = 2;
|
src2.f2 = 2;
|
||||||
src.f1 = src2;
|
src.f1 = src2;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
OptionalBaseClass dst = (OptionalBaseClass) tmpl
|
OptionalBaseClass dst = (OptionalBaseClass) tmpl.unpack(new Unpacker(in));
|
||||||
.unpack(new Unpacker(in));
|
|
||||||
assertTrue(src.f0 == dst.f0);
|
assertTrue(src.f0 == dst.f0);
|
||||||
assertTrue(src.f1.f2 == dst.f1.f2);
|
assertTrue(src.f1.f2 == dst.f1.f2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOptionalNestedFieldClass01() throws Exception {
|
public void testOptionalNestedFieldClass01() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(OptionalNestedClass.class);
|
|
||||||
CustomPacker.register(OptionalNestedClass.class, packer2);
|
|
||||||
MessagePacker packer = DynamicPacker.create(OptionalBaseClass.class);
|
|
||||||
CustomPacker.register(OptionalBaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||||
CustomUnpacker.register(OptionalNestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||||
CustomConverter.register(OptionalNestedClass.class, tmpl2);
|
|
||||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||||
CustomUnpacker.register(OptionalBaseClass.class, tmpl);
|
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||||
CustomConverter.register(OptionalBaseClass.class, tmpl);
|
|
||||||
OptionalBaseClass src = new OptionalBaseClass();
|
OptionalBaseClass src = new OptionalBaseClass();
|
||||||
src.f1 = null;
|
src.f1 = null;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
OptionalBaseClass dst = (OptionalBaseClass) tmpl
|
OptionalBaseClass dst = (OptionalBaseClass) tmpl.unpack(new Unpacker(in));
|
||||||
.unpack(new Unpacker(in));
|
|
||||||
assertTrue(src.f0 == dst.f0);
|
assertTrue(src.f0 == dst.f0);
|
||||||
assertTrue(src.f1 == dst.f1);
|
assertTrue(src.f1 == dst.f1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOptionalNestedFieldClass02() throws Exception {
|
public void testOptionalNestedFieldClass02() throws Exception {
|
||||||
MessagePacker packer2 = DynamicPacker.create(OptionalNestedClass.class);
|
|
||||||
CustomPacker.register(OptionalNestedClass.class, packer2);
|
|
||||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
|
||||||
.create(OptionalBaseClass.class));
|
|
||||||
CustomPacker.register(OptionalBaseClass.class, packer);
|
|
||||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||||
CustomUnpacker.register(OptionalNestedClass.class, tmpl2);
|
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||||
CustomConverter.register(OptionalNestedClass.class, tmpl2);
|
Template tmpl = new OptionalTemplate(DynamicTemplate.create(OptionalBaseClass.class));
|
||||||
Template tmpl = new OptionalTemplate(DynamicTemplate
|
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||||
.create(OptionalBaseClass.class));
|
|
||||||
CustomUnpacker.register(OptionalBaseClass.class, tmpl);
|
|
||||||
CustomConverter.register(OptionalBaseClass.class, tmpl);
|
|
||||||
OptionalBaseClass src = null;
|
OptionalBaseClass src = null;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
packer.pack(new Packer(out), src);
|
tmpl.pack(new Packer(out), src);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
OptionalBaseClass dst = (OptionalBaseClass) tmpl
|
OptionalBaseClass dst = (OptionalBaseClass) tmpl
|
||||||
.unpack(new Unpacker(in));
|
.unpack(new Unpacker(in));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user