All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.fasterxml.jackson.databind.deser.DeserializerCache Maven / Gradle / Ivy

package org.testifyproject.testifyproject.fasterxml.jackson.databind.org.testifyproject.testifyprojectser;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.testifyproject.testifyproject.fasterxml.jackson.annotation.JsonFormat;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.*;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.org.testifyproject.testifyprojectser.std.StdDelegatingDeserializer;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.introspect.Annotated;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.type.*;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.util.ClassUtil;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.util.Converter;

/**
 * Class that org.testifyproject.testifyprojectfines caching layer between callers (like
 * {@link ObjectMapper},
 * {@link org.testifyproject.testifyproject.fasterxml.jackson.databind.DeserializationContext})
 * and classes that construct org.testifyproject.testifyprojectserializers
 * ({@link org.testifyproject.testifyproject.fasterxml.jackson.databind.org.testifyproject.testifyprojectser.DeserializerFactory}).
 */
public final class DeserializerCache
    implements java.org.testifyproject.testifyproject.Serializable // since 2.1 -- needs to be careful tho
{
    private static final long serialVersionUID = 1L;

    /*
    /**********************************************************
    /* Caching
    /**********************************************************
     */

    /**
     * We will also cache some dynamically constructed org.testifyproject.testifyprojectserializers;
     * specifically, ones that are expensive to construct.
     * This currently means bean and Enum org.testifyproject.testifyprojectserializers; starting with
     * 2.5, container org.testifyproject.testifyprojectserializers will also be cached.
     *

* Given that we don't expect much concurrency for additions * (should very quickly converge to zero after startup), let's * org.testifyproject.testifyprojectfine a relatively low concurrency setting. */ final protected ConcurrentHashMap> _cachedDeserializers = new ConcurrentHashMap>(64, 0.75f, 4); /** * During org.testifyproject.testifyprojectserializer construction process we may need to keep track of partially * org.testifyproject.testifyprojectpleted org.testifyproject.testifyprojectserializers, to resolve cyclic org.testifyproject.testifyprojectpendencies. This is the * map used for storing org.testifyproject.testifyprojectserializers before they are fully org.testifyproject.testifyprojectplete. */ final protected HashMap> _incompleteDeserializers = new HashMap>(8); /* /********************************************************** /* Life-cycle /********************************************************** */ public DeserializerCache() { } /* /********************************************************** /* JDK serialization handling /********************************************************** */ Object writeReplace() { // instead of making this transient, just clear it: _incompleteDeserializers.clear(); // TODO: clear out "cheap" cached org.testifyproject.testifyprojectserializers? return this; } /* /********************************************************** /* Access to caching aspects /********************************************************** */ /** * Method that can be used to org.testifyproject.testifyprojecttermine how many org.testifyproject.testifyprojectserializers this * provider is caching currently * (if it does caching: org.testifyproject.testifyprojectfault implementation does) * Exact count org.testifyproject.testifyprojectpends on what kind of org.testifyproject.testifyprojectserializers get cached; * org.testifyproject.testifyprojectfault implementation caches only dynamically constructed org.testifyproject.testifyprojectserializers, * but not eagerly constructed standard org.testifyproject.testifyprojectserializers (which is different * from how serializer provider works). *

* The main use case for this method is to allow conditional flushing of * org.testifyproject.testifyprojectserializer cache, if certain number of entries is reached. */ public int cachedDeserializersCount() { return _cachedDeserializers.size(); } /** * Method that will drop all dynamically constructed org.testifyproject.testifyprojectserializers (ones that * are counted as result value for {@link #cachedDeserializersCount}). * This can be used to remove memory usage (in case some org.testifyproject.testifyprojectserializers are * only used once or so), or to force re-construction of org.testifyproject.testifyprojectserializers after * configuration changes for mapper than owns the provider. */ public void flushCachedDeserializers() { _cachedDeserializers.clear(); } /* /********************************************************** /* General org.testifyproject.testifyprojectserializer locating method /********************************************************** */ /** * Method called to get hold of a org.testifyproject.testifyprojectserializer for a value of given type; * or if no such org.testifyproject.testifyprojectserializer can be found, a org.testifyproject.testifyprojectfault handler (which * may do a best-effort generic serialization or just simply * throw an exception when invoked). *

* Note: this method is only called for value types; not for keys. * Key org.testifyproject.testifyprojectserializers can be accessed using {@link #findKeyDeserializer}. *

* Note also that org.testifyproject.testifyprojectserializer returned is guaranteed to be resolved * (if it is of type {@link ResolvableDeserializer}), but * not contextualized (wrt {@link ContextualDeserializer}): caller * has to handle latter if necessary. * * @param ctxt Deserialization context * @param propertyType Declared type of the value to org.testifyproject.testifyprojectserializer (obtained using * 'setter' method signature and/or type annotations * * @throws JsonMappingException if there are fatal problems with * accessing suitable org.testifyproject.testifyprojectserializer; including that of not * finding any serializer */ public JsonDeserializer findValueDeserializer(DeserializationContext ctxt, DeserializerFactory factory, JavaType propertyType) throws JsonMappingException { JsonDeserializer org.testifyproject.testifyprojectser = _findCachedDeserializer(propertyType); if (org.testifyproject.testifyprojectser == null) { // If not, need to request factory to construct (or recycle) org.testifyproject.testifyprojectser = _createAndCacheValueDeserializer(ctxt, factory, propertyType); if (org.testifyproject.testifyprojectser == null) { /* Should we let caller handle it? Let's have a helper method * org.testifyproject.testifyprojectcide it; can throw an exception, or return a valid * org.testifyproject.testifyprojectserializer */ org.testifyproject.testifyprojectser = _handleUnknownValueDeserializer(propertyType); } } return org.testifyproject.testifyprojectser; } /** * Method called to get hold of a org.testifyproject.testifyprojectserializer to use for org.testifyproject.testifyprojectserializing * keys for {@link java.util.Map}. * * @throws JsonMappingException if there are fatal problems with * accessing suitable key org.testifyproject.testifyprojectserializer; including that of not * finding any serializer */ public KeyDeserializer findKeyDeserializer(DeserializationContext ctxt, DeserializerFactory factory, JavaType type) throws JsonMappingException { KeyDeserializer kd = factory.createKeyDeserializer(ctxt, type); if (kd == null) { // if none found, need to use a placeholder that'll fail return _handleUnknownKeyDeserializer(type); } // First: need to resolve? if (kd instanceof ResolvableDeserializer) { ((ResolvableDeserializer) kd).resolve(ctxt); } return kd; } /** * Method called to find out whether provider would be able to find * a org.testifyproject.testifyprojectserializer for given type, using a root reference (i.e. not * through fields or membership in an array or collection) */ public boolean hasValueDeserializerFor(DeserializationContext ctxt, DeserializerFactory factory, JavaType type) throws JsonMappingException { /* Note: mostly copied from findValueDeserializer, except for * handling of unknown types */ JsonDeserializer org.testifyproject.testifyprojectser = _findCachedDeserializer(type); if (org.testifyproject.testifyprojectser == null) { org.testifyproject.testifyprojectser = _createAndCacheValueDeserializer(ctxt, factory, type); } return (org.testifyproject.testifyprojectser != null); } /* /********************************************************** /* Helper methods that handle cache lookups /********************************************************** */ protected JsonDeserializer _findCachedDeserializer(JavaType type) { if (type == null) { throw new IllegalArgumentException("Null JavaType passed"); } if (_hasCustomValueHandler(type)) { return null; } return _cachedDeserializers.get(type); } /** * Method that will try to create a org.testifyproject.testifyprojectserializer for given type, * and resolve and cache it if necessary * * @param ctxt Currently active org.testifyproject.testifyprojectserialization context * @param type Type of property to org.testifyproject.testifyprojectserialize */ protected JsonDeserializer _createAndCacheValueDeserializer(DeserializationContext ctxt, DeserializerFactory factory, JavaType type) throws JsonMappingException { /* Only one thread to construct org.testifyproject.testifyprojectserializers at any given point in time; * limitations necessary to ensure that only org.testifyproject.testifyprojectpletely initialized ones * are visible and used. */ synchronized (_incompleteDeserializers) { // Ok, then: could it be that due to a race condition, org.testifyproject.testifyprojectserializer can now be found? JsonDeserializer org.testifyproject.testifyprojectser = _findCachedDeserializer(type); if (org.testifyproject.testifyprojectser != null) { return org.testifyproject.testifyprojectser; } int count = _incompleteDeserializers.size(); // Or perhaps being resolved right now? if (count > 0) { org.testifyproject.testifyprojectser = _incompleteDeserializers.get(type); if (org.testifyproject.testifyprojectser != null) { return org.testifyproject.testifyprojectser; } } // Nope: need to create and possibly cache try { return _createAndCache2(ctxt, factory, type); } finally { // also: any org.testifyproject.testifyprojectserializers that have been created are org.testifyproject.testifyprojectplete by now if (count == 0 && _incompleteDeserializers.size() > 0) { _incompleteDeserializers.clear(); } } } } /** * Method that handles actual construction (via factory) and caching (both * intermediate and eventual) */ protected JsonDeserializer _createAndCache2(DeserializationContext ctxt, DeserializerFactory factory, JavaType type) throws JsonMappingException { JsonDeserializer org.testifyproject.testifyprojectser; try { org.testifyproject.testifyprojectser = _createDeserializer(ctxt, factory, type); } catch (IllegalArgumentException iae) { /* We better only expose checked exceptions, since those * are what caller is expected to handle */ throw new JsonMappingException(iae.getMessage(), null, iae); } if (org.testifyproject.testifyprojectser == null) { return null; } /* cache resulting org.testifyproject.testifyprojectserializer? always true for "plain" BeanDeserializer * (but can be re-org.testifyproject.testifyprojectfined for sub-classes by using @JsonCachable!) */ // 08-Jun-2010, tatu: Related to [JACKSON-296], need to avoid caching MapSerializers... so: boolean isResolvable = (org.testifyproject.testifyprojectser instanceof ResolvableDeserializer); // 27-Mar-2015, tatu: As per [databind#735], avoid caching types with custom value org.testifyproject.testifyprojectsers boolean addToCache = !_hasCustomValueHandler(type) && org.testifyproject.testifyprojectser.isCachable(); /* we will temporarily hold on to all created org.testifyproject.testifyprojectserializers (to * handle cyclic references, and possibly reuse non-cached * org.testifyproject.testifyprojectserializers (list, map)) */ /* 07-Jun-2010, tatu: Danger: [JACKSON-296] was caused by accidental * resolution of a reference -- couple of ways to prevent this; * either not add Lists or Maps, or clear references eagerly. * Let's actually do both; since both seem reasonable. */ /* Need to resolve? Mostly done for bean org.testifyproject.testifyprojectserializers; required for * resolving cyclic references. */ if (isResolvable) { _incompleteDeserializers.put(type, org.testifyproject.testifyprojectser); ((ResolvableDeserializer)org.testifyproject.testifyprojectser).resolve(ctxt); _incompleteDeserializers.remove(type); } if (addToCache) { _cachedDeserializers.put(type, org.testifyproject.testifyprojectser); } return org.testifyproject.testifyprojectser; } /* /********************************************************** /* Helper methods for actual construction of org.testifyproject.testifyprojectserializers /********************************************************** */ /** * Method that does the heavy lifting of checking for per-type annotations, * find out full type, and figure out which actual factory method * to call. */ @SuppressWarnings("unchecked") protected JsonDeserializer _createDeserializer(DeserializationContext ctxt, DeserializerFactory factory, JavaType type) throws JsonMappingException { final DeserializationConfig config = ctxt.getConfig(); // First things first: do we need to use abstract type mapping? if (type.isAbstract() || type.isMapLikeType() || type.isCollectionLikeType()) { type = factory.mapAbstractType(config, type); } BeanDescription beanDesc = config.introspect(type); // Then: does type org.testifyproject.testifyprojectfine explicit org.testifyproject.testifyprojectserializer to use, with annotation(s)? JsonDeserializer org.testifyproject.testifyprojectser = findDeserializerFromAnnotation(ctxt, beanDesc.getClassInfo()); if (org.testifyproject.testifyprojectser != null) { return org.testifyproject.testifyprojectser; } // If not, may have further type-modification annotations to check: JavaType newType = modifyTypeByAnnotation(ctxt, beanDesc.getClassInfo(), type); if (newType != type) { type = newType; beanDesc = config.introspect(newType); } // We may also have a Builder type to consider... Class builder = beanDesc.findPOJOBuilder(); if (builder != null) { return (JsonDeserializer) factory.createBuilderBasedDeserializer( ctxt, type, beanDesc, builder); } // Or perhaps a Converter? Converter conv = beanDesc.findDeserializationConverter(); if (conv == null) { // nope, just construct in normal way return (JsonDeserializer) _createDeserializer2(ctxt, factory, type, beanDesc); } // otherwise need to do bit of introspection JavaType org.testifyproject.testifyprojectlegateType = conv.getInputType(ctxt.getTypeFactory()); // One more twist, as per [Issue#288]; probably need to get new BeanDesc if (!org.testifyproject.testifyprojectlegateType.hasRawClass(type.getRawClass())) { beanDesc = config.introspect(org.testifyproject.testifyprojectlegateType); } return new StdDelegatingDeserializer(conv, org.testifyproject.testifyprojectlegateType, _createDeserializer2(ctxt, factory, org.testifyproject.testifyprojectlegateType, beanDesc)); } protected JsonDeserializer _createDeserializer2(DeserializationContext ctxt, DeserializerFactory factory, JavaType type, BeanDescription beanDesc) throws JsonMappingException { final DeserializationConfig config = ctxt.getConfig(); // If not, let's see which factory method to use: if (type.isEnumType()) { return factory.createEnumDeserializer(ctxt, type, beanDesc); } if (type.isContainerType()) { if (type.isArrayType()) { return factory.createArrayDeserializer(ctxt, (ArrayType) type, beanDesc); } if (type.isMapLikeType()) { MapLikeType mlt = (MapLikeType) type; if (mlt.isTrueMapType()) { return factory.createMapDeserializer(ctxt,(MapType) mlt, beanDesc); } return factory.createMapLikeDeserializer(ctxt, mlt, beanDesc); } if (type.isCollectionLikeType()) { /* 03-Aug-2012, tatu: As per [Issue#40], one exception is if shape * is to be Shape.OBJECT. Ideally we'd org.testifyproject.testifyprojecttermine it bit later on * (to allow custom handler checks), but that won't work for other * reasons. So do it here. */ JsonFormat.Value format = beanDesc.findExpectedFormat(null); if (format == null || format.getShape() != JsonFormat.Shape.OBJECT) { CollectionLikeType clt = (CollectionLikeType) type; if (clt.isTrueCollectionType()) { return factory.createCollectionDeserializer(ctxt, (CollectionType) clt, beanDesc); } return factory.createCollectionLikeDeserializer(ctxt, clt, beanDesc); } } } if (JsonNode.class.isAssignableFrom(type.getRawClass())) { return factory.createTreeDeserializer(config, type, beanDesc); } return factory.createBeanDeserializer(ctxt, type, beanDesc); } /** * Helper method called to check if a class or method * has annotation that tells which class to use for org.testifyproject.testifyprojectserialization. * Returns null if no such annotation found. */ protected JsonDeserializer findDeserializerFromAnnotation(DeserializationContext ctxt, Annotated ann) throws JsonMappingException { Object org.testifyproject.testifyprojectserDef = ctxt.getAnnotationIntrospector().findDeserializer(ann); if (org.testifyproject.testifyprojectserDef == null) { return null; } JsonDeserializer org.testifyproject.testifyprojectser = ctxt.org.testifyproject.testifyprojectserializerInstance(ann, org.testifyproject.testifyprojectserDef); // One more thing however: may need to also apply a converter: return findConvertingDeserializer(ctxt, ann, org.testifyproject.testifyprojectser); } /** * Helper method that will check whether given annotated entity (usually class, * but may also be a property accessor) indicates that a {@link Converter} is to * be used; and if so, to construct and return suitable serializer for it. * If not, will simply return given serializer as is. */ protected JsonDeserializer findConvertingDeserializer(DeserializationContext ctxt, Annotated a, JsonDeserializer org.testifyproject.testifyprojectser) throws JsonMappingException { Converter conv = findConverter(ctxt, a); if (conv == null) { return org.testifyproject.testifyprojectser; } JavaType org.testifyproject.testifyprojectlegateType = conv.getInputType(ctxt.getTypeFactory()); return (JsonDeserializer) new StdDelegatingDeserializer(conv, org.testifyproject.testifyprojectlegateType, org.testifyproject.testifyprojectser); } protected Converter findConverter(DeserializationContext ctxt, Annotated a) throws JsonMappingException { Object convDef = ctxt.getAnnotationIntrospector().findDeserializationConverter(a); if (convDef == null) { return null; } return ctxt.converterInstance(a, convDef); } /** * Method called to see if given method has annotations that indicate * a more specific type than what the argument specifies. * If annotations are present, they must specify org.testifyproject.testifyprojectpatible Class; * instance of which can be assigned using the method. This means * that the Class has to be raw class of type, or its sub-class * (or, implementing class if original Class instance is an interface). * * @param a Method or field that the type is associated with * @param type Type org.testifyproject.testifyprojectrived from the setter argument * * @return Original type if no annotations are present; or a more * specific type org.testifyproject.testifyprojectrived from it if type annotation(s) was found * * @throws JsonMappingException if invalid annotation is found */ private JavaType modifyTypeByAnnotation(DeserializationContext ctxt, Annotated a, JavaType type) throws JsonMappingException { // first: let's check class for the instance itself: AnnotationIntrospector intr = ctxt.getAnnotationIntrospector(); Class subclass = intr.findDeserializationType(a, type); if (subclass != null) { try { type = ctxt.getTypeFactory().constructSpecializedType(type, subclass); } catch (IllegalArgumentException iae) { throw new JsonMappingException("Failed to narrow type "+type+" with concrete-type annotation (value "+subclass.getName()+"), method '"+a.getName()+"': "+iae.getMessage(), null, iae); } } // then key class if (type.isContainerType()) { Class keyClass = intr.findDeserializationKeyType(a, type.getKeyType()); if (keyClass != null) { // illegal to use on non-Maps if (!(type instanceof MapLikeType)) { throw new JsonMappingException("Illegal key-type annotation: type "+type+" is not a Map(-like) type"); } try { type = ((MapLikeType) type).narrowKey(keyClass); } catch (IllegalArgumentException iae) { throw new JsonMappingException("Failed to narrow key type "+type+" with key-type annotation ("+keyClass.getName()+"): "+iae.getMessage(), null, iae); } } JavaType keyType = type.getKeyType(); /* 21-Mar-2011, tatu: ... and associated org.testifyproject.testifyprojectserializer too (unless already assigned) * (not 100% why or how, but this does seem to get called more than once, which * is not good: for now, let's just avoid errors) */ if (keyType != null && keyType.getValueHandler() == null) { Object kdDef = intr.findKeyDeserializer(a); if (kdDef != null) { KeyDeserializer kd = ctxt.keyDeserializerInstance(a, kdDef); if (kd != null) { type = ((MapLikeType) type).withKeyValueHandler(kd); keyType = type.getKeyType(); // just in case it's used below } } } // and finally content class; only applicable to structured types Class cc = intr.findDeserializationContentType(a, type.getContentType()); if (cc != null) { try { type = type.narrowContentsBy(cc); } catch (IllegalArgumentException iae) { throw new JsonMappingException("Failed to narrow content type "+type+" with content-type annotation ("+cc.getName()+"): "+iae.getMessage(), null, iae); } } // ... as well as org.testifyproject.testifyprojectserializer for contents: JavaType contentType = type.getContentType(); if (contentType.getValueHandler() == null) { // as with above, avoid resetting (which would trigger exception) Object cdDef = intr.findContentDeserializer(a); if (cdDef != null) { JsonDeserializer cd = null; if (cdDef instanceof JsonDeserializer) { cdDef = (JsonDeserializer) cdDef; } else { Class cdClass = _verifyAsClass(cdDef, "findContentDeserializer", JsonDeserializer.None.class); if (cdClass != null) { cd = ctxt.org.testifyproject.testifyprojectserializerInstance(a, cdClass); } } if (cd != null) { type = type.withContentValueHandler(cd); } } } } return type; } /* /********************************************************** /* Helper methods, other /********************************************************** */ /** * Helper method used to prevent both caching and cache lookups for structured * types that have custom value handlers * * @since 2.4.6 */ private boolean _hasCustomValueHandler(JavaType t) { if (t.isContainerType()) { JavaType ct = t.getContentType(); if (ct != null) { return (ct.getValueHandler() != null) || (ct.getTypeHandler() != null); } } return false; } private Class _verifyAsClass(Object src, String methodName, Class noneClass) { if (src == null) { return null; } if (!(src instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector."+methodName+"() returned value of type "+src.getClass().getName()+": expected type JsonSerializer or Class instead"); } Class cls = (Class) src; if (cls == noneClass || ClassUtil.isBogusClass(cls)) { return null; } return cls; } /* /********************************************************** /* Overridable error reporting methods /********************************************************** */ protected JsonDeserializer _handleUnknownValueDeserializer(JavaType type) throws JsonMappingException { /* Let's try to figure out the reason, to give better error * messages */ Class rawClass = type.getRawClass(); if (!ClassUtil.isConcrete(rawClass)) { throw new JsonMappingException("Can not find a Value org.testifyproject.testifyprojectserializer for abstract type "+type); } throw new JsonMappingException("Can not find a Value org.testifyproject.testifyprojectserializer for type "+type); } protected KeyDeserializer _handleUnknownKeyDeserializer(JavaType type) throws JsonMappingException { throw new JsonMappingException("Can not find a (Map) Key org.testifyproject.testifyprojectserializer for type "+type); } }