1 package com.fasterxml.jackson.datatype.jdk8;
2
3 import java.lang.reflect.Type;
4 import java.util.*;
5
6 import com.fasterxml.jackson.databind.JavaType;
7 import com.fasterxml.jackson.databind.type.ReferenceType;
8 import com.fasterxml.jackson.databind.type.TypeBindings;
9 import com.fasterxml.jackson.databind.type.TypeFactory;
10 import com.fasterxml.jackson.databind.type.TypeModifier;
11
12 /**
13  * We need to ensure `Optional` is a `ReferenceType`
14  */

15 public class Jdk8TypeModifier extends TypeModifier
16     implements java.io.Serializable
17 {
18     private static final long serialVersionUID = 1L;
19
20     @Override
21     public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, TypeFactory typeFactory)
22     {
23         if (type.isReferenceType() || type.isContainerType()) {
24             return type;
25         }
26         final Class<?> raw = type.getRawClass();
27
28         JavaType refType;
29
30         if (raw == Optional.class) {
31             // 19-Oct-2015, tatu: Looks like we may be missing type information occasionally,
32             //    perhaps due to raw types.
33             refType = type.containedTypeOrUnknown(0);
34         } else if (raw == OptionalInt.class) {
35             refType = typeFactory.constructType(Integer.TYPE);
36         } else if (raw == OptionalLong.class) {
37             refType = typeFactory.constructType(Long.TYPE);
38         } else if (raw == OptionalDouble.class) {
39             refType = typeFactory.constructType(Double.TYPE);
40         } else {
41             return type;
42         }
43         return ReferenceType.upgradeFrom(type, refType);
44     }
45 }
46