Skip to content

Commit 4766178

Browse files
committed
refactored TemplateRegistry class
1 parent 843baad commit 4766178

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/main/java/org/msgpack/MessagePack.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ public <T> byte[] write(T v) throws IOException {
189189
BufferPacker pk = createBufferPacker();
190190
if (v == null) {
191191
pk.writeNil();
192-
} else if(v instanceof Value){
193-
return write((Value)v);
194-
}else {
192+
}else {
195193
@SuppressWarnings("unchecked")
196194
Template<T> tmpl = registry.lookup(v.getClass());
197195
tmpl.write(pk, v);

src/main/java/org/msgpack/packer/AbstractPacker.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ public Packer write(String o) throws IOException {
199199
public Packer write(Object o) throws IOException {
200200
if(o == null) {
201201
writeNil();
202-
} else if(o instanceof Value){
203-
write((Value)o);
204202
} else {
205203
Template tmpl = msgpack.lookup(o.getClass());
206204
tmpl.write(this, o);

src/main/java/org/msgpack/template/AnyTemplate.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public AnyTemplate(TemplateRegistry registry) {
3434

3535
@SuppressWarnings("unchecked")
3636
public void write(Packer pk, T target, boolean required) throws IOException {
37-
if (target instanceof Value) {
38-
pk.write((Value) target);
39-
} else if (target == null) {
37+
if (target == null) {
4038
if(required) {
4139
throw new MessageTypeException("Attempted to write null");
4240
}

src/main/java/org/msgpack/template/TemplateRegistry.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ public synchronized Template lookup(Type targetType) {
233233
return tmpl;
234234
}
235235

236+
// lookup template of interface type of superclasss
237+
tmpl = lookupSuperclassInterfaceTypes(targetClass);
238+
if (tmpl != null) {
239+
return tmpl;
240+
}
241+
236242
throw new MessageTypeException(
237243
"Cannot find template for " + targetClass + " class. Try to add @Message annotation to the class or call MessagePack.register(Type).");
238244
}
@@ -342,11 +348,6 @@ private <T> Template<T> lookupSuperclasses(Class<T> targetClass) {
342348
if (tmpl != null) {
343349
register(targetClass, tmpl);
344350
return tmpl;
345-
} else {
346-
tmpl = (Template<T>) lookupInterfaceTypes(superClass);
347-
if (tmpl != null) {
348-
return tmpl;
349-
}
350351
}
351352
} catch (NullPointerException e) { // ignore
352353
}
@@ -356,6 +357,30 @@ private <T> Template<T> lookupSuperclasses(Class<T> targetClass) {
356357
return tmpl;
357358
}
358359

360+
private <T> Template<T> lookupSuperclassInterfaceTypes(Class<T> targetClass) {
361+
Class<?> superClass = targetClass.getSuperclass();
362+
Template<T> tmpl = null;
363+
if (superClass != null) {
364+
for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
365+
tmpl = (Template<T>) lookupInterfaceTypes(superClass);
366+
if (tmpl != null) {
367+
register(targetClass, tmpl);
368+
return tmpl;
369+
} else {
370+
try {
371+
tmpl = (Template<T>) parent.lookupCache(superClass);
372+
if (tmpl != null) {
373+
register(targetClass, tmpl);
374+
return tmpl;
375+
}
376+
} catch (NullPointerException e) { // ignore
377+
}
378+
}
379+
}
380+
}
381+
return tmpl;
382+
}
383+
359384
private synchronized Template buildAndRegister(TemplateBuilder builder, final Class targetClass,
360385
final boolean hasAnnotation, final FieldList flist) {
361386
Template newTmpl = null;

0 commit comments

Comments
 (0)