Skip to content

Commit 75b1bd3

Browse files
committed
Further streamlining of TypeResolverBuilder
1 parent b4bec1c commit 75b1bd3

File tree

8 files changed

+44
-113
lines changed

8 files changed

+44
-113
lines changed

src/main/java/com/fasterxml/jackson/databind/cfg/BaseSettings.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public final class BaseSettings
4040
protected final static BaseSettings DEFAULT_BASE = new BaseSettings(
4141
DEFAULT_ANNOTATION_INTROSPECTOR,
4242
null, TypeFactory.defaultInstance(),
43-
null, StdDateFormat.instance, null,
43+
null, // no default typing, by default
44+
StdDateFormat.instance, null,
4445
Locale.getDefault(),
4546
null, // to indicate "use Jackson default TimeZone" (UTC since Jackson 2.7)
4647
Base64Variants.getDefaultVariant(),
@@ -77,11 +78,10 @@ public final class BaseSettings
7778
*/
7879

7980
/**
80-
* Type information handler used for "untyped" values (ones declared
81-
* to have type <code>Object.class</code>)
81+
* Type information handler used for "default typing".
8282
*/
83-
protected final TypeResolverBuilder<?> _typeResolverBuilder;
84-
83+
protected final TypeResolverBuilder<?> _defaultTyper;
84+
8585
/*
8686
/**********************************************************
8787
/* Configuration settings; other
@@ -142,14 +142,14 @@ public final class BaseSettings
142142

143143
public BaseSettings(AnnotationIntrospector ai,
144144
PropertyNamingStrategy pns, TypeFactory tf,
145-
TypeResolverBuilder<?> typer, DateFormat dateFormat, HandlerInstantiator hi,
145+
TypeResolverBuilder<?> defaultTyper, DateFormat dateFormat, HandlerInstantiator hi,
146146
Locale locale, TimeZone tz, Base64Variant defaultBase64,
147147
JsonNodeFactory nodeFactory)
148148
{
149149
_annotationIntrospector = ai;
150150
_propertyNamingStrategy = pns;
151151
_typeFactory = tf;
152-
_typeResolverBuilder = typer;
152+
_defaultTyper = defaultTyper;
153153
_dateFormat = dateFormat;
154154
_handlerInstantiator = hi;
155155
_locale = locale;
@@ -176,7 +176,7 @@ public BaseSettings withAnnotationIntrospector(AnnotationIntrospector ai) {
176176
return this;
177177
}
178178
return new BaseSettings(ai, _propertyNamingStrategy, _typeFactory,
179-
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
179+
_defaultTyper, _dateFormat, _handlerInstantiator, _locale,
180180
_timeZone, _defaultBase64, _nodeFactory);
181181
}
182182

@@ -193,7 +193,7 @@ public BaseSettings with(PropertyNamingStrategy pns) {
193193
return this;
194194
}
195195
return new BaseSettings(_annotationIntrospector, pns, _typeFactory,
196-
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
196+
_defaultTyper, _dateFormat, _handlerInstantiator, _locale,
197197
_timeZone, _defaultBase64, _nodeFactory);
198198
}
199199

@@ -202,12 +202,12 @@ public BaseSettings with(TypeFactory tf) {
202202
return this;
203203
}
204204
return new BaseSettings(_annotationIntrospector, _propertyNamingStrategy, tf,
205-
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
205+
_defaultTyper, _dateFormat, _handlerInstantiator, _locale,
206206
_timeZone, _defaultBase64, _nodeFactory);
207207
}
208208

209209
public BaseSettings with(TypeResolverBuilder<?> typer) {
210-
if (_typeResolverBuilder == typer) {
210+
if (_defaultTyper == typer) {
211211
return this;
212212
}
213213
return new BaseSettings(_annotationIntrospector, _propertyNamingStrategy, _typeFactory,
@@ -225,7 +225,7 @@ public BaseSettings with(DateFormat df) {
225225
df = _force(df, _timeZone);
226226
}
227227
return new BaseSettings(_annotationIntrospector, _propertyNamingStrategy, _typeFactory,
228-
_typeResolverBuilder, df, _handlerInstantiator, _locale,
228+
_defaultTyper, df, _handlerInstantiator, _locale,
229229
_timeZone, _defaultBase64, _nodeFactory);
230230
}
231231

@@ -234,7 +234,7 @@ public BaseSettings with(HandlerInstantiator hi) {
234234
return this;
235235
}
236236
return new BaseSettings(_annotationIntrospector, _propertyNamingStrategy, _typeFactory,
237-
_typeResolverBuilder, _dateFormat, hi, _locale,
237+
_defaultTyper, _dateFormat, hi, _locale,
238238
_timeZone, _defaultBase64, _nodeFactory);
239239
}
240240

@@ -243,7 +243,7 @@ public BaseSettings with(Locale l) {
243243
return this;
244244
}
245245
return new BaseSettings(_annotationIntrospector, _propertyNamingStrategy, _typeFactory,
246-
_typeResolverBuilder, _dateFormat, _handlerInstantiator, l,
246+
_defaultTyper, _dateFormat, _handlerInstantiator, l,
247247
_timeZone, _defaultBase64, _nodeFactory);
248248
}
249249

@@ -264,7 +264,7 @@ public BaseSettings with(TimeZone tz)
264264
DateFormat df = _force(_dateFormat, tz);
265265
return new BaseSettings(_annotationIntrospector,
266266
_propertyNamingStrategy, _typeFactory,
267-
_typeResolverBuilder, df, _handlerInstantiator, _locale,
267+
_defaultTyper, df, _handlerInstantiator, _locale,
268268
tz, _defaultBase64, _nodeFactory);
269269
}
270270

@@ -274,7 +274,7 @@ public BaseSettings with(Base64Variant base64) {
274274
}
275275
return new BaseSettings(_annotationIntrospector,
276276
_propertyNamingStrategy, _typeFactory,
277-
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
277+
_defaultTyper, _dateFormat, _handlerInstantiator, _locale,
278278
_timeZone, base64, _nodeFactory);
279279
}
280280

@@ -284,7 +284,7 @@ public BaseSettings with(JsonNodeFactory nodeFactory) {
284284
}
285285
return new BaseSettings(_annotationIntrospector,
286286
_propertyNamingStrategy, _typeFactory,
287-
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
287+
_defaultTyper, _dateFormat, _handlerInstantiator, _locale,
288288
_timeZone, _defaultBase64, nodeFactory);
289289
}
290290

@@ -306,8 +306,8 @@ public TypeFactory getTypeFactory() {
306306
return _typeFactory;
307307
}
308308

309-
public TypeResolverBuilder<?> getTypeResolverBuilder() {
310-
return _typeResolverBuilder;
309+
public TypeResolverBuilder<?> getDefaultTyper() {
310+
return _defaultTyper;
311311
}
312312

313313
public DateFormat getDateFormat() {

src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ public final HandlerInstantiator getHandlerInstantiator() {
243243
/**
244244
* Method called to locate a type info handler for types that do not have
245245
* one explicitly declared via annotations (or other configuration).
246-
* If such default handler is configured, it is returned; otherwise
246+
* If such a default handler is configured, it is returned; otherwise
247247
* null is returned.
248248
*/
249249
public final TypeResolverBuilder<?> getDefaultTyper(JavaType baseType) {
250-
return _base.getTypeResolverBuilder();
250+
return _base.getDefaultTyper();
251251
}
252-
252+
253253
public abstract SubtypeResolver getSubtypeResolver();
254254

255255
public final TypeFactory getTypeFactory() {

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,21 +1255,28 @@ protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
12551255
// First: maybe we have explicit type resolver?
12561256
TypeResolverBuilder<?> b;
12571257
JsonTypeResolver resAnn = _findAnnotation(ann, JsonTypeResolver.class);
1258-
12591258
if (resAnn != null) {
1260-
if (!JsonTypeInfo.Value.isEnabled(typeInfo)) {
1259+
// 08-Mar-2018, tatu: Should `NONE` block custom one? Or not?
1260+
if ((typeInfo != null) && (typeInfo.getIdType() == JsonTypeInfo.Id.NONE)) {
12611261
return null;
12621262
}
12631263
b = config.typeResolverBuilderInstance(ann, resAnn.value());
1264-
} else { // if not, use standard one, if indicated by annotations
1264+
} else { // if not, use standard one, but only if indicated by annotations
12651265
if (typeInfo == null) {
12661266
return null;
12671267
}
12681268
// bit special; must return 'marker' to block use of default typing:
12691269
if (typeInfo.getIdType() == JsonTypeInfo.Id.NONE) {
12701270
return _constructNoTypeResolverBuilder();
12711271
}
1272-
b = _constructStdTypeResolverBuilder();
1272+
// 13-Aug-2011, tatu: One complication; external id
1273+
// only works for properties; so if declared for a Class, we will need
1274+
// to map it to "PROPERTY" instead of "EXTERNAL_PROPERTY"
1275+
JsonTypeInfo.As inclusion = typeInfo.getInclusionType();
1276+
if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY && (ann instanceof AnnotatedClass)) {
1277+
typeInfo = typeInfo.withInclusionType(JsonTypeInfo.As.PROPERTY);
1278+
}
1279+
b = _constructStdTypeResolverBuilder(typeInfo);
12731280
}
12741281
// Does it define a custom type id resolver?
12751282
JsonTypeIdResolver idResInfo = _findAnnotation(ann, JsonTypeIdResolver.class);
@@ -1279,26 +1286,15 @@ protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
12791286
idRes.init(baseType);
12801287
}
12811288
b = b.init(typeInfo, idRes);
1282-
// 13-Aug-2011, tatu: One complication; external id
1283-
// only works for properties; so if declared for a Class, we will need
1284-
// to map it to "PROPERTY" instead of "EXTERNAL_PROPERTY"
1285-
JsonTypeInfo.As inclusion = typeInfo.getInclusionType();
1286-
if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY && (ann instanceof AnnotatedClass)) {
1287-
inclusion = JsonTypeInfo.As.PROPERTY;
1288-
}
1289-
b = b.inclusion(inclusion);
1290-
b = b.typeProperty(typeInfo.getPropertyName());
1291-
b = b.defaultImpl(typeInfo.getDefaultImpl());
1292-
b = b.typeIdVisibility(typeInfo.getIdVisible());
12931289
return b;
12941290
}
12951291

