Skip to content

Commit ab25a78

Browse files
authored
Part 2 of changes for #4907 (converter access) (#5168)
1 parent 9aa9fd0 commit ab25a78

File tree

7 files changed

+75
-102
lines changed

7 files changed

+75
-102
lines changed

src/main/java/tools/jackson/databind/BeanDescription.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import tools.jackson.databind.introspect.*;
1010
import tools.jackson.databind.util.Annotations;
11-
import tools.jackson.databind.util.Converter;
1211

1312
/**
1413
* Basic container for information gathered by {@link ClassIntrospector} to
@@ -227,33 +226,12 @@ public AnnotatedMember findJsonKeyAccessor() {
227226
*/
228227
public abstract JsonInclude.Value findPropertyInclusion(JsonInclude.Value defValue);
229228

230-
/**
231-
* Method for checking what is the expected format for POJO, as
232-
* defined by possible annotations (but NOT config overrides)
233-
*
234-
* @deprecated Since 3.0
235-
*/
236-
@Deprecated // since 3.0
237-
public abstract JsonFormat.Value findExpectedFormat();
238-
239229
/**
240230
* Method for checking what is the expected format for POJO, as
241231
* defined by possible annotations and possible per-type config overrides.
242232
*/
243233
public abstract JsonFormat.Value findExpectedFormat(Class<?> baseType);
244234

245-
/**
246-
* Method for finding {@link Converter} used for serializing instances
247-
* of this class.
248-
*/
249-
public abstract Converter<Object,Object> findSerializationConverter();
250-
251-
/**
252-
* Method for finding {@link Converter} used for serializing instances
253-
* of this class.
254-
*/
255-
public abstract Converter<Object,Object> findDeserializationConverter();
256-
257235
/*
258236
/**********************************************************************
259237
/* Basic API, other

src/main/java/tools/jackson/databind/DeserializationConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import tools.jackson.databind.type.LogicalType;
99
import tools.jackson.databind.type.TypeFactory;
1010
import tools.jackson.databind.util.ArrayIterator;
11+
import tools.jackson.databind.util.Converter;
1112
import tools.jackson.databind.util.LinkedNode;
1213
import tools.jackson.databind.util.RootNameLookup;
1314

@@ -676,4 +677,17 @@ public CoercionAction findCoercionFromBlankString(LogicalType targetType,
676677
return _coercionConfigs.findCoercionFromBlankString(this,
677678
targetType, targetClass, actionIfBlankNotAllowed);
678679
}
680+
681+
/*
682+
/**********************************************************************
683+
/* Introspection support
684+
/**********************************************************************
685+
*/
686+
687+
// @since 3.0
688+
public Converter<Object,Object> findDeserializationConverter(Annotated ann)
689+
{
690+
return _createConverter(ann,
691+
getAnnotationIntrospector().findDeserializationConverter(this, ann));
692+
}
679693
}

src/main/java/tools/jackson/databind/SerializationConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import tools.jackson.core.*;
66
import tools.jackson.core.util.Instantiatable;
77
import tools.jackson.databind.cfg.*;
8+
import tools.jackson.databind.introspect.Annotated;
89
import tools.jackson.databind.introspect.ClassIntrospector;
910
import tools.jackson.databind.introspect.MixInHandler;
1011
import tools.jackson.databind.jsontype.SubtypeResolver;
1112
import tools.jackson.databind.ser.FilterProvider;
1213
import tools.jackson.databind.ser.SerializerFactory;
1314
import tools.jackson.databind.type.TypeFactory;
15+
import tools.jackson.databind.util.Converter;
1416
import tools.jackson.databind.util.RootNameLookup;
1517

1618
/**
@@ -571,4 +573,17 @@ public FilterProvider getFilterProvider() {
571573
public PrettyPrinter getDefaultPrettyPrinter() {
572574
return _defaultPrettyPrinter;
573575
}
576+
577+
/*
578+
/**********************************************************************
579+
/* Introspection support
580+
/**********************************************************************
581+
*/
582+
583+
// @since 3.0
584+
public Converter<Object,Object> findSerializationConverter(Annotated ann)
585+
{
586+
return _createConverter(ann,
587+
getAnnotationIntrospector().findSerializationConverter(this, ann));
588+
}
574589
}

