|
| 1 | +package com.fasterxml.jackson.databind.introspect; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.*; |
| 11 | +import com.fasterxml.jackson.databind.cfg.MapperConfig; |
| 12 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 13 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | + |
| 17 | +// Tests for [databind#4620]: |
| 18 | +// |
| 19 | +// @since 2.18 |
| 20 | +public class DefaultCreatorResolution4620Test extends DatabindTestUtil |
| 21 | +{ |
| 22 | + static class PrimaryCreatorFindingIntrospector extends ImplicitNameIntrospector |
| 23 | + { |
| 24 | + private static final long serialVersionUID = 1L; |
| 25 | + |
| 26 | + private final Class<?>[] _argTypes; |
| 27 | + |
| 28 | + private JsonCreator.Mode _mode; |
| 29 | + |
| 30 | + private final String _factoryName; |
| 31 | + |
| 32 | + public PrimaryCreatorFindingIntrospector(JsonCreator.Mode mode, |
| 33 | + Class<?>... argTypes) { |
| 34 | + _mode = mode; |
| 35 | + _factoryName = null; |
| 36 | + _argTypes = argTypes; |
| 37 | + } |
| 38 | + |
| 39 | + public PrimaryCreatorFindingIntrospector(JsonCreator.Mode mode, |
| 40 | + String factoryName) { |
| 41 | + _mode = mode; |
| 42 | + _factoryName = factoryName; |
| 43 | + _argTypes = new Class<?>[0]; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public PotentialCreator findDefaultCreator(MapperConfig<?> config, |
| 48 | + AnnotatedClass valueClass, |
| 49 | + List<PotentialCreator> declaredConstructors, |
| 50 | + List<PotentialCreator> declaredFactories) |
| 51 | + { |
| 52 | + // Apply to all test POJOs here but nothing else |
| 53 | + if (!valueClass.getRawType().toString().contains("4584")) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + if (_factoryName != null) { |
| 58 | + for (PotentialCreator ctor : declaredFactories) { |
| 59 | + if (ctor.creator().getName().equals(_factoryName)) { |
| 60 | + return ctor; |
| 61 | + } |
| 62 | + } |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + List<PotentialCreator> combo = new ArrayList<>(declaredConstructors); |
| 67 | + combo.addAll(declaredFactories); |
| 68 | + final int argCount = _argTypes.length; |
| 69 | + for (PotentialCreator ctor : combo) { |
| 70 | + if (ctor.paramCount() == argCount) { |
| 71 | + int i = 0; |
| 72 | + for (; i < argCount; ++i) { |
| 73 | + if (_argTypes[i] != ctor.param(i).getRawType()) { |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + if (i == argCount) { |
| 78 | + ctor.overrideMode(_mode); |
| 79 | + return ctor; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /* |
| 88 | + /********************************************************************** |
| 89 | + /* Test methods; simple properties-based Creators |
| 90 | + /********************************************************************** |
| 91 | + */ |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testCanonicalConstructor1ArgPropertiesCreator() throws Exception |
| 95 | + { |
| 96 | + // TODO |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + /********************************************************************** |
| 101 | + /* Helper methods |
| 102 | + /********************************************************************** |
| 103 | + */ |
| 104 | + |
| 105 | + /* |
| 106 | + private ObjectReader readerWith(AnnotationIntrospector intr) { |
| 107 | + return mapperWith(intr).readerFor(POJO4584.class); |
| 108 | + } |
| 109 | + */ |
| 110 | + |
| 111 | + private ObjectMapper mapperWith(AnnotationIntrospector intr) { |
| 112 | + return JsonMapper.builder() |
| 113 | + .annotationIntrospector(intr) |
| 114 | + .build(); |
| 115 | + } |
| 116 | +} |
0 commit comments