1 /*
2  * Copyright (C) 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.google.gson.internal.bind;
17
18 import java.io.IOException;
19 import java.lang.reflect.Type;
20 import java.lang.reflect.TypeVariable;
21
22 import com.google.gson.Gson;
23 import com.google.gson.TypeAdapter;
24 import com.google.gson.reflect.TypeToken;
25 import com.google.gson.stream.JsonReader;
26 import com.google.gson.stream.JsonWriter;
27
28 final class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
29   private final Gson context;
30   private final TypeAdapter<T> delegate;
31   private final Type type;
32
33   TypeAdapterRuntimeTypeWrapper(Gson context, TypeAdapter<T> delegate, Type type) {
34     this.context = context;
35     this.delegate = delegate;
36     this.type = type;
37   }
38
39   @Override
40   public T read(JsonReader in) throws IOException {
41     return delegate.read(in);
42   }
43
44   @SuppressWarnings({"rawtypes""unchecked"})
45   @Override
46   public void write(JsonWriter out, T value) throws IOException {
47     // Order of preference for choosing type adapters
48     // First preference: a type adapter registered for the runtime type
49     // Second preference: a type adapter registered for the declared type
50     // Third preference: reflective type adapter for the runtime type (if it is a sub class of the declared type)
51     // Fourth preference: reflective type adapter for the declared type
52
53     TypeAdapter chosen = delegate;
54     Type runtimeType = getRuntimeTypeIfMoreSpecific(type, value);
55     if (runtimeType != type) {
56       TypeAdapter runtimeTypeAdapter = context.getAdapter(TypeToken.get(runtimeType));
57       if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) {
58         // The user registered a type adapter for the runtime type, so we will use that
59         chosen = runtimeTypeAdapter;
60       } else if (!(delegate instanceof ReflectiveTypeAdapterFactory.Adapter)) {
61         // The user registered a type adapter for Base class, so we prefer it over the
62         // reflective type adapter for the runtime type
63         chosen = delegate;
64       } else {
65         // Use the type adapter for runtime type
66         chosen = runtimeTypeAdapter;
67       }
68     }
69     chosen.write(out, value);
70   }
71
72   /**
73    * Finds a compatible runtime type if it is more specific
74    */

75   private Type getRuntimeTypeIfMoreSpecific(Type type, Object value) {
76     if (value != null
77         && (type == Object.class || type instanceof TypeVariable<?> || type instanceof Class<?>)) {
78       type = value.getClass();
79     }
80     return type;
81   }
82 }
83