java: uses CustomMessage class on Packer, Unpacker and ClassTemplate

This commit is contained in:
frsyuki
2010-09-27 03:33:21 +09:00
parent 391034a785
commit 7161a235f1
7 changed files with 62 additions and 16 deletions

View File

@@ -20,8 +20,8 @@ package org.msgpack;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
// FIXME public? // FIXME package private?
class CustomConverter { public class CustomConverter {
public static void register(Class target, MessageConverter converter) { public static void register(Class target, MessageConverter converter) {
map.put(target, converter); map.put(target, converter);
} }

View File

@@ -20,8 +20,8 @@ package org.msgpack;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
// FIXME public? // FIXME package private?
class CustomPacker { public class CustomPacker {
public static void register(Class target, MessagePacker converter) { public static void register(Class target, MessagePacker converter) {
map.put(target, converter); map.put(target, converter);
} }

View File

@@ -20,8 +20,8 @@ package org.msgpack;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
// FIXME public? // FIXME package private?
class CustomUnpacker { public class CustomUnpacker {
public static void register(Class target, MessageUnpacker converter) { public static void register(Class target, MessageUnpacker converter) {
map.put(target, converter); map.put(target, converter);
} }

View File

@@ -473,11 +473,19 @@ public class Packer {
return packDouble((Double)o); return packDouble((Double)o);
} else if(o instanceof BigInteger) { } else if(o instanceof BigInteger) {
return packBigInteger((BigInteger)o); return packBigInteger((BigInteger)o);
} else {
// FIXME check CustomPacker.get(o.getClass());
// FIXME check annotations
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
} }
Class klass = o.getClass();
MessagePacker packer = CustomPacker.get(klass);
if(packer != null) {
packer.pack(this, o);
return this;
}
// FIXME check annotations -> code generation -> CustomMessage.registerPacker
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
} }
} }

View File

@@ -28,6 +28,10 @@ public class Templates {
return new MapTemplate(keyTemplate, valueTemplate); return new MapTemplate(keyTemplate, valueTemplate);
} }
public static Template tClass(Class target) {
return new ClassTemplate(target);
}
public static final Template TString = StringTemplate.getInstance(); public static final Template TString = StringTemplate.getInstance();

View File

@@ -573,10 +573,25 @@ public class Unpacker implements Iterable<MessagePackObject> {
obj.messageUnpack(this); obj.messageUnpack(this);
} }
final public Object unpack(Class klass) throws MessageTypeException { final public Object unpack(Class klass) throws IOException, MessageTypeException, InstantiationException, IllegalAccessException {
// FIXME check MessageUnpackable if(MessageUnpackable.class.isAssignableFrom(klass)) {
// FIXME check CustomPacker Object obj = klass.newInstance();
// FIXME check annotations ((MessageUnpackable)obj).messageUnpack(this);
return obj;
}
MessageUnpacker unpacker = CustomUnpacker.get(klass);
if(unpacker != null) {
return unpacker.unpack(this);
}
// FIXME check annotations -> code generation -> CustomMessage.registerTemplate
MessageConverter converter = CustomConverter.get(klass);
if(converter != null) {
return converter.convert(unpackObject());
}
throw new MessageTypeException(); throw new MessageTypeException();
} }
} }

View File

@@ -28,16 +28,35 @@ public class ClassTemplate implements Template {
} }
public Object unpack(Unpacker pac) throws IOException, MessageTypeException { public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
return pac.unpack(klass); try {
return pac.unpack(klass);
} catch (IllegalAccessException e) {
throw new MessageTypeException(e.getMessage()); // FIXME
} catch (InstantiationException e) {
throw new MessageTypeException(e.getMessage()); // FIXME
}
} }
public Object convert(MessagePackObject from) throws MessageTypeException { public Object convert(MessagePackObject from) throws MessageTypeException {
if(MessageConvertable.class.isAssignableFrom(klass)) {
Object obj;
try {
obj = klass.newInstance();
} catch (IllegalAccessException e) {
throw new MessageTypeException(e.getMessage()); // FIXME
} catch (InstantiationException e) {
throw new MessageTypeException(e.getMessage()); // FIXME
}
((MessageConvertable)obj).messageConvert(from);
return obj;
}
MessageConverter converter = CustomConverter.get(klass); MessageConverter converter = CustomConverter.get(klass);
if(converter != null) { if(converter != null) {
return converter.convert(from); return converter.convert(from);
} }
// FIXME check annotations // FIXME check annotations -> code generation -> CustomMessage.registerTemplate
throw new MessageTypeException(); throw new MessageTypeException();
} }