12961292
/**
12971293
* Helper method for constructing standard {@link TypeResolverBuilder}
12981294
* implementation.
12991295
*/
1300-
protected StdTypeResolverBuilder _constructStdTypeResolverBuilder() {
1301-
return new StdTypeResolverBuilder();
1296+
protected StdTypeResolverBuilder _constructStdTypeResolverBuilder(JsonTypeInfo.Value typeInfo) {
1297+
return new StdTypeResolverBuilder(typeInfo);
13021298
}
13031299

13041300
/**

src/main/java/com/fasterxml/jackson/databind/jsontype/TypeResolverBuilder.java

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Collection;
44

55
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6-
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
6+
77
import com.fasterxml.jackson.databind.DeserializationConfig;
88
import com.fasterxml.jackson.databind.JavaType;
99
import com.fasterxml.jackson.databind.SerializationConfig;
@@ -15,8 +15,7 @@
1515
* handling type information embedded in JSON to allow for safe
1616
* polymorphic type handling.
1717
*<p>
18-
* Builder is first initialized by calling {@link #init} method, and then
19-
* configured using 'set' methods like {@link #inclusion}.
18+
* Builder is first initialized by calling {@link #init} method.
2019
* Finally, after calling all configuration methods,
2120
* {@link #buildTypeSerializer} or {@link #buildTypeDeserializer}
2221
* will be called to get actual type resolver constructed
@@ -103,35 +102,6 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config,
103102
/* Methods for configuring resolver to build
104103
/**********************************************************
105104
*/
106-
107-
/**
108-
* Method for specifying mechanism to use for including type metadata
109-
* in JSON.
110-
* If not explicitly called, setting defaults to
111-
* {@link As#PROPERTY}.
112-
*
113-
* @param includeAs Mechanism used for including type metadata in JSON
114-
*
115-
* @return Resulting builder instance (usually this builder,
116-
* but may be a newly constructed instance for immutable builders}
117-
*/
118-
public T inclusion(As includeAs);
119-
120-
/**
121-
* Method for specifying name of property used for including type
122-
* information. Not used for all inclusion mechanisms;
123-
* usually only used with {@link As#PROPERTY}.
124-
*<p>
125-
* If not explicitly called, name of property to use is based on
126-
* defaults for {@link com.fasterxml.jackson.annotation.JsonTypeInfo.Id} configured.
127-
*
128-
* @param propName Name of JSON property to use for including
129-
* type information
130-
*
131-
* @return Resulting builder instance (usually this builder,
132-
* but may be a newly constructed instance for immutable builders}
133-
*/
134-
public T typeProperty(String propName);
135105

136106
/**
137107
* Method for specifying default implementation to use if type id
@@ -141,13 +111,4 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config,
141111
* but may be a newly constructed instance for immutable builders}
142112
*/
143113
public T defaultImpl(Class<?> defaultImpl);
144-
145-
/**
146-
* Method for specifying whether type id should be visible to
147-
* {@link com.fasterxml.jackson.databind.JsonDeserializer}s or not.
148-
*
149-
* @return Resulting builder instance (usually this builder,
150-
* but may be a newly constructed instance for immutable builders}
151-
*/
152-
public T typeIdVisibility(boolean isVisible);
153114
}

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/DefaultTypeResolverBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public TypeSerializer buildTypeSerializer(SerializationConfig config,
7474
return useForType(baseType) ? super.buildTypeSerializer(config, baseType, subtypes) : null;
7575
}
7676

