@@ -62,21 +62,31 @@ public class TemplateRegistry {
6262
6363 private Map <Type , GenericTemplate > genericCache ;
6464
65- public TemplateRegistry () {
66- this (null );
65+ /**
66+ * create <code>TemplateRegistry</code> object of root.
67+ */
68+ private TemplateRegistry () {
69+ parent = null ;
70+ chain = new TemplateBuilderChain ();
71+ chain .init (this );
72+ cache = new HashMap <Type , Template <Type >>(); // FIXME #MN cache must be immutable map
73+ genericCache = new HashMap <Type , GenericTemplate >();
74+ registerTemplates ();
6775 }
6876
77+ /**
78+ *
79+ * @param registry
80+ */
6981 public TemplateRegistry (TemplateRegistry registry ) {
70- parent = registry ;
71- cache = new HashMap <Type , Template <Type >>();
72- genericCache = new HashMap <Type , GenericTemplate >();
73- if (parent == null ) {
74- registerTemplates ();
75- chain = new TemplateBuilderChain ();
76- chain .init (this );
82+ if (registry != null ) {
83+ parent = registry ;
7784 } else {
78- chain = registry . chain ;
85+ parent = new TemplateRegistry () ;
7986 }
87+ chain = parent .chain ;
88+ cache = new HashMap <Type , Template <Type >>();
89+ genericCache = new HashMap <Type , GenericTemplate >();
8090 }
8191
8292 private void registerTemplates () {
@@ -163,45 +173,102 @@ public Template lookup(Type targetType) {
163173 private synchronized Template lookupImpl (Type targetType ) {
164174 Template tmpl ;
165175
176+ tmpl = lookupGenericImpl (targetType );
177+ if (tmpl != null ) {
178+ return tmpl ;
179+ }
180+
181+ tmpl = lookupCacheImpl (targetType );
182+ if (tmpl != null ) {
183+ return tmpl ;
184+ }
185+
186+ Class <?> targetClass = (Class <?>) targetType ;
187+
188+ if (MessagePackable .class .isAssignableFrom (targetClass )) {
189+ tmpl = new MessagePackableTemplate (targetClass );
190+ register (targetClass , tmpl );
191+ return tmpl ;
192+ }
193+
194+ // find matched template builder
195+ tmpl = lookupWithTemplateBuilderImpl (targetClass );
196+ if (tmpl != null ) {
197+ return tmpl ;
198+ }
199+
200+ // lookup template of interface type
201+ tmpl = lookupInterfacesImpl (targetClass );
202+ if (tmpl != null ) {
203+ return tmpl ;
204+ }
205+
206+ // lookup template of superclass type
207+ tmpl = lookupSuperclassesImpl (targetClass );
208+ if (tmpl != null ) {
209+ return tmpl ;
210+ }
211+
212+ throw new MessageTypeException (
213+ "Cannot find template for " + targetClass + " class. Try to add @Message annotation to the class or call MessagePack.register(Type)." );
214+ }
215+
216+ private Template lookupGenericImpl (Type targetType ) {
217+ Template tmpl = null ;
166218 if (targetType instanceof ParameterizedType ) {
167219 ParameterizedType pType = (ParameterizedType ) targetType ;
168220 // ParameterizedType is not a Class<?>?
169- tmpl = lookupGenericImpl (pType );
221+ tmpl = lookupGenericImpl0 (pType );
170222 if (tmpl != null ) {
171223 return tmpl ;
172224 }
173225 try {
174- tmpl = parent .lookupGenericImpl (pType );
226+ tmpl = parent .lookupGenericImpl0 (pType );
175227 if (tmpl != null ) {
176228 return tmpl ;
177229 }
178230 } catch (NullPointerException e ) { // ignore
179231 }
180232 targetType = pType .getRawType ();
181233 }
234+ return tmpl ;
235+ }
236+
237+ private Template lookupGenericImpl0 (final ParameterizedType targetType ) {
238+ Type rawType = targetType .getRawType ();
239+
240+ GenericTemplate tmpl = genericCache .get (rawType );
241+ if (tmpl == null ) {
242+ return null ;
243+ }
244+
245+ Type [] types = targetType .getActualTypeArguments ();
246+ Template [] tmpls = new Template [types .length ];
247+ for (int i = 0 ; i < types .length ; ++i ) {
248+ tmpls [i ] = lookup (types [i ]);
249+ }
250+
251+ return tmpl .build (tmpls );
252+ }
182253
183- tmpl = cache .get (targetType );
254+ private Template lookupCacheImpl (Type targetType ) {
255+ Template tmpl = cache .get (targetType );
184256 if (tmpl != null ) {
185257 return tmpl ;
186258 }
187259 try {
188- tmpl = parent .cache . get (targetType );
260+ tmpl = parent .lookupCacheImpl (targetType );
189261 if (tmpl != null ) {
190262 return tmpl ;
191263 }
192264 } catch (NullPointerException e ) { // ignore
193265 }
266+ return tmpl ;
267+ }
194268
195- Class <?> targetClass = (Class <?>) targetType ;
196-
197- if (MessagePackable .class .isAssignableFrom (targetClass )) {
198- tmpl = new MessagePackableTemplate (targetClass );
199- register (targetClass , tmpl );
200- return tmpl ;
201- }
202-
203- // find matched template builder
269+ private Template lookupWithTemplateBuilderImpl (Class <?> targetClass ) {
204270 TemplateBuilder builder = chain .select (targetClass , true );
271+ Template tmpl = null ;
205272 if (builder != null ) {
206273 tmpl = builder .loadTemplate (targetClass );
207274 if (tmpl != null ) {
@@ -214,17 +281,20 @@ private synchronized Template lookupImpl(Type targetType) {
214281 return tmpl ;
215282 }
216283 }
284+ return tmpl ;
285+ }
217286
218- // lookup template of interface type
287+ private Template lookupInterfacesImpl ( Class <?> targetClass ) {
219288 Class <?>[] infTypes = targetClass .getInterfaces ();
289+ Template tmpl = null ;
220290 for (Class <?> infType : infTypes ) {
221291 tmpl = cache .get (infType );
222292 if (tmpl != null ) {
223293 register (targetClass , tmpl );
224294 return tmpl ;
225295 } else {
226296 try {
227- tmpl = parent .cache . get (infType );
297+ tmpl = parent .lookupCacheImpl (infType );
228298 if (tmpl != null ) {
229299 register (targetClass , tmpl );
230300 return tmpl ;
@@ -233,9 +303,12 @@ private synchronized Template lookupImpl(Type targetType) {
233303 }
234304 }
235305 }
306+ return tmpl ;
307+ }
236308
237- // lookup template of superclass type
309+ private Template lookupSuperclassesImpl ( Class <?> targetClass ) {
238310 Class <?> superClass = targetClass .getSuperclass ();
311+ Template tmpl = null ;
239312 if (superClass != null ) {
240313 for (; superClass != Object .class ; superClass = superClass .getSuperclass ()) {
241314 tmpl = cache .get (superClass );
@@ -244,7 +317,7 @@ private synchronized Template lookupImpl(Type targetType) {
244317 return tmpl ;
245318 } else {
246319 try {
247- tmpl = parent .cache . get (superClass );
320+ tmpl = parent .lookupCacheImpl (superClass );
248321 if (tmpl != null ) {
249322 register (targetClass , tmpl );
250323 return tmpl ;
@@ -254,26 +327,7 @@ private synchronized Template lookupImpl(Type targetType) {
254327 }
255328 }
256329 }
257-
258- throw new MessageTypeException (
259- "Cannot find template for " + targetClass + " class. Try to add @Message annotation to the class or call MessagePack.register(Type)." );
260- }
261-
262- private Template lookupGenericImpl (final ParameterizedType targetType ) {
263- Type rawType = targetType .getRawType ();
264-
265- GenericTemplate tmpl = genericCache .get (rawType );
266- if (tmpl == null ) {
267- return null ;
268- }
269-
270- Type [] types = targetType .getActualTypeArguments ();
271- Template [] tmpls = new Template [types .length ];
272- for (int i = 0 ; i < types .length ; ++i ) {
273- tmpls [i ] = lookup (types [i ]);
274- }
275-
276- return tmpl .build (tmpls );
330+ return null ;
277331 }
278332
279333 private synchronized Template buildAndRegister (TemplateBuilder builder , final Class <?> targetClass ,
0 commit comments