1 package com.fasterxml.jackson.databind.cfg; 2 3 import com.fasterxml.jackson.annotation.ObjectIdGenerator; 4 import com.fasterxml.jackson.annotation.ObjectIdResolver; 5 import com.fasterxml.jackson.databind.*; 6 import com.fasterxml.jackson.databind.deser.ValueInstantiator; 7 import com.fasterxml.jackson.databind.introspect.Annotated; 8 import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; 9 import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; 10 import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder; 11 import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter; 12 import com.fasterxml.jackson.databind.util.Converter; 13 14 /** 15 * Helper class used for handling details of creating handler instances (things 16 * like {@link JsonSerializer}s, {@link JsonDeserializer}s, various type 17 * handlers) of specific types. Actual handler type has been resolved at this 18 * point, so instantiator is strictly responsible for providing a configured 19 * instance by constructing and configuring a new instance, or possibly by 20 * recycling a shared instance. One use case is that of allowing 21 * dependency injection, which would otherwise be difficult to do. 22 *<p> 23 * Custom instances are allowed to return null to indicate that caller should 24 * use the default instantiation handling (which just means calling no-argument 25 * constructor via reflection). 26 *<p> 27 * Care has to be taken to ensure that if instance returned is shared, it will 28 * be thread-safe; caller will not synchronize access to returned instances. 29 */ 30 public abstract class HandlerInstantiator 31 { 32 /* 33 /********************************************************** 34 /* Public API 35 /********************************************************** 36 */ 37 38 /** 39 * Method called to get an instance of deserializer of specified type. 40 * 41 * @param config Deserialization configuration in effect 42 * @param annotated Element (Class, Method, Field, constructor parameter) that 43 * had annotation defining class of deserializer to construct (to allow 44 * implementation use information from other annotations) 45 * @param deserClass Class of deserializer instance to return 46 * 47 * @return Deserializer instance to use 48 */ deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass)49 public abstract JsonDeserializer<?> deserializerInstance(DeserializationConfig config, 50 Annotated annotated, Class<?> deserClass); 51 52 /** 53 * Method called to get an instance of key deserializer of specified type. 54 * 55 * @param config Deserialization configuration in effect 56 * @param annotated Element (Class, Method, Field, constructor parameter) that 57 * had annotation defining class of key deserializer to construct (to allow 58 * implementation use information from other annotations) 59 * @param keyDeserClass Class of key deserializer instance to return 60 * 61 * @return Key deserializer instance to use 62 */ keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> keyDeserClass)63 public abstract KeyDeserializer keyDeserializerInstance(DeserializationConfig config, 64 Annotated annotated, Class<?> keyDeserClass); 65 66 /** 67 * Method called to get an instance of serializer of specified type. 68 * 69 * @param config Serialization configuration in effect 70 * @param annotated Element (Class, Method, Field) that 71 * had annotation defining class of serializer to construct (to allow 72 * implementation use information from other annotations) 73 * @param serClass Class of serializer instance to return 74 * 75 * @return Serializer instance to use 76 */ serializerInstance(SerializationConfig config, Annotated annotated, Class<?> serClass)77 public abstract JsonSerializer<?> serializerInstance(SerializationConfig config, 78 Annotated annotated, Class<?> serClass); 79 80 /** 81 * Method called to get an instance of TypeResolverBuilder of specified type. 82 * 83 * @param config Mapper configuration in effect (either SerializationConfig or 84 * DeserializationConfig, depending on when instance is being constructed) 85 * @param annotated annotated Element (Class, Method, Field) that 86 * had annotation defining class of builder to construct (to allow 87 * implementation use information from other annotations) 88 * @param builderClass Class of builder instance to return 89 * 90 * @return TypeResolverBuilder instance to use 91 */ typeResolverBuilderInstance(MapperConfig<?> config, Annotated annotated, Class<?> builderClass)92 public abstract TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> config, 93 Annotated annotated, Class<?> builderClass); 94 95 /** 96 * Method called to get an instance of TypeIdResolver of specified type. 97 * 98 * @param config Mapper configuration in effect (either SerializationConfig or 99 * DeserializationConfig, depending on when instance is being constructed) 100 * @param annotated annotated Element (Class, Method, Field) that 101 * had annotation defining class of resolver to construct (to allow 102 * implementation use information from other annotations) 103 * @param resolverClass Class of resolver instance to return 104 * 105 * @return TypeResolverBuilder instance to use 106 */ typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass)107 public abstract TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, 108 Annotated annotated, Class<?> resolverClass); 109 110 /** 111 * Method called to construct an instance of ValueInstantiator of specified type. 112 */ valueInstantiatorInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass)113 public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config, 114 Annotated annotated, Class<?> resolverClass) { 115 return null; 116 } 117 118 119 /** 120 * Method called to construct a ObjectIdHandler instance of specified type. 121 * 122 * @since 2.0 123 */ objectIdGeneratorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass)124 public ObjectIdGenerator<?> objectIdGeneratorInstance(MapperConfig<?> config, 125 Annotated annotated, Class<?> implClass) { 126 return null; 127 } 128 resolverIdGeneratorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass)129 public ObjectIdResolver resolverIdGeneratorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) { 130 return null; 131 } 132 133 /** 134 * Method called to construct a NamingStrategy instance used for specified 135 * class. 136 * 137 * @since 2.1 138 */ namingStrategyInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass)139 public PropertyNamingStrategy namingStrategyInstance(MapperConfig<?> config, 140 Annotated annotated, Class<?> implClass) { 141 return null; 142 } 143 144 /** 145 * Method called to construct a Converter instance used for specified class. 146 * 147 * @since 2.2 148 */ converterInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass)149 public Converter<?,?> converterInstance(MapperConfig<?> config, 150 Annotated annotated, Class<?> implClass) { 151 return null; 152 } 153 154 /** 155 * Method called to construct a {@link VirtualBeanPropertyWriter} instance 156 * of specified type. 157 * 158 * @since 2.5 159 */ virtualPropertyWriterInstance(MapperConfig<?> config, Class<?> implClass)160 public VirtualBeanPropertyWriter virtualPropertyWriterInstance(MapperConfig<?> config, 161 Class<?> implClass) { 162 return null; 163 } 164 165 /** 166 * Method called to construct a Filter (any Object with implementation of 167 * <code>equals(Object)</code> that determines if given value is to be 168 * excluded (true) or included (false)) to be used based on 169 * {@link com.fasterxml.jackson.annotation.JsonInclude} annotation (or 170 * equivalent). 171 *<p> 172 * Default implementation returns `null` to indicate that default instantiation 173 * (use zero-arg constructor of the <code>filterClass</code>) should be 174 * used. 175 * 176 * @since 2.9 177 */ includeFilterInstance(SerializationConfig config, BeanPropertyDefinition forProperty, Class<?> filterClass)178 public Object includeFilterInstance(SerializationConfig config, 179 BeanPropertyDefinition forProperty, Class<?> filterClass) { 180 return null; 181 } 182 } 183