-
Notifications
You must be signed in to change notification settings - Fork 748
/
AdapterMethodsFactory.java
404 lines (366 loc) · 15.5 KB
/
AdapterMethodsFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi;
import static com.squareup.moshi.internal.Util.canonicalize;
import static com.squareup.moshi.internal.Util.jsonAnnotations;
import static com.squareup.moshi.internal.Util.typeAnnotatedWithAnnotations;
import com.squareup.moshi.internal.Util;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
final class AdapterMethodsFactory implements JsonAdapter.Factory {
private final List<AdapterMethod> toAdapters;
private final List<AdapterMethod> fromAdapters;
AdapterMethodsFactory(List<AdapterMethod> toAdapters, List<AdapterMethod> fromAdapters) {
this.toAdapters = toAdapters;
this.fromAdapters = fromAdapters;
}
@Override
public @Nullable JsonAdapter<?> create(
final Type type, final Set<? extends Annotation> annotations, final Moshi moshi) {
final AdapterMethod toAdapter = get(toAdapters, type, annotations);
final AdapterMethod fromAdapter = get(fromAdapters, type, annotations);
if (toAdapter == null && fromAdapter == null) return null;
final JsonAdapter<Object> delegate;
if (toAdapter == null || fromAdapter == null) {
try {
delegate = moshi.nextAdapter(this, type, annotations);
} catch (IllegalArgumentException e) {
String missingAnnotation = toAdapter == null ? "@ToJson" : "@FromJson";
throw new IllegalArgumentException(
"No "
+ missingAnnotation
+ " adapter for "
+ typeAnnotatedWithAnnotations(type, annotations),
e);
}
} else {
delegate = null;
}
if (toAdapter != null) toAdapter.bind(moshi, this);
if (fromAdapter != null) fromAdapter.bind(moshi, this);
return new JsonAdapter<Object>() {
@Override
public void toJson(JsonWriter writer, @Nullable Object value) throws IOException {
if (toAdapter == null) {
delegate.toJson(writer, value);
} else if (!toAdapter.nullable && value == null) {
writer.nullValue();
} else {
try {
toAdapter.toJson(moshi, writer, value);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException) throw (IOException) cause;
throw new JsonDataException(cause + " at " + writer.getPath(), cause);
}
}
}
@Override
public @Nullable Object fromJson(JsonReader reader) throws IOException {
if (fromAdapter == null) {
return delegate.fromJson(reader);
} else if (!fromAdapter.nullable && reader.peek() == JsonReader.Token.NULL) {
reader.nextNull();
return null;
} else {
try {
return fromAdapter.fromJson(moshi, reader);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException) throw (IOException) cause;
throw new JsonDataException(cause + " at " + reader.getPath(), cause);
}
}
}
@Override
public String toString() {
return "JsonAdapter" + annotations + "(" + type + ")";
}
};
}
public static AdapterMethodsFactory get(Object adapter) {
List<AdapterMethod> toAdapters = new ArrayList<>();
List<AdapterMethod> fromAdapters = new ArrayList<>();
for (Class<?> c = adapter.getClass(); c != Object.class; c = c.getSuperclass()) {
for (Method m : c.getDeclaredMethods()) {
if (m.isAnnotationPresent(ToJson.class)) {
AdapterMethod toAdapter = toAdapter(adapter, m);
AdapterMethod conflicting = get(toAdapters, toAdapter.type, toAdapter.annotations);
if (conflicting != null) {
throw new IllegalArgumentException(
"Conflicting @ToJson methods:\n"
+ " "
+ conflicting.method
+ "\n"
+ " "
+ toAdapter.method);
}
toAdapters.add(toAdapter);
}
if (m.isAnnotationPresent(FromJson.class)) {
AdapterMethod fromAdapter = fromAdapter(adapter, m);
AdapterMethod conflicting = get(fromAdapters, fromAdapter.type, fromAdapter.annotations);
if (conflicting != null) {
throw new IllegalArgumentException(
"Conflicting @FromJson methods:\n"
+ " "
+ conflicting.method
+ "\n"
+ " "
+ fromAdapter.method);
}
fromAdapters.add(fromAdapter);
}
}
}
if (toAdapters.isEmpty() && fromAdapters.isEmpty()) {
throw new IllegalArgumentException(
"Expected at least one @ToJson or @FromJson method on " + adapter.getClass().getName());
}
return new AdapterMethodsFactory(toAdapters, fromAdapters);
}
/**
* Returns an object that calls a {@code method} method on {@code adapter} in service of
* converting an object to JSON.
*/
static AdapterMethod toAdapter(Object adapter, Method method) {
method.setAccessible(true);
final Type returnType = method.getGenericReturnType();
final Type[] parameterTypes = method.getGenericParameterTypes();
final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
if (parameterTypes.length >= 2
&& parameterTypes[0] == JsonWriter.class
&& returnType == void.class
&& parametersAreJsonAdapters(2, parameterTypes)) {
// void pointToJson(JsonWriter jsonWriter, Point point) {
// void pointToJson(JsonWriter jsonWriter, Point point, JsonAdapter<?> adapter, ...) {
Set<? extends Annotation> qualifierAnnotations = jsonAnnotations(parameterAnnotations[1]);
return new AdapterMethod(
parameterTypes[1],
qualifierAnnotations,
adapter,
method,
parameterTypes.length,
2,
true) {
@Override
public void toJson(Moshi moshi, JsonWriter writer, @Nullable Object value)
throws IOException, InvocationTargetException {
invoke(writer, value);
}
};
} else if (parameterTypes.length == 1 && returnType != void.class) {
// List<Integer> pointToJson(Point point) {
final Set<? extends Annotation> returnTypeAnnotations = jsonAnnotations(method);
final Set<? extends Annotation> qualifierAnnotations =
jsonAnnotations(parameterAnnotations[0]);
boolean nullable = Util.hasNullable(parameterAnnotations[0]);
return new AdapterMethod(
parameterTypes[0],
qualifierAnnotations,
adapter,
method,
parameterTypes.length,
1,
nullable) {
private JsonAdapter<Object> delegate;
@Override
public void bind(Moshi moshi, JsonAdapter.Factory factory) {
super.bind(moshi, factory);
delegate =
Types.equals(parameterTypes[0], returnType)
&& qualifierAnnotations.equals(returnTypeAnnotations)
? moshi.nextAdapter(factory, returnType, returnTypeAnnotations)
: moshi.adapter(returnType, returnTypeAnnotations);
}
@Override
public void toJson(Moshi moshi, JsonWriter writer, @Nullable Object value)
throws IOException, InvocationTargetException {
Object intermediate = invoke(value);
delegate.toJson(writer, intermediate);
}
};
} else {
throw new IllegalArgumentException(
"Unexpected signature for "
+ method
+ ".\n"
+ "@ToJson method signatures may have one of the following structures:\n"
+ " <any access modifier> void toJson(JsonWriter writer, T value) throws <any>;\n"
+ " <any access modifier> void toJson(JsonWriter writer, T value,"
+ " JsonAdapter<any> delegate, <any more delegates>) throws <any>;\n"
+ " <any access modifier> R toJson(T value) throws <any>;\n");
}
}
/** Returns true if {@code parameterTypes[offset..]} contains only JsonAdapters. */
private static boolean parametersAreJsonAdapters(int offset, Type[] parameterTypes) {
for (int i = offset, length = parameterTypes.length; i < length; i++) {
if (!(parameterTypes[i] instanceof ParameterizedType)) return false;
if (((ParameterizedType) parameterTypes[i]).getRawType() != JsonAdapter.class) return false;
}
return true;
}
/**
* Returns an object that calls a {@code method} method on {@code adapter} in service of
* converting an object from JSON.
*/
static AdapterMethod fromAdapter(Object adapter, Method method) {
method.setAccessible(true);
final Type returnType = method.getGenericReturnType();
final Set<? extends Annotation> returnTypeAnnotations = jsonAnnotations(method);
final Type[] parameterTypes = method.getGenericParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
if (parameterTypes.length >= 1
&& parameterTypes[0] == JsonReader.class
&& returnType != void.class
&& parametersAreJsonAdapters(1, parameterTypes)) {
// Point pointFromJson(JsonReader jsonReader) {
// Point pointFromJson(JsonReader jsonReader, JsonAdapter<?> adapter, ...) {
return new AdapterMethod(
returnType, returnTypeAnnotations, adapter, method, parameterTypes.length, 1, true) {
@Override
public Object fromJson(Moshi moshi, JsonReader reader)
throws IOException, InvocationTargetException {
return invoke(reader);
}
};
} else if (parameterTypes.length == 1 && returnType != void.class) {
// Point pointFromJson(List<Integer> o) {
final Set<? extends Annotation> qualifierAnnotations =
jsonAnnotations(parameterAnnotations[0]);
boolean nullable = Util.hasNullable(parameterAnnotations[0]);
return new AdapterMethod(
returnType, returnTypeAnnotations, adapter, method, parameterTypes.length, 1, nullable) {
JsonAdapter<Object> delegate;
@Override
public void bind(Moshi moshi, JsonAdapter.Factory factory) {
super.bind(moshi, factory);
delegate =
Types.equals(parameterTypes[0], returnType)
&& qualifierAnnotations.equals(returnTypeAnnotations)
? moshi.nextAdapter(factory, parameterTypes[0], qualifierAnnotations)
: moshi.adapter(parameterTypes[0], qualifierAnnotations);
}
@Override
public Object fromJson(Moshi moshi, JsonReader reader)
throws IOException, InvocationTargetException {
Object intermediate = delegate.fromJson(reader);
return invoke(intermediate);
}
};
} else {
throw new IllegalArgumentException(
"Unexpected signature for "
+ method
+ ".\n"
+ "@FromJson method signatures may have one of the following structures:\n"
+ " <any access modifier> R fromJson(JsonReader jsonReader) throws <any>;\n"
+ " <any access modifier> R fromJson(JsonReader jsonReader,"
+ " JsonAdapter<any> delegate, <any more delegates>) throws <any>;\n"
+ " <any access modifier> R fromJson(T value) throws <any>;\n");
}
}
/** Returns the matching adapter method from the list. */
private static @Nullable AdapterMethod get(
List<AdapterMethod> adapterMethods, Type type, Set<? extends Annotation> annotations) {
for (int i = 0, size = adapterMethods.size(); i < size; i++) {
AdapterMethod adapterMethod = adapterMethods.get(i);
if (Types.equals(adapterMethod.type, type) && adapterMethod.annotations.equals(annotations)) {
return adapterMethod;
}
}
return null;
}
abstract static class AdapterMethod {
final Type type;
final Set<? extends Annotation> annotations;
final Object adapter;
final Method method;
final int adaptersOffset;
final JsonAdapter<?>[] jsonAdapters;
final boolean nullable;
AdapterMethod(
Type type,
Set<? extends Annotation> annotations,
Object adapter,
Method method,
int parameterCount,
int adaptersOffset,
boolean nullable) {
this.type = canonicalize(type);
this.annotations = annotations;
this.adapter = adapter;
this.method = method;
this.adaptersOffset = adaptersOffset;
this.jsonAdapters = new JsonAdapter[parameterCount - adaptersOffset];
this.nullable = nullable;
}
public void bind(Moshi moshi, JsonAdapter.Factory factory) {
if (jsonAdapters.length > 0) {
Type[] parameterTypes = method.getGenericParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = adaptersOffset, size = parameterTypes.length; i < size; i++) {
Type type = ((ParameterizedType) parameterTypes[i]).getActualTypeArguments()[0];
Set<? extends Annotation> jsonAnnotations = jsonAnnotations(parameterAnnotations[i]);
jsonAdapters[i - adaptersOffset] =
Types.equals(this.type, type) && annotations.equals(jsonAnnotations)
? moshi.nextAdapter(factory, type, jsonAnnotations)
: moshi.adapter(type, jsonAnnotations);
}
}
}
public void toJson(Moshi moshi, JsonWriter writer, @Nullable Object value)
throws IOException, InvocationTargetException {
throw new AssertionError();
}
public @Nullable Object fromJson(Moshi moshi, JsonReader reader)
throws IOException, InvocationTargetException {
throw new AssertionError();
}
/** Invoke the method with one fixed argument, plus any number of JSON adapter arguments. */
protected @Nullable Object invoke(@Nullable Object a1) throws InvocationTargetException {
Object[] args = new Object[1 + jsonAdapters.length];
args[0] = a1;
System.arraycopy(jsonAdapters, 0, args, 1, jsonAdapters.length);
try {
return method.invoke(adapter, args);
} catch (IllegalAccessException e) {
throw new AssertionError();
}
}
/** Invoke the method with two fixed arguments, plus any number of JSON adapter arguments. */
protected Object invoke(@Nullable Object a1, @Nullable Object a2)
throws InvocationTargetException {
Object[] args = new Object[2 + jsonAdapters.length];
args[0] = a1;
args[1] = a2;
System.arraycopy(jsonAdapters, 0, args, 2, jsonAdapters.length);
try {
return method.invoke(adapter, args);
} catch (IllegalAccessException e) {
throw new AssertionError();
}
}
}
}