java: uses TemplateRepostry as default

This commit is contained in:
FURUHASHI Sadayuki 2010-12-01 20:37:32 +09:00
parent a3cd13b399
commit 461b147897
6 changed files with 35 additions and 41 deletions

View File

@ -18,6 +18,7 @@
package org.msgpack; package org.msgpack;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import org.msgpack.template.TemplateRegistry;
public class CustomMessage { public class CustomMessage {
public static void registerPacker(Class<?> target, MessagePacker packer) { public static void registerPacker(Class<?> target, MessagePacker packer) {
@ -33,6 +34,7 @@ public class CustomMessage {
} }
public static void register(Class<?> target, Template tmpl) { public static void register(Class<?> target, Template tmpl) {
TemplateRegistry.register(target, tmpl);
CustomPacker.register(target, tmpl); CustomPacker.register(target, tmpl);
CustomConverter.register(target, tmpl); CustomConverter.register(target, tmpl);
CustomUnpacker.register(target, tmpl); CustomUnpacker.register(target, tmpl);

View File

@ -21,10 +21,11 @@ import java.io.OutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import org.msgpack.buffer.VectoredByteBuffer;
import org.msgpack.util.codegen.DynamicTemplate; import org.msgpack.util.codegen.DynamicTemplate;
import org.msgpack.util.codegen.DynamicOrdinalEnumTemplate; import org.msgpack.util.codegen.DynamicOrdinalEnumTemplate;
import org.msgpack.util.codegen.FieldList; import org.msgpack.util.codegen.FieldList;
import org.msgpack.template.TemplateRegistry;
import org.msgpack.template.TemplateBuilder;
public class MessagePack { public class MessagePack {
public static byte[] pack(Object obj) { public static byte[] pack(Object obj) {
@ -148,6 +149,8 @@ public class MessagePack {
} }
public static void register(Class<?> target) { // auto-detect public static void register(Class<?> target) { // auto-detect
TemplateRegistry.register(target);
Template tmpl; Template tmpl;
if(target.isEnum()) { if(target.isEnum()) {
tmpl = DynamicOrdinalEnumTemplate.create(target); tmpl = DynamicOrdinalEnumTemplate.create(target);
@ -166,6 +169,7 @@ public class MessagePack {
} }
public static void register(Class<?> target, FieldList opts) { public static void register(Class<?> target, FieldList opts) {
TemplateRegistry.register(target); // FIXME FieldList
Template tmpl = DynamicTemplate.create(target, opts); Template tmpl = DynamicTemplate.create(target, opts);
CustomPacker.register(target, tmpl); CustomPacker.register(target, tmpl);
CustomConverter.register(target, tmpl); CustomConverter.register(target, tmpl);
@ -173,6 +177,7 @@ public class MessagePack {
} }
public static void register(Class<?> target, Template tmpl) { public static void register(Class<?> target, Template tmpl) {
TemplateRegistry.register(target, tmpl);
CustomPacker.register(target, tmpl); CustomPacker.register(target, tmpl);
CustomConverter.register(target, tmpl); CustomConverter.register(target, tmpl);
CustomUnpacker.register(target, tmpl); CustomUnpacker.register(target, tmpl);

View File

@ -21,14 +21,9 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.Map; import java.util.Map;
import java.math.BigInteger; import java.math.BigInteger;
import org.msgpack.template.ClassTemplate; import org.msgpack.template.TemplateRegistry;
import org.msgpack.template.NullableTemplate;
public abstract class MessagePackObject implements Cloneable, MessagePackable { public abstract class MessagePackObject implements Cloneable, MessagePackable {
static {
Templates.load();
}
public boolean isNil() { public boolean isNil() {
return false; return false;
} }
@ -156,8 +151,10 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable {
} }
public <T> T convert(Class<T> klass, T to) throws MessageTypeException { public <T> T convert(Class<T> klass, T to) throws MessageTypeException {
// FIXME nullable? if(isNil()) {
return (T)convert(new NullableTemplate(new ClassTemplate(klass)), to); return null;
}
return (T)convert(TemplateRegistry.lookup(klass), to);
} }
} }

View File

@ -25,11 +25,7 @@ import java.util.Set;
import java.util.Map; import java.util.Map;
import java.util.Collection; import java.util.Collection;
import java.math.BigInteger; import java.math.BigInteger;
import org.msgpack.template.TemplateRegistry;
import org.msgpack.annotation.MessagePackDelegate;
import org.msgpack.annotation.MessagePackMessage;
import org.msgpack.annotation.MessagePackOrdinalEnum;
import org.msgpack.util.codegen.DynamicTemplate;
/** /**
* Packer enables you to serialize objects into OutputStream. * Packer enables you to serialize objects into OutputStream.
@ -48,10 +44,6 @@ import org.msgpack.util.codegen.DynamicTemplate;
* You can serialize objects that implements {@link MessagePackable} interface. * You can serialize objects that implements {@link MessagePackable} interface.
*/ */
public class Packer { public class Packer {
static {
Templates.load();
}
public static void load() { } public static void load() { }
protected byte[] castBytes = new byte[9]; protected byte[] castBytes = new byte[9];
@ -525,7 +517,8 @@ public class Packer {
} }
public Packer pack(Object o) throws IOException { public Packer pack(Object o) throws IOException {
Templates.TAny.pack(this, o); if(o == null) { return packNil(); }
TemplateRegistry.lookup(o.getClass()).pack(this, o);
return this; return this;
} }

View File

@ -20,8 +20,6 @@ package org.msgpack;
import org.msgpack.template.*; import org.msgpack.template.*;
public class Templates { public class Templates {
public static void load() { }
public static Template tNullable(Template elementTemplate) { public static Template tNullable(Template elementTemplate) {
return new NullableTemplate(elementTemplate); return new NullableTemplate(elementTemplate);
} }
@ -46,7 +44,11 @@ public class Templates {
} }
public static Template tClass(Class target) { public static Template tClass(Class target) {
return new ClassTemplate(target); Template tmpl = TemplateRegistry.lookup(target);
if(tmpl == null) {
// FIXME
}
return tmpl;
} }
public static final Template TByte = ByteTemplate.getInstance(); public static final Template TByte = ByteTemplate.getInstance();

View File

@ -23,8 +23,7 @@ import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.math.BigInteger; import java.math.BigInteger;
import org.msgpack.template.ClassTemplate; import org.msgpack.template.TemplateRegistry;
import org.msgpack.template.NullableTemplate;
/** /**
* Unpacker enables you to deserialize objects from stream. * Unpacker enables you to deserialize objects from stream.
@ -105,10 +104,6 @@ import org.msgpack.template.NullableTemplate;
* </pre> * </pre>
*/ */
public class Unpacker implements Iterable<MessagePackObject> { public class Unpacker implements Iterable<MessagePackObject> {
static {
Templates.load();
}
// buffer: // buffer:
// +---------------------------------------------+ // +---------------------------------------------+
// | [object] | [obje| unparsed ... | unused ...| // | [object] | [obje| unparsed ... | unused ...|
@ -578,6 +573,19 @@ public class Unpacker implements Iterable<MessagePackObject> {
// return unpackObject(); // return unpackObject();
//} //}
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) throws IOException, MessageTypeException {
return unpack(klass, null);
}
final public <T> T unpack(Class<T> klass, T to) throws IOException, MessageTypeException {
if(tryUnpackNull()) { return null; }
return (T)TemplateRegistry.lookup(klass).unpack(this, to);
}
final public Object unpack(Template tmpl) throws IOException, MessageTypeException { final public Object unpack(Template tmpl) throws IOException, MessageTypeException {
return unpack(tmpl, null); return unpack(tmpl, null);
} }
@ -585,18 +593,5 @@ public class Unpacker implements Iterable<MessagePackObject> {
final public <T> T unpack(Template tmpl, T to) throws IOException, MessageTypeException { final public <T> T unpack(Template tmpl, T to) throws IOException, MessageTypeException {
return (T)tmpl.unpack(this, to); 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)), to);
}
} }