forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateRegistry.java
More file actions
417 lines (367 loc) · 13 KB
/
TemplateRegistry.java
File metadata and controls
417 lines (367 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//
// MessagePack for Java
//
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.template;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import org.msgpack.MessagePackable;
import org.msgpack.MessageTypeException;
import org.msgpack.template.BigIntegerTemplate;
import org.msgpack.template.BooleanTemplate;
import org.msgpack.template.ByteArrayTemplate;
import org.msgpack.template.ByteTemplate;
import org.msgpack.template.DoubleArrayTemplate;
import org.msgpack.template.DoubleTemplate;
import org.msgpack.template.FieldList;
import org.msgpack.template.FloatArrayTemplate;
import org.msgpack.template.FloatTemplate;
import org.msgpack.template.GenericTemplate;
import org.msgpack.template.IntegerArrayTemplate;
import org.msgpack.template.IntegerTemplate;
import org.msgpack.template.LongArrayTemplate;
import org.msgpack.template.LongTemplate;
import org.msgpack.template.ShortArrayTemplate;
import org.msgpack.template.ShortTemplate;
import org.msgpack.template.StringTemplate;
import org.msgpack.template.Template;
import org.msgpack.template.ValueTemplate;
import org.msgpack.template.builder.TemplateBuilder;
import org.msgpack.template.builder.TemplateBuilderChain;
import org.msgpack.type.Value;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TemplateRegistry {
private TemplateRegistry parent = null;
private TemplateBuilderChain chain;
Map<Type, Template<Type>> cache;
private Map<Type, GenericTemplate> genericCache;
/**
* create <code>TemplateRegistry</code> object of root.
*/
private TemplateRegistry() {
parent = null;
chain = createTemplateBuilderChain();
genericCache = new HashMap<Type, GenericTemplate>();
cache = new HashMap<Type, Template<Type>>();
registerTemplates();
cache = Collections.unmodifiableMap(cache);
}
/**
*
* @param registry
*/
public TemplateRegistry(TemplateRegistry registry) {
if (registry != null) {
parent = registry;
} else {
parent = new TemplateRegistry();
}
chain = createTemplateBuilderChain();
cache = new HashMap<Type, Template<Type>>();
genericCache = new HashMap<Type, GenericTemplate>();
registerTemplatesWhichRefersRegistry();
}
protected TemplateBuilderChain createTemplateBuilderChain(){
return new TemplateBuilderChain(this);
}
public void setClassLoader(final ClassLoader cl) {
chain = new TemplateBuilderChain(this, cl);
}
private void registerTemplates() {
register(boolean.class, BooleanTemplate.getInstance());
register(Boolean.class, BooleanTemplate.getInstance());
register(byte.class, ByteTemplate.getInstance());
register(Byte.class, ByteTemplate.getInstance());
register(short.class, ShortTemplate.getInstance());
register(Short.class, ShortTemplate.getInstance());
register(int.class, IntegerTemplate.getInstance());
register(Integer.class, IntegerTemplate.getInstance());
register(long.class, LongTemplate.getInstance());
register(Long.class, LongTemplate.getInstance());
register(float.class, FloatTemplate.getInstance());
register(Float.class, FloatTemplate.getInstance());
register(double.class, DoubleTemplate.getInstance());
register(Double.class, DoubleTemplate.getInstance());
register(BigInteger.class, BigIntegerTemplate.getInstance());
register(char.class, CharacterTemplate.getInstance());
register(Character.class, CharacterTemplate.getInstance());
register(boolean[].class, BooleanArrayTemplate.getInstance());
register(short[].class, ShortArrayTemplate.getInstance());
register(int[].class, IntegerArrayTemplate.getInstance());
register(long[].class, LongArrayTemplate.getInstance());
register(float[].class, FloatArrayTemplate.getInstance());
register(double[].class, DoubleArrayTemplate.getInstance());
register(String.class, StringTemplate.getInstance());
register(byte[].class, ByteArrayTemplate.getInstance());
register(ByteBuffer.class, ByteBufferTemplate.getInstance());
register(Value.class, ValueTemplate.getInstance());
register(BigDecimal.class, BigDecimalTemplate.getInstance());
register(Date.class, DateTemplate.getInstance());
registerTemplatesWhichRefersRegistry();
}
protected void registerTemplatesWhichRefersRegistry() {
AnyTemplate anyTemplate = new AnyTemplate(this);
register(List.class, new ListTemplate(anyTemplate));
register(Collection.class, new CollectionTemplate(anyTemplate));
register(Map.class, new MapTemplate(anyTemplate,anyTemplate));
registerGeneric(List.class, new GenericCollectionTemplate(this, ListTemplate.class));
registerGeneric(Collection.class, new GenericCollectionTemplate(this, CollectionTemplate.class));
registerGeneric(Map.class, new GenericMapTemplate(this, MapTemplate.class));
}
public void register(final Class<?> targetClass) {
buildAndRegister(null, targetClass, false, null);
}
public void register(final Class<?> targetClass, final FieldList flist) {
if (flist == null) {
throw new NullPointerException("FieldList object is null");
}
buildAndRegister(null, targetClass, false, flist);
}
public synchronized void register(final Type targetType, final Template tmpl) {
if (tmpl == null) {
throw new NullPointerException("Template object is null");
}
if (targetType instanceof ParameterizedType) {
cache.put(((ParameterizedType) targetType).getRawType(), tmpl);
} else {
cache.put(targetType, tmpl);
}
}
public synchronized void registerGeneric(final Type targetType, final GenericTemplate tmpl) {
if (targetType instanceof ParameterizedType) {
genericCache.put(((ParameterizedType) targetType).getRawType(), tmpl);
} else {
genericCache.put(targetType, tmpl);
}
}
public synchronized boolean unregister(final Type targetType) {
Template<Type> tmpl = cache.remove(targetType);
return tmpl != null;
}
public synchronized void unregister() {
cache.clear();
}
public synchronized Template lookup(Type targetType) {
Template tmpl;
tmpl = lookupGenericType(targetType);
if (tmpl != null) {
return tmpl;
}
tmpl = lookupCache(targetType);
if (tmpl != null) {
return tmpl;
}
Class<?> targetClass = (Class<?>) targetType;
// MessagePackable interface is implemented
if (MessagePackable.class.isAssignableFrom(targetClass)) {
// FIXME #MN
// following processing should be merged into lookAfterBuilding
// or lookupInterfaceTypes method in next version
tmpl = new MessagePackableTemplate(targetClass);
register(targetClass, tmpl);
return tmpl;
}
// find matched template builder and build template
tmpl = lookupAfterBuilding(targetClass);
if (tmpl != null) {
return tmpl;
}
// lookup template of interface type
tmpl = lookupInterfaceTypes(targetClass);
if (tmpl != null) {
return tmpl;
}
// lookup template of superclass type
tmpl = lookupSuperclasses(targetClass);
if (tmpl != null) {
return tmpl;
}
// lookup template of interface type of superclasss
tmpl = lookupSuperclassInterfaceTypes(targetClass);
if (tmpl != null) {
return tmpl;
}
throw new MessageTypeException(
"Cannot find template for " + targetClass + " class. Try to add @Message annotation to the class or call MessagePack.register(Type).");
}
private Template<Type> lookupGenericType(Type targetType) {
Template<Type> tmpl = null;
if (targetType instanceof ParameterizedType) {
ParameterizedType paramedType = (ParameterizedType) targetType;
// ParameterizedType is not a Class<?>?
tmpl = lookupGenericTypeImpl(paramedType);
if (tmpl != null) {
return tmpl;
}
try {
tmpl = parent.lookupGenericTypeImpl(paramedType);
if (tmpl != null) {
return tmpl;
}
} catch (NullPointerException e) { // ignore
}
targetType = paramedType.getRawType();
}
return tmpl;
}
private Template lookupGenericTypeImpl(final ParameterizedType targetType) {
Type rawType = targetType.getRawType();
GenericTemplate tmpl = genericCache.get(rawType);
if (tmpl == null) {
return null;
}
Type[] types = targetType.getActualTypeArguments();
Template[] tmpls = new Template[types.length];
for (int i = 0; i < types.length; ++i) {
tmpls[i] = lookup(types[i]);
}
return tmpl.build(tmpls);
}
private Template<Type> lookupCache(Type targetType) {
Template<Type> tmpl = cache.get(targetType);
if (tmpl != null) {
return tmpl;
}
try {
tmpl = parent.lookupCache(targetType);
} catch (NullPointerException e) { // ignore
}
return tmpl;
}
private <T> Template<T> lookupAfterBuilding(Class<T> targetClass) {
TemplateBuilder builder = chain.select(targetClass, true);
Template<T> tmpl = null;
if (builder != null) {
// TODO #MN for Android, we should modify here
tmpl = chain.getForceBuilder().loadTemplate(targetClass);
if (tmpl != null) {
register(targetClass, tmpl);
return tmpl;
}
tmpl = buildAndRegister(builder, targetClass, true, null);
}
return tmpl;
}
private <T> Template<T> lookupInterfaceTypes(Class<T> targetClass) {
Class<?>[] infTypes = targetClass.getInterfaces();
Template<T> tmpl = null;
for (Class<?> infType : infTypes) {
tmpl = (Template<T>) cache.get(infType);
if (tmpl != null) {
register(targetClass, tmpl);
return tmpl;
} else {
try {
tmpl = (Template<T>) parent.lookupCache(infType);
if (tmpl != null) {
register(targetClass, tmpl);
return tmpl;
}
} catch (NullPointerException e) { // ignore
}
}
}
return tmpl;
}
private <T> Template<T> lookupSuperclasses(Class<T> targetClass) {
Class<?> superClass = targetClass.getSuperclass();
Template<T> tmpl = null;
if (superClass != null) {
for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
tmpl = (Template<T>) cache.get(superClass);
if (tmpl != null) {
register(targetClass, tmpl);
return tmpl;
} else {
try {
tmpl = (Template<T>) parent.lookupCache(superClass);
if (tmpl != null) {
register(targetClass, tmpl);
return tmpl;
}
} catch (NullPointerException e) { // ignore
}
}
}
}
return tmpl;
}
private <T> Template<T> lookupSuperclassInterfaceTypes(Class<T> targetClass) {
Class<?> superClass = targetClass.getSuperclass();
Template<T> tmpl = null;
if (superClass != null) {
for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
tmpl = (Template<T>) lookupInterfaceTypes(superClass);
if (tmpl != null) {
register(targetClass, tmpl);
return tmpl;
} else {
try {
tmpl = (Template<T>) parent.lookupCache(superClass);
if (tmpl != null) {
register(targetClass, tmpl);
return tmpl;
}
} catch (NullPointerException e) { // ignore
}
}
}
}
return tmpl;
}
private synchronized Template buildAndRegister(TemplateBuilder builder, final Class targetClass,
final boolean hasAnnotation, final FieldList flist) {
Template newTmpl = null;
Template oldTmpl = null;
try {
if (cache.containsKey(targetClass)) {
oldTmpl = cache.get(targetClass);
}
newTmpl = new TemplateReference(this, targetClass);
cache.put(targetClass, newTmpl);
if (builder == null) {
builder = chain.select(targetClass, hasAnnotation);
}
newTmpl = flist != null ? builder.buildTemplate(targetClass, flist) : builder.buildTemplate(targetClass);
return newTmpl;
} catch (Exception e) {
if (oldTmpl != null) {
cache.put(targetClass, oldTmpl);
} else {
cache.remove(targetClass);
}
newTmpl = null;
if (e instanceof MessageTypeException) {
throw (MessageTypeException) e;
} else {
throw new MessageTypeException(e);
}
} finally {
if (newTmpl != null) {
cache.put(targetClass, newTmpl);
}
}
}
}