Skip to content

Commit d42d951

Browse files
committed
TemplateRegistry::lookup try to traverse ancestor interfaces
1 parent 35fd20a commit d42d951

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ private <T> Template<T> lookupSuperclasses(Class<T> targetClass) {
342342
if (tmpl != null) {
343343
register(targetClass, tmpl);
344344
return tmpl;
345+
} else {
346+
tmpl = (Template<T>) lookupInterfaceTypes(superClass);
347+
if (tmpl != null) {
348+
return tmpl;
349+
}
345350
}
346351
} catch (NullPointerException e) { // ignore
347352
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.msgpack.template;
2+
3+
import static org.hamcrest.CoreMatchers.*;
4+
import static org.junit.Assert.*;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import org.junit.Test;
11+
12+
public class TestTemplateRegistry {
13+
14+
private static final TemplateRegistry root = new TemplateRegistry(null);
15+
16+
static class MyArrayList<E> extends ArrayList<E> {
17+
private static final long serialVersionUID = 1L;
18+
}
19+
20+
@Test
21+
public void testTraverseInterface() throws Exception {
22+
Template<?> template = root.lookup(List.class);
23+
assertThat(template, is(instanceOf(ListTemplate.class)));
24+
}
25+
26+
@Test
27+
public void testTraverseDescentInterface() throws Exception {
28+
Template<?> template = root.lookup(MyArrayList.class);
29+
assertThat(template, is(instanceOf(ListTemplate.class)));
30+
}
31+
32+
@Test
33+
public void testArraysAsListIsPackable() throws Exception {
34+
Template<?> template = root.lookup(Arrays.asList().getClass());
35+
assertThat(template, is(instanceOf(ListTemplate.class)));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)