Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.jsontype.impl;
  
  import java.util.*;
  
  
 
 public class TypeNameIdResolver
     extends TypeIdResolverBase
 {
     protected final MapperConfig<?> _config;
    
    
Mappings from class name to type id, used for serialization
 
     protected final HashMap<StringString_typeToId;

    
Mappings from type id to JavaType, used for deserialization
 
     protected final HashMap<StringJavaType_idToType;
     
     protected TypeNameIdResolver(MapperConfig<?> configJavaType baseType,
             HashMap<StringStringtypeToIdHashMap<StringJavaTypeidToType)
     {
         super(baseTypeconfig.getTypeFactory());
          = config;
          = typeToId;
          = idToType;
     }
  
     public static TypeNameIdResolver construct(MapperConfig<?> config,
             JavaType baseType,
             Collection<NamedTypesubtypesboolean forSerboolean forDeser)
     {
         // sanity check
         if (forSer == forDeserthrow new IllegalArgumentException();
         HashMap<StringStringtypeToId = null;
         HashMap<StringJavaTypeidToType = null;
 
         if (forSer) {
             typeToId = new HashMap<StringString>();
         }
         if (forDeser) {
             idToType = new HashMap<StringJavaType>();
         }
         if (subtypes != null) {
             for (NamedType t : subtypes) {
                 /* no name? Need to figure out default; for now, let's just
                  * use non-qualified class name
                  */
                 Class<?> cls = t.getType();
                 String id = t.hasName() ? t.getName() : _defaultTypeId(cls);
                 if (forSer) {
                     typeToId.put(cls.getName(), id);
                 }
                 if (forDeser) {
                     /* 24-Feb-2011, tatu: [JACKSON-498] One more problem; sometimes
                      *   we have same name for multiple types; if so, use most specific
                      *   one.
                      */
                     JavaType prev = idToType.get(id);
                     if (prev != null) { // Can only override if more specific
                         if (cls.isAssignableFrom(prev.getRawClass())) { // nope, more generic (or same)
                             continue;
                         }
                     }
                     idToType.put(idconfig.constructType(cls));
                 }
             }
         }
         return new TypeNameIdResolver(configbaseTypetypeToIdidToType);
     }
 
 //  @Override
     public JsonTypeInfo.Id getMechanism() { return ..; }
 
 //  @Override
     public String idFromValue(Object value)
     {
         Class<?> cls = value.getClass();
         final String key = cls.getName();
         String name;
         synchronized () {
             name = .get(key);
             if (name == null) {
                 // 24-Feb-2011, tatu: As per [JACKSON-498], may need to dynamically look up name
                 // can either throw an exception, or use default name...
                 if (.isAnnotationProcessingEnabled()) {
                     BeanDescription beanDesc = .introspectClassAnnotations(cls);
                     name = .getAnnotationIntrospector().findTypeName(beanDesc.getClassInfo());
                 }
                 if (name == null) {
                     // And if still not found, let's choose default?
                     name = _defaultTypeId(cls);
                }
                .put(keyname);
            }
        }
        return name;
    }
//  @Override
    public String idFromValueAndType(Object valueClass<?> type)
    {
        return idFromValue(value);
    }
    
//  @Override
    public JavaType typeFromId(String id)
        throws IllegalArgumentException
    {
        JavaType t = .get(id);
        /* Now: if no type is found, should we try to locate it by
         * some other means? (specifically, if in same package as base type,
         * could just try Class.forName)
         * For now let's not add any such workarounds; can add if need be
         */
        return t;
    }    
    @Override
    public String toString()
    {
    	StringBuilder sb = new StringBuilder();
    	sb.append('[').append(getClass().getName());
    	sb.append("; id-to-type=").append();
    	sb.append(']');
    	return sb.toString();
    }
    
    /*
    /*********************************************************
    /* Helper methods
    /*********************************************************
     */
    
    
If no name was explicitly given for a class, we will just use non-qualified class name
    protected static String _defaultTypeId(Class<?> cls)
    {
        String n = cls.getName();
        int ix = n.lastIndexOf('.');
        return (ix < 0) ? n : n.substring(ix+1);
    }
New to GrepCode? Check out our FAQ X