/
BindingClass.java
529 lines (457 loc) · 19.6 KB
/
BindingClass.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
package butterknife.compiler;
import butterknife.internal.ListenerClass;
import butterknife.internal.ListenerMethod;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import com.squareup.javapoet.WildcardTypeName;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static butterknife.compiler.ButterKnifeProcessor.NO_ID;
import static butterknife.compiler.ButterKnifeProcessor.VIEW_TYPE;
import static java.util.Collections.singletonList;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;
final class BindingClass {
private static final ClassName FINDER = ClassName.get("butterknife.internal", "Finder");
private static final ClassName VIEW_BINDER = ClassName.get("butterknife.internal", "ViewBinder");
private static final ClassName UNBINDER = ClassName.get("butterknife", "ButterKnife", "Unbinder");
private static final ClassName UTILS = ClassName.get("butterknife.internal", "Utils");
private static final ClassName VIEW = ClassName.get("android.view", "View");
private static final ClassName CONTEXT = ClassName.get("android.content", "Context");
private static final ClassName RESOURCES = ClassName.get("android.content.res", "Resources");
private static final ClassName THEME = RESOURCES.nestedClass("Theme");
private static final ClassName BITMAP_FACTORY =
ClassName.get("android.graphics", "BitmapFactory");
private final Map<Integer, ViewBindings> viewIdMap = new LinkedHashMap<>();
private final Map<FieldCollectionViewBinding, int[]> collectionBindings = new LinkedHashMap<>();
private final List<FieldBitmapBinding> bitmapBindings = new ArrayList<>();
private final List<FieldDrawableBinding> drawableBindings = new ArrayList<>();
private final List<FieldResourceBinding> resourceBindings = new ArrayList<>();
private final String classPackage;
private final String className;
private final String targetClass;
private String parentViewBinder;
private UnbinderBinding unbinderBinding;
BindingClass(String classPackage, String className, String targetClass) {
this.classPackage = classPackage;
this.className = className;
this.targetClass = targetClass;
}
void requiresUnbinder(String unbinderFieldName) {
unbinderBinding = new UnbinderBinding(classPackage, className, unbinderFieldName);
}
void addBitmap(FieldBitmapBinding binding) {
bitmapBindings.add(binding);
}
void addDrawable(FieldDrawableBinding binding) {
drawableBindings.add(binding);
}
void addField(int id, FieldViewBinding binding) {
getOrCreateViewBindings(id).addFieldBinding(binding);
}
void addFieldCollection(int[] ids, FieldCollectionViewBinding binding) {
collectionBindings.put(binding, ids);
}
boolean addMethod(int id, ListenerClass listener, ListenerMethod method,
MethodViewBinding binding) {
ViewBindings viewBindings = getOrCreateViewBindings(id);
if (viewBindings.hasMethodBinding(listener, method) && !"void".equals(method.returnType())) {
return false;
}
viewBindings.addMethodBinding(listener, method, binding);
return true;
}
void addResource(FieldResourceBinding binding) {
resourceBindings.add(binding);
}
void setParentViewBinder(String parentViewBinder) {
this.parentViewBinder = parentViewBinder;
}
ViewBindings getViewBinding(int id) {
return viewIdMap.get(id);
}
private ViewBindings getOrCreateViewBindings(int id) {
ViewBindings viewId = viewIdMap.get(id);
if (viewId == null) {
viewId = new ViewBindings(id);
viewIdMap.put(id, viewId);
}
return viewId;
}
JavaFile brewJava() {
TypeSpec.Builder result = TypeSpec.classBuilder(className)
.addModifiers(PUBLIC)
.addTypeVariable(TypeVariableName.get("T", ClassName.bestGuess(targetClass)));
if (parentViewBinder != null) {
result.superclass(ParameterizedTypeName.get(ClassName.bestGuess(parentViewBinder),
TypeVariableName.get("T")));
} else {
result.addSuperinterface(ParameterizedTypeName.get(VIEW_BINDER, TypeVariableName.get("T")));
}
if (hasUnbinder()) {
result.addType(createUnbinderClass());
}
result.addMethod(createBindMethod());
return JavaFile.builder(classPackage, result.build())
.addFileComment("Generated code from Butter Knife. Do not modify!")
.build();
}
private TypeSpec createUnbinderClass() {
ClassName targetClassName = ClassName.bestGuess(targetClass);
MethodSpec unbinderConstructor = MethodSpec.constructorBuilder()
.addParameter(targetClassName, "target")
.addStatement("this.$1N = $1N", "target")
.build();
TypeSpec.Builder result =
TypeSpec.classBuilder(unbinderBinding.getUnbinderClassName().simpleName())
.addSuperinterface(UNBINDER)
.addModifiers(PRIVATE, STATIC, FINAL)
.addField(targetClassName, "target", PRIVATE)
.addMethod(unbinderConstructor);
// Even if there are no bindings we need to implement the interface method.
MethodSpec.Builder unbindMethod = MethodSpec.methodBuilder("unbind")
.addAnnotation(Override.class)
.addModifiers(PUBLIC);
// Throw exception if unbind called twice.
unbindMethod.addStatement("if (target == null) throw new $T($S)", IllegalStateException.class,
"Bindings already cleared.");
for (ViewBindings bindings : viewIdMap.values()) {
addFieldAndUnbindStatement(result, unbindMethod, bindings);
for (FieldViewBinding fieldBinding : bindings.getFieldBindings()) {
unbindMethod.addStatement("target.$L = null", fieldBinding.getName());
}
}
for (FieldCollectionViewBinding fieldCollectionBinding : collectionBindings.keySet()) {
unbindMethod.addStatement("target.$L = null", fieldCollectionBinding.getName());
}
unbindMethod.addStatement("target.$L = null", unbinderBinding.getUnbinderFieldName());
unbindMethod.addStatement("target = null");
result.addMethod(unbindMethod.build());
return result.build();
}
private void addFieldAndUnbindStatement(TypeSpec.Builder result, MethodSpec.Builder unbindMethod,
ViewBindings bindings) {
// Only add fields to the unbinder if there are method bindings.
Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings =
bindings.getMethodBindings();
if (classMethodBindings.isEmpty()) {
return;
}
// Using view id for name uniqueness.
String fieldName = "view" + bindings.getId();
result.addField(VIEW, fieldName);
// We only need to emit the null check if there are zero required bindings.
boolean needsNullChecked = bindings.getRequiredBindings().isEmpty();
if (needsNullChecked) {
unbindMethod.beginControlFlow("if ($L != null)", fieldName);
}
for (ListenerClass listenerClass : classMethodBindings.keySet()) {
if (!VIEW_TYPE.equals(listenerClass.targetType())) {
unbindMethod.addStatement("(($T) $L).$L(null)", bestGuess(listenerClass.targetType()),
fieldName, listenerClass.setter());
} else {
unbindMethod.addStatement("$L.$L(null)", fieldName, listenerClass.setter());
}
}
if (needsNullChecked) {
unbindMethod.endControlFlow();
}
}
private MethodSpec createBindMethod() {
MethodSpec.Builder result = MethodSpec.methodBuilder("bind")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.addParameter(FINDER, "finder", FINAL)
.addParameter(TypeVariableName.get("T"), "target", FINAL)
.addParameter(Object.class, "source");
if (hasResourceBindings()) {
// Aapt can change IDs out from underneath us, just suppress since all will work at runtime.
result.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "ResourceType")
.build());
}
// Emit a call to the superclass binder, if any.
if (parentViewBinder != null) {
result.addStatement("super.bind(finder, target, source)");
}
// If the caller requested an unbinder, we need to create an instance of it.
if (hasUnbinder()) {
result.addStatement("$T unbinder = new $T($N)", unbinderBinding.getUnbinderClassName(),
unbinderBinding.getUnbinderClassName(), "target");
}
if (!viewIdMap.isEmpty() || !collectionBindings.isEmpty()) {
// Local variable in which all views will be temporarily stored.
result.addStatement("$T view", VIEW);
// Loop over each view bindings and emit it.
for (ViewBindings bindings : viewIdMap.values()) {
addViewBindings(result, bindings);
}
// Loop over each collection binding and emit it.
for (Map.Entry<FieldCollectionViewBinding, int[]> entry : collectionBindings.entrySet()) {
emitCollectionBinding(result, entry.getKey(), entry.getValue());
}
}
// Bind unbinder if was requested.
if (hasUnbinder()) {
result.addStatement("target.$L = unbinder", unbinderBinding.getUnbinderFieldName());
}
if (hasResourceBindings()) {
if (hasResourceBindingsNeedingTheme()) {
result.addStatement("$T context = finder.getContext(source)", CONTEXT);
result.addStatement("$T res = context.getResources()", RESOURCES);
result.addStatement("$T theme = context.getTheme()", THEME);
} else {
result.addStatement("$T res = finder.getContext(source).getResources()", RESOURCES);
}
for (FieldBitmapBinding binding : bitmapBindings) {
result.addStatement("target.$L = $T.decodeResource(res, $L)", binding.getName(),
BITMAP_FACTORY, binding.getId());
}
for (FieldDrawableBinding binding : drawableBindings) {
int tintAttributeId = binding.getTintAttributeId();
if (tintAttributeId != 0) {
result.addStatement("target.$L = $T.getTintedDrawable(res, theme, $L, $L)",
binding.getName(), UTILS, binding.getId(), tintAttributeId);
} else {
result.addStatement("target.$L = $T.getDrawable(res, theme, $L)", binding.getName(),
UTILS, binding.getId());
}
}
for (FieldResourceBinding binding : resourceBindings) {
// TODO being themeable is poor correlation to the need to use Utils.
if (binding.isThemeable()) {
result.addStatement("target.$L = $T.$L(res, theme, $L)", binding.getName(),
UTILS, binding.getMethod(), binding.getId());
} else {
result.addStatement("target.$L = res.$L($L)", binding.getName(), binding.getMethod(),
binding.getId());
}
}
}
return result.build();
}
private void emitCollectionBinding(MethodSpec.Builder result, FieldCollectionViewBinding binding,
int[] ids) {
String ofName;
switch (binding.getKind()) {
case ARRAY:
ofName = "arrayOf";
break;
case LIST:
ofName = "listOf";
break;
default:
throw new IllegalStateException("Unknown kind: " + binding.getKind());
}
CodeBlock.Builder builder = CodeBlock.builder();
for (int i = 0; i < ids.length; i++) {
if (i > 0) {
builder.add(", ");
}
String findMethod = binding.isRequired() ? "findRequiredView" : "findOptionalView";
builder.add("\nfinder.<$T>$L(source, $L, $S)", binding.getType(), findMethod, ids[i],
asHumanDescription(singletonList(binding)));
}
result.addStatement("target.$L = $T.$L($L)", binding.getName(), UTILS, ofName, builder.build());
}
private void addViewBindings(MethodSpec.Builder result, ViewBindings bindings) {
List<ViewBinding> requiredViewBindings = bindings.getRequiredBindings();
if (requiredViewBindings.isEmpty()) {
result.addStatement("view = finder.findOptionalView(source, $L, null)", bindings.getId());
} else {
if (bindings.getId() == NO_ID) {
result.addStatement("view = target");
} else {
result.addStatement("view = finder.findRequiredView(source, $L, $S)", bindings.getId(),
asHumanDescription(requiredViewBindings));
}
}
addFieldBindings(result, bindings);
addMethodBindings(result, bindings);
}
private void addFieldBindings(MethodSpec.Builder result, ViewBindings bindings) {
Collection<FieldViewBinding> fieldBindings = bindings.getFieldBindings();
for (FieldViewBinding fieldBinding : fieldBindings) {
if (fieldBinding.requiresCast()) {
result.addStatement("target.$L = finder.castView(view, $L, $S)", fieldBinding.getName(),
bindings.getId(), asHumanDescription(fieldBindings));
} else {
result.addStatement("target.$L = view", fieldBinding.getName());
}
}
}
private void addMethodBindings(MethodSpec.Builder result, ViewBindings bindings) {
Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings =
bindings.getMethodBindings();
if (classMethodBindings.isEmpty()) {
return;
}
// We only need to emit the null check if there are zero required bindings.
boolean needsNullChecked = bindings.getRequiredBindings().isEmpty();
if (needsNullChecked) {
result.beginControlFlow("if (view != null)");
}
// Add the view reference to the unbinder.
if (hasUnbinder()) {
result.addStatement("unbinder.$L = view", "view" + bindings.getId());
}
for (Map.Entry<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> e
: classMethodBindings.entrySet()) {
ListenerClass listener = e.getKey();
Map<ListenerMethod, Set<MethodViewBinding>> methodBindings = e.getValue();
TypeSpec.Builder callback = TypeSpec.anonymousClassBuilder("")
.superclass(ClassName.bestGuess(listener.type()));
for (ListenerMethod method : getListenerMethods(listener)) {
MethodSpec.Builder callbackMethod = MethodSpec.methodBuilder(method.name())
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.returns(bestGuess(method.returnType()));
String[] parameterTypes = method.parameters();
for (int i = 0, count = parameterTypes.length; i < count; i++) {
callbackMethod.addParameter(bestGuess(parameterTypes[i]), "p" + i);
}
boolean hasReturnType = !"void".equals(method.returnType());
CodeBlock.Builder builder = CodeBlock.builder();
if (hasReturnType) {
builder.add("return ");
}
if (methodBindings.containsKey(method)) {
for (MethodViewBinding binding : methodBindings.get(method)) {
builder.add("target.$L(", binding.getName());
List<Parameter> parameters = binding.getParameters();
String[] listenerParameters = method.parameters();
for (int i = 0, count = parameters.size(); i < count; i++) {
if (i > 0) {
builder.add(", ");
}
Parameter parameter = parameters.get(i);
int listenerPosition = parameter.getListenerPosition();
if (parameter.requiresCast(listenerParameters[listenerPosition])) {
builder.add("finder.<$T>castParam(p$L, $S, $L, $S, $L)\n", parameter.getType(),
listenerPosition, method.name(), listenerPosition, binding.getName(), i);
} else {
builder.add("p$L", listenerPosition);
}
}
builder.add(");\n");
}
} else if (hasReturnType) {
builder.add("$L;\n", method.defaultReturn());
}
callbackMethod.addCode(builder.build());
callback.addMethod(callbackMethod.build());
}
if (!VIEW_TYPE.equals(listener.targetType())) {
result.addStatement("(($T) view).$L($L)", bestGuess(listener.targetType()),
listener.setter(), callback.build());
} else {
result.addStatement("view.$L($L)", listener.setter(), callback.build());
}
}
if (needsNullChecked) {
result.endControlFlow();
}
}
static List<ListenerMethod> getListenerMethods(ListenerClass listener) {
if (listener.method().length == 1) {
return Arrays.asList(listener.method());
}
try {
List<ListenerMethod> methods = new ArrayList<>();
Class<? extends Enum<?>> callbacks = listener.callbacks();
for (Enum<?> callbackMethod : callbacks.getEnumConstants()) {
Field callbackField = callbacks.getField(callbackMethod.name());
ListenerMethod method = callbackField.getAnnotation(ListenerMethod.class);
if (method == null) {
throw new IllegalStateException(String.format("@%s's %s.%s missing @%s annotation.",
callbacks.getEnclosingClass().getSimpleName(), callbacks.getSimpleName(),
callbackMethod.name(), ListenerMethod.class.getSimpleName()));
}
methods.add(method);
}
return methods;
} catch (NoSuchFieldException e) {
throw new AssertionError(e);
}
}
static String asHumanDescription(Collection<? extends ViewBinding> bindings) {
Iterator<? extends ViewBinding> iterator = bindings.iterator();
switch (bindings.size()) {
case 1:
return iterator.next().getDescription();
case 2:
return iterator.next().getDescription() + " and " + iterator.next().getDescription();
default:
StringBuilder builder = new StringBuilder();
for (int i = 0, count = bindings.size(); i < count; i++) {
if (i != 0) {
builder.append(", ");
}
if (i == count - 1) {
builder.append("and ");
}
builder.append(iterator.next().getDescription());
}
return builder.toString();
}
}
static TypeName bestGuess(String type) {
switch (type) {
case "void": return TypeName.VOID;
case "boolean": return TypeName.BOOLEAN;
case "byte": return TypeName.BYTE;
case "char": return TypeName.CHAR;
case "double": return TypeName.DOUBLE;
case "float": return TypeName.FLOAT;
case "int": return TypeName.INT;
case "long": return TypeName.LONG;
case "short": return TypeName.SHORT;
default:
int left = type.indexOf('<');
if (left != -1) {
ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
List<TypeName> typeArguments = new ArrayList<>();
do {
typeArguments.add(WildcardTypeName.subtypeOf(Object.class));
left = type.indexOf('<', left + 1);
} while (left != -1);
return ParameterizedTypeName.get(typeClassName,
typeArguments.toArray(new TypeName[typeArguments.size()]));
}
return ClassName.bestGuess(type);
}
}
boolean hasUnbinder() {
return unbinderBinding != null;
}
private boolean hasResourceBindings() {
return !(bitmapBindings.isEmpty() && drawableBindings.isEmpty() && resourceBindings.isEmpty());
}
private boolean hasResourceBindingsNeedingTheme() {
if (!drawableBindings.isEmpty()) {
return true;
}
for (FieldResourceBinding resourceBinding : resourceBindings) {
if (resourceBinding.isThemeable()) {
return true;
}
}
return false;
}
}