src/main/java/tools/jackson/databind/cfg/MapperConfigBase.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import tools.jackson.databind.node.JsonNodeFactory;
1616
import tools.jackson.databind.type.TypeFactory;
1717
import tools.jackson.databind.util.ClassUtil;
18+
import tools.jackson.databind.util.Converter;
1819
import tools.jackson.databind.util.RootNameLookup;
1920

2021
@SuppressWarnings("serial")
@@ -716,4 +717,42 @@ public MixInResolver snapshot() {
716717
public final int mixInCount() {
717718
return _mixIns.localSize();
718719
}
720+
721+
/*
722+
/**********************************************************************
723+
/* Helper methods for implementations
724+
/**********************************************************************
725+
*/
726+
727+
@SuppressWarnings("unchecked")
728+
protected Converter<Object,Object> _createConverter(Annotated annotated,
729+
Object converterDef)
730+
{
731+
if (converterDef == null) {
732+
return null;
733+
}
734+
if (converterDef instanceof Converter<?,?>) {
735+
return (Converter<Object,Object>) converterDef;
736+
}
737+
if (!(converterDef instanceof Class)) {
738+
throw new IllegalStateException("`AnnotationIntrospector` returned `Converter` definition of type "
739+
+ClassUtil.classNameOf(converterDef)+"; expected type `Converter` or `Class<Converter>` instead");
740+
}
741+
Class<?> converterClass = (Class<?>)converterDef;
742+
// there are some known "no class" markers to consider too:
743+
if (converterClass == Converter.None.class || ClassUtil.isBogusClass(converterClass)) {
744+
return null;
745+
}
746+
if (!Converter.class.isAssignableFrom(converterClass)) {
747+
throw new IllegalStateException("AnnotationIntrospector returned `Class<"
748+
+ClassUtil.classNameOf(converterClass)+"`>; expected `Class<Converter>`");
749+
}
750+
HandlerInstantiator hi = getHandlerInstantiator();
751+
Converter<?,?> conv = (hi == null) ? null : hi.converterInstance(this, annotated, converterClass);
752+
if (conv == null) {
753+
conv = (Converter<?,?>) ClassUtil.createInstance(converterClass,
754+
canOverrideAccessModifiers());
755+
}
756+
return (Converter<Object,Object>) conv;
757+
}
719758
}

src/main/java/tools/jackson/databind/deser/DeserializerCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ protected ValueDeserializer<Object> _createDeserializer(DeserializationContext c
346346
}
347347

