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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
public class XWorkConverter extends DefaultTypeConverter {
protected static final Logger LOG = LoggerFactory.getLogger(XWorkConverter.class);
public static final String REPORT_CONVERSION_ERRORS = "report.conversion.errors";
public static final String CONVERSION_PROPERTY_FULLNAME = "conversion.property.fullName";
public static final String CONVERSION_ERROR_PROPERTY_PREFIX = "invalid.fieldvalue.";
public static final String CONVERSION_COLLECTION_PREFIX = "Collection_";
public static final String LAST_BEAN_CLASS_ACCESSED = "last.bean.accessed";
public static final String LAST_BEAN_PROPERTY_ACCESSED = "last.property.accessed";
public static final String MESSAGE_INDEX_PATTERN = "\\[\\d+\\]\\.";
public static final String MESSAGE_INDEX_BRACKET_PATTERN = "[\\[\\]\\.]";
public static final String PERIOD = ".";
public static final Pattern messageIndexPattern = Pattern.compile(MESSAGE_INDEX_PATTERN);
/**
* Target class conversion Mappings.
* <pre>
* Map<Class, Map<String, Object>>
* - Class -> convert to class
* - Map<String, Object>
* - String -> property name
* eg. Element_property, property etc.
* - Object -> String to represent properties
* eg. value part of
* KeyProperty_property=id
* -> TypeConverter to represent an Ognl TypeConverter
* eg. value part of
* property=foo.bar.MyConverter
* -> Class to represent a class
* eg. value part of
* Element_property=foo.bar.MyObject
* </pre>
*/
protected HashMap<Class, Map<String, Object>> mappings = new HashMap<Class, Map<String, Object>>(); // action
/**
* Unavailable target class conversion mappings, serves as a simple cache.
*/
protected HashSet<Class> noMapping = new HashSet<Class>(); // action
/**
* Record class and its type converter mapping.
* <pre>
* - String - classname as String
* - TypeConverter - instance of TypeConverter
* </pre>
*/
protected HashMap<String, TypeConverter> defaultMappings = new HashMap<String, TypeConverter>(); // non-action (eg. returned value)
/**
* Record classes that doesn't have conversion mapping defined.
* <pre>
* - String -> classname as String
* </pre>
*/
protected HashSet<String> unknownMappings = new HashSet<String>(); // non-action (eg. returned value)
private TypeConverter defaultTypeConverter;
private ObjectFactory objectFactory;
protected XWorkConverter() {
}
@Inject
public void setObjectFactory(ObjectFactory factory) {
this.objectFactory = factory;
// note: this file is deprecated
loadConversionProperties("xwork-default-conversion.properties");
loadConversionProperties("xwork-conversion.properties");
}
@Inject
public void setDefaultTypeConverter(XWorkBasicConverter conv) {
this.defaultTypeConverter = conv;
}
public static String getConversionErrorMessage(String propertyName, ValueStack stack) {
String defaultMessage = LocalizedTextUtil.findDefaultText(XWorkMessages.DEFAULT_INVALID_FIELDVALUE,
ActionContext.getContext().getLocale(),
new Object[]{
propertyName
});
List<String> indexValues = getIndexValues(propertyName);
propertyName = removeAllIndexesInProperyName(propertyName);
String getTextExpression = "getText('" + CONVERSION_ERROR_PROPERTY_PREFIX + propertyName + "','" + defaultMessage + "')";
String message = (String) stack.findValue(getTextExpression);
if (message == null) {
message = defaultMessage;
} else {
message = MessageFormat.format(message, indexValues.toArray());
}
return message;
}
private static String removeAllIndexesInProperyName(String propertyName) {
return propertyName.replaceAll(MESSAGE_INDEX_PATTERN, PERIOD);
}
private static List<String> getIndexValues(String propertyName) {
Matcher matcher = messageIndexPattern.matcher(propertyName);
List<String> indexes = new ArrayList<String>();
while (matcher.find()) {
Integer index = new Integer(matcher.group().replaceAll(MESSAGE_INDEX_BRACKET_PATTERN, "")) + 1;
indexes.add(Integer.toString(index));
}
return indexes;
}
public static String buildConverterFilename(Class clazz) {
String className = clazz.getName();
return className.replace('.', '/') + "-conversion.properties";
}
@Override
public Object convertValue(Map<String, Object> map, Object o, Class aClass) {
return convertValue(map, null, null, null, o, aClass);
}
/**
* Convert value from one form to another.
* Minimum requirement of arguments:
* <ul>
* <li>supplying context, toClass and value</li>
* <li>supplying context, target and value.</li>
* </ul>
*
* @see TypeConverter#convertValue(java.util.Map, java.lang.Object, java.lang.reflect.Member, java.lang.String, java.lang.Object, java.lang.Class)
*/
@Override
public Object convertValue(Map<String, Object> context, Object target, Member member, String property, Object value, Class toClass) {
//
// Process the conversion using the default mappings, if one exists
//
TypeConverter tc = null;
if ((value != null) && (toClass == value.getClass())) {
return value;
}
// allow this method to be called without any context
// i.e. it can be called with as little as "Object value" and "Class toClass"
if (target != null) {
Class clazz = target.getClass();
Object[] classProp = null;
// this is to handle weird issues with setValue with a different type
if ((target instanceof CompoundRoot) && (context != null)) {
classProp = getClassProperty(context);
}
if (classProp != null) {
clazz = (Class) classProp[0];
property = (String) classProp[1];
}
tc = (TypeConverter) getConverter(clazz, property);
if (LOG.isDebugEnabled())
LOG.debug("field-level type converter for property [" + property + "] = " + (tc == null ? "none found" : tc));
}
if (tc == null && context != null) {
// ok, let's see if we can look it up by path as requested in XW-297
Object lastPropertyPath = context.get(ReflectionContextState.CURRENT_PROPERTY_PATH);
Class clazz = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);
if (lastPropertyPath != null && clazz != null) {
String path = lastPropertyPath + "." + property;
tc = (TypeConverter) getConverter(clazz, path);
}
}
if (tc == null) {
if (toClass.equals(String.class) && (value != null) && !(value.getClass().equals(String.class) || value.getClass().equals(String[].class))) {
// when converting to a string, use the source target's class's converter
tc = lookup(value.getClass());
} else {
// when converting from a string, use the toClass's converter
tc = lookup(toClass);
}
if (LOG.isDebugEnabled())
LOG.debug("global-level type converter for property [" + property + "] = " + (tc == null ? "none found" : tc));
}
if (tc != null) {
try {
return tc.convertValue(context, target, member, property, value, toClass);
} catch (Exception e) {
if (LOG.isDebugEnabled())
LOG.debug("unable to convert value using type converter [#0]", e, tc.getClass().getName());
handleConversionException(context, property, value, target);
return TypeConverter.NO_CONVERSION_POSSIBLE;
}
}
if (defaultTypeConverter != null) {
try {
if (LOG.isDebugEnabled())
LOG.debug("falling back to default type converter [" + defaultTypeConverter + "]");
return defaultTypeConverter.convertValue(context, target, member, property, value, toClass);
} catch (Exception e) {
if (LOG.isDebugEnabled())
LOG.debug("unable to convert value using type converter [#0]", e, defaultTypeConverter.getClass().getName());
handleConversionException(context, property, value, target);
return TypeConverter.NO_CONVERSION_POSSIBLE;
}
} else {
try {
if (LOG.isDebugEnabled())
LOG.debug("falling back to Ognl's default type conversion");
return super.convertValue(value, toClass);
} catch (Exception e) {
if (LOG.isDebugEnabled())
LOG.debug("unable to convert value using type converter [#0]", e, super.getClass().getName());
handleConversionException(context, property, value, target);
return TypeConverter.NO_CONVERSION_POSSIBLE;
}
}
}
/**
* Looks for a TypeConverter in the default mappings.
*
* @param className name of the class the TypeConverter must handle
* @return a TypeConverter to handle the specified class or null if none can be found
*/
public TypeConverter lookup(String className) {
if (unknownMappings.contains(className) && !defaultMappings.containsKey(className)) {
return null;
}
TypeConverter result = defaultMappings.get(className);
//Looks for super classes
if (result == null) {
Class clazz = null;
try {
clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException cnfe) {
//swallow
}
result = lookupSuper(clazz);
if (result != null) {
//Register now, the next lookup will be faster
registerConverter(className, result);
} else {
// if it isn't found, never look again (also faster)
registerConverterNotFound(className);
}
}
return result;
}
/**
* Looks for a TypeConverter in the default mappings.
*
* @param clazz the class the TypeConverter must handle
* @return a TypeConverter to handle the specified class or null if none can be found
*/
public TypeConverter lookup(Class clazz) {
return lookup(clazz.getName());
}
protected Object getConverter(Class clazz, String property) {
if (LOG.isDebugEnabled()) {
LOG.debug("Property: " + property);
LOG.debug("Class: " + clazz.getName());
}
synchronized (clazz) {
if ((property != null) && !noMapping.contains(clazz)) {
try {
Map<String, Object> mapping = mappings.get(clazz);
if (mapping == null) {
mapping = buildConverterMapping(clazz);
} else {
mapping = conditionalReload(clazz, mapping);
}
Object converter = mapping.get(property);
if (LOG.isDebugEnabled() && converter == null) {
LOG.debug("converter is null for property " + property + ". Mapping size: " + mapping.size());
for (String next : mapping.keySet()) {
LOG.debug(next + ":" + mapping.get(next));
}
}
return converter;
} catch (Throwable t) {
noMapping.add(clazz);
}
}
}
return null;
}
protected void handleConversionException(Map<String, Object> context, String property, Object value, Object object) {
if (context != null && (Boolean.TRUE.equals(context.get(REPORT_CONVERSION_ERRORS)))) {
String realProperty = property;
String fullName = (String) context.get(CONVERSION_PROPERTY_FULLNAME);
if (fullName != null) {
realProperty = fullName;
}
Map<String, Object> conversionErrors = (Map<String, Object>) context.get(ActionContext.CONVERSION_ERRORS);
if (conversionErrors == null) {
conversionErrors = new HashMap<String, Object>();
context.put(ActionContext.CONVERSION_ERRORS, conversionErrors);
}
conversionErrors.put(realProperty, value);
}
}
public synchronized void registerConverter(String className, TypeConverter converter) {
defaultMappings.put(className, converter);
if (unknownMappings.contains(className)) {
unknownMappings.remove(className);
}
}
public synchronized void registerConverterNotFound(String className) {
unknownMappings.add(className);
}
private Object[] getClassProperty(Map<String, Object> context) {
Object lastClass = context.get(LAST_BEAN_CLASS_ACCESSED);
Object lastProperty = context.get(LAST_BEAN_PROPERTY_ACCESSED);
return (lastClass != null && lastProperty != null) ? new Object[] {lastClass, lastProperty} : null;
}
/**
* Looks for converter mappings for the specified class and adds it to an existing map. Only new converters are
* added. If a converter is defined on a key that already exists, the converter is ignored.
*
* @param mapping an existing map to add new converter mappings to
* @param clazz class to look for converter mappings for
*/
protected void addConverterMapping(Map<String, Object> mapping, Class clazz) {
try {
String converterFilename = buildConverterFilename(clazz);
InputStream is = FileManager.loadFile(converterFilename, clazz);
if (is != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("processing conversion file [" + converterFilename + "] [class=" + clazz + "]");
}
Properties prop = new Properties();
prop.load(is);
for (Map.Entry<Object, Object> entry : prop.entrySet()) {
String key = (String) entry.getKey();
if (mapping.containsKey(key)) {
break;
}
// for keyProperty of Set
if (key.startsWith(DefaultObjectTypeDeterminer.KEY_PROPERTY_PREFIX)
|| key.startsWith(DefaultObjectTypeDeterminer.CREATE_IF_NULL_PREFIX)) {
if (LOG.isDebugEnabled()) {
LOG.debug("\t" + key + ":" + entry.getValue() + "[treated as String]");
}
mapping.put(key, entry.getValue());
}
//for properties of classes
else if (!(key.startsWith(DefaultObjectTypeDeterminer.ELEMENT_PREFIX) ||
key.startsWith(DefaultObjectTypeDeterminer.KEY_PREFIX) ||
key.startsWith(DefaultObjectTypeDeterminer.DEPRECATED_ELEMENT_PREFIX))
) {
TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("\t" + key + ":" + entry.getValue() + "[treated as TypeConverter " + _typeConverter + "]");
}
mapping.put(key, _typeConverter);
}
//for keys of Maps
else if (key.startsWith(DefaultObjectTypeDeterminer.KEY_PREFIX)) {
Class converterClass = Thread.currentThread().getContextClassLoader().loadClass((String) entry.getValue());
//check if the converter is a type converter if it is one
//then just put it in the map as is. Otherwise
//put a value in for the type converter of the class
if (converterClass.isAssignableFrom(TypeConverter.class)) {
TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("\t" + key + ":" + entry.getValue() + "[treated as TypeConverter " + _typeConverter + "]");
}
mapping.put(key, _typeConverter);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("\t" + key + ":" + entry.getValue() + "[treated as Class " + converterClass + "]");
}
mapping.put(key, converterClass);
}
}
//elements(values) of maps / lists
else {
Class _c = Thread.currentThread().getContextClassLoader().loadClass((String) entry.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("\t" + key + ":" + entry.getValue() + "[treated as Class " + _c + "]");
}
mapping.put(key, _c);
}
}
}
} catch (Exception ex) {
LOG.error("Problem loading properties for " + clazz.getName(), ex);
}
// Process annotations
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Conversion) {
Conversion conversion = (Conversion) annotation;
for (TypeConversion tc : conversion.conversions()) {
String key = tc.key();
if (mapping.containsKey(key)) {
break;
}
if (LOG.isDebugEnabled()) {
LOG.debug(key + ":" + key);
}
if (key != null) {
try {
if (tc.type() == ConversionType.APPLICATION) {
defaultMappings.put(key, createTypeConverter(tc.converter()));
} else {
if (tc.rule().toString().equals(ConversionRule.KEY_PROPERTY) || tc.rule().toString().equals(ConversionRule.CREATE_IF_NULL)) {
mapping.put(key, tc.value());
}
//for properties of classes
else if (!(tc.rule().toString().equals(ConversionRule.ELEMENT.toString())) ||
tc.rule().toString().equals(ConversionRule.KEY.toString()) ||
tc.rule().toString().equals(ConversionRule.COLLECTION.toString())
) {
mapping.put(key, createTypeConverter(tc.converter()));
}
//for keys of Maps
else if (tc.rule().toString().equals(ConversionRule.KEY.toString())) {
Class converterClass = Thread.currentThread().getContextClassLoader().loadClass(tc.converter());
if (LOG.isDebugEnabled()) {
LOG.debug("Converter class: " + converterClass);
}
//check if the converter is a type converter if it is one
//then just put it in the map as is. Otherwise
//put a value in for the type converter of the class
if (converterClass.isAssignableFrom(TypeConverter.class)) {
mapping.put(key, createTypeConverter(tc.converter()));
} else {
mapping.put(key, converterClass);
if (LOG.isDebugEnabled()) {
LOG.debug("Object placed in mapping for key "
+ key
+ " is "
+ mapping.get(key));
}
}
}
//elements(values) of maps / lists
else {
mapping.put(key, Thread.currentThread().getContextClassLoader().loadClass(tc.converter()));
}
}
} catch (Exception e) {
}
}
}
}
}
Method[] methods = clazz.getMethods();
for (Method method : methods) {
annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof TypeConversion) {
TypeConversion tc = (TypeConversion) annotation;
String key = tc.key();
if (mapping.containsKey(key)) {
break;
}
// Default to the property name
if (key != null && key.length() == 0) {
key = AnnotationUtils.resolvePropertyName(method);
LOG.debug("key from method name... " + key + " - " + method.getName());
}
if (LOG.isDebugEnabled()) {
LOG.debug(key + ":" + key);
}
if (key != null) {
try {
if (tc.type() == ConversionType.APPLICATION) {
defaultMappings.put(key, createTypeConverter(tc.converter()));
} else {
if (tc.rule().toString().equals(ConversionRule.KEY_PROPERTY)) {
mapping.put(key, tc.value());
}
//for properties of classes
else if (!(tc.rule().toString().equals(ConversionRule.ELEMENT.toString())) ||
tc.rule().toString().equals(ConversionRule.KEY.toString()) ||
tc.rule().toString().equals(ConversionRule.COLLECTION.toString())
) {
mapping.put(key, createTypeConverter(tc.converter()));
}
//for keys of Maps
else if (tc.rule().toString().equals(ConversionRule.KEY.toString())) {
Class converterClass = Thread.currentThread().getContextClassLoader().loadClass(tc.converter());
if (LOG.isDebugEnabled()) {
LOG.debug("Converter class: " + converterClass);
}
//check if the converter is a type converter if it is one
//then just put it in the map as is. Otherwise
//put a value in for the type converter of the class
if (converterClass.isAssignableFrom(TypeConverter.class)) {
mapping.put(key, createTypeConverter(tc.converter()));
} else {
mapping.put(key, converterClass);
if (LOG.isDebugEnabled()) {
LOG.debug("Object placed in mapping for key "
+ key
+ " is "
+ mapping.get(key));
}
}
}
//elements(values) of maps / lists
else {
mapping.put(key, Thread.currentThread().getContextClassLoader().loadClass(tc.converter()));
}
}
} catch (Exception e) {
}
}
}
}
}
}
/**
* Looks for converter mappings for the specified class, traversing up its class hierarchy and interfaces and adding
* any additional mappings it may find. Mappings lower in the hierarchy have priority over those higher in the
* hierarcy.
*
* @param clazz the class to look for converter mappings for
* @return the converter mappings
*/
protected Map<String, Object> buildConverterMapping(Class clazz) throws Exception {
Map<String, Object> mapping = new HashMap<String, Object>();
// check for conversion mapping associated with super classes and any implemented interfaces
Class curClazz = clazz;
while (!curClazz.equals(Object.class)) {
// add current class' mappings
addConverterMapping(mapping, curClazz);
// check interfaces' mappings
Class[] interfaces = curClazz.getInterfaces();
for (Class anInterface : interfaces) {
addConverterMapping(mapping, anInterface);
}
curClazz = curClazz.getSuperclass();
}
if (mapping.size() > 0) {
mappings.put(clazz, mapping);
} else {
noMapping.add(clazz);
}
return mapping;
}
private Map<String, Object> conditionalReload(Class clazz, Map<String, Object> oldValues) throws Exception {
Map<String, Object> mapping = oldValues;
if (FileManager.isReloadingConfigs()) {
if (FileManager.fileNeedsReloading(buildConverterFilename(clazz), clazz)) {
mapping = buildConverterMapping(clazz);
}
}
return mapping;
}
TypeConverter createTypeConverter(String className) throws Exception {
// type converters are used across users
Object obj = objectFactory.buildBean(className, null);
if (obj instanceof TypeConverter) {
return (TypeConverter) obj;
// For backwards compatibility
} else if (obj instanceof ognl.TypeConverter) {
return new XWorkTypeConverterWrapper((ognl.TypeConverter) obj);
} else {
throw new IllegalArgumentException("Type converter class " + obj.getClass() + " doesn't implement com.opensymphony.xwork2.conversion.TypeConverter");
}
}
public void loadConversionProperties(String propsName) {
loadConversionProperties(propsName, false);
}
public void loadConversionProperties(String propsName, boolean require) {
try {
Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true);
while (resources.hasNext()) {
URL url = resources.next();
Properties props = new Properties();
props.load(url.openStream());
if (LOG.isDebugEnabled()) {
LOG.debug("processing conversion file [" + propsName + "]");
}
for (Object o : props.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String key = (String) entry.getKey();
try {
TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("\t" + key + ":" + entry.getValue() + " [treated as TypeConverter " + _typeConverter + "]");
}
defaultMappings.put(key, _typeConverter);
} catch (Exception e) {
LOG.error("Conversion registration error", e);
}
}
}
} catch (IOException ex) {
if (require) {
throw new XWorkException("Cannot load conversion properties file: "+propsName, ex);
} else {
LOG.debug("Cannot load conversion properties file: "+propsName, ex);
}
}
}
/**
* Recurses through a class' interfaces and class hierarchy looking for a TypeConverter in the default mapping that
* can handle the specified class.
*
* @param clazz the class the TypeConverter must handle
* @return a TypeConverter to handle the specified class or null if none can be found
*/
TypeConverter lookupSuper(Class clazz) {
TypeConverter result = null;
if (clazz != null) {
result = defaultMappings.get(clazz.getName());
if (result == null) {
// Looks for direct interfaces (depth = 1 )
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface : interfaces) {
if (defaultMappings.containsKey(anInterface.getName())) {
result = (TypeConverter) defaultMappings.get(anInterface.getName());
break;
}
}
if (result == null) {
// Looks for the superclass
// If 'clazz' is the Object class, an interface, a primitive type or void then clazz.getSuperClass() returns null
result = lookupSuper(clazz.getSuperclass());
}
}
}
return result;
}
}
|