77+
public DefaultTypeResolverBuilder typeIdVisibility(boolean isVisible) {
78+
_typeIdVisible = isVisible;
79+
return this;
80+
}
81+
7782
/**
7883
* Method called to check if the default type handler should be
7984
* used for given type.

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -192,41 +192,12 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config,
192192
/**********************************************************
193193
*/
194194

195-
@Override
196-
public StdTypeResolverBuilder inclusion(JsonTypeInfo.As includeAs) {
197-
if (includeAs == null) {
198-
throw new IllegalArgumentException("includeAs cannot be null");
199-
}
200-
_includeAs = includeAs;
201-
return this;
202-
}
203-
204-
/**
205-
* Method for constructing an instance with specified type property name
206-
* (property name to use for type id when using "as-property" inclusion).
207-
*/
208-
@Override
209-
public StdTypeResolverBuilder typeProperty(String typeIdPropName) {
210-
// ok to have null/empty; will restore to use defaults
211-
if (typeIdPropName == null || typeIdPropName.length() == 0) {
212-
typeIdPropName = _idType.getDefaultPropertyName();
213-
}
214-
_typeProperty = typeIdPropName;
215-
return this;
216-
}
217-
218195
@Override
219196
public StdTypeResolverBuilder defaultImpl(Class<?> defaultImpl) {
220197
_defaultImpl = defaultImpl;
221198
return this;
222199
}
223200