348348
// Or perhaps a Converter?
349-
Converter<Object,Object> conv = beanDescRef.get().findDeserializationConverter();
349+
Converter<Object,Object> conv = config.findDeserializationConverter(beanDescRef.getClassInfo());
350350
if (conv != null) {
351351
// otherwise need to do bit of introspection
352352
JavaType delegateType = conv.getInputType(ctxt.getTypeFactory());

src/main/java/tools/jackson/databind/introspect/BasicBeanDescription.java

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
import com.fasterxml.jackson.annotation.JsonInclude;
88

99
import tools.jackson.databind.*;
10-
import tools.jackson.databind.cfg.HandlerInstantiator;
1110
import tools.jackson.databind.cfg.MapperConfig;
1211
import tools.jackson.databind.util.Annotations;
1312
import tools.jackson.databind.util.ClassUtil;
14-
import tools.jackson.databind.util.Converter;
1513

1614
/**
1715
* Default {@link BeanDescription} implementation used by Jackson.
@@ -373,30 +371,14 @@ public AnnotatedMethod findMethod(String name, Class<?>[] paramTypes) {
373371
/**********************************************************************
374372
*/
375373

376-
@Deprecated // since 3.0
377-
@Override
378-
public JsonFormat.Value findExpectedFormat()
379-
{
380-
JsonFormat.Value v = _classFormat;
381-
if (v == null) {
382-
// 18-Apr-2018, tatu: Bit unclean but apparently `_config` is `null` for
383-
// a small set of pre-discovered simple types that `BasicClassIntrospector`
384-
// may expose. If so, nothing we can do
385-
v = (_config == null) ? null
386-
: _intr.findFormat(_config, _classInfo);
387-
if (v == null) {
388-
v = JsonFormat.Value.empty();
389-
}
390-
_classFormat = v;
391-
}
392-
return v;
393-
}
394-
395374
@Override
396375
public JsonFormat.Value findExpectedFormat(Class<?> baseType)
397376
{
398377
JsonFormat.Value v0 = _classFormat;
399378
if (v0 == null) { // copied from above
379+
// 18-Apr-2018, tatu: Bit unclean but apparently `_config` is `null` for
380+
// a small set of pre-discovered simple types that `BasicClassIntrospector`
381+
// may expose. If so, nothing we can do
400382
v0 = (_config == null) ? null
401383
: _intr.findFormat(_config, _classInfo);
402384
if (v0 == null) {
@@ -434,12 +416,6 @@ public Class<?>[] findDefaultViews()
434416
/**********************************************************************
435417
*/
436418

437-
@Override
438-
public Converter<Object,Object> findSerializationConverter()
439-
{
440-
return _createConverter(_intr.findSerializationConverter(_config, _classInfo));
441-
}
442-
443419
/**
444420
* Method for determining whether null properties should be written
445421
* out for a Bean of introspected type. This is based on global
@@ -646,54 +622,4 @@ protected AnnotatedAndMetadata<AnnotatedMethod, JsonCreator.Mode> findFactoryMet
646622
}
647623
return null;
648624
}
649-
650-
/*
651-
/**********************************************************************
652-
/* Introspection for deserialization, other
653-
/**********************************************************************
654-
*/
655-
656-
@Override
657-
public Converter<Object,Object> findDeserializationConverter()
658-
{
659-
return _createConverter(_intr
660-
.findDeserializationConverter(_config, _classInfo));
661-
}
662-
663-
/*
664-
/**********************************************************************
665-
/* Helper methods, other
666-
/**********************************************************************
667-
*/
668-
669-
@SuppressWarnings("unchecked")
670-
protected Converter<Object,Object> _createConverter(Object converterDef)
671-
{
672-
if (converterDef == null) {
673-
return null;
674-
}
675-
if (converterDef instanceof Converter<?,?>) {
676-
return (Converter<Object,Object>) converterDef;
677-
}
678-
if (!(converterDef instanceof Class)) {
679-
throw new IllegalStateException("AnnotationIntrospector returned Converter definition of type "
680-
+converterDef.getClass().getName()+"; expected type Converter or Class<Converter> instead");
681-
}
682-
Class<?> converterClass = (Class<?>)converterDef;
683-
// there are some known "no class" markers to consider too:
684-
if (converterClass == Converter.None.class || ClassUtil.isBogusClass(converterClass)) {
685-
return null;
686-
}
687-
if (!Converter.class.isAssignableFrom(converterClass)) {
688-
throw new IllegalStateException("AnnotationIntrospector returned Class "
689-
+converterClass.getName()+"; expected Class<Converter>");
690-
}
691-
HandlerInstantiator hi = _config.getHandlerInstantiator();
692-
Converter<?,?> conv = (hi == null) ? null : hi.converterInstance(_config, _classInfo, converterClass);
693-
if (conv == null) {
694-
conv = (Converter<?,?>) ClassUtil.createInstance(converterClass,
695-
_config.canOverrideAccessModifiers());
696-
}
697-
return (Converter<Object,Object>) conv;
698-
}
699625
}

src/main/java/tools/jackson/databind/ser/BeanSerializerFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public ValueSerializer<Object> createSerializer(SerializationContext ctxt, JavaT
132132
BeanDescription.Supplier beanDescRef, JsonFormat.Value formatOverrides)
133133
{
134134
// Very first thing, let's check if there is explicit serializer annotation:
135-
ValueSerializer<?> ser = findSerializerFromAnnotation(ctxt, beanDescRef.getClassInfo());
135+
ValueSerializer<?> ser = findSerializerFromAnnotation(ctxt,
136+
beanDescRef.getClassInfo());
136137
if (ser != null) {
137138
return (ValueSerializer<Object>) ser;
138139
}
@@ -160,7 +161,7 @@ public ValueSerializer<Object> createSerializer(SerializationContext ctxt, JavaT
160161
}
161162
}
162163
// Slight detour: do we have a Converter to consider?
163-
Converter<Object,Object> conv = beanDescRef.get().findSerializationConverter();
164+
Converter<Object,Object> conv = config.findSerializationConverter(beanDescRef.getClassInfo());
164165
if (conv != null) { // yup, need converter
165166
JavaType delegateType = conv.getOutputType(ctxt.getTypeFactory());
166167

0 commit comments

Comments
 (0)