1 package com.fasterxml.jackson.databind.util;
2
3 import com.fasterxml.jackson.databind.*;
4 import com.fasterxml.jackson.databind.cfg.MapperConfig;
5 import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
6 import com.fasterxml.jackson.databind.type.ClassKey;
7
8 /**
9  * Helper class for caching resolved root names.
10  */

11 public class RootNameLookup implements java.io.Serializable
12 {
13     private static final long serialVersionUID = 1L;
14
15     /**
16      * For efficient operation, let's try to minimize number of times we
17      * need to introspect root element name to use.
18      */

19     protected transient LRUMap<ClassKey,PropertyName> _rootNames;
20
21     public RootNameLookup() {
22         _rootNames = new LRUMap<ClassKey,PropertyName>(20, 200);
23    }
24
25     public PropertyName findRootName(JavaType rootType, MapperConfig<?> config) {
26         return findRootName(rootType.getRawClass(), config);
27     }
28
29     public PropertyName findRootName(Class<?> rootType, MapperConfig<?> config)
30     {
31         ClassKey key = new ClassKey(rootType);
32         PropertyName name = _rootNames.get(key); 
33         if (name != null) {
34             return name;
35         }
36         BeanDescription beanDesc = config.introspectClassAnnotations(rootType);
37         AnnotationIntrospector intr = config.getAnnotationIntrospector();
38         AnnotatedClass ac = beanDesc.getClassInfo();
39         name = intr.findRootName(ac);
40         // No answer so far? Let's just default to using simple class name
41         if (name == null || !name.hasSimpleName()) {
42             // Should we strip out enclosing class tho? For now, nope:
43             name = PropertyName.construct(rootType.getSimpleName());
44         }
45         _rootNames.put(key, name);
46         return name;
47     }
48
49     /*
50     /**********************************************************
51     /* Serializable overrides
52     /**********************************************************
53      */

54
55     /**
56      * Need to override to reproduce cache object via constructor, instead
57      * of serialize/deserialize (since we do NOT want to retain cached data)
58      */

59     protected Object readResolve() {
60         return new RootNameLookup();
61     }
62 }
63