224-
@Override
225-
public StdTypeResolverBuilder typeIdVisibility(boolean isVisible) {
226-
_typeIdVisible = isVisible;
227-
return this;
228-
}
229-
230201
/*
231202
/**********************************************************
232203
/* Accessors

src/test/java/com/fasterxml/jackson/databind/introspect/TestJacksonAnnotationIntrospector.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public static class DummyBuilder extends StdTypeResolverBuilder
125125
@JsonTypeResolver(DummyBuilder.class)
126126
static class TypeResolverBean { }
127127

128-
// @since 1.7
129128
@JsonIgnoreType
130129
static class IgnoredType { }
131130

src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ public void testUnWrappedMapWithDefaultType() throws Exception{
210210
.addModule(mod)
211211
.build();
212212
TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL,
213-
JsonTypeInfo.As.PROPERTY, JsonTypeInfo.Id.NAME, null);
214-
//typer = typer.typeProperty(TYPE_FIELD);
215-
typer = typer.typeIdVisibility(true);
213+
JsonTypeInfo.As.PROPERTY, JsonTypeInfo.Id.NAME, null)
214+
.typeIdVisibility(true);
216215
mapper.setDefaultTyping(typer);
217216

218217
Map<ABC,String> stuff = new HashMap<ABC,String>();

0 commit comments

Comments
 (0)