|
| 1 | +package com.fasterxml.jackson.module.spisubtypes; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.ServiceLoader; |
| 7 | +import java.util.concurrent.ConcurrentHashMap; |
| 8 | +import java.util.function.Function; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.core.Version; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.databind.AnnotationIntrospector; |
| 13 | +import com.fasterxml.jackson.databind.introspect.Annotated; |
| 14 | +import com.fasterxml.jackson.databind.jsontype.NamedType; |
| 15 | + |
| 16 | +/** |
| 17 | + * Annotation introspector that handles {@link JacksonSubType} annotation. |
| 18 | + * <p> |
| 19 | + * It caches the subclasses of a parent class, so it's not-real-time. |
| 20 | + * When the parent class not found in cache, |
| 21 | + * it will try to load all found child classes via SPI then cache it. |
| 22 | + * </p> |
| 23 | + * |
| 24 | + * @since 2.21 / 3.1 |
| 25 | + */ |
| 26 | +public class SubtypesAnnotationIntrospector extends AnnotationIntrospector { |
| 27 | + private final ConcurrentHashMap<Class<?>, List<NamedType>> subtypes = new ConcurrentHashMap<>(); |
| 28 | + |
| 29 | + @Override |
| 30 | + public Version version() { |
| 31 | + return PackageVersion.VERSION; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public List<NamedType> findSubtypes(Annotated a) { |
| 36 | + registerTypes(a.getRawType()); |
| 37 | + |
| 38 | + List<NamedType> list1 = _findSubtypes(a.getRawType(), a::getAnnotation); |
| 39 | + List<NamedType> list2 = subtypes.getOrDefault(a.getRawType(), Collections.emptyList()); |
| 40 | + |
| 41 | + if (list1.isEmpty()) return list2; |
| 42 | + if (list2.isEmpty()) return list1; |
| 43 | + List<NamedType> list = new ArrayList<>(list1.size() + list2.size()); |
| 44 | + list.addAll(list1); |
| 45 | + list.addAll(list2); |
| 46 | + return list; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * load parent's subclass by SPI. |
| 51 | + * |
| 52 | + * @param parent parent class. |
| 53 | + * @param <S> parent class type. |
| 54 | + */ |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + private <S> void registerTypes(Class<S> parent) { |
| 57 | + // If parent is already registered (either by spi or manually by the user), then skip it |
| 58 | + if (subtypes.containsKey(parent)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + List<NamedType> result = new ArrayList<>(); |
| 62 | + for (S instance : ServiceLoader.load(parent)) { |
| 63 | + Class<S> subclass = (Class<S>) instance.getClass(); |
| 64 | + result.addAll(_findSubtypes(subclass, subclass::getAnnotation)); |
| 65 | + } |
| 66 | + subtypes.put(parent, result); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * find all {@link JacksonSubType} names. |
| 71 | + * |
| 72 | + * @param clazz class which annotate with {@link JacksonSubType}. |
| 73 | + * @param getter getAnnotation. |
| 74 | + * @param <S> class type. |
| 75 | + * @return all names. |
| 76 | + */ |
| 77 | + private <S> List<NamedType> _findSubtypes(Class<S> clazz, Function<Class<JacksonSubType>, JacksonSubType> getter) { |
| 78 | + if (clazz == null) { |
| 79 | + return Collections.emptyList(); |
| 80 | + } |
| 81 | + JacksonSubType subtype = getter.apply(JacksonSubType.class); |
| 82 | + if (subtype == null) { |
| 83 | + return Collections.emptyList(); |
| 84 | + } |
| 85 | + List<NamedType> result = new ArrayList<>(); |
| 86 | + result.add(new NamedType(clazz, subtype.value())); |
| 87 | + // [databind#2761]: alternative set of names to use |
| 88 | + for (String name : subtype.names()) { |
| 89 | + result.add(new NamedType(clazz, name)); |
| 90 | + } |
| 91 | + return result; |
| 92 | + } |
| 93 | +} |
0 commit comments