16
16
17
17
package com .google .gson .internal .bind ;
18
18
19
- import java .util .HashMap ;
20
- import java .util .Map ;
21
-
22
19
import com .google .gson .Gson ;
23
20
import com .google .gson .JsonDeserializer ;
24
21
import com .google .gson .JsonSerializer ;
35
32
* @since 2.3
36
33
*/
37
34
public final class JsonAdapterAnnotationTypeAdapterFactory implements TypeAdapterFactory {
38
-
39
- @ SuppressWarnings ("rawtypes" )
40
- private final ThreadLocal <Map <Class , TypeAdapterFactory >> activeJsonAdapterFactories = new ThreadLocal <Map <Class , TypeAdapterFactory >>() {
41
- @ Override protected Map <Class , TypeAdapterFactory > initialValue () {
42
- // No need for a thread-safe map since we are using it in a single thread
43
- return new HashMap <Class , TypeAdapterFactory >();
44
- }
45
- };
46
-
47
35
private final ConstructorConstructor constructorConstructor ;
48
36
49
37
public JsonAdapterAnnotationTypeAdapterFactory (ConstructorConstructor constructorConstructor ) {
@@ -61,75 +49,34 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
61
49
return (TypeAdapter <T >) getTypeAdapter (constructorConstructor , gson , targetType , annotation );
62
50
}
63
51
64
- public <T > TypeAdapter <T > getDelegateAdapter (Gson gson , TypeAdapterFactory skipPast , TypeToken <T > targetType ) {
65
- TypeAdapterFactory factory = getDelegateAdapterFactory (targetType );
66
- if (factory == skipPast ) factory = null ;
67
- return factory == null ? null : factory .create (gson , targetType );
68
- }
69
-
70
- public <T > TypeAdapterFactory getDelegateAdapterFactory (TypeToken <T > targetType ) {
71
- Class <?> annotatedClass = targetType .getRawType ();
72
- JsonAdapter annotation = annotatedClass .getAnnotation (JsonAdapter .class );
73
- if (annotation == null ) {
74
- return null ;
75
- }
76
- return getTypeAdapterFactory (annotation , constructorConstructor );
77
- }
78
-
79
52
@ SuppressWarnings ({ "unchecked" , "rawtypes" }) // Casts guarded by conditionals.
80
53
TypeAdapter <?> getTypeAdapter (ConstructorConstructor constructorConstructor , Gson gson ,
81
54
TypeToken <?> type , JsonAdapter annotation ) {
82
- Class <?> value = annotation .value ();
83
- boolean isTypeAdapter = TypeAdapter .class .isAssignableFrom (value );
84
- boolean isJsonSerializer = JsonSerializer .class .isAssignableFrom (value );
85
- boolean isJsonDeserializer = JsonDeserializer .class .isAssignableFrom (value );
55
+ Object instance = constructorConstructor .get (TypeToken .get (annotation .value ())).construct ();
86
56
87
57
TypeAdapter <?> typeAdapter ;
88
- if (isTypeAdapter || isJsonSerializer || isJsonDeserializer ) {
89
- if (isTypeAdapter ) {
90
- Class <TypeAdapter <?>> typeAdapterClass = (Class <TypeAdapter <?>>) value ;
91
- typeAdapter = constructorConstructor .get (TypeToken .get (typeAdapterClass )).construct ();
92
- } else if (isJsonSerializer || isJsonDeserializer ) {
93
- JsonSerializer serializer = null ;
94
- if (isJsonSerializer ) {
95
- Class <JsonSerializer <?>> serializerClass = (Class <JsonSerializer <?>>) value ;
96
- serializer = constructorConstructor .get (TypeToken .get (serializerClass )).construct ();
97
- }
98
- JsonDeserializer deserializer = null ;
99
- if (isJsonDeserializer ) {
100
- Class <JsonDeserializer <?>> deserializerClass = (Class <JsonDeserializer <?>>) value ;
101
- deserializer = constructorConstructor .get (TypeToken .get (deserializerClass )).construct ();
102
- }
103
- typeAdapter = new TreeTypeAdapter (serializer , deserializer , gson , type , null );
104
- } else {
105
- typeAdapter = null ;
106
- }
107
- } else if (TypeAdapterFactory .class .isAssignableFrom (value )) {
108
- TypeAdapterFactory factory = getTypeAdapterFactory (annotation , constructorConstructor );
109
- typeAdapter = factory == null ? null : factory .create (gson , type );
58
+ if (instance instanceof TypeAdapter ) {
59
+ typeAdapter = (TypeAdapter <?>) instance ;
60
+ } else if (instance instanceof TypeAdapterFactory ) {
61
+ typeAdapter = ((TypeAdapterFactory ) instance ).create (gson , type );
62
+ } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer ) {
63
+ JsonSerializer <?> serializer = instance instanceof JsonSerializer
64
+ ? (JsonSerializer ) instance
65
+ : null ;
66
+ JsonDeserializer <?> deserializer = instance instanceof JsonDeserializer
67
+ ? (JsonDeserializer ) instance
68
+ : null ;
69
+ typeAdapter = new TreeTypeAdapter (serializer , deserializer , gson , type , null );
110
70
} else {
111
71
throw new IllegalArgumentException (
112
- "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, JsonSerializer or JsonDeserializer reference." );
72
+ "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
73
+ + "JsonSerializer or JsonDeserializer reference." );
113
74
}
75
+
114
76
if (typeAdapter != null ) {
115
77
typeAdapter = typeAdapter .nullSafe ();
116
78
}
117
- return typeAdapter ;
118
- }
119
79
120
-
121
- @ SuppressWarnings ({ "unchecked" , "rawtypes" }) // Casts guarded by conditionals.
122
- TypeAdapterFactory getTypeAdapterFactory (JsonAdapter annotation , ConstructorConstructor constructorConstructor ) {
123
- Class <?> value = annotation .value ();
124
- if (!TypeAdapterFactory .class .isAssignableFrom (value )) return null ;
125
- Map <Class , TypeAdapterFactory > adapterFactories = activeJsonAdapterFactories .get ();
126
- TypeAdapterFactory factory = adapterFactories .get (value );
127
- if (factory == null ) {
128
- Class <TypeAdapterFactory > typeAdapterFactoryClass = (Class <TypeAdapterFactory >) value ;
129
- factory = constructorConstructor .get (TypeToken .get (typeAdapterFactoryClass ))
130
- .construct ();
131
- adapterFactories .put (value , factory );
132
- }
133
- return factory ;
80
+ return typeAdapter ;
134
81
}
135
82
}
0 commit comments