Created
October 18, 2011 03:37
-
-
Save frsyuki/1294548 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/main/java/org/msgpack/MessagePack.java b/src/main/java/org/msgpack/MessagePack.java | |
index cb047b5..fd8427c 100644 | |
--- a/src/main/java/org/msgpack/MessagePack.java | |
+++ b/src/main/java/org/msgpack/MessagePack.java | |
@@ -186,17 +186,15 @@ public class MessagePack { | |
* @throws IOException | |
*/ | |
public <T> byte[] write(T v) throws IOException { | |
- BufferPacker pk = createBufferPacker(); | |
- if (v == null) { | |
- pk.writeNil(); | |
- } else if(v instanceof Value){ | |
- return write((Value)v); | |
- }else { | |
- @SuppressWarnings("unchecked") | |
- Template<T> tmpl = registry.lookup(v.getClass()); | |
- tmpl.write(pk, v); | |
- } | |
- return pk.toByteArray(); | |
+ BufferPacker pk = createBufferPacker(); | |
+ if (v == null) { | |
+ pk.writeNil(); | |
+ } else { | |
+ @SuppressWarnings("unchecked") | |
+ Template<T> tmpl = registry.lookup(v.getClass()); | |
+ tmpl.write(pk, v); | |
+ } | |
+ return pk.toByteArray(); | |
} | |
/** | |
diff --git a/src/main/java/org/msgpack/packer/AbstractPacker.java b/src/main/java/org/msgpack/packer/AbstractPacker.java | |
index 3858d3a..f8738a1 100644 | |
--- a/src/main/java/org/msgpack/packer/AbstractPacker.java | |
+++ b/src/main/java/org/msgpack/packer/AbstractPacker.java | |
@@ -199,8 +199,6 @@ public abstract class AbstractPacker implements Packer { | |
public Packer write(Object o) throws IOException { | |
if(o == null) { | |
writeNil(); | |
- } else if(o instanceof Value){ | |
- write((Value)o); | |
} else { | |
Template tmpl = msgpack.lookup(o.getClass()); | |
tmpl.write(this, o); | |
diff --git a/src/main/java/org/msgpack/template/AnyTemplate.java b/src/main/java/org/msgpack/template/AnyTemplate.java | |
index 533b873..2119d82 100644 | |
--- a/src/main/java/org/msgpack/template/AnyTemplate.java | |
+++ b/src/main/java/org/msgpack/template/AnyTemplate.java | |
@@ -44,9 +44,7 @@ public class AnyTemplate<T> extends AbstractTemplate<T> { | |
@SuppressWarnings("unchecked") | |
public void write(Packer pk, T target, boolean required) throws IOException { | |
- if(target instanceof Value) { | |
- pk.write((Value) target); | |
- } else if(target == null) { | |
+ if(target == null) { | |
if(required) { | |
throw new MessageTypeException("Attempted to write null"); | |
} | |
diff --git a/src/main/java/org/msgpack/template/TemplateRegistry.java b/src/main/java/org/msgpack/template/TemplateRegistry.java | |
index 022310c..957ef7c 100644 | |
--- a/src/main/java/org/msgpack/template/TemplateRegistry.java | |
+++ b/src/main/java/org/msgpack/template/TemplateRegistry.java | |
@@ -222,6 +222,12 @@ public class TemplateRegistry { | |
return tmpl; | |
} | |
+ // lookup template of interface type of superclasss | |
+ tmpl = lookupSuperclassInterfaceTypes(targetClass); | |
+ if (tmpl != null) { | |
+ return tmpl; | |
+ } | |
+ | |
throw new MessageTypeException( | |
"Cannot find template for " + targetClass + " class. Try to add @Message annotation to the class or call MessagePack.register(Type)."); | |
} | |
@@ -340,6 +346,30 @@ public class TemplateRegistry { | |
return tmpl; | |
} | |
+ private <T> Template<T> lookupSuperclassInterfaceTypes(Class<T> targetClass) { | |
+ Class<?> superClass = targetClass.getSuperclass(); | |
+ Template<T> tmpl = null; | |
+ if (superClass != null) { | |
+ for (; superClass != Object.class; superClass = superClass.getSuperclass()) { | |
+ tmpl = (Template<T>) lookupInterfaceTypes(superClass); | |
+ if (tmpl != null) { | |
+ register(targetClass, tmpl); | |
+ return tmpl; | |
+ } else { | |
+ try { | |
+ tmpl = (Template<T>) parent.lookupCache(superClass); | |
+ if (tmpl != null) { | |
+ register(targetClass, tmpl); | |
+ return tmpl; | |
+ } | |
+ } catch (NullPointerException e) { // ignore | |
+ } | |
+ } | |
+ } | |
+ } | |
+ return tmpl; | |
+ } | |
+ | |
private synchronized Template buildAndRegister(TemplateBuilder builder, final Class targetClass, | |
final boolean hasAnnotation, final FieldList flist) { | |
Template newTmpl = null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment