1 package com.fasterxml.jackson.databind.type;
2
3 import java.util.*;
4 import java.util.concurrent.atomic.AtomicReference;
5 import java.lang.reflect.*;
6
7 import com.fasterxml.jackson.core.type.TypeReference;
8 import com.fasterxml.jackson.databind.JavaType;
9 import com.fasterxml.jackson.databind.JsonNode;
10 import com.fasterxml.jackson.databind.util.ArrayBuilders;
11 import com.fasterxml.jackson.databind.util.ClassUtil;
12 import com.fasterxml.jackson.databind.util.LRUMap;
13
14 /**
15 * Class used for creating concrete {@link JavaType} instances,
16 * given various inputs.
17 *<p>
18 * Instances of this class are accessible using {@link com.fasterxml.jackson.databind.ObjectMapper}
19 * as well as many objects it constructs (like
20 * {@link com.fasterxml.jackson.databind.DeserializationConfig} and
21 * {@link com.fasterxml.jackson.databind.SerializationConfig})),
22 * but usually those objects also
23 * expose convenience methods (<code>constructType</code>).
24 * So, you can do for example:
25 *<pre>
26 * JavaType stringType = mapper.constructType(String.class);
27 *</pre>
28 * However, more advanced methods are only exposed by factory so that you
29 * may need to use:
30 *<pre>
31 * JavaType stringCollection = mapper.getTypeFactory().constructCollectionType(List.class, String.class);
32 *</pre>
33 */
34 @SuppressWarnings({"rawtypes" })
35 public class TypeFactory // note: was final in 2.9, removed from 2.10
36 implements java.io.Serializable
37 {
38 private static final long serialVersionUID = 1L;
39
40 private final static JavaType[] NO_TYPES = new JavaType[0];
41
42 /**
43 * Globally shared singleton. Not accessed directly; non-core
44 * code should use per-ObjectMapper instance (via configuration objects).
45 * Core Jackson code uses {@link #defaultInstance} for accessing it.
46 */
47 protected final static TypeFactory instance = new TypeFactory();
48
49 protected final static TypeBindings EMPTY_BINDINGS = TypeBindings.emptyBindings();
50
51 /*
52 /**********************************************************
53 /* Constants for "well-known" classes
54 /**********************************************************
55 */
56
57 // // // Let's assume that a small set of core primitive/basic types
58 // // // will not be modified, and can be freely shared to streamline
59 // // // parts of processing
60
61 private final static Class<?> CLS_STRING = String.class;
62 private final static Class<?> CLS_OBJECT = Object.class;
63
64 private final static Class<?> CLS_COMPARABLE = Comparable.class;
65 private final static Class<?> CLS_CLASS = Class.class;
66 private final static Class<?> CLS_ENUM = Enum.class;
67 private final static Class<?> CLS_JSON_NODE = JsonNode.class; // since 2.10
68
69 private final static Class<?> CLS_BOOL = Boolean.TYPE;
70 private final static Class<?> CLS_INT = Integer.TYPE;
71 private final static Class<?> CLS_LONG = Long.TYPE;
72
73 /*
74 /**********************************************************
75 /* Cached pre-constructed JavaType instances
76 /**********************************************************
77 */
78
79 // note: these are primitive, hence no super types
80 protected final static SimpleType CORE_TYPE_BOOL = new SimpleType(CLS_BOOL);
81 protected final static SimpleType CORE_TYPE_INT = new SimpleType(CLS_INT);
82 protected final static SimpleType CORE_TYPE_LONG = new SimpleType(CLS_LONG);
83
84 // and as to String... well, for now, ignore its super types
85 protected final static SimpleType CORE_TYPE_STRING = new SimpleType(CLS_STRING);
86
87 // @since 2.7
88 protected final static SimpleType CORE_TYPE_OBJECT = new SimpleType(CLS_OBJECT);
89
90 /**
91 * Cache {@link Comparable} because it is both parameteric (relatively costly to
92 * resolve) and mostly useless (no special handling), better handle directly
93 *
94 * @since 2.7
95 */
96 protected final static SimpleType CORE_TYPE_COMPARABLE = new SimpleType(CLS_COMPARABLE);
97
98 /**
99 * Cache {@link Enum} because it is parametric AND self-referential (costly to
100 * resolve) and useless in itself (no special handling).
101 *
102 * @since 2.7
103 */
104 protected final static SimpleType CORE_TYPE_ENUM = new SimpleType(CLS_ENUM);
105
106 /**
107 * Cache {@link Class} because it is nominally parametric, but has no really
108 * useful information.
109 *
110 * @since 2.7
111 */
112 protected final static SimpleType CORE_TYPE_CLASS = new SimpleType(CLS_CLASS);
113
114 /**
115 * Cache {@link JsonNode} because it is no critical path of simple tree model
116 * reading and does not have things to override
117 *
118 * @since 2.10
119 */
120 protected final static SimpleType CORE_TYPE_JSON_NODE = new SimpleType(CLS_JSON_NODE);
121
122 /**
123 * Since type resolution can be expensive (specifically when resolving
124 * actual generic types), we will use small cache to avoid repetitive
125 * resolution of core types
126 */
127 protected final LRUMap<Object,JavaType> _typeCache;
128
129 /*
130 /**********************************************************
131 /* Configuration
132 /**********************************************************
133 */
134
135 /**
136 * Registered {@link TypeModifier}s: objects that can change details
137 * of {@link JavaType} instances factory constructs.
138 */
139 protected final TypeModifier[] _modifiers;
140
141 protected final TypeParser _parser;
142
143 /**
144 * ClassLoader used by this factory [databind#624].
145 */
146 protected final ClassLoader _classLoader;
147
148 /*
149 /**********************************************************
150 /* Life-cycle
151 /**********************************************************
152 */
153
154 private TypeFactory() {
155 this(null);
156 }
157
158 /**
159 * @since 2.8
160 */
161 protected TypeFactory(LRUMap<Object,JavaType> typeCache) {
162 if (typeCache == null) {
163 typeCache = new LRUMap<Object,JavaType>(16, 200);
164 }
165 _typeCache = typeCache;
166 _parser = new TypeParser(this);
167 _modifiers = null;
168 _classLoader = null;
169 }
170
171 protected TypeFactory(LRUMap<Object,JavaType> typeCache, TypeParser p,
172 TypeModifier[] mods, ClassLoader classLoader)
173 {
174 if (typeCache == null) {
175 typeCache = new LRUMap<Object,JavaType>(16, 200);
176 }
177 _typeCache = typeCache;
178 // As per [databind#894] must ensure we have back-linkage from TypeFactory:
179 _parser = p.withFactory(this);
180 _modifiers = mods;
181 _classLoader = classLoader;
182 }
183
184 /**
185 * "Mutant factory" method which will construct a new instance with specified
186 * {@link TypeModifier} added as the first modifier to call (in case there
187 * are multiple registered).
188 */
189 public TypeFactory withModifier(TypeModifier mod)
190 {
191 LRUMap<Object,JavaType> typeCache = _typeCache;
192 TypeModifier[] mods;
193 if (mod == null) { // mostly for unit tests
194 mods = null;
195 // 30-Jun-2016, tatu: for some reason expected semantics are to clear cache
196 // in this case; can't recall why, but keeping the same
197 typeCache = null;
198 } else if (_modifiers == null) {
199 mods = new TypeModifier[] { mod };
200 // 29-Jul-2019, tatu: Actually I think we better clear cache in this case
201 // as well to ensure no leakage occurs (see [databind#2395])
202 typeCache = null;
203 } else {
204 // but may keep existing cache otherwise
205 mods = ArrayBuilders.insertInListNoDup(_modifiers, mod);
206 }
207 return new TypeFactory(typeCache, _parser, mods, _classLoader);
208 }
209
210 /**
211 * "Mutant factory" method which will construct a new instance with specified
212 * {@link ClassLoader} to use by {@link #findClass}.
213 */
214 public TypeFactory withClassLoader(ClassLoader classLoader) {
215 return new TypeFactory(_typeCache, _parser, _modifiers, classLoader);
216 }
217
218 /**
219 * Mutant factory method that will construct new {@link TypeFactory} with
220 * identical settings except for different cache; most likely one with
221 * bigger maximum size.
222 *
223 * @since 2.8
224 */
225 public TypeFactory withCache(LRUMap<Object,JavaType> cache) {
226 return new TypeFactory(cache, _parser, _modifiers, _classLoader);
227 }
228
229 /**
230 * Method used to access the globally shared instance, which has
231 * no custom configuration. Used by <code>ObjectMapper</code> to
232 * get the default factory when constructed.
233 */
234 public static TypeFactory defaultInstance() { return instance; }
235
236 /**
237 * Method that will clear up any cached type definitions that may
238 * be cached by this {@link TypeFactory} instance.
239 * This method should not be commonly used, that is, only use it
240 * if you know there is a problem with retention of type definitions;
241 * the most likely (and currently only known) problem is retention
242 * of {@link Class} instances via {@link JavaType} reference.
243 *
244 * @since 2.4.1
245 */
246 public void clearCache() {
247 _typeCache.clear();
248 }
249
250 public ClassLoader getClassLoader() {
251 return _classLoader;
252 }
253
254 /*
255 /**********************************************************
256 /* Static methods for non-instance-specific functionality
257 /**********************************************************
258 */
259
260 /**
261 * Method for constructing a marker type that indicates missing generic
262 * type information, which is handled same as simple type for
263 * <code>java.lang.Object</code>.
264 */
265 public static JavaType unknownType() {
266 return defaultInstance()._unknownType();
267 }
268
269 /**
270 * Static helper method that can be called to figure out type-erased
271 * call for given JDK type. It can be called statically since type resolution
272 * process can never change actual type-erased class; thereby static
273 * default instance is used for determination.
274 */
275 public static Class<?> rawClass(Type t) {
276 if (t instanceof Class<?>) {
277 return (Class<?>) t;
278 }
279 // Should be able to optimize bit more in future...
280 return defaultInstance().constructType(t).getRawClass();
281 }
282
283 /*
284 /**********************************************************
285 /* Low-level helper methods
286 /**********************************************************
287 */
288
289 /**
290 * Low-level lookup method moved from {@link com.fasterxml.jackson.databind.util.ClassUtil},
291 * to allow for overriding of lookup functionality in environments like OSGi.
292 *
293 * @since 2.6
294 */
295 public Class<?> findClass(String className) throws ClassNotFoundException
296 {
297 if (className.indexOf('.') < 0) {
298 Class<?> prim = _findPrimitive(className);
299 if (prim != null) {
300 return prim;
301 }
302 }
303 // Two-phase lookup: first using context ClassLoader; then default
304 Throwable prob = null;
305 ClassLoader loader = this.getClassLoader();
306 if (loader == null) {
307 loader = Thread.currentThread().getContextClassLoader();
308 }
309 if (loader != null) {
310 try {
311 return classForName(className, true, loader);
312 } catch (Exception e) {
313 prob = ClassUtil.getRootCause(e);
314 }
315 }
316 try {
317 return classForName(className);
318 } catch (Exception e) {
319 if (prob == null) {
320 prob = ClassUtil.getRootCause(e);
321 }
322 }
323 ClassUtil.throwIfRTE(prob);
324 throw new ClassNotFoundException(prob.getMessage(), prob);
325 }
326
327 protected Class<?> classForName(String name, boolean initialize,
328 ClassLoader loader) throws ClassNotFoundException {
329 return Class.forName(name, true, loader);
330 }
331
332 protected Class<?> classForName(String name) throws ClassNotFoundException {
333 return Class.forName(name);
334 }
335
336 protected Class<?> _findPrimitive(String className)
337 {
338 if ("int".equals(className)) return Integer.TYPE;
339 if ("long".equals(className)) return Long.TYPE;
340 if ("float".equals(className)) return Float.TYPE;
341 if ("double".equals(className)) return Double.TYPE;
342 if ("boolean".equals(className)) return Boolean.TYPE;
343 if ("byte".equals(className)) return Byte.TYPE;
344 if ("char".equals(className)) return Character.TYPE;
345 if ("short".equals(className)) return Short.TYPE;
346 if ("void".equals(className)) return Void.TYPE;
347 return null;
348 }
349
350 /*
351 /**********************************************************
352 /* Type conversion, parameterization resolution methods
353 /**********************************************************
354 */
355
356 /**
357 * Factory method for creating a subtype of given base type, as defined
358 * by specified subclass; but retaining generic type information if any.
359 * Can be used, for example, to get equivalent of "HashMap<String,Integer>"
360 * from "Map<String,Integer>" by giving <code>HashMap.class</code>
361 * as subclass.
362 * Short-cut for:
363 *<pre>
364 * constructSpecializedType(baseType, subclass, class);
365 *</pre>
366 * that is, will use "strict" compatibility checking, usually used for
367 * deserialization purposes (but often not for serialization).
368 */
369 public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass)
370 throws IllegalArgumentException
371 {
372 return constructSpecializedType(baseType, subclass, false);
373 }
374
375 /**
376 * Factory method for creating a subtype of given base type, as defined
377 * by specified subclass; but retaining generic type information if any.
378 * Can be used, for example, to get equivalent of "HashMap<String,Integer>"
379 * from "Map<String,Integer>" by giving <code>HashMap.class</code>
380 * as subclass.
381 *
382 * @param baseType Declared base type with resolved type parameters
383 * @param subclass Runtime subtype to use for resolving
384 * @param relaxedCompatibilityCheck Whether checking for type-assignment compatibility
385 * should be "relaxed" ({@code true}) or "strict" ({@code false}): typically
386 * serialization uses relaxed, deserialization strict checking.
387 *
388 * @return Resolved sub-type
389 *
390 * @since 2.11
391 */
392 public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass,
393 boolean relaxedCompatibilityCheck)
394 throws IllegalArgumentException
395 {
396 // simple optimization to avoid costly introspection if type-erased type does NOT differ
397 final Class<?> rawBase = baseType.getRawClass();
398 if (rawBase == subclass) {
399 return baseType;
400 }
401 JavaType newType;
402
403 // also: if we start from untyped, not much to save
404 do { // bogus loop to be able to break
405 if (rawBase == Object.class) {
406 newType = _fromClass(null, subclass, EMPTY_BINDINGS);
407 break;
408 }
409 if (!rawBase.isAssignableFrom(subclass)) {
410 throw new IllegalArgumentException(String.format("Class %s not subtype of %s",
411 ClassUtil.nameOf(subclass), ClassUtil.getTypeDescription(baseType)
412 ));
413 }
414 // A few special cases where we can simplify handling:
415
416 // (1) A small set of "well-known" List/Map subtypes where can take a short-cut
417 if (baseType.isContainerType()) {
418 if (baseType.isMapLikeType()) {
419 if ((subclass == HashMap.class)
420 || (subclass == LinkedHashMap.class)
421 || (subclass == EnumMap.class)
422 || (subclass == TreeMap.class)) {
423 newType = _fromClass(null, subclass,
424 TypeBindings.create(subclass, baseType.getKeyType(), baseType.getContentType()));
425 break;
426 }
427 } else if (baseType.isCollectionLikeType()) {
428 if ((subclass == ArrayList.class)
429 || (subclass == LinkedList.class)
430 || (subclass == HashSet.class)
431 || (subclass == TreeSet.class)) {
432 newType = _fromClass(null, subclass,
433 TypeBindings.create(subclass, baseType.getContentType()));
434 break;
435 }
436 // 29-Oct-2015, tatu: One further shortcut: there are variants of `EnumSet`,
437 // but they are impl details and we basically do not care...
438 if (rawBase == EnumSet.class) {
439 return baseType;
440 }
441 }
442 }
443 // (2) Original target type has no generics -- just resolve subtype
444 if (baseType.getBindings().isEmpty()) {
445 newType = _fromClass(null, subclass, EMPTY_BINDINGS);
446 break;
447 }
448
449 // (3) Sub-class does not take type parameters -- just resolve subtype
450 int typeParamCount = subclass.getTypeParameters().length;
451 if (typeParamCount == 0) {
452 newType = _fromClass(null, subclass, EMPTY_BINDINGS);
453 break;
454 }
455 // (4) If all else fails, do the full traversal using placeholders
456 TypeBindings tb = _bindingsForSubtype(baseType, typeParamCount,
457 subclass, relaxedCompatibilityCheck);
458 newType = _fromClass(null, subclass, tb);
459
460 } while (false);
461
462 // 25-Sep-2016, tatu: As per [databind#1384] also need to ensure handlers get
463 // copied as well
464 newType = newType.withHandlersFrom(baseType);
465 return newType;
466 }
467
468 private TypeBindings _bindingsForSubtype(JavaType baseType, int typeParamCount,
469 Class<?> subclass, boolean relaxedCompatibilityCheck)
470 {
471 PlaceholderForType[] placeholders = new PlaceholderForType[typeParamCount];
472 for (int i = 0; i < typeParamCount; ++i) {
473 placeholders[i] = new PlaceholderForType(i);
474 }
475 TypeBindings b = TypeBindings.create(subclass, placeholders);
476 // First: pseudo-resolve to get placeholders in place:
477 JavaType tmpSub = _fromClass(null, subclass, b);
478 // Then find super-type
479 JavaType baseWithPlaceholders = tmpSub.findSuperType(baseType.getRawClass());
480 if (baseWithPlaceholders == null) { // should be found but...
481 throw new IllegalArgumentException(String.format(
482 "Internal error: unable to locate supertype (%s) from resolved subtype %s", baseType.getRawClass().getName(),
483 subclass.getName()));
484 }
485 // and traverse type hierarchies to both verify and to resolve placeholders
486 String error = _resolveTypePlaceholders(baseType, baseWithPlaceholders);
487 if (error != null) {
488 // 28-Mar-2020, tatu: As per [databind#2632], need to ignore the issue in
489 // some cases. For now, just fully ignore; may need to refine in future
490 if (!relaxedCompatibilityCheck) {
491 throw new IllegalArgumentException("Failed to specialize base type "+baseType.toCanonical()+" as "
492 +subclass.getName()+", problem: "+error);
493 }
494 }
495
496 final JavaType[] typeParams = new JavaType[typeParamCount];
497 for (int i = 0; i < typeParamCount; ++i) {
498 JavaType t = placeholders[i].actualType();
499 // 18-Oct-2017, tatu: Looks like sometimes we have incomplete bindings (even if not
500 // common, it is possible if subtype is type-erased class with added type
501 // variable -- see test(s) with "bogus" type(s)).
502 if (t == null) {
503 t = unknownType();
504 }
505 typeParams[i] = t;
506 }
507 return TypeBindings.create(subclass, typeParams);
508 }
509
510 private String _resolveTypePlaceholders(JavaType sourceType, JavaType actualType)
511 throws IllegalArgumentException
512 {
513 List<JavaType> expectedTypes = sourceType.getBindings().getTypeParameters();
514 List<JavaType> actualTypes = actualType.getBindings().getTypeParameters();
515
516 final int actCount = actualTypes.size();
517
518 for (int i = 0, expCount = expectedTypes.size(); i < expCount; ++i) {
519 JavaType exp = expectedTypes.get(i);
520 JavaType act = (i < actCount) ? actualTypes.get(i) : unknownType();
521
522 if (!_verifyAndResolvePlaceholders(exp, act)) {
523 // 14-May-2018, tatu: As per [databind#2034] it seems we better relax assignment
524 // rules further -- at least likely "raw" (untyped, non-generic) base should probably
525 // allow specialization.
526 if (exp.hasRawClass(Object.class)) {
527 continue;
528 }
529 // 19-Apr-2018, tatu: Hack for [databind#1964] -- allow type demotion
530 // for `java.util.Map` key type if (and only if) target type is
531 // `java.lang.Object`
532 // 19-Aug-2019, tatu: Further, allow for all Map-like types, with assumption
533 // first argument would be key; initially just because Scala Maps have
534 // some issues (see [databind#2422])
535 if (i == 0) {
536 if (sourceType.isMapLikeType()
537 && act.hasRawClass(Object.class)) {
538 continue;
539 }
540 }
541 // 19-Nov-2018, tatu: To solve [databind#2155], let's allow type-compatible
542 // assignment for interfaces at least...
543 if (exp.isInterface()) {
544 if (exp.isTypeOrSuperTypeOf(act.getRawClass())) {
545 continue;
546 }
547 }
548 return String.format("Type parameter #%d/%d differs; can not specialize %s with %s",
549 (i+1), expCount, exp.toCanonical(), act.toCanonical());
550 }
551 }
552 return null;
553 }
554
555 private boolean _verifyAndResolvePlaceholders(JavaType exp, JavaType act)
556 {
557 // See if we have an actual type placeholder to resolve; if yes, replace
558 if (act instanceof PlaceholderForType) {
559 ((PlaceholderForType) act).actualType(exp);
560 return true;
561 }
562 // if not, try to verify compatibility. But note that we can not
563 // use simple equality as we need to resolve recursively
564 if (exp.getRawClass() != act.getRawClass()) {
565 return false;
566 }
567 // But we can check type parameters "blindly"
568 List<JavaType> expectedTypes = exp.getBindings().getTypeParameters();
569 List<JavaType> actualTypes = act.getBindings().getTypeParameters();
570 for (int i = 0, len = expectedTypes.size(); i < len; ++i) {
571 JavaType exp2 = expectedTypes.get(i);
572 JavaType act2 = actualTypes.get(i);
573 if (!_verifyAndResolvePlaceholders(exp2, act2)) {
574 return false;
575 }
576 }
577 return true;
578 }
579
580 /**
581 * Method similar to {@link #constructSpecializedType}, but that creates a
582 * less-specific type of given type. Usually this is as simple as simply
583 * finding super-type with type erasure of <code>superClass</code>, but
584 * there may be need for some additional work-arounds.
585 *
586 * @param superClass
587 *
588 * @since 2.7
589 */
590 public JavaType constructGeneralizedType(JavaType baseType, Class<?> superClass)
591 {
592 // simple optimization to avoid costly introspection if type-erased type does NOT differ
593 final Class<?> rawBase = baseType.getRawClass();
594 if (rawBase == superClass) {
595 return baseType;
596 }
597 JavaType superType = baseType.findSuperType(superClass);
598 if (superType == null) {
599 // Most likely, caller did not verify sub/super-type relationship
600 if (!superClass.isAssignableFrom(rawBase)) {
601 throw new IllegalArgumentException(String.format(
602 "Class %s not a super-type of %s", superClass.getName(), baseType));
603 }
604 // 01-Nov-2015, tatu: Should never happen, but ch
605 throw new IllegalArgumentException(String.format(
606 "Internal error: class %s not included as super-type for %s",
607 superClass.getName(), baseType));
608 }
609 return superType;
610 }
611
612 /**
613 * Factory method for constructing a {@link JavaType} out of its canonical
614 * representation (see {@link JavaType#toCanonical()}).
615 *
616 * @param canonical Canonical string representation of a type
617 *
618 * @throws IllegalArgumentException If canonical representation is malformed,
619 * or class that type represents (including its generic parameters) is
620 * not found
621 */
622 public JavaType constructFromCanonical(String canonical) throws IllegalArgumentException
623 {
624 return _parser.parse(canonical);
625 }
626
627 /**
628 * Method that is to figure out actual type parameters that given
629 * class binds to generic types defined by given (generic)
630 * interface or class.
631 * This could mean, for example, trying to figure out
632 * key and value types for Map implementations.
633 *
634 * @param type Sub-type (leaf type) that implements <code>expType</code>
635 */
636 public JavaType[] findTypeParameters(JavaType type, Class<?> expType)
637 {
638 JavaType match = type.findSuperType(expType);
639 if (match == null) {
640 return NO_TYPES;
641 }
642 return match.getBindings().typeParameterArray();
643 }
644
645 /**
646 * @deprecated Since 2.7 resolve raw type first, then find type parameters
647 */
648 @Deprecated // since 2.7
649 public JavaType[] findTypeParameters(Class<?> clz, Class<?> expType, TypeBindings bindings) {
650 return findTypeParameters(constructType(clz, bindings), expType);
651 }
652
653 /**
654 * @deprecated Since 2.7 resolve raw type first, then find type parameters
655 */
656 @Deprecated // since 2.7
657 public JavaType[] findTypeParameters(Class<?> clz, Class<?> expType) {
658 return findTypeParameters(constructType(clz), expType);
659 }
660
661 /**
662 * Method that can be called to figure out more specific of two
663 * types (if they are related; that is, one implements or extends the
664 * other); or if not related, return the primary type.
665 *
666 * @param type1 Primary type to consider
667 * @param type2 Secondary type to consider
668 *
669 * @since 2.2
670 */
671 public JavaType moreSpecificType(JavaType type1, JavaType type2)
672 {
673 if (type1 == null) {
674 return type2;
675 }
676 if (type2 == null) {
677 return type1;
678 }
679 Class<?> raw1 = type1.getRawClass();
680 Class<?> raw2 = type2.getRawClass();
681 if (raw1 == raw2) {
682 return type1;
683 }
684 // TODO: maybe try sub-classing, to retain generic types?
685 if (raw1.isAssignableFrom(raw2)) {
686 return type2;
687 }
688 return type1;
689 }
690
691 /*
692 /**********************************************************
693 /* Public factory methods
694 /**********************************************************
695 */
696
697 public JavaType constructType(Type type) {
698 return _fromAny(null, type, EMPTY_BINDINGS);
699 }
700
701 public JavaType constructType(Type type, TypeBindings bindings) {
702 // 15-Jun-2020, tatu: To resolve (parts of) [databind#2796], need to
703 // call _fromClass() directly if we get `Class` argument
704 if (type instanceof Class<?>) {
705 JavaType resultType = _fromClass(null, (Class<?>) type, bindings);
706 return _applyModifiers(type, resultType);
707 }
708 return _fromAny(null, type, bindings);
709 }
710
711 public JavaType constructType(TypeReference<?> typeRef)
712 {
713 // 19-Oct-2015, tatu: Simpler variant like so should work
714 return _fromAny(null, typeRef.getType(), EMPTY_BINDINGS);
715
716 // but if not, due to funky sub-classing, type variables, what follows
717 // is a more complete processing a la Java ClassMate.
718
719 /*
720 final Class<?> refdRawType = typeRef.getClass();
721 JavaType type = _fromClass(null, refdRawType, EMPTY_BINDINGS);
722 JavaType genType = type.findSuperType(TypeReference.class);
723 if (genType == null) { // sanity check; shouldn't occur
724 throw new IllegalArgumentException("Unparameterized GenericType instance ("+refdRawType.getName()+")");
725 }
726 TypeBindings b = genType.getBindings();
727 JavaType[] params = b.typeParameterArray();
728 if (params.length == 0) {
729 throw new IllegalArgumentException("Unparameterized GenericType instance ("+refdRawType.getName()+")");
730 }
731 return params[0];
732 */
733 }
734
735 /**
736 * @deprecated Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)
737 */
738 @Deprecated
739 public JavaType constructType(Type type, Class<?> contextClass) {
740 JavaType contextType = (contextClass == null) ? null : constructType(contextClass);
741 return constructType(type, contextType);
742 }
743
744 /**
745 * @deprecated Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)
746 */
747 @Deprecated
748 public JavaType constructType(Type type, JavaType contextType) {
749 TypeBindings bindings;
750 if (contextType == null) {
751 bindings = EMPTY_BINDINGS;
752 } else {
753 bindings = contextType.getBindings();
754 // 16-Nov-2016, tatu: Unfortunately as per [databind#1456] this can't
755 // be made to work for some cases used to work (even if accidentally);
756 // however, we can try a simple heuristic to increase chances of
757 // compatibility from 2.6 code
758 if (type.getClass() != Class.class) {
759 // Ok: so, ideally we would test super-interfaces if necessary;
760 // but let's assume most if not all cases are for classes.
761 while (bindings.isEmpty()) {
762 contextType = contextType.getSuperClass();
763 if (contextType == null) {
764 break;
765 }
766 bindings = contextType.getBindings();
767 }
768 }
769 }
770 return _fromAny(null, type, bindings);
771 }
772
773 /*
774 /**********************************************************
775 /* Direct factory methods
776 /**********************************************************
777 */
778
779 /**
780 * Method for constructing an {@link ArrayType}.
781 *<p>
782 * NOTE: type modifiers are NOT called on array type itself; but are called
783 * for element type (and other contained types)
784 */
785 public ArrayType constructArrayType(Class<?> elementType) {
786 return ArrayType.construct(_fromAny(null, elementType, null), null);
787 }
788
789 /**
790 * Method for constructing an {@link ArrayType}.
791 *<p>
792 * NOTE: type modifiers are NOT called on array type itself; but are called
793 * for contained types.
794 */
795 public ArrayType constructArrayType(JavaType elementType) {
796 return ArrayType.construct(elementType, null);
797 }
798
799 /**
800 * Method for constructing a {@link CollectionType}.
801 *<p>
802 * NOTE: type modifiers are NOT called on Collection type itself; but are called
803 * for contained types.
804 */
805 public CollectionType constructCollectionType(Class<? extends Collection> collectionClass,
806 Class<?> elementClass) {
807 return constructCollectionType(collectionClass,
808 _fromClass(null, elementClass, EMPTY_BINDINGS));
809 }
810
811 /**
812 * Method for constructing a {@link CollectionType}.
813 *<p>
814 * NOTE: type modifiers are NOT called on Collection type itself; but are called
815 * for contained types.
816 */
817 public CollectionType constructCollectionType(Class<? extends Collection> collectionClass,
818 JavaType elementType)
819 {
820 TypeBindings bindings = TypeBindings.createIfNeeded(collectionClass, elementType);
821 CollectionType result = (CollectionType) _fromClass(null, collectionClass, bindings);
822 // 17-May-2017, tatu: As per [databind#1415], we better verify bound values if (but only if)
823 // type being resolved was non-generic (i.e.element type was ignored)
824 if (bindings.isEmpty() && (elementType != null)) {
825 JavaType t = result.findSuperType(Collection.class);
826 JavaType realET = t.getContentType();
827 if (!realET.equals(elementType)) {
828 throw new IllegalArgumentException(String.format(
829 "Non-generic Collection class %s did not resolve to something with element type %s but %s ",
830 ClassUtil.nameOf(collectionClass), elementType, realET));
831 }
832 }
833 return result;
834 }
835
836 /**
837 * Method for constructing a {@link CollectionLikeType}.
838 *<p>
839 * NOTE: type modifiers are NOT called on constructed type itself; but are called
840 * for contained types.
841 */
842 public CollectionLikeType constructCollectionLikeType(Class<?> collectionClass, Class<?> elementClass) {
843 return constructCollectionLikeType(collectionClass,
844 _fromClass(null, elementClass, EMPTY_BINDINGS));
845 }
846
847 /**
848 * Method for constructing a {@link CollectionLikeType}.
849 *<p>
850 * NOTE: type modifiers are NOT called on constructed type itself; but are called
851 * for contained types.
852 */
853 public CollectionLikeType constructCollectionLikeType(Class<?> collectionClass, JavaType elementType) {
854 JavaType type = _fromClass(null, collectionClass,
855 TypeBindings.createIfNeeded(collectionClass, elementType));
856 if (type instanceof CollectionLikeType) {
857 return (CollectionLikeType) type;
858 }
859 return CollectionLikeType.upgradeFrom(type, elementType);
860 }
861
862 /**
863 * Method for constructing a {@link MapType} instance
864 *<p>
865 * NOTE: type modifiers are NOT called on constructed type itself; but are called
866 * for contained types.
867 */
868 public MapType constructMapType(Class<? extends Map> mapClass,
869 Class<?> keyClass, Class<?> valueClass) {
870 JavaType kt, vt;
871 if (mapClass == Properties.class) {
872 kt = vt = CORE_TYPE_STRING;
873 } else {
874 kt = _fromClass(null, keyClass, EMPTY_BINDINGS);
875 vt = _fromClass(null, valueClass, EMPTY_BINDINGS);
876 }
877 return constructMapType(mapClass, kt, vt);
878 }
879
880 /**
881 * Method for constructing a {@link MapType} instance
882 *<p>
883 * NOTE: type modifiers are NOT called on constructed type itself.
884 */
885 public MapType constructMapType(Class<? extends Map> mapClass, JavaType keyType, JavaType valueType) {
886 TypeBindings bindings = TypeBindings.createIfNeeded(mapClass, new JavaType[] { keyType, valueType });
887 MapType result = (MapType) _fromClass(null, mapClass, bindings);
888 // 17-May-2017, tatu: As per [databind#1415], we better verify bound values if (but only if)
889 // type being resolved was non-generic (i.e.element type was ignored)
890 if (bindings.isEmpty()) {
891 JavaType t = result.findSuperType(Map.class);
892 JavaType realKT = t.getKeyType();
893 if (!realKT.equals(keyType)) {
894 throw new IllegalArgumentException(String.format(
895 "Non-generic Map class %s did not resolve to something with key type %s but %s ",
896 ClassUtil.nameOf(mapClass), keyType, realKT));
897 }
898 JavaType realVT = t.getContentType();
899 if (!realVT.equals(valueType)) {
900 throw new IllegalArgumentException(String.format(
901 "Non-generic Map class %s did not resolve to something with value type %s but %s ",
902 ClassUtil.nameOf(mapClass), valueType, realVT));
903 }
904 }
905 return result;
906 }
907
908 /**
909 * Method for constructing a {@link MapLikeType} instance
910 *<p>
911 * NOTE: type modifiers are NOT called on constructed type itself; but are called
912 * for contained types.
913 */
914 public MapLikeType constructMapLikeType(Class<?> mapClass, Class<?> keyClass, Class<?> valueClass) {
915 return constructMapLikeType(mapClass,
916 _fromClass(null, keyClass, EMPTY_BINDINGS),
917 _fromClass(null, valueClass, EMPTY_BINDINGS));
918 }
919
920 /**
921 * Method for constructing a {@link MapLikeType} instance
922 *<p>
923 * NOTE: type modifiers are NOT called on constructed type itself.
924 */
925 public MapLikeType constructMapLikeType(Class<?> mapClass, JavaType keyType, JavaType valueType) {
926 // 19-Oct-2015, tatu: Allow case of no-type-variables, since it seems likely to be
927 // a valid use case here
928 JavaType type = _fromClass(null, mapClass,
929 TypeBindings.createIfNeeded(mapClass, new JavaType[] { keyType, valueType }));
930 if (type instanceof MapLikeType) {
931 return (MapLikeType) type;
932 }
933 return MapLikeType.upgradeFrom(type, keyType, valueType);
934 }
935
936 /**
937 * Method for constructing a type instance with specified parameterization.
938 *<p>
939 * NOTE: type modifiers are NOT called on constructed type itself.
940 */
941 public JavaType constructSimpleType(Class<?> rawType, JavaType[] parameterTypes) {
942 return _fromClass(null, rawType, TypeBindings.create(rawType, parameterTypes));
943 }
944
945 /**
946 * Method for constructing a type instance with specified parameterization.
947 *
948 * @since 2.6
949 *
950 * @deprecated Since 2.7
951 */
952 @Deprecated
953 public JavaType constructSimpleType(Class<?> rawType, Class<?> parameterTarget,
954 JavaType[] parameterTypes)
955 {
956 return constructSimpleType(rawType, parameterTypes);
957 }
958
959 /**
960 * Method for constructing a {@link ReferenceType} instance with given type parameter
961 * (type MUST take one and only one type parameter)
962 *<p>
963 * NOTE: type modifiers are NOT called on constructed type itself.
964 *
965 * @since 2.6
966 */
967 public JavaType constructReferenceType(Class<?> rawType, JavaType referredType)
968 {
969 return ReferenceType.construct(rawType, null, // no bindings
970 null, null, // or super-class, interfaces?
971 referredType);
972 }
973
974 /**
975 * Method that use by core Databind functionality, and that should NOT be called
976 * by application code outside databind package.
977 *<p>
978 * Unchecked here not only means that no checks are made as to whether given class
979 * might be non-simple type (like {@link CollectionType}) but also that most of supertype
980 * information is not gathered. This means that unless called on primitive types or
981 * {@link java.lang.String}, results are probably not what you want to use.
982 *
983 * @deprecated Since 2.8, to indicate users should never call this method.
984 */
985 @Deprecated // since 2.8
986 public JavaType uncheckedSimpleType(Class<?> cls) {
987 // 18-Oct-2015, tatu: Not sure how much problem missing super-type info is here
988 return _constructSimple(cls, EMPTY_BINDINGS, null, null);
989 }
990
991 /**
992 * Factory method for constructing {@link JavaType} that
993 * represents a parameterized type. For example, to represent
994 * type {@code List<Set<Integer>>}, you could
995 * call
996 *<pre>
997 * JavaType inner = TypeFactory.constructParametricType(Set.class, Set.class, Integer.class);
998 * return TypeFactory.constructParametricType(ArrayList.class, List.class, inner);
999 *</pre>
1000 *<p>
1001 * The reason for first two arguments to be separate is that parameterization may
1002 * apply to a super-type. For example, if generic type was instead to be
1003 * constructed for {@code ArrayList<Integer>}, the usual call would be:
1004 *<pre>
1005 * TypeFactory.constructParametricType(ArrayList.class, List.class, Integer.class);
1006 *</pre>
1007 * since parameterization is applied to {@link java.util.List}.
1008 * In most cases distinction does not matter, but there are types where it does;
1009 * one such example is parameterization of types that implement {@link java.util.Iterator}.
1010 *<p>
1011 * NOTE: type modifiers are NOT called on constructed type.
1012 *
1013 * @param parametrized Actual full type
1014 * @param parameterClasses Type parameters to apply
1015 *
1016 * @since 2.5 NOTE: was briefly deprecated for 2.6
1017 */
1018 public JavaType constructParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
1019 int len = parameterClasses.length;
1020 JavaType[] pt = new JavaType[len];
1021 for (int i = 0; i < len; ++i) {
1022 pt[i] = _fromClass(null, parameterClasses[i], EMPTY_BINDINGS);
1023 }
1024 return constructParametricType(parametrized, pt);
1025 }
1026
1027 /**
1028 * Factory method for constructing {@link JavaType} that
1029 * represents a parameterized type. For example, to represent
1030 * type {@code List<Set<Integer>>}, you could
1031 *<pre>
1032 * JavaType inner = TypeFactory.constructParametricType(Set.class, Set.class, Integer.class);
1033 * return TypeFactory.constructParametricType(ArrayList.class, List.class, inner);
1034 *</pre>
1035 *<p>
1036 * The reason for first two arguments to be separate is that parameterization may
1037 * apply to a super-type. For example, if generic type was instead to be
1038 * constructed for {@code ArrayList<Integer>}, the usual call would be:
1039 *<pre>
1040 * TypeFactory.constructParametricType(ArrayList.class, List.class, Integer.class);
1041 *</pre>
1042 * since parameterization is applied to {@link java.util.List}.
1043 * In most cases distinction does not matter, but there are types where it does;
1044 * one such example is parameterization of types that implement {@link java.util.Iterator}.
1045 *<p>
1046 * NOTE: since 2.11.2 {@link TypeModifier}s ARE called on result (fix for [databind#2796])
1047 *
1048 * @param rawType Actual type-erased type
1049 * @param parameterTypes Type parameters to apply
1050 *
1051 * @return Fully resolved type for given base type and type parameters
1052 */
1053 public JavaType constructParametricType(Class<?> rawType, JavaType... parameterTypes)
1054 {
1055 // 16-Jul-2020, tatu: Since we do not call `_fromAny()`, need to make
1056 // sure `TypeModifier`s are applied:
1057 JavaType resultType = _fromClass(null, rawType, TypeBindings.create(rawType, parameterTypes));
1058 return _applyModifiers(rawType, resultType);
1059 }
1060
1061 /**
1062 * @since 2.5 -- but will probably deprecated in 2.7 or 2.8 (not needed with 2.7)
1063 *
1064 * @deprecated since 2.9 Use {@link #constructParametricType(Class,JavaType...)} instead
1065 */
1066 @Deprecated
1067 public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor,
1068 JavaType... parameterTypes)
1069 {
1070 return constructParametricType(parametrized, parameterTypes);
1071 }
1072
1073 /**
1074 * @since 2.5 -- but will probably deprecated in 2.7 or 2.8 (not needed with 2.7)
1075 *
1076 * @deprecated since 2.9 Use {@link #constructParametricType(Class,Class...)} instead
1077 */
1078 @Deprecated
1079 public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor,
1080 Class<?>... parameterClasses)
1081 {
1082 return constructParametricType(parametrized, parameterClasses);
1083 }
1084
1085 /*
1086 /**********************************************************
1087 /* Direct factory methods for "raw" variants, used when
1088 /* parameterization is unknown
1089 /**********************************************************
1090 */
1091
1092 /**
1093 * Method that can be used to construct "raw" Collection type; meaning that its
1094 * parameterization is unknown.
1095 * This is similar to using <code>Object.class</code> parameterization,
1096 * and is equivalent to calling:
1097 *<pre>
1098 * typeFactory.constructCollectionType(collectionClass, typeFactory.unknownType());
1099 *</pre>
1100 *<p>
1101 * This method should only be used if parameterization is completely unavailable.
1102 */
1103 public CollectionType constructRawCollectionType(Class<? extends Collection> collectionClass) {
1104 return constructCollectionType(collectionClass, unknownType());
1105 }
1106
1107 /**
1108 * Method that can be used to construct "raw" Collection-like type; meaning that its
1109 * parameterization is unknown.
1110 * This is similar to using <code>Object.class</code> parameterization,
1111 * and is equivalent to calling:
1112 *<pre>
1113 * typeFactory.constructCollectionLikeType(collectionClass, typeFactory.unknownType());
1114 *</pre>
1115 *<p>
1116 * This method should only be used if parameterization is completely unavailable.
1117 */
1118 public CollectionLikeType constructRawCollectionLikeType(Class<?> collectionClass) {
1119 return constructCollectionLikeType(collectionClass, unknownType());
1120 }
1121
1122 /**
1123 * Method that can be used to construct "raw" Map type; meaning that its
1124 * parameterization is unknown.
1125 * This is similar to using <code>Object.class</code> parameterization,
1126 * and is equivalent to calling:
1127 *<pre>
1128 * typeFactory.constructMapType(collectionClass, typeFactory.unknownType(), typeFactory.unknownType());
1129 *</pre>
1130 *<p>
1131 * This method should only be used if parameterization is completely unavailable.
1132 */
1133 public MapType constructRawMapType(Class<? extends Map> mapClass) {
1134 return constructMapType(mapClass, unknownType(), unknownType());
1135 }
1136
1137 /**
1138 * Method that can be used to construct "raw" Map-like type; meaning that its
1139 * parameterization is unknown.
1140 * This is similar to using <code>Object.class</code> parameterization,
1141 * and is equivalent to calling:
1142 *<pre>
1143 * typeFactory.constructMapLikeType(collectionClass, typeFactory.unknownType(), typeFactory.unknownType());
1144 *</pre>
1145 *<p>
1146 * This method should only be used if parameterization is completely unavailable.
1147 */
1148 public MapLikeType constructRawMapLikeType(Class<?> mapClass) {
1149 return constructMapLikeType(mapClass, unknownType(), unknownType());
1150 }
1151
1152 /*
1153 /**********************************************************
1154 /* Low-level factory methods
1155 /**********************************************************
1156 */
1157
1158 private JavaType _mapType(Class<?> rawClass, TypeBindings bindings,
1159 JavaType superClass, JavaType[] superInterfaces)
1160 {
1161 JavaType kt, vt;
1162
1163 // 28-May-2015, tatu: Properties are special, as per [databind#810]; fake "correct" parameter sig
1164 if (rawClass == Properties.class) {
1165 kt = vt = CORE_TYPE_STRING;
1166 } else {
1167 List<JavaType> typeParams = bindings.getTypeParameters();
1168 // ok to have no types ("raw")
1169 final int pc = typeParams.size();
1170 switch (pc) {
1171 case 0: // acceptable?
1172 kt = vt = _unknownType();
1173 break;
1174 case 2:
1175 kt = typeParams.get(0);
1176 vt = typeParams.get(1);
1177 break;
1178 default:
1179 throw new IllegalArgumentException(String.format(
1180 "Strange Map type %s with %d type parameter%s (%s), can not resolve",
1181 ClassUtil.nameOf(rawClass), pc, (pc == 1) ? "" : "s", bindings));
1182 }
1183 }
1184 return MapType.construct(rawClass, bindings, superClass, superInterfaces, kt, vt);
1185 }
1186
1187 private JavaType _collectionType(Class<?> rawClass, TypeBindings bindings,
1188 JavaType superClass, JavaType[] superInterfaces)
1189 {
1190 List<JavaType> typeParams = bindings.getTypeParameters();
1191 // ok to have no types ("raw")
1192 JavaType ct;
1193 if (typeParams.isEmpty()) {
1194 ct = _unknownType();
1195 } else if (typeParams.size() == 1) {
1196 ct = typeParams.get(0);
1197 } else {
1198 throw new IllegalArgumentException("Strange Collection type "+rawClass.getName()+": cannot determine type parameters");
1199 }
1200 return CollectionType.construct(rawClass, bindings, superClass, superInterfaces, ct);
1201 }
1202
1203 private JavaType _referenceType(Class<?> rawClass, TypeBindings bindings,
1204 JavaType superClass, JavaType[] superInterfaces)
1205 {
1206 List<JavaType> typeParams = bindings.getTypeParameters();
1207 // ok to have no types ("raw")
1208 JavaType ct;
1209 if (typeParams.isEmpty()) {
1210 ct = _unknownType();
1211 } else if (typeParams.size() == 1) {
1212 ct = typeParams.get(0);
1213 } else {
1214 throw new IllegalArgumentException("Strange Reference type "+rawClass.getName()+": cannot determine type parameters");
1215 }
1216 return ReferenceType.construct(rawClass, bindings, superClass, superInterfaces, ct);
1217 }
1218
1219 /**
1220 * Factory method to call when no special {@link JavaType} is needed,
1221 * no generic parameters are passed. Default implementation may check
1222 * pre-constructed values for "well-known" types, but if none found
1223 * will simply call {@link #_newSimpleType}
1224 *
1225 * @since 2.7
1226 */
1227 protected JavaType _constructSimple(Class<?> raw, TypeBindings bindings,
1228 JavaType superClass, JavaType[] superInterfaces)
1229 {
1230 if (bindings.isEmpty()) {
1231 JavaType result = _findWellKnownSimple(raw);
1232 if (result != null) {
1233 return result;
1234 }
1235 }
1236 return _newSimpleType(raw, bindings, superClass, superInterfaces);
1237 }
1238
1239 /**
1240 * Factory method that is to create a new {@link SimpleType} with no
1241 * checks whatsoever. Default implementation calls the single argument
1242 * constructor of {@link SimpleType}.
1243 *
1244 * @since 2.7
1245 */
1246 protected JavaType _newSimpleType(Class<?> raw, TypeBindings bindings,
1247 JavaType superClass, JavaType[] superInterfaces)
1248 {
1249 return new SimpleType(raw, bindings, superClass, superInterfaces);
1250 }
1251
1252 protected JavaType _unknownType() {
1253 /* 15-Sep-2015, tatu: Prior to 2.7, we constructed new instance for each call.
1254 * This may have been due to potential mutability of the instance; but that
1255 * should not be issue any more, and creation is somewhat wasteful. So let's
1256 * try reusing singleton/flyweight instance.
1257 */
1258 return CORE_TYPE_OBJECT;
1259 }
1260
1261 /**
1262 * Helper method called to see if requested, non-generic-parameterized
1263 * type is one of common, "well-known" types, instances of which are
1264 * pre-constructed and do not need dynamic caching.
1265 *
1266 * @since 2.7
1267 */
1268 protected JavaType _findWellKnownSimple(Class<?> clz) {
1269 if (clz.isPrimitive()) {
1270 if (clz == CLS_BOOL) return CORE_TYPE_BOOL;
1271 if (clz == CLS_INT) return CORE_TYPE_INT;
1272 if (clz == CLS_LONG) return CORE_TYPE_LONG;
1273 } else {
1274 if (clz == CLS_STRING) return CORE_TYPE_STRING;
1275 if (clz == CLS_OBJECT) return CORE_TYPE_OBJECT; // since 2.7
1276 if (clz == CLS_JSON_NODE) return CORE_TYPE_JSON_NODE; // since 2.10
1277 }
1278 return null;
1279 }
1280
1281 /*
1282 /**********************************************************
1283 /* Actual type resolution, traversal
1284 /**********************************************************
1285 */
1286
1287 /**
1288 * Factory method that can be used if type information is passed
1289 * as Java typing returned from <code>getGenericXxx</code> methods
1290 * (usually for a return or argument type).
1291 */
1292 protected JavaType _fromAny(ClassStack context, Type srcType, TypeBindings bindings)
1293 {
1294 JavaType resultType;
1295
1296 // simple class?
1297 if (srcType instanceof Class<?>) {
1298 // Important: remove possible bindings since this is type-erased thingy
1299 resultType = _fromClass(context, (Class<?>) srcType, EMPTY_BINDINGS);
1300 }
1301 // But if not, need to start resolving.
1302 else if (srcType instanceof ParameterizedType) {
1303 resultType = _fromParamType(context, (ParameterizedType) srcType, bindings);
1304 }
1305 else if (srcType instanceof JavaType) { // [databind#116]
1306 // no need to modify further if we already had JavaType
1307 return (JavaType) srcType;
1308 }
1309 else if (srcType instanceof GenericArrayType) {
1310 resultType = _fromArrayType(context, (GenericArrayType) srcType, bindings);
1311 }
1312 else if (srcType instanceof TypeVariable<?>) {
1313 resultType = _fromVariable(context, (TypeVariable<?>) srcType, bindings);
1314 }
1315 else if (srcType instanceof WildcardType) {
1316 resultType = _fromWildcard(context, (WildcardType) srcType, bindings);
1317 } else {
1318 // sanity check
1319 throw new IllegalArgumentException("Unrecognized Type: "+((srcType == null) ? "[null]" : srcType.toString()));
1320 }
1321 // 21-Feb-2016, nateB/tatu: as per [databind#1129] (applied for 2.7.2),
1322 // we do need to let all kinds of types to be refined, esp. for Scala module.
1323 return _applyModifiers(srcType, resultType);
1324 }
1325
1326 protected JavaType _applyModifiers(Type srcType, JavaType resolvedType)
1327 {
1328 if (_modifiers == null) {
1329 return resolvedType;
1330 }
1331 JavaType resultType = resolvedType;
1332 TypeBindings b = resultType.getBindings();
1333 if (b == null) {
1334 b = EMPTY_BINDINGS;
1335 }
1336 for (TypeModifier mod : _modifiers) {
1337 JavaType t = mod.modifyType(resultType, srcType, b, this);
1338 if (t == null) {
1339 throw new IllegalStateException(String.format(
1340 "TypeModifier %s (of type %s) return null for type %s",
1341 mod, mod.getClass().getName(), resultType));
1342 }
1343 resultType = t;
1344 }
1345 return resultType;
1346 }
1347
1348 /**
1349 * @param bindings Mapping of formal parameter declarations (for generic
1350 * types) into actual types
1351 */
1352 protected JavaType _fromClass(ClassStack context, Class<?> rawType, TypeBindings bindings)
1353 {
1354 // Very first thing: small set of core types we know well:
1355 JavaType result = _findWellKnownSimple(rawType);
1356 if (result != null) {
1357 return result;
1358 }
1359 // Barring that, we may have recently constructed an instance
1360 final Object key;
1361 if ((bindings == null) || bindings.isEmpty()) {
1362 key = rawType;
1363 } else {
1364 key = bindings.asKey(rawType);
1365 }
1366 result = _typeCache.get(key); // ok, cache object is synced
1367 if (result != null) {
1368 return result;
1369 }
1370
1371 // 15-Oct-2015, tatu: recursive reference?
1372 if (context == null) {
1373 context = new ClassStack(rawType);
1374 } else {
1375 ClassStack prev = context.find(rawType);
1376 if (prev != null) {
1377 // Self-reference: needs special handling, then...
1378 ResolvedRecursiveType selfRef = new ResolvedRecursiveType(rawType, EMPTY_BINDINGS);
1379 prev.addSelfReference(selfRef);
1380 return selfRef;
1381 }
1382 // no, but need to update context to allow for proper cycle resolution
1383 context = context.child(rawType);
1384 }
1385
1386 // First: do we have an array type?
1387 if (rawType.isArray()) {
1388 result = ArrayType.construct(_fromAny(context, rawType.getComponentType(), bindings),
1389 bindings);
1390 } else {
1391 // If not, need to proceed by first resolving parent type hierarchy
1392
1393 JavaType superClass;
1394 JavaType[] superInterfaces;
1395
1396 if (rawType.isInterface()) {
1397 superClass = null;
1398 superInterfaces = _resolveSuperInterfaces(context, rawType, bindings);
1399 } else {
1400 // Note: even Enums can implement interfaces, so cannot drop those
1401 superClass = _resolveSuperClass(context, rawType, bindings);
1402 superInterfaces = _resolveSuperInterfaces(context, rawType, bindings);
1403 }
1404
1405 // 19-Oct-2015, tatu: Bit messy, but we need to 'fix' java.util.Properties here...
1406 if (rawType == Properties.class) {
1407 result = MapType.construct(rawType, bindings, superClass, superInterfaces,
1408 CORE_TYPE_STRING, CORE_TYPE_STRING);
1409 }
1410 // And then check what flavor of type we got. Start by asking resolved
1411 // super-type if refinement is all that is needed?
1412 else if (superClass != null) {
1413 result = superClass.refine(rawType, bindings, superClass, superInterfaces);
1414 }
1415 // if not, perhaps we are now resolving a well-known class or interface?
1416 if (result == null) {
1417 result = _fromWellKnownClass(context, rawType, bindings, superClass, superInterfaces);
1418 if (result == null) {
1419 result = _fromWellKnownInterface(context, rawType, bindings, superClass, superInterfaces);
1420 if (result == null) {
1421 // but if nothing else, "simple" class for now:
1422 result = _newSimpleType(rawType, bindings, superClass, superInterfaces);
1423 }
1424 }
1425 }
1426 }
1427 context.resolveSelfReferences(result);
1428 // 16-Jul-2016, tatu: [databind#1302] is solved different way, but ideally we shouldn't
1429 // cache anything with partially resolved `ResolvedRecursiveType`... so maybe improve
1430 if (!result.hasHandlers()) {
1431 _typeCache.putIfAbsent(key, result); // cache object syncs
1432 }
1433 return result;
1434 }
1435
1436 protected JavaType _resolveSuperClass(ClassStack context, Class<?> rawType, TypeBindings parentBindings)
1437 {
1438 Type parent = ClassUtil.getGenericSuperclass(rawType);
1439 if (parent == null) {
1440 return null;
1441 }
1442 return _fromAny(context, parent, parentBindings);
1443 }
1444
1445 protected JavaType[] _resolveSuperInterfaces(ClassStack context, Class<?> rawType, TypeBindings parentBindings)
1446 {
1447 Type[] types = ClassUtil.getGenericInterfaces(rawType);
1448 if (types == null || types.length == 0) {
1449 return NO_TYPES;
1450 }
1451 int len = types.length;
1452 JavaType[] resolved = new JavaType[len];
1453 for (int i = 0; i < len; ++i) {
1454 Type type = types[i];
1455 resolved[i] = _fromAny(context, type, parentBindings);
1456 }
1457 return resolved;
1458 }
1459
1460 /**
1461 * Helper class used to check whether exact class for which type is being constructed
1462 * is one of well-known base interfaces or classes that indicates alternate
1463 * {@link JavaType} implementation.
1464 */
1465 protected JavaType _fromWellKnownClass(ClassStack context, Class<?> rawType, TypeBindings bindings,
1466 JavaType superClass, JavaType[] superInterfaces)
1467 {
1468 if (bindings == null) {
1469 bindings = EMPTY_BINDINGS;
1470 }
1471 // Quite simple when we resolving exact class/interface; start with that
1472 if (rawType == Map.class) {
1473 return _mapType(rawType, bindings, superClass, superInterfaces);
1474 }
1475 if (rawType == Collection.class) {
1476 return _collectionType(rawType, bindings, superClass, superInterfaces);
1477 }
1478 // and since 2.6 one referential type
1479 if (rawType == AtomicReference.class) {
1480 return _referenceType(rawType, bindings, superClass, superInterfaces);
1481 }
1482 // 01-Nov-2015, tatu: As of 2.7, couple of potential `CollectionLikeType`s (like
1483 // `Iterable`, `Iterator`), and `MapLikeType`s (`Map.Entry`) are not automatically
1484 // detected, related to difficulties in propagating type upwards (Iterable, for
1485 // example, is a weak, tag-on type). They may be detectable in future.
1486 return null;
1487 }
1488
1489 protected JavaType _fromWellKnownInterface(ClassStack context, Class<?> rawType, TypeBindings bindings,
1490 JavaType superClass, JavaType[] superInterfaces)
1491 {
1492 // But that's not all: may be possible current type actually implements an
1493 // interface type. So...
1494 final int intCount = superInterfaces.length;
1495
1496 for (int i = 0; i < intCount; ++i) {
1497 JavaType result = superInterfaces[i].refine(rawType, bindings, superClass, superInterfaces);
1498 if (result != null) {
1499 return result;
1500 }
1501 }
1502 return null;
1503 }
1504
1505 /**
1506 * This method deals with parameterized types, that is,
1507 * first class generic classes.
1508 */
1509 protected JavaType _fromParamType(ClassStack context, ParameterizedType ptype,
1510 TypeBindings parentBindings)
1511 {
1512 // Assumption here is we'll always get Class, not one of other Types
1513 Class<?> rawType = (Class<?>) ptype.getRawType();
1514
1515 // 29-Oct-2015, tatu: For performance reasons, let's streamline handling of
1516 // couple of not-so-useful parametric types
1517 if (rawType == CLS_ENUM) {
1518 return CORE_TYPE_ENUM;
1519 }
1520 if (rawType == CLS_COMPARABLE) {
1521 return CORE_TYPE_COMPARABLE;
1522 }
1523 if (rawType == CLS_CLASS) {
1524 return CORE_TYPE_CLASS;
1525 }
1526
1527 // First: what is the actual base type? One odd thing is that 'getRawType'
1528 // returns Type, not Class<?> as one might expect. But let's assume it is
1529 // always of type Class: if not, need to add more code to resolve it to Class.
1530 Type[] args = ptype.getActualTypeArguments();
1531 int paramCount = (args == null) ? 0 : args.length;
1532 TypeBindings newBindings;
1533
1534 if (paramCount == 0) {
1535 newBindings = EMPTY_BINDINGS;
1536 } else {
1537 JavaType[] pt = new JavaType[paramCount];
1538 for (int i = 0; i < paramCount; ++i) {
1539 pt[i] = _fromAny(context, args[i], parentBindings);
1540 }
1541 newBindings = TypeBindings.create(rawType, pt);
1542 }
1543 return _fromClass(context, rawType, newBindings);
1544 }
1545
1546 protected JavaType _fromArrayType(ClassStack context, GenericArrayType type, TypeBindings bindings)
1547 {
1548 JavaType elementType = _fromAny(context, type.getGenericComponentType(), bindings);
1549 return ArrayType.construct(elementType, bindings);
1550 }
1551
1552 protected JavaType _fromVariable(ClassStack context, TypeVariable<?> var, TypeBindings bindings)
1553 {
1554 // ideally should find it via bindings:
1555 final String name = var.getName();
1556 if (bindings == null) {
1557 throw new IllegalArgumentException("Null `bindings` passed (type variable \""+name+"\")");
1558 }
1559 JavaType type = bindings.findBoundType(name);
1560 if (type != null) {
1561 return type;
1562 }
1563 // but if not, use bounds... note that approach here is simplistic; not taking
1564 // into account possible multiple bounds, nor consider upper bounds.
1565 if (bindings.hasUnbound(name)) {
1566 return CORE_TYPE_OBJECT;
1567 }
1568 bindings = bindings.withUnboundVariable(name);
1569
1570 final Type[] bounds;
1571
1572 // 15-Jan-2019, tatu: As weird as this looks, apparently on some platforms (Arm CPU, mobile
1573 // devices), unsynchronized internal access can lead to issues, see:
1574 //
1575 // https://vmlens.com/articles/java-lang-reflect-typevariable-getbounds-is-not-thread-safe/
1576 //
1577 // No good way to reproduce but since this should not be on critical path, let's add
1578 // syncing as it seems potentially necessary.
1579 synchronized (var) {
1580 bounds = var.getBounds();
1581 }
1582 return _fromAny(context, bounds[0], bindings);
1583 }
1584
1585 protected JavaType _fromWildcard(ClassStack context, WildcardType type, TypeBindings bindings)
1586 {
1587 /* Similar to challenges with TypeVariable, we may have multiple upper bounds.
1588 * But it is also possible that if upper bound defaults to Object, we might
1589 * want to consider lower bounds instead.
1590 * For now, we won't try anything more advanced; above is just for future reference.
1591 */
1592 return _fromAny(context, type.getUpperBounds()[0], bindings);
1593 }
1594 }
1595