1 package com.fasterxml.jackson.databind.jsontype; 2 3 import java.util.Collection; 4 5 import com.fasterxml.jackson.databind.AnnotationIntrospector; 6 import com.fasterxml.jackson.databind.JavaType; 7 import com.fasterxml.jackson.databind.cfg.MapperConfig; 8 import com.fasterxml.jackson.databind.introspect.AnnotatedClass; 9 import com.fasterxml.jackson.databind.introspect.AnnotatedMember; 10 11 /** 12 * Helper object used for handling registration on resolving of super-types 13 * to sub-types. 14 */ 15 public abstract class SubtypeResolver 16 { 17 /** 18 * Method called by {@code ObjectMapper.copy()} to make sure that 19 * {@link SubtypeResolver} instances used by two independent mappers 20 * can not cause thread-safety issues: if resolver is immutable, it 21 * may return {@code this}, but if not, it should create a copy with 22 * same configuration and return that instead. 23 * 24 * @return Either new instance with same configuration as this one (if 25 * instances are mutable), or this instance (if immutable) 26 * 27 * @since 2.12 28 */ copy()29 public SubtypeResolver copy() { 30 return this; 31 } 32 33 /* 34 /********************************************************** 35 /* Methods for registering external subtype definitions (init/config) 36 /********************************************************** 37 */ 38 39 /** 40 * Method for registering specified subtypes (possibly including type 41 * names); for type entries without name, non-qualified class name 42 * as used as name (unless overridden by annotation). 43 */ registerSubtypes(NamedType... types)44 public abstract void registerSubtypes(NamedType... types); 45 registerSubtypes(Class<?>.... classes)46 public abstract void registerSubtypes(Class<?>... classes); 47 48 /** 49 * @since 2.9 50 */ registerSubtypes(Collection<Class<?>> subtypes)51 public abstract void registerSubtypes(Collection<Class<?>> subtypes); 52 53 /* 54 /********************************************************** 55 /* Subtype resolution (public API) 56 /********************************************************** 57 */ 58 59 /** 60 * Method for finding out all reachable subtypes for a property specified 61 * by given element (method or field), 62 * such that access is by type, 63 * typically needed for serialization (converting from type to type name). 64 * 65 * @param baseType Effective property base type to use; may differ from 66 * actual type of property; for structured types it is content (value) type and NOT 67 * structured type. 68 * 69 * @since 2.6 70 */ collectAndResolveSubtypesByClass(MapperConfig<?> config, AnnotatedMember property, JavaType baseType)71 public Collection<NamedType> collectAndResolveSubtypesByClass(MapperConfig<?> config, 72 AnnotatedMember property, JavaType baseType) { 73 // for backwards compatibility... 74 return collectAndResolveSubtypes(property, config, 75 config.getAnnotationIntrospector(), baseType); 76 } 77 78 /** 79 * Method for finding out all reachable subtypes for given type, 80 * such that access is by type, 81 * typically needed for serialization (converting from type to type name). 82 * 83 * @param baseType Effective property base type to use; may differ from 84 * actual type of property; for structured types it is content (value) type and NOT 85 * structured type. 86 * 87 * @since 2.6 88 */ collectAndResolveSubtypesByClass(MapperConfig<?> config, AnnotatedClass baseType)89 public Collection<NamedType> collectAndResolveSubtypesByClass(MapperConfig<?> config, 90 AnnotatedClass baseType) { 91 // for backwards compatibility... 92 return collectAndResolveSubtypes(baseType, config, config.getAnnotationIntrospector()); 93 } 94 95 /** 96 * Method for finding out all reachable subtypes for a property specified 97 * by given element (method or field), 98 * such that access is by type id, 99 * typically needed for deserialization (converting from type id to type). 100 * 101 * @param baseType Effective property base type to use; may differ from 102 * actual type of property; for structured types it is content (value) type and NOT 103 * structured type. 104 * 105 * @since 2.6 106 */ collectAndResolveSubtypesByTypeId(MapperConfig<?> config, AnnotatedMember property, JavaType baseType)107 public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config, 108 AnnotatedMember property, JavaType baseType) { 109 // for backwards compatibility... 110 return collectAndResolveSubtypes(property, config, 111 config.getAnnotationIntrospector(), baseType); 112 } 113 114 /** 115 * Method for finding out all reachable subtypes for given type, 116 * such that access is by type id, 117 * typically needed for deserialization (converting from type id to type). 118 * 119 * @param baseType Effective property base type to use; may differ from 120 * actual type of property; for structured types it is content (value) type and NOT 121 * structured type. 122 * 123 * @since 2.6 124 */ collectAndResolveSubtypesByTypeId(MapperConfig<?> config, AnnotatedClass baseType)125 public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config, 126 AnnotatedClass baseType) { 127 // for backwards compatibility... 128 return collectAndResolveSubtypes(baseType, config, config.getAnnotationIntrospector()); 129 } 130 131 /* 132 /********************************************************** 133 /* Deprecated methods 134 /********************************************************** 135 */ 136 137 /** 138 * @deprecated Since 2.6 Use either 139 * {@link #collectAndResolveSubtypesByClass(MapperConfig, AnnotatedMember, JavaType)} 140 * or {@link #collectAndResolveSubtypesByTypeId(MapperConfig, AnnotatedMember, JavaType)} 141 * instead. 142 */ 143 @Deprecated collectAndResolveSubtypes(AnnotatedMember property, MapperConfig<?> config, AnnotationIntrospector ai, JavaType baseType)144 public Collection<NamedType> collectAndResolveSubtypes(AnnotatedMember property, 145 MapperConfig<?> config, AnnotationIntrospector ai, JavaType baseType) { 146 return collectAndResolveSubtypesByClass(config, property, baseType); 147 } 148 149 /** 150 * @deprecated Since 2.6 Use either 151 * {@link #collectAndResolveSubtypesByClass(MapperConfig, AnnotatedClass)} 152 * or {@link #collectAndResolveSubtypesByTypeId(MapperConfig, AnnotatedClass)} 153 * instead. 154 */ 155 @Deprecated collectAndResolveSubtypes(AnnotatedClass baseType, MapperConfig<?> config, AnnotationIntrospector ai)156 public Collection<NamedType> collectAndResolveSubtypes(AnnotatedClass baseType, 157 MapperConfig<?> config, AnnotationIntrospector ai) { 158 return collectAndResolveSubtypesByClass(config, baseType); 159 } 160 } 161