protected String _subTypeName(NamedType type) { return type.getType().getName(); }
protected ValueReferenceTypeIdResolver(JavaType baseType, TypeFactory typeFactory, Collection<NamedType> subtypes) { super(baseType, typeFactory); this.subtypes = subtypes.stream().collect(Collectors.toMap(NamedType::getName, v -> typeFactory.constructSimpleType(v.getType(), new JavaType[0]))); }
private NamedType resolveAutoValue(NamedType namedType) { final Class<?> cls = namedType.getType(); if (cls.getSimpleName().startsWith("AutoValue_")) { return new NamedType(cls.getSuperclass(), namedType.getName()); } else { return namedType; } } }
/** * Helper method used for merging explicitly named types and handled classes * without explicit names. */ protected Collection<NamedType> _combineNamedAndUnnamed(Class<?> rawBase, Set<Class<?>> typesHandled, Map<String,NamedType> byName) { ArrayList<NamedType> result = new ArrayList<NamedType>(byName.values()); // Ok, so... we will figure out which classes have no explicitly assigned name, // by removing Classes from Set. And for remaining classes, add an anonymous // marker for (NamedType t : byName.values()) { typesHandled.remove(t.getType()); } for (Class<?> cls : typesHandled) { // 27-Apr-2017, tatu: [databind#1616] Do not add base type itself unless // it is concrete (or has explicit type name) if ((cls == rawBase) && Modifier.isAbstract(cls.getModifiers())) { continue; } result.add(new NamedType(cls)); } return result; } }
@Override public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config, AnnotatedClass baseType) { final Class<?> rawBase = baseType.getRawType(); Set<Class<?>> typesHandled = new HashSet<Class<?>>(); Map<String,NamedType> byName = new LinkedHashMap<String,NamedType>(); NamedType rootType = new NamedType(rawBase, null); _collectAndResolveByTypeId(baseType, rootType, config, typesHandled, byName); if (_registeredSubtypes != null) { for (NamedType subtype : _registeredSubtypes) { // is it a subtype of root type? if (rawBase.isAssignableFrom(subtype.getType())) { // yes AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config, subtype.getType()); _collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName); } } } return _combineNamedAndUnnamed(rawBase, typesHandled, byName); }
@Override public Collection<NamedType> collectAndResolveSubtypesByClass(MapperConfig<?> config, AnnotatedClass type) { final AnnotationIntrospector ai = config.getAnnotationIntrospector(); HashMap<NamedType, NamedType> subtypes = new HashMap<NamedType, NamedType>(); // then consider registered subtypes (which have precedence over annotations) if (_registeredSubtypes != null) { Class<?> rawBase = type.getRawType(); for (NamedType subtype : _registeredSubtypes) { // is it a subtype of root type? if (rawBase.isAssignableFrom(subtype.getType())) { // yes AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config, subtype.getType()); _collectAndResolve(curr, subtype, config, ai, subtypes); } } } // and then check subtypes via annotations from base type (recursively) NamedType rootType = new NamedType(type.getRawType(), null); _collectAndResolve(type, rootType, config, ai, subtypes); return new ArrayList<NamedType>(subtypes.values()); }
Class<?> cls = t.getType(); String id = t.hasName() ? t.getName() : _defaultTypeId(cls); if (forSer) {
/** * Method called to find subtypes for a specific type (class), using * type id as the unique key (in case of conflicts). */ protected void _collectAndResolveByTypeId(AnnotatedClass annotatedType, NamedType namedType, MapperConfig<?> config, Set<Class<?>> typesHandled, Map<String,NamedType> byName) { final AnnotationIntrospector ai = config.getAnnotationIntrospector(); if (!namedType.hasName()) { String name = ai.findTypeName(annotatedType); if (name != null) { namedType = new NamedType(namedType.getType(), name); } } if (namedType.hasName()) { byName.put(namedType.getName(), namedType); } // only check subtypes if this type hadn't yet been handled if (typesHandled.add(namedType.getType())) { Collection<NamedType> st = ai.findSubtypes(annotatedType); if (st != null && !st.isEmpty()) { for (NamedType subtype : st) { AnnotatedClass subtypeClass = AnnotatedClassResolver.resolveWithoutSuperTypes(config, subtype.getType()); _collectAndResolveByTypeId(subtypeClass, subtype, config, typesHandled, byName); } } } }
String name = ai.findTypeName(annotatedType); if (name != null) { namedType = new NamedType(namedType.getType(), name); for (NamedType subtype : st) { AnnotatedClass subtypeClass = AnnotatedClassResolver.resolveWithoutSuperTypes(config, subtype.getType()); _collectAndResolve(subtypeClass, subtype, config, ai, collectedSubtypes);
for (NamedType subtype : _registeredSubtypes) { if (rawBase.isAssignableFrom(subtype.getType())) { // yes AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config, subtype.getType()); _collectAndResolve(curr, subtype, config, ai, collected); for (NamedType nt : st) { AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, nt.getType()); _collectAndResolve(ac, nt, config, ai, collected);
private boolean isSubtype(AnnotatedClass childClass, Class<?> parentClass) { final BeanDescription parentDesc = _mapper.getSerializationConfig().introspectClassAnnotations(parentClass); List<NamedType> subTypes =_intr.findSubtypes(parentDesc.getClassInfo()); if (subTypes == null) { return false; } for (NamedType subtype : subTypes) { final Class<?> subtypeType = subtype.getType(); if (childClass.getRawType().isAssignableFrom(subtypeType)) { return true; } } return false; }
if (st != null) { for (NamedType nt : st) { ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, nt.getType()); _collectAndResolveByTypeId(ac, nt, config, typesHandled, byName); for (NamedType subtype : _registeredSubtypes) { if (rawBase.isAssignableFrom(subtype.getType())) { // yes AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config, subtype.getType()); _collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
final Class<?> beanClass = bean.getClassInfo().getAnnotated(); for (NamedType subtype : types) { final Class<?> subtypeType = subtype.getType(); if (!beanClass.isAssignableFrom(subtypeType)) { continue; Class<?> currentType = subtype.getType(); if (StringUtils.isNotBlank(composedSchema.getName())) { context.defineModel(composedSchema.getName(), composedSchema, new AnnotatedType().type(currentType), null);
protected String _subTypeName(NamedType type) { return type.getType().getName(); }
/** * Helper method used for merging explicitly named types and handled classes * without explicit names. */ protected Collection<NamedType> _combineNamedAndUnnamed(Set<Class<?>> typesHandled, Map<String,NamedType> byName) { ArrayList<NamedType> result = new ArrayList<NamedType>(byName.values()); // Ok, so... we will figure out which classes have no explicitly assigned name, // by removing Classes from Set. And for remaining classes, add an anonymous // marker for (NamedType t : byName.values()) { typesHandled.remove(t.getType()); } for (Class<?> cls : typesHandled) { result.add(new NamedType(cls)); } return result; } }
public AvroTypeIdResolver(JavaType baseType, TypeFactory typeFactory, Collection<NamedType> subTypes) { this(baseType, typeFactory); if (subTypes != null) { for (NamedType namedType : subTypes) { registerSubtype(namedType.getType(), namedType.getName()); } } }
@Override public Collection<NamedType> collectAndResolveSubtypes(AnnotatedClass type, MapperConfig<?> config, AnnotationIntrospector ai) { HashMap<NamedType, NamedType> subtypes = new HashMap<NamedType, NamedType>(); // [JACKSON-257] then consider registered subtypes (which have precedence over annotations) if (_registeredSubtypes != null) { Class<?> rawBase = type.getRawType(); for (NamedType subtype : _registeredSubtypes) { // is it a subtype of root type? if (rawBase.isAssignableFrom(subtype.getType())) { // yes AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), ai, config); _collectAndResolve(curr, subtype, config, ai, subtypes); } } } // and then check subtypes via annotations from base type (recursively) NamedType rootType = new NamedType(type.getRawType(), null); _collectAndResolve(type, rootType, config, ai, subtypes); return new ArrayList<NamedType>(subtypes.values()); }
public static List<Class> getSubtypeClasses(ObjectMapper mapper, Class c) { return getSubtypeClasses(mapper.getDeserializationConfig(), c); } public static List<Class> getSubtypeClasses(MapperConfig config, Class c) { AnnotationIntrospector ai = config.getAnnotationIntrospector(); AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(c, ai, config); List<NamedType> subtypes = ai.findSubtypes(ac); List<Class> result = new ArrayList<>(); if (subtypes != null) { for (NamedType subtype : subtypes) { result.add(subtype.getType()); } } return result; }
@Override public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config, AnnotatedClass type) { Set<Class<?>> typesHandled = new HashSet<Class<?>>(); Map<String,NamedType> byName = new LinkedHashMap<String,NamedType>(); NamedType rootType = new NamedType(type.getRawType(), null); _collectAndResolveByTypeId(type, rootType, config, typesHandled, byName); if (_registeredSubtypes != null) { Class<?> rawBase = type.getRawType(); for (NamedType subtype : _registeredSubtypes) { // is it a subtype of root type? if (rawBase.isAssignableFrom(subtype.getType())) { // yes AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config); _collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName); } } } return _combineNamedAndUnnamed(typesHandled, byName); }
private boolean isSubtype(AnnotatedClass childClass, Class<?> parentClass) { final BeanDescription parentDesc = _mapper.getSerializationConfig().introspectClassAnnotations(parentClass); List<NamedType> subTypes =_intr.findSubtypes(parentDesc.getClassInfo()); if (subTypes == null) { return false; } for (NamedType subtype : subTypes) { final Class<?> subtypeType = subtype.getType(); if (childClass.getRawType().isAssignableFrom(subtypeType)) { return true; } } return false; }