|
2 | 2 |
|
3 | 3 | import java.lang.reflect.Constructor;
|
4 | 4 | import java.lang.reflect.Method;
|
| 5 | +import java.util.List; |
5 | 6 |
|
| 7 | +import com.fasterxml.jackson.annotation.JsonCreator; |
6 | 8 | import com.fasterxml.jackson.databind.*;
|
7 | 9 | import com.fasterxml.jackson.databind.deser.KeyDeserializers;
|
| 10 | +import com.fasterxml.jackson.databind.introspect.AnnotatedAndMetadata; |
| 11 | +import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor; |
| 12 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; |
8 | 13 | import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
|
9 | 14 | import com.fasterxml.jackson.databind.util.ClassUtil;
|
10 | 15 | import com.fasterxml.jackson.databind.util.EnumResolver;
|
@@ -47,36 +52,123 @@ public static KeyDeserializer constructDelegatingKeyDeserializer(Deserialization
|
47 | 52 |
|
48 | 53 | public static KeyDeserializer findStringBasedKeyDeserializer(DeserializationConfig config,
|
49 | 54 | JavaType type)
|
50 |
| - { |
| 55 | + throws JsonMappingException |
| 56 | + { |
| 57 | + // 15-Jun-2021, tatu: As per [databind#3143], full introspection needs to consider |
| 58 | + // as set of possibilities. Basically, precedence is: |
| 59 | + // |
| 60 | + // 1. Explicitly annotated 1-String-arg constructor, if one exists |
| 61 | + // 2. Explicitly annotated Factory method: just one allowed (exception if multiple) |
| 62 | + // 3. Implicit 1-String-arg constructor (no visibility checks for backwards |
| 63 | + // compatibility reasons; should probably be checked in future, 3.0?) |
| 64 | + // 4. Implicit Factory method with name of "valueOf()" (primary) or |
| 65 | + // "fromString()" (secondary). Likewise, no visibility check as of yet. |
| 66 | + |
51 | 67 | // We don't need full deserialization information, just need to know creators.
|
52 |
| - BeanDescription beanDesc = config.introspect(type); |
| 68 | + final BeanDescription beanDesc = config.introspectForCreation(type); |
53 | 69 | // Ok, so: can we find T(String) constructor?
|
54 |
| - Constructor<?> ctor = beanDesc.findSingleArgConstructor(String.class); |
55 |
| - if (ctor != null) { |
56 |
| - if (config.canOverrideAccessModifiers()) { |
57 |
| - ClassUtil.checkAndFixAccess(ctor, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); |
58 |
| - } |
59 |
| - return new StdKeyDeserializer.StringCtorKeyDeserializer(ctor); |
| 70 | + final AnnotatedAndMetadata<AnnotatedConstructor, JsonCreator.Mode> ctorInfo = _findStringConstructor(beanDesc); |
| 71 | + // Explicit? |
| 72 | + if ((ctorInfo != null) && (ctorInfo.metadata != null)) { |
| 73 | + return _constructCreatorKeyDeserializer(config, ctorInfo.annotated); |
60 | 74 | }
|
61 | 75 | // or if not, "static T valueOf(String)" (or equivalent marked
|
62 | 76 | // with @JsonCreator annotation?)
|
63 |
| - Method m = beanDesc.findFactoryMethod(String.class); |
64 |
| - if (m != null){ |
| 77 | + final List<AnnotatedAndMetadata<AnnotatedMethod, JsonCreator.Mode>> factoryCandidates |
| 78 | + = beanDesc.getFactoryMethodsWithMode(); |
| 79 | + |
| 80 | + // But must now filter out invalid candidates, both by signature (must take 1 and |
| 81 | + // only 1 arg; that arg must be of type `String`) and by annotations (we only |
| 82 | + // accept "delegating" style, so remove PROPERTIES) |
| 83 | + factoryCandidates.removeIf(m -> |
| 84 | + (m.annotated.getParameterCount() != 1) |
| 85 | + || (m.annotated.getRawParameterType(0) != String.class) |
| 86 | + || (m.metadata == JsonCreator.Mode.PROPERTIES) |
| 87 | + ); |
| 88 | + |
| 89 | + // Any explicit? |
| 90 | + final AnnotatedMethod explicitFactory = _findExplicitStringFactoryMethod(factoryCandidates); |
| 91 | + if (explicitFactory != null) { |
| 92 | + return _constructCreatorKeyDeserializer(config, explicitFactory); |
| 93 | + } |
| 94 | + // If we had implicit Constructor, that'd work now |
| 95 | + if (ctorInfo != null) { |
| 96 | + return _constructCreatorKeyDeserializer(config, ctorInfo.annotated); |
| 97 | + } |
| 98 | + // And finally, if any implicit factory methods, acceptable now |
| 99 | + // nope, no such luck... |
| 100 | + if (!factoryCandidates.isEmpty()) { |
| 101 | + // 15-Jun-2021, tatu: Ideally we would provide stabler ordering, but for now |
| 102 | + // let's simply pick the first one |
| 103 | + return _constructCreatorKeyDeserializer(config, factoryCandidates.get(0).annotated); |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + private static KeyDeserializer _constructCreatorKeyDeserializer(DeserializationConfig config, |
| 109 | + AnnotatedMember creator) |
| 110 | + { |
| 111 | + if (creator instanceof AnnotatedConstructor) { |
| 112 | + Constructor<?> rawCtor = ((AnnotatedConstructor) creator).getAnnotated(); |
65 | 113 | if (config.canOverrideAccessModifiers()) {
|
66 |
| - ClassUtil.checkAndFixAccess(m, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); |
| 114 | + ClassUtil.checkAndFixAccess(rawCtor, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); |
| 115 | + } |
| 116 | + return new StdKeyDeserializer.StringCtorKeyDeserializer(rawCtor); |
| 117 | + } |
| 118 | + Method m = ((AnnotatedMethod) creator).getAnnotated(); |
| 119 | + if (config.canOverrideAccessModifiers()) { |
| 120 | + ClassUtil.checkAndFixAccess(m, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); |
| 121 | + } |
| 122 | + return new StdKeyDeserializer.StringFactoryKeyDeserializer(m); |
| 123 | + } |
| 124 | + |
| 125 | + // 13-Jun-2021, tatu: For now just look for constructor that takes one `String` |
| 126 | + // argument (could look for CharSequence) and hence can have just one, no dups |
| 127 | + private static AnnotatedAndMetadata<AnnotatedConstructor, JsonCreator.Mode> _findStringConstructor(BeanDescription beanDesc) |
| 128 | + { |
| 129 | + for (AnnotatedAndMetadata<AnnotatedConstructor, JsonCreator.Mode> entry |
| 130 | + : beanDesc.getConstructorsWithMode()) |
| 131 | + { |
| 132 | + // BeanDescription method does NOT filter out various types so check |
| 133 | + // it takes single argument. |
| 134 | + final AnnotatedConstructor ctor = entry.annotated; |
| 135 | + if ((ctor.getParameterCount() == 1) |
| 136 | + && (String.class == ctor.getRawParameterType(0))) { |
| 137 | + return entry; |
67 | 138 | }
|
68 |
| - return new StdKeyDeserializer.StringFactoryKeyDeserializer(m); |
69 | 139 | }
|
70 |
| - // nope, no such luck... |
71 | 140 | return null;
|
72 | 141 | }
|
73 |
| - |
| 142 | + |
| 143 | + private static AnnotatedMethod _findExplicitStringFactoryMethod( |
| 144 | + List<AnnotatedAndMetadata<AnnotatedMethod, JsonCreator.Mode>> candidates) |
| 145 | + throws JsonMappingException |
| 146 | + { |
| 147 | + AnnotatedMethod match = null; |
| 148 | + for (AnnotatedAndMetadata<AnnotatedMethod, JsonCreator.Mode> entry : candidates) { |
| 149 | + // Note: caller has filtered out invalid candidates; all we need to check are dups |
| 150 | + if (entry.metadata != null) { |
| 151 | + if (match == null) { |
| 152 | + match = entry.annotated; |
| 153 | + } else { |
| 154 | + // 15-Jun-2021, tatu: Not optimal type or information, but has to do for now |
| 155 | + // since we do not get DeserializationContext |
| 156 | + Class<?> rawKeyType = entry.annotated.getDeclaringClass(); |
| 157 | + throw new IllegalArgumentException( |
| 158 | +"Multiple suitable annotated Creator factory methods to be used as the Key deserializer for type " |
| 159 | + +ClassUtil.nameOf(rawKeyType)); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + return match; |
| 164 | + } |
| 165 | + |
74 | 166 | /*
|
75 | 167 | /**********************************************************
|
76 | 168 | /* KeyDeserializers implementation
|
77 | 169 | /**********************************************************
|
78 | 170 | */
|
79 |
| - |
| 171 | + |
80 | 172 | @Override
|
81 | 173 | public KeyDeserializer findKeyDeserializer(JavaType type,
|
82 | 174 | DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException
|
|
0 commit comments