/
ArgProcessor.java
822 lines (690 loc) · 31 KB
/
ArgProcessor.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
package com.hannesdorfmann.fragmentargs.processor;
import com.hannesdorfmann.fragmentargs.FragmentArgs;
import com.hannesdorfmann.fragmentargs.FragmentArgsInjector;
import com.hannesdorfmann.fragmentargs.annotation.Arg;
import com.hannesdorfmann.fragmentargs.annotation.FragmentArgsInherited;
import com.hannesdorfmann.fragmentargs.annotation.FragmentWithArgs;
import com.hannesdorfmann.fragmentargs.bundler.ArgsBundler;
import com.hannesdorfmann.fragmentargs.repacked.com.squareup.javawriter.JavaWriter;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
/**
* This is the annotation processor for FragmentArgs
*
* @author Hannes Dorfmann
*/
public class ArgProcessor extends AbstractProcessor {
private static final String CUSTOM_BUNDLER_BUNDLE_KEY =
"com.hannesdorfmann.fragmentargs.custom.bundler.2312A478rand.";
private static final Map<String, String> ARGUMENT_TYPES = new HashMap<String, String>(20);
/**
* Annotation Processor Option
*/
private static final String OPTION_IS_LIBRARY = "fragmentArgsLib";
static {
ARGUMENT_TYPES.put("java.lang.String", "String");
ARGUMENT_TYPES.put("int", "Int");
ARGUMENT_TYPES.put("java.lang.Integer", "Int");
ARGUMENT_TYPES.put("long", "Long");
ARGUMENT_TYPES.put("java.lang.Long", "Long");
ARGUMENT_TYPES.put("double", "Double");
ARGUMENT_TYPES.put("java.lang.Double", "Double");
ARGUMENT_TYPES.put("short", "Short");
ARGUMENT_TYPES.put("java.lang.Short", "Short");
ARGUMENT_TYPES.put("float", "Float");
ARGUMENT_TYPES.put("java.lang.Float", "Float");
ARGUMENT_TYPES.put("byte", "Byte");
ARGUMENT_TYPES.put("java.lang.Byte", "Byte");
ARGUMENT_TYPES.put("boolean", "Boolean");
ARGUMENT_TYPES.put("java.lang.Boolean", "Boolean");
ARGUMENT_TYPES.put("char", "Char");
ARGUMENT_TYPES.put("java.lang.Character", "Char");
ARGUMENT_TYPES.put("java.lang.CharSequence", "CharSequence");
ARGUMENT_TYPES.put("android.os.Bundle", "Bundle");
ARGUMENT_TYPES.put("android.os.Parcelable", "Parcelable");
}
private Elements elementUtils;
private Types typeUtils;
private Filer filer;
private TypeElement TYPE_FRAGMENT;
private TypeElement TYPE_SUPPORT_FRAGMENT;
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> supportTypes = new LinkedHashSet<String>();
supportTypes.add(Arg.class.getCanonicalName());
supportTypes.add(FragmentArgsInherited.class.getCanonicalName());
return supportTypes;
}
@Override
public Set<String> getSupportedOptions() {
Set<String> suppotedOptions = new LinkedHashSet<String>();
suppotedOptions.add(OPTION_IS_LIBRARY);
return suppotedOptions;
}
@Override
public synchronized void init(ProcessingEnvironment env) {
super.init(env);
elementUtils = env.getElementUtils();
typeUtils = env.getTypeUtils();
filer = env.getFiler();
TYPE_FRAGMENT = elementUtils.getTypeElement("android.app.Fragment");
TYPE_SUPPORT_FRAGMENT =
elementUtils.getTypeElement("android.support.v4.app.Fragment");
}
protected String getOperation(ArgumentAnnotatedField arg) {
String op = ARGUMENT_TYPES.get(arg.getRawType());
if (op != null) {
if (arg.isArray()) {
return op + "Array";
} else {
return op;
}
}
Elements elements = processingEnv.getElementUtils();
TypeMirror type = arg.getElement().asType();
Types types = processingEnv.getTypeUtils();
String[] arrayListTypes = new String[]{
String.class.getName(), Integer.class.getName(), CharSequence.class.getName()
};
String[] arrayListOps =
new String[]{"StringArrayList", "IntegerArrayList", "CharSequenceArrayList"};
for (int i = 0; i < arrayListTypes.length; i++) {
TypeMirror tm = getArrayListType(arrayListTypes[i]);
if (types.isAssignable(type, tm)) {
return arrayListOps[i];
}
}
if (types.isAssignable(type,
getWildcardType(ArrayList.class.getName(), "android.os.Parcelable"))) {
return "ParcelableArrayList";
}
TypeMirror sparseParcelableArray =
getWildcardType("android.util.SparseArray", "android.os.Parcelable");
if (types.isAssignable(type, sparseParcelableArray)) {
return "SparseParcelableArray";
}
if (types.isAssignable(type, elements.getTypeElement(Serializable.class.getName()).asType())) {
return "Serializable";
}
if (types.isAssignable(type, elements.getTypeElement("android.os.Parcelable").asType())) {
return "Parcelable";
}
return null;
}
private TypeMirror getWildcardType(String type, String elementType) {
TypeElement arrayList = processingEnv.getElementUtils().getTypeElement(type);
TypeMirror elType = processingEnv.getElementUtils().getTypeElement(elementType).asType();
return processingEnv.getTypeUtils()
.getDeclaredType(arrayList, processingEnv.getTypeUtils().getWildcardType(elType, null));
}
private TypeMirror getArrayListType(String elementType) {
TypeElement arrayList = processingEnv.getElementUtils().getTypeElement("java.util.ArrayList");
TypeMirror elType = processingEnv.getElementUtils().getTypeElement(elementType).asType();
return processingEnv.getTypeUtils().getDeclaredType(arrayList, elType);
}
protected void writePutArguments(JavaWriter jw, String sourceVariable, String bundleVariable,
ArgumentAnnotatedField arg) throws IOException, ProcessingException {
jw.emitEmptyLine();
if (arg.hasCustomBundler()) {
jw.emitStatement("%s.putBoolean(\"%s\", true)", bundleVariable,
CUSTOM_BUNDLER_BUNDLE_KEY + arg.getKey());
jw.emitStatement("%s.put(\"%s\", %s, %s)", arg.getBundlerFieldName(), arg.getKey(),
sourceVariable, bundleVariable);
} else {
String op = getOperation(arg);
if (op == null) {
throw new ProcessingException(arg.getElement(),
"Don't know how to put %s in a Bundle. This type is not supported by default. "
+ "However, you can specify your own %s implementation in @Arg( bundler = YourBundler.class)",
arg.getElement().asType().toString(), ArgsBundler.class.getSimpleName());
}
if ("Serializable".equals(op)) {
processingEnv.getMessager()
.printMessage(Diagnostic.Kind.WARNING,
String.format("%1$s will be stored as Serializable", arg.getName()),
arg.getElement());
}
jw.emitStatement("%4$s.put%1$s(\"%2$s\", %3$s)", op, arg.getKey(), sourceVariable,
bundleVariable);
}
}
protected void writePackage(JavaWriter jw, TypeElement type) throws IOException {
PackageElement pkg = processingEnv.getElementUtils().getPackageOf(type);
if (!pkg.isUnnamed()) {
jw.emitPackage(pkg.getQualifiedName().toString());
} else {
jw.emitPackage("");
}
}
/**
* Scans for @Arg annotations in the class itself and all super classes (complete inheritance
* hierarchy)
*/
private AnnotatedFragment getAllInclSuperClasses(TypeElement type) throws ProcessingException {
AnnotatedFragment fragment = new AnnotatedFragment(type);
TypeElement currentClass = type;
do {
for (Element e : currentClass.getEnclosedElements()) {
if (e.getKind() != ElementKind.FIELD) {
continue;
}
// It's a field
Arg annotation = null;
if ((annotation = e.getAnnotation(Arg.class)) != null) {
ArgumentAnnotatedField annotatedField =
new ArgumentAnnotatedField(e, (TypeElement) e.getEnclosingElement(), annotation);
addAnnotatedField(annotatedField, fragment, annotation);
}
}
TypeMirror superClassType = currentClass.getSuperclass();
if (superClassType.getKind() == TypeKind.NONE) {
// Basis class (java.lang.Object) reached, so exit
currentClass = null;
break;
} else {
currentClass = (TypeElement) typeUtils.asElement(superClassType);
}
} while (currentClass != null);
return fragment;
}
/**
* Generates an error String with detailed information about that a field with the same name is
* already defined in a super class
*/
private String getErrorMessageDuplicatedField(AnnotatedFragment fragment,
TypeElement problemClass, String fieldName) {
String base =
"A field with the name '%s' in class %s is already annotated with @%s in super class %s ! "
+ "Fields name must be unique within inheritance hierarchy.";
// Assumption: The problemClass is already a super class of the real problem,
// So determine the real problem by searching for the subclass that cause this problem
TypeElement otherClass = null;
for (ArgumentAnnotatedField otherField : fragment.getAll()) {
if (otherField.getVariableName().equals(fieldName)) {
otherClass = otherField.getClassElement();
break;
}
}
if (otherClass != null) {
// Check who is the super class
TypeElement currentClass = otherClass;
while (currentClass != null) {
TypeMirror currentClassSuperclass = currentClass.getSuperclass();
if (currentClassSuperclass == null || currentClassSuperclass.getKind() == TypeKind.NONE) {
// They are not super classes
break;
}
if (currentClass.getQualifiedName() != null && currentClass.getQualifiedName()
.toString()
.equals(problemClass.getQualifiedName().toString())) {
// The problem causing class is a super class, so we found the superclass
// and the sub class that cause the problem
return String.format(base, fieldName, otherClass.getQualifiedName().toString(),
Arg.class.getSimpleName(), problemClass.getQualifiedName());
}
currentClass = (TypeElement) typeUtils.asElement(currentClassSuperclass);
}
}
// Since the previous check wasn't successfull we can assume:
// The problemClass must be a sub class, so find the super class that contains the field
TypeMirror superClass = problemClass.getSuperclass();
TypeElement superClassElement = null;
if (superClass == null) {
return String.format(
"A field with the name '%s' in class %s is already annotated with @%s in a super class or sub class! "
+ "Fields name must be unique within inheritance hierarchy.", fieldName,
problemClass.getQualifiedName().toString(), Arg.class.getSimpleName());
}
boolean superClassFound = false;
while (superClass != null
&& superClass.getKind() != TypeKind.NONE
&& (superClassElement = (TypeElement) typeUtils.asElement(superClass)) != null) {
for (Element e : superClassElement.getEnclosedElements()) {
if (e.getKind() == ElementKind.FIELD && e.getSimpleName() != null &&
e.getSimpleName().toString() != null && e.getSimpleName()
.toString()
.equals(fieldName)) {
superClassFound = true;
break;
}
}
if (superClassFound) {
break;
}
superClass = superClassElement.getSuperclass();
}
if (superClassElement == null) {
// Should never be the case, however to ensure we return a error message without superclass
return String.format(
"A field with the name '%s' in class %s is already annotated with @%s in a "
+ "super class or sub class of %s ! "
+ "Fields name must be unique within inheritance hierarchy.", fieldName,
problemClass.getQualifiedName().toString(), Arg.class.getSimpleName(),
problemClass.getQualifiedName().toString());
}
return String.format(base, fieldName, problemClass.getQualifiedName().toString(),
Arg.class.getSimpleName(), superClassElement.getQualifiedName());
}
/**
* Checks if the annotated field can be added to the given fragment. Otherwise a error message
* will be printed
*/
private void addAnnotatedField(ArgumentAnnotatedField annotatedField, AnnotatedFragment fragment,
Arg annotation) throws ProcessingException {
if (fragment.containsField(annotatedField)) {
// A field already with the name is here
throw new ProcessingException(annotatedField.getElement(),
getErrorMessageDuplicatedField(fragment, annotatedField.getClassElement(),
annotatedField.getVariableName()));
} else if (fragment.containsBundleKey(annotatedField) != null) {
// key for bundle is already in use
ArgumentAnnotatedField otherField = fragment.containsBundleKey(annotatedField);
throw new ProcessingException(annotatedField.getElement(),
"The bundle key '%s' for field %s in %s is already used by another "
+ "argument in %s (field name is '%s'). Bundle keys must be unique in inheritance hierarchy!",
annotatedField.getKey(), annotatedField.getVariableName(),
annotatedField.getClassElement().getQualifiedName().toString(),
otherField.getClassElement().getQualifiedName().toString(), otherField.getVariableName());
} else {
if (annotation.required()) {
fragment.addRequired(annotatedField);
} else {
fragment.addOptional(annotatedField);
}
}
}
/**
* Checks if inheritance hiererachy should be scanned for @Args annotations as well
*
* @param type The Fragment class
* @return true if super type should be scanned as well, otherwise false;
*/
private boolean shouldScanSuperClassesFragmentArgs(TypeElement type) throws ProcessingException {
boolean scanSuperClasses = true;
FragmentWithArgs fragmentWithArgs = type.getAnnotation(FragmentWithArgs.class);
if (fragmentWithArgs != null) {
scanSuperClasses = fragmentWithArgs.inherited();
}
// DEPRECATED annotation
FragmentArgsInherited inheritedFragmentArgs = type.getAnnotation(FragmentArgsInherited.class);
if (inheritedFragmentArgs != null) {
scanSuperClasses = inheritedFragmentArgs.value();
}
if (fragmentWithArgs != null && inheritedFragmentArgs != null) {
throw new ProcessingException(type, "Class %s is annotated with @%s and with the old deprecated @%s annotation. You have to migrate to the new annotation and use @%s", type.getSimpleName(), FragmentWithArgs.class.getSimpleName(), FragmentArgsInherited.class
.getSimpleName(), FragmentWithArgs.class);
}
return scanSuperClasses; // Default value
}
/**
* Collects the fields that are annotated by the fragmentarg
*/
private AnnotatedFragment collectArgumentsForType(TypeElement type) throws ProcessingException {
// incl. super classes
if (shouldScanSuperClassesFragmentArgs(type)) {
return getAllInclSuperClasses(type);
}
// Without super classes (inheritance)
AnnotatedFragment fragment = new AnnotatedFragment(type);
for (Element element : type.getEnclosedElements()) {
if (element.getKind() == ElementKind.FIELD) {
Arg annotation = element.getAnnotation(Arg.class);
if (annotation != null) {
ArgumentAnnotatedField field = new ArgumentAnnotatedField(element, type, annotation);
addAnnotatedField(field, fragment, annotation);
}
}
}
return fragment;
}
@Override
public boolean process(Set<? extends TypeElement> type, RoundEnvironment env) {
Elements elementUtils = processingEnv.getElementUtils();
Types typeUtils = processingEnv.getTypeUtils();
Filer filer = processingEnv.getFiler();
boolean isLibrary = false;
String fragmentArgsLib = processingEnv.getOptions().get(OPTION_IS_LIBRARY);
if (fragmentArgsLib != null && fragmentArgsLib.equalsIgnoreCase("true")) {
isLibrary = true;
}
JavaWriter jw = null;
try { // Catch processing exceptions
// REMEMBER: It's a SET! it uses .equals() .hashCode() to determine if element already in set
Set<TypeElement> fragmentClasses = new HashSet<TypeElement>();
Element[] origHelper = null;
// Search for @Arg fields
for (Element element : env.getElementsAnnotatedWith(Arg.class)) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Check if its a fragment
if (!isFragmentClass(enclosingElement, TYPE_FRAGMENT, TYPE_SUPPORT_FRAGMENT)) {
throw new ProcessingException(element, "@Arg can only be used on fragment fields (%s.%s)",
enclosingElement.getQualifiedName(), element);
}
if (element.getModifiers().contains(Modifier.FINAL)
|| element.getModifiers()
.contains(Modifier.STATIC)
|| element.getModifiers().contains(Modifier.PRIVATE)
|| element.getModifiers().contains(Modifier.PROTECTED)) {
throw new ProcessingException(element,
"@Arg fields must not be private, protected, final or static (%s.%s)",
enclosingElement.getQualifiedName(), element);
}
// Skip abstract classes
if (!enclosingElement.getModifiers().contains(Modifier.ABSTRACT)) {
fragmentClasses.add(enclosingElement);
}
}
// Search for "just" @InheritedFragmentArgs --> DEPRECATED
scanForAnnotatedFragmentClasses(env, FragmentArgsInherited.class, fragmentClasses);
// Search for "just" @FragmentWithArgs
scanForAnnotatedFragmentClasses(env, FragmentWithArgs.class, fragmentClasses);
// Store the key - value for the generated FragmentArtMap class
Map<String, String> autoMapping = new HashMap<String, String>();
for (TypeElement fragmentClass : fragmentClasses) {
try {
AnnotatedFragment fragment = collectArgumentsForType(fragmentClass);
String builder = fragment.getSimpleName() + "Builder";
List<Element> originating = new ArrayList<Element>(10);
originating.add(fragmentClass);
TypeMirror superClass = fragmentClass.getSuperclass();
while (superClass.getKind() != TypeKind.NONE) {
TypeElement element = (TypeElement) typeUtils.asElement(superClass);
if (element.getQualifiedName().toString().startsWith("android.")) {
break;
}
originating.add(element);
superClass = element.getSuperclass();
}
String qualifiedFragmentName = fragment.getQualifiedName().toString();
String qualifiedBuilderName = qualifiedFragmentName + "Builder";
Element[] orig = originating.toArray(new Element[originating.size()]);
origHelper = orig;
JavaFileObject jfo = filer.createSourceFile(qualifiedBuilderName, orig);
Writer writer = jfo.openWriter();
jw = new JavaWriter(writer);
writePackage(jw, fragmentClass);
jw.emitImports("android.os.Bundle");
jw.emitEmptyLine();
jw.beginType(builder, "class", EnumSet.of(Modifier.PUBLIC, Modifier.FINAL));
if (!fragment.getBundlerVariableMap().isEmpty()) {
jw.emitEmptyLine();
for (Map.Entry<String, String> e : fragment.getBundlerVariableMap().entrySet()) {
jw.emitField(e.getKey(), e.getValue(),
EnumSet.of(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC),
"new " + e.getKey() + "()");
}
}
jw.emitEmptyLine();
jw.emitField("Bundle", "mArguments", EnumSet.of(Modifier.PRIVATE, Modifier.FINAL),
"new Bundle()");
jw.emitEmptyLine();
Set<ArgumentAnnotatedField> required = fragment.getRequiredFields();
String[] args = new String[required.size() * 2];
int index = 0;
for (ArgumentAnnotatedField arg : required) {
args[index++] = arg.getType();
args[index++] = arg.getVariableName();
}
jw.beginMethod(null, builder, EnumSet.of(Modifier.PUBLIC), args);
for (ArgumentAnnotatedField arg : required) {
writePutArguments(jw, arg.getVariableName(), "mArguments", arg);
}
jw.endMethod();
if (!required.isEmpty()) {
jw.emitEmptyLine();
writeNewFragmentWithRequiredMethod(builder, fragmentClass, jw, args);
}
Set<ArgumentAnnotatedField> optionalArguments = fragment.getOptionalFields();
for (ArgumentAnnotatedField arg : optionalArguments) {
writeBuilderMethod(builder, jw, arg);
}
jw.emitEmptyLine();
writeInjectMethod(jw, fragmentClass, fragment.getAll());
jw.emitEmptyLine();
writeBuildMethod(jw, fragmentClass);
jw.emitEmptyLine();
writeBuildSubclassMethod(jw, fragmentClass);
jw.endType();
jw.close();
autoMapping.put(qualifiedFragmentName, qualifiedBuilderName);
} catch (IOException e) {
throw new ProcessingException(fragmentClass, "Unable to write builder for type %s: %s",
fragmentClass, e.getMessage());
}
}
// Write the automapping class
if (origHelper != null && !isLibrary) {
writeAutoMapping(autoMapping, origHelper);
}
} catch (ProcessingException e) {
// print error
if (jw != null) {
// Do cleanup if not closed yet
try {
jw.close();
} catch (Exception catched) {
}
}
error(e);
}
return true;
}
/**
* Scans a fragment for a given {@link FragmentWithArgs} annotation or ({@link
* FragmentArgsInherited} (which is deprectated)
*
* @param env The round enviroment
* @param annotationClass The annotation (.class) to scan for
* @param fragmentClasses The set of classes already scanned (containing annotations)
* @throws ProcessingException
*/
private void scanForAnnotatedFragmentClasses(RoundEnvironment env, Class<? extends Annotation> annotationClass, Set<TypeElement> fragmentClasses) throws ProcessingException {
for (Element element : env.getElementsAnnotatedWith(annotationClass)) {
if (element.getKind() != ElementKind.CLASS) {
throw new ProcessingException(element, "%s can only be applied on Fragment classes",
annotationClass.getSimpleName());
}
TypeElement classElement = (TypeElement) element;
// Check if its a fragment
if (!isFragmentClass(element, TYPE_FRAGMENT, TYPE_SUPPORT_FRAGMENT)) {
throw new ProcessingException(element,
"%s can only be used on fragments, but %s is not a subclass of fragment",
annotationClass.getSimpleName(), classElement.getQualifiedName());
}
// Skip abstract classes
if (!classElement.getModifiers().contains(Modifier.ABSTRACT)) {
fragmentClasses.add(classElement);
}
}
}
/**
* Checks if the given element is in a valid Fragment class
*/
private boolean isFragmentClass(Element classElement, TypeElement fragmentType,
TypeElement supportFragmentType) {
return (fragmentType != null && typeUtils.isSubtype(classElement.asType(),
fragmentType.asType())) || (supportFragmentType != null && typeUtils.isSubtype(
classElement.asType(), supportFragmentType.asType()));
}
/**
* Key is the fully qualified fragment name, value is the fully qualified Builder class name
*/
private void writeAutoMapping(Map<String, String> mapping, Element[] element)
throws ProcessingException {
try {
JavaFileObject jfo =
filer.createSourceFile(FragmentArgs.AUTO_MAPPING_QUALIFIED_CLASS, element);
Writer writer = jfo.openWriter();
JavaWriter jw = new JavaWriter(writer);
// Package
jw.emitPackage(FragmentArgs.AUTO_MAPPING_PACKAGE);
// Imports
jw.emitImports("android.os.Bundle");
// Class
jw.beginType(FragmentArgs.AUTO_MAPPING_CLASS_NAME, "class",
EnumSet.of(Modifier.PUBLIC, Modifier.FINAL), null,
FragmentArgsInjector.class.getCanonicalName());
jw.emitEmptyLine();
// The mapping Method
jw.beginMethod("void", "inject", EnumSet.of(Modifier.PUBLIC), "Object", "target");
jw.emitEmptyLine();
jw.emitStatement("Class<?> targetClass = target.getClass()");
jw.emitStatement("String targetName = targetClass.getCanonicalName()");
// TODO should be targetClass.getName()? Inner anonymous class not possible?
for (Map.Entry<String, String> entry : mapping.entrySet()) {
jw.emitEmptyLine();
jw.beginControlFlow("if ( %s.class.getName().equals(targetName) )", entry.getKey());
jw.emitStatement("%s.injectArguments( ( %s ) target)", entry.getValue(), entry.getKey());
jw.emitStatement("return");
jw.endControlFlow();
}
// End Mapping method
jw.endMethod();
jw.endType();
jw.close();
} catch (IOException e) {
throw new ProcessingException(null,
"Unable to write the automapping class for builder to fragment: %s: %s",
FragmentArgs.AUTO_MAPPING_QUALIFIED_CLASS, e.getMessage());
}
}
private void writeNewFragmentWithRequiredMethod(String builder, TypeElement element,
JavaWriter jw, String[] args) throws IOException {
jw.beginMethod(element.getQualifiedName().toString(), "new" + element.getSimpleName(),
EnumSet.of(Modifier.STATIC, Modifier.PUBLIC), args);
StringBuilder argNames = new StringBuilder();
for (int i = 1; i < args.length; i += 2) {
argNames.append(args[i]);
if (i < args.length - 1) {
argNames.append(", ");
}
}
jw.emitStatement("return new %1$s(%2$s).build()", builder, argNames);
jw.endMethod();
}
private void writeBuildMethod(JavaWriter jw, TypeElement element) throws IOException {
jw.beginMethod(element.getSimpleName().toString(), "build", EnumSet.of(Modifier.PUBLIC));
jw.emitStatement("%1$s fragment = new %1$s()", element.getSimpleName().toString());
jw.emitStatement("fragment.setArguments(mArguments)");
jw.emitStatement("return fragment");
jw.endMethod();
}
private void writeBuildSubclassMethod(JavaWriter jw, TypeElement element) throws IOException {
jw.beginMethod("<F extends " + element.getSimpleName().toString() + "> F", "build",
EnumSet.of(Modifier.PUBLIC), "F", "fragment");
jw.emitStatement("fragment.setArguments(mArguments)");
jw.emitStatement("return fragment");
jw.endMethod();
}
private void writeInjectMethod(JavaWriter jw, TypeElement element,
Set<ArgumentAnnotatedField> allArguments) throws IOException, ProcessingException {
jw.beginMethod("void", "injectArguments",
EnumSet.of(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC),
element.getSimpleName().toString(), "fragment");
jw.emitStatement("Bundle args = fragment.getArguments()");
jw.beginControlFlow("if (args == null)");
jw.emitStatement("throw new IllegalStateException(\"No arguments set\")");
jw.endControlFlow();
for (ArgumentAnnotatedField type : allArguments) {
jw.emitEmptyLine();
// Args Bundler
if (type.hasCustomBundler()) {
// Required
if (type.isRequired()) {
jw.beginControlFlow("if (!args.containsKey(" + JavaWriter.stringLiteral(
CUSTOM_BUNDLER_BUNDLE_KEY + type.getKey()) + "))");
jw.emitStatement("throw new IllegalStateException(\"required argument %1$s is not set\")",
type.getKey());
jw.endControlFlow();
jw.emitStatement("fragment.%s = %s.get(\"%s\", args)", type.getName(),
type.getBundlerFieldName(), type.getKey());
} else {
// not required bundler
jw.beginControlFlow("if (args.getBoolean(" + JavaWriter.stringLiteral(
CUSTOM_BUNDLER_BUNDLE_KEY + type.getKey()) + "))");
jw.emitStatement("fragment.%s = %s.get(\"%s\", args)", type.getName(),
type.getBundlerFieldName(), type.getKey());
jw.endControlFlow();
}
} else {
// Build in functions
String op = getOperation(type);
if (op == null) {
throw new ProcessingException(element,
"Can't write injector, the type is not supported by default. "
+ "However, You can provide your own implementation by providing an %s like this: @Arg( bundler = YourBundler.class )",
ArgsBundler.class.getSimpleName());
}
String cast = "Serializable".equals(op) ? "(" + type.getType() + ") " : "";
if (!type.isRequired()) {
jw.beginControlFlow(
"if (args.containsKey(" + JavaWriter.stringLiteral(type.getKey()) + "))");
} else {
jw.beginControlFlow(
"if (!args.containsKey(" + JavaWriter.stringLiteral(type.getKey()) + "))");
jw.emitStatement("throw new IllegalStateException(\"required argument %1$s is not set\")",
type.getKey());
jw.endControlFlow();
}
jw.emitStatement("fragment.%1$s = %4$sargs.get%2$s(\"%3$s\")", type.getName(), op,
type.getKey(), cast);
if (!type.isRequired()) {
jw.endControlFlow();
}
}
}
jw.endMethod();
}
private void writeBuilderMethod(String type, JavaWriter writer, ArgumentAnnotatedField arg)
throws IOException, ProcessingException {
writer.emitEmptyLine();
writer.beginMethod(type, arg.getVariableName(), EnumSet.of(Modifier.PUBLIC), arg.getType(),
arg.getVariableName());
writePutArguments(writer, arg.getVariableName(), "mArguments", arg);
writer.emitStatement("return this");
writer.endMethod();
}
public void error(ProcessingException e) {
String message = e.getMessage();
if (e.getMessageArgs().length > 0) {
message = String.format(message, e.getMessageArgs());
}
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, e.getElement());
}
public void warn(Element element, String message, Object... args) {
if (args.length > 0) {
message = String.format(message, args);
}
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, message, element);
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}