2626import java .lang .reflect .GenericArrayType ;
2727import java .lang .reflect .ParameterizedType ;
2828import java .lang .reflect .Type ;
29+ import java .lang .reflect .WildcardType ;
2930import java .math .BigDecimal ;
3031import java .math .BigInteger ;
3132import java .nio .ByteBuffer ;
@@ -194,10 +195,14 @@ public synchronized void unregister() {
194195 public synchronized Template lookup (Type targetType ) {
195196 Template tmpl ;
196197
197- // TODO FIXME #MN should refactor this method call.
198- tmpl = lookupGenericType (targetType );
199- if (tmpl != null ) {
200- return tmpl ;
198+ if (targetType instanceof ParameterizedType ) {
199+ // ParameterizedType is not a Class<?>
200+ ParameterizedType paramedType = (ParameterizedType ) targetType ;
201+ tmpl = lookupGenericType (paramedType );
202+ if (tmpl != null ) {
203+ return tmpl ;
204+ }
205+ targetType = paramedType .getRawType ();
201206 }
202207
203208 tmpl = lookupGenericArrayType (targetType );
@@ -210,6 +215,13 @@ public synchronized Template lookup(Type targetType) {
210215 return tmpl ;
211216 }
212217
218+ if (targetType instanceof WildcardType ) {
219+ // WildcardType is not a Class<?>
220+ tmpl = new AnyTemplate <Object >(this );
221+ register (targetType , tmpl );
222+ return tmpl ;
223+ }
224+
213225 Class <?> targetClass = (Class <?>) targetType ;
214226
215227 // MessagePackable interface is implemented
@@ -222,6 +234,14 @@ public synchronized Template lookup(Type targetType) {
222234 return tmpl ;
223235 }
224236
237+ if (targetClass .isInterface ()) {
238+ // writing interfaces will succeed
239+ // reading into interfaces will fail
240+ tmpl = new AnyTemplate <Object >(this );
241+ register (targetType , tmpl );
242+ return tmpl ;
243+ }
244+
225245 // find matched template builder and build template
226246 tmpl = lookupAfterBuilding (targetClass );
227247 if (tmpl != null ) {
@@ -251,34 +271,28 @@ public synchronized Template lookup(Type targetType) {
251271 "Try to add @Message annotation to the class or call MessagePack.register(Type)." );
252272 }
253273
254- private Template <Type > lookupGenericType (Type targetType ) {
255- Template <Type > tmpl = null ;
256- if (targetType instanceof ParameterizedType ) {
257- ParameterizedType paramedType = (ParameterizedType ) targetType ;
274+ private Template <Type > lookupGenericType (ParameterizedType paramedType ) {
275+ Template <Type > tmpl = lookupGenericTypeImpl (paramedType );
276+ if (tmpl != null ) {
277+ return tmpl ;
278+ }
258279
259- // ParameterizedType is not a Class<?>?
260- tmpl = lookupGenericTypeImpl (paramedType );
280+ try {
281+ tmpl = parent . lookupGenericTypeImpl (paramedType );
261282 if (tmpl != null ) {
262283 return tmpl ;
263284 }
264-
265- try {
266- tmpl = parent .lookupGenericTypeImpl (paramedType );
267- if (tmpl != null ) {
268- return tmpl ;
269- }
270- } catch (NullPointerException e ) { // ignore
271- }
272- targetType = paramedType .getRawType ();
285+ } catch (NullPointerException e ) { // ignore
273286 }
274- return tmpl ;
287+
288+ return null ;
275289 }
276290
277291 private Template lookupGenericTypeImpl (final ParameterizedType targetType ) {
278292 Type rawType = targetType .getRawType ();
279293
280- GenericTemplate tmpl = genericCache .get (rawType );
281- if (tmpl == null ) {
294+ GenericTemplate gtmpl = genericCache .get (rawType );
295+ if (gtmpl == null ) {
282296 return null ;
283297 }
284298
@@ -288,7 +302,7 @@ private Template lookupGenericTypeImpl(final ParameterizedType targetType) {
288302 tmpls [i ] = lookup (types [i ]);
289303 }
290304
291- return tmpl .build (tmpls );
305+ return gtmpl .build (tmpls );
292306 }
293307
294308 private Template <Type > lookupGenericArrayType (Type targetType ) {
0 commit comments