mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-28 09:14:12 +01:00
java: registers buit-in templates to TemplateRegistry
This commit is contained in:
parent
469ac7891d
commit
9a059285d9
@ -24,10 +24,13 @@ public class AnyTemplate implements Template {
|
|||||||
private AnyTemplate() { }
|
private AnyTemplate() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
if(target == null) {
|
if(target instanceof MessagePackObject) {
|
||||||
|
pk.pack((MessagePackObject)target);
|
||||||
|
} else if(target == null) {
|
||||||
pk.packNil();
|
pk.packNil();
|
||||||
} else {
|
} else {
|
||||||
new ClassTemplate(target.getClass()).pack(pk, target);
|
TemplateRegistry.lookup(target.getClass()).pack(pk, target);
|
||||||
|
//new ClassTemplate(target.getClass()).pack(pk, target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +50,7 @@ public class AnyTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(MessagePackObject.class, instance);
|
CustomMessage.register(MessagePackObject.class, instance);
|
||||||
|
TemplateRegistry.register(MessagePackObject.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@ public class BigIntegerTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(BigInteger.class, instance);
|
CustomMessage.register(BigInteger.class, instance);
|
||||||
|
TemplateRegistry.register(BigInteger.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ public class BooleanTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(Boolean.class, instance);
|
CustomMessage.register(Boolean.class, instance);
|
||||||
|
TemplateRegistry.register(Boolean.class, instance);
|
||||||
|
TemplateRegistry.register(boolean.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,5 +56,9 @@ public class ByteBufferTemplate implements Template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static final ByteBufferTemplate instance = new ByteBufferTemplate();
|
static final ByteBufferTemplate instance = new ByteBufferTemplate();
|
||||||
|
|
||||||
|
static {
|
||||||
|
TemplateRegistry.register(ByteBuffer.class, instance);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ public class ByteTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(Byte.class, instance);
|
CustomMessage.register(Byte.class, instance);
|
||||||
|
TemplateRegistry.register(Byte.class, instance);
|
||||||
|
TemplateRegistry.register(byte.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
package org.msgpack.template;
|
package org.msgpack.template;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.AccessibleObject;
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
import org.msgpack.annotation.MessagePackDelegate;
|
import org.msgpack.annotation.MessagePackDelegate;
|
||||||
import org.msgpack.annotation.MessagePackMessage;
|
import org.msgpack.annotation.MessagePackMessage;
|
||||||
@ -30,10 +32,6 @@ import java.math.BigInteger;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class ClassTemplate implements Template {
|
public class ClassTemplate implements Template {
|
||||||
static {
|
|
||||||
Templates.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Class<?> klass;
|
private Class<?> klass;
|
||||||
|
|
||||||
public ClassTemplate(Class<?> klass) {
|
public ClassTemplate(Class<?> klass) {
|
||||||
@ -119,14 +117,14 @@ public class ClassTemplate implements Template {
|
|||||||
// pk.packDouble((Double)o);
|
// pk.packDouble((Double)o);
|
||||||
// return;
|
// return;
|
||||||
//}
|
//}
|
||||||
if(o instanceof BigInteger) {
|
//if(o instanceof BigInteger) {
|
||||||
pk.packBigInteger((BigInteger)o);
|
// pk.packBigInteger((BigInteger)o);
|
||||||
return;
|
// return;
|
||||||
}
|
//}
|
||||||
if (o instanceof ByteBuffer) { // FIXME
|
//if (o instanceof ByteBuffer) {
|
||||||
Templates.tByteBuffer().pack(pk, o);
|
// Templates.tByteBuffer().pack(pk, o);
|
||||||
return;
|
// return;
|
||||||
}
|
//}
|
||||||
|
|
||||||
MessagePacker packer = CustomPacker.get(klass);
|
MessagePacker packer = CustomPacker.get(klass);
|
||||||
if(packer != null) {
|
if(packer != null) {
|
||||||
@ -134,15 +132,15 @@ public class ClassTemplate implements Template {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
|
if (isAnnotated(klass, MessagePackMessage.class)) {
|
||||||
Template tmpl = DynamicTemplate.create(klass);
|
Template tmpl = DynamicTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
tmpl.pack(pk, o);
|
tmpl.pack(pk, o);
|
||||||
return;
|
return;
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
|
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
|
||||||
// FIXME DelegatePacker
|
// FIXME DelegatePacker
|
||||||
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
} else if (isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
||||||
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
tmpl.pack(pk, o);
|
tmpl.pack(pk, o);
|
||||||
@ -171,14 +169,14 @@ public class ClassTemplate implements Template {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
|
if (isAnnotated(klass, MessagePackMessage.class)) {
|
||||||
Template tmpl = DynamicTemplate.create(klass);
|
Template tmpl = DynamicTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
return tmpl.unpack(pac, to);
|
return tmpl.unpack(pac, to);
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
|
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
|
||||||
// TODO DelegateUnpacker
|
// TODO DelegateUnpacker
|
||||||
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
} else if (isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
||||||
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
return tmpl.unpack(pac, to);
|
return tmpl.unpack(pac, to);
|
||||||
@ -187,14 +185,14 @@ public class ClassTemplate implements Template {
|
|||||||
// fallback
|
// fallback
|
||||||
MessageConverter converter = null;
|
MessageConverter converter = null;
|
||||||
|
|
||||||
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
|
if (isAnnotated(klass, MessagePackMessage.class)) {
|
||||||
Template tmpl = DynamicTemplate.create(klass);
|
Template tmpl = DynamicTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
return tmpl.convert(pac.unpackObject(), to);
|
return tmpl.convert(pac.unpackObject(), to);
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
|
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
|
||||||
// TODO DelegateConverter
|
// TODO DelegateConverter
|
||||||
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
} else if (isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
||||||
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
return tmpl.convert(pac.unpackObject(), to);
|
return tmpl.convert(pac.unpackObject(), to);
|
||||||
@ -228,14 +226,14 @@ public class ClassTemplate implements Template {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
|
if (isAnnotated(klass, MessagePackMessage.class)) {
|
||||||
Template tmpl = DynamicTemplate.create(klass);
|
Template tmpl = DynamicTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
return tmpl.convert(from, to);
|
return tmpl.convert(from, to);
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
|
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
|
||||||
// TODO DelegateConverter
|
// TODO DelegateConverter
|
||||||
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
||||||
} else if (CustomMessage.isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
} else if (isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
||||||
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
Template tmpl = DynamicOrdinalEnumTemplate.create(klass);
|
||||||
CustomMessage.register(klass, tmpl);
|
CustomMessage.register(klass, tmpl);
|
||||||
return tmpl.convert(from, to);
|
return tmpl.convert(from, to);
|
||||||
@ -249,5 +247,9 @@ public class ClassTemplate implements Template {
|
|||||||
throw new MessageTypeException(e.getMessage()); // FIXME
|
throw new MessageTypeException(e.getMessage()); // FIXME
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
|
||||||
|
return ao.getAnnotation(with) != null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ public class DoubleTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(Double.class, instance);
|
CustomMessage.register(Double.class, instance);
|
||||||
|
TemplateRegistry.register(Double.class, instance);
|
||||||
|
TemplateRegistry.register(double.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ public class FloatTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(Float.class, instance);
|
CustomMessage.register(Float.class, instance);
|
||||||
|
TemplateRegistry.register(Float.class, instance);
|
||||||
|
TemplateRegistry.register(float.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ public class IntegerTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(Integer.class, instance);
|
CustomMessage.register(Integer.class, instance);
|
||||||
|
TemplateRegistry.register(Integer.class, instance);
|
||||||
|
TemplateRegistry.register(int.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ public class LongTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(Long.class, instance);
|
CustomMessage.register(Long.class, instance);
|
||||||
|
TemplateRegistry.register(Long.class, instance);
|
||||||
|
TemplateRegistry.register(long.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,8 @@ import java.io.IOException;
|
|||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class NullableTemplate implements Template {
|
public class NullableTemplate implements Template {
|
||||||
|
static void load() { }
|
||||||
|
|
||||||
private Template elementTemplate;
|
private Template elementTemplate;
|
||||||
|
|
||||||
public NullableTemplate(Template elementTemplate) {
|
public NullableTemplate(Template elementTemplate) {
|
||||||
|
@ -43,6 +43,8 @@ public class ShortTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(Short.class, instance);
|
CustomMessage.register(Short.class, instance);
|
||||||
|
TemplateRegistry.register(Short.class, instance);
|
||||||
|
TemplateRegistry.register(short.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ public class StringTemplate implements Template {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
CustomMessage.register(String.class, instance);
|
CustomMessage.register(String.class, instance);
|
||||||
|
TemplateRegistry.register(String.class, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user