Skip to content

Commit 5e3b8d2

Browse files
committed
Fixed bug: Writing message with java.util.Set field causes StackOverflowError (MSGPACK-74)
1 parent 8338357 commit 5e3b8d2

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,25 @@ private Template<Type> lookupGenericType(ParameterizedType paramedType) {
285285
} catch (NullPointerException e) { // ignore
286286
}
287287

288+
tmpl = lookupGenericInterfaceTypes(paramedType);
289+
if (tmpl != null) {
290+
return tmpl;
291+
}
292+
293+
tmpl = lookupGenericSuperclasses(paramedType);
294+
if (tmpl != null) {
295+
return tmpl;
296+
}
297+
288298
return null;
289299
}
290300

291-
private Template lookupGenericTypeImpl(final ParameterizedType targetType) {
301+
private Template lookupGenericTypeImpl(ParameterizedType targetType) {
292302
Type rawType = targetType.getRawType();
303+
return lookupGenericTypeImpl0(targetType, rawType);
304+
}
293305

306+
private Template lookupGenericTypeImpl0(ParameterizedType targetType, Type rawType) {
294307
GenericTemplate gtmpl = genericCache.get(rawType);
295308
if (gtmpl == null) {
296309
return null;
@@ -305,6 +318,47 @@ private Template lookupGenericTypeImpl(final ParameterizedType targetType) {
305318
return gtmpl.build(tmpls);
306319
}
307320

321+
private <T> Template<T> lookupGenericInterfaceTypes(ParameterizedType targetType) {
322+
Type rawType = targetType.getRawType();
323+
Template<T> tmpl = null;
324+
325+
try {
326+
Class<?>[] infTypes = ((Class) rawType).getInterfaces();
327+
for (Class<?> infType : infTypes) {
328+
tmpl = lookupGenericTypeImpl0(targetType, infType);
329+
if (tmpl != null) {
330+
return tmpl;
331+
}
332+
}
333+
} catch (ClassCastException e) { // ignore
334+
}
335+
336+
return tmpl;
337+
}
338+
339+
private <T> Template<T> lookupGenericSuperclasses(ParameterizedType targetType) {
340+
Type rawType = targetType.getRawType();
341+
Template<T> tmpl = null;
342+
343+
try {
344+
Class<?> superClass = ((Class) rawType).getSuperclass();
345+
if (superClass == null) {
346+
return null;
347+
}
348+
349+
for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
350+
tmpl = lookupGenericTypeImpl0(targetType, superClass);
351+
if (tmpl != null) {
352+
register(targetType, tmpl);
353+
return tmpl;
354+
}
355+
}
356+
} catch (ClassCastException e) { // ignore
357+
}
358+
359+
return tmpl;
360+
}
361+
308362
private Template<Type> lookupGenericArrayType(Type targetType) {
309363
// TODO GenericArrayType is not a Class<?> => buildArrayTemplate
310364
if (! (targetType instanceof GenericArrayType)) {

0 commit comments

Comments
 (0)