java: registers buit-in templates to TemplateRegistry

This commit is contained in:
FURUHASHI Sadayuki 2010-12-01 20:32:22 +09:00
parent 469ac7891d
commit 9a059285d9
13 changed files with 54 additions and 26 deletions

View File

@ -24,10 +24,13 @@ public class AnyTemplate implements Template {
private AnyTemplate() { }
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();
} 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 {
CustomMessage.register(MessagePackObject.class, instance);
TemplateRegistry.register(MessagePackObject.class, instance);
}
}

View File

@ -44,6 +44,7 @@ public class BigIntegerTemplate implements Template {
static {
CustomMessage.register(BigInteger.class, instance);
TemplateRegistry.register(BigInteger.class, instance);
}
}

View File

@ -43,6 +43,8 @@ public class BooleanTemplate implements Template {
static {
CustomMessage.register(Boolean.class, instance);
TemplateRegistry.register(Boolean.class, instance);
TemplateRegistry.register(boolean.class, instance);
}
}

View File

@ -56,5 +56,9 @@ public class ByteBufferTemplate implements Template {
}
static final ByteBufferTemplate instance = new ByteBufferTemplate();
static {
TemplateRegistry.register(ByteBuffer.class, instance);
}
}

View File

@ -43,6 +43,8 @@ public class ByteTemplate implements Template {
static {
CustomMessage.register(Byte.class, instance);
TemplateRegistry.register(Byte.class, instance);
TemplateRegistry.register(byte.class, instance);
}
}

View File

@ -18,6 +18,8 @@
package org.msgpack.template;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.annotation.Annotation;
import org.msgpack.*;
import org.msgpack.annotation.MessagePackDelegate;
import org.msgpack.annotation.MessagePackMessage;
@ -30,10 +32,6 @@ import java.math.BigInteger;
import java.nio.ByteBuffer;
public class ClassTemplate implements Template {
static {
Templates.load();
}
private Class<?> klass;
public ClassTemplate(Class<?> klass) {
@ -119,14 +117,14 @@ public class ClassTemplate implements Template {
// pk.packDouble((Double)o);
// return;
//}
if(o instanceof BigInteger) {
pk.packBigInteger((BigInteger)o);
return;
}
if (o instanceof ByteBuffer) { // FIXME
Templates.tByteBuffer().pack(pk, o);
return;
}
//if(o instanceof BigInteger) {
// pk.packBigInteger((BigInteger)o);
// return;
//}
//if (o instanceof ByteBuffer) {
// Templates.tByteBuffer().pack(pk, o);
// return;
//}
MessagePacker packer = CustomPacker.get(klass);
if(packer != null) {
@ -134,15 +132,15 @@ public class ClassTemplate implements Template {
return;
}
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
if (isAnnotated(klass, MessagePackMessage.class)) {
Template tmpl = DynamicTemplate.create(klass);
CustomMessage.register(klass, tmpl);
tmpl.pack(pk, o);
return;
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
// FIXME DelegatePacker
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);
CustomMessage.register(klass, tmpl);
tmpl.pack(pk, o);
@ -171,14 +169,14 @@ public class ClassTemplate implements Template {
return obj;
}
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
if (isAnnotated(klass, MessagePackMessage.class)) {
Template tmpl = DynamicTemplate.create(klass);
CustomMessage.register(klass, tmpl);
return tmpl.unpack(pac, to);
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
// TODO DelegateUnpacker
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);
CustomMessage.register(klass, tmpl);
return tmpl.unpack(pac, to);
@ -187,14 +185,14 @@ public class ClassTemplate implements Template {
// fallback
MessageConverter converter = null;
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
if (isAnnotated(klass, MessagePackMessage.class)) {
Template tmpl = DynamicTemplate.create(klass);
CustomMessage.register(klass, tmpl);
return tmpl.convert(pac.unpackObject(), to);
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
// TODO DelegateConverter
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);
CustomMessage.register(klass, tmpl);
return tmpl.convert(pac.unpackObject(), to);
@ -228,14 +226,14 @@ public class ClassTemplate implements Template {
return obj;
}
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
if (isAnnotated(klass, MessagePackMessage.class)) {
Template tmpl = DynamicTemplate.create(klass);
CustomMessage.register(klass, tmpl);
return tmpl.convert(from, to);
} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) {
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
// TODO DelegateConverter
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);
CustomMessage.register(klass, tmpl);
return tmpl.convert(from, to);
@ -249,5 +247,9 @@ public class ClassTemplate implements Template {
throw new MessageTypeException(e.getMessage()); // FIXME
}
}
private boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
return ao.getAnnotation(with) != null;
}
}

View File

@ -43,6 +43,8 @@ public class DoubleTemplate implements Template {
static {
CustomMessage.register(Double.class, instance);
TemplateRegistry.register(Double.class, instance);
TemplateRegistry.register(double.class, instance);
}
}

View File

@ -43,6 +43,8 @@ public class FloatTemplate implements Template {
static {
CustomMessage.register(Float.class, instance);
TemplateRegistry.register(Float.class, instance);
TemplateRegistry.register(float.class, instance);
}
}

View File

@ -43,6 +43,8 @@ public class IntegerTemplate implements Template {
static {
CustomMessage.register(Integer.class, instance);
TemplateRegistry.register(Integer.class, instance);
TemplateRegistry.register(int.class, instance);
}
}

View File

@ -43,6 +43,8 @@ public class LongTemplate implements Template {
static {
CustomMessage.register(Long.class, instance);
TemplateRegistry.register(Long.class, instance);
TemplateRegistry.register(long.class, instance);
}
}

View File

@ -21,6 +21,8 @@ import java.io.IOException;
import org.msgpack.*;
public class NullableTemplate implements Template {
static void load() { }
private Template elementTemplate;
public NullableTemplate(Template elementTemplate) {

View File

@ -43,6 +43,8 @@ public class ShortTemplate implements Template {
static {
CustomMessage.register(Short.class, instance);
TemplateRegistry.register(Short.class, instance);
TemplateRegistry.register(short.class, instance);
}
}

View File

@ -43,6 +43,7 @@ public class StringTemplate implements Template {
static {
CustomMessage.register(String.class, instance);
TemplateRegistry.register(String.class, instance);
}
}