-
Notifications
You must be signed in to change notification settings - Fork 273
/
Proto.java
1840 lines (1605 loc) · 56.5 KB
/
Proto.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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2014-2017 Immutables Authors and Contributors
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
http://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 org.immutables.value.processor.meta;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import com.google.common.collect.Lists;
import com.google.common.collect.ObjectArrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import org.immutables.generator.ClasspathFence;
import org.immutables.generator.SourceExtraction;
import org.immutables.value.Value;
import org.immutables.value.processor.encode.EncMetadataMirror;
import org.immutables.value.processor.encode.EncodingInfo;
import org.immutables.value.processor.encode.EncodingMirror;
import org.immutables.value.processor.encode.Inflater;
import org.immutables.value.processor.encode.Instantiator;
import org.immutables.value.processor.encode.Type;
import org.immutables.value.processor.meta.Styles.UsingName.TypeNames;
import static com.google.common.base.Verify.verify;
@Value.Enclosing
public class Proto {
private Proto() {}
static final Type.Factory TYPE_FACTORY = new Type.Producer();
private static final Inflater ENCODING_INFLATER = new Inflater(TYPE_FACTORY);
final static class Interning {
private final Interner<DeclaringPackage> packageInterner = Interners.newStrongInterner();
private final Interner<DeclaringType> typeInterner = Interners.newStrongInterner();
private final Interner<Protoclass> protoclassInterner = Interners.newStrongInterner();
DeclaringPackage forPackage(DeclaringPackage declaringPackage) {
return packageInterner.intern(declaringPackage);
}
DeclaringType forType(DeclaringType declaringType) {
return typeInterner.intern(declaringType);
}
Protoclass forProto(Protoclass protoclass) {
return protoclassInterner.intern(protoclass);
}
}
@Value.Immutable(builder = false)
public static abstract class MetaAnnotated {
private static final ConcurrentMap<String, MetaAnnotated> cache = new ConcurrentHashMap<>(16, 0.7f, 1);
@Value.Parameter
@Value.Auxiliary
public abstract Element element();
@Value.Parameter
@Value.Auxiliary
public abstract String qualifiedName();
@Value.Parameter
@Value.Auxiliary
public abstract Environment environment();
@Value.Derived
@Value.Auxiliary
public Set<EncodingInfo> encodings() {
if (qualifiedName().endsWith("Enabled")
|| CustomImmutableAnnotations.annotations().contains(qualifiedName())
|| style().isPresent()) {
// See if it is encoding enabled itself
Optional<EncodingInfo> encoding = EncMetadataMirror.find(element()).transform(ENCODING_INFLATER);
if (encoding.isPresent()) {
return encoding.asSet();
}
// trying to find it as meta-meta annotation
List<EncodingInfo> result = new ArrayList<>();
for (AnnotationMirror m : element().getAnnotationMirrors()) {
MetaAnnotated metaAnnotated = MetaAnnotated.from(m, environment());
result.addAll(metaAnnotated.encodings());
}
if (!result.isEmpty()) {
return FluentIterable.from(result).toSet();
}
}
return ImmutableSet.of();
}
@Value.Derived
@Value.Auxiliary
public Optional<StyleInfo> style() {
return StyleMirror.find(element()).transform(ToStyleInfo.FUNCTION);
}
@Value.Derived
@Value.Auxiliary
public Optional<String[]> depluralize() {
Optional<DepluralizeMirror> d = DepluralizeMirror.find(element());
if (d.isPresent()) {
return Optional.of(d.get().dictionary());
}
return Optional.absent();
}
@Value.Derived
@Value.Auxiliary
public Optional<Long> serialVersion() {
if (!environment().hasSerialModule()) {
return Optional.absent();
}
Optional<VersionMirror> version = VersionMirror.find(element());
return version.isPresent()
? Optional.of(version.get().value())
: Optional.<Long>absent();
}
@Value.Derived
@Value.Auxiliary
public boolean isSerialStructural() {
return environment().hasSerialModule()
&& StructuralMirror.isPresent(element());
}
@Value.Derived
@Value.Auxiliary
public boolean isJacksonSerialized() {
return environment().hasJacksonLib()
&& isJacksonSerializedAnnotated(element());
}
@Value.Derived
@Value.Auxiliary
public boolean isJacksonDeserialized() {
return environment().hasJacksonLib()
&& isJacksonDeserializedAnnotated(element());
}
@Value.Derived
@Value.Auxiliary
public boolean isJacksonJsonTypeInfo() {
return environment().hasJacksonLib()
&& isJacksonJsonTypeInfoAnnotated(element());
}
@Value.Derived
@Value.Auxiliary
public boolean isJsonQualifier() {
return environment().hasOkJsonLib()
&& OkQualifierMirror.isPresent(element());
}
@Value.Derived
@Value.Auxiliary
public boolean isEnclosing() {
return EnclosingMirror.isPresent(element());
}
public static MetaAnnotated from(AnnotationMirror mirror, Environment environment) {
TypeElement element = (TypeElement) mirror.getAnnotationType().asElement();
String name = element.getQualifiedName().toString();
@Nullable MetaAnnotated metaAnnotated = cache.get(name);
if (metaAnnotated == null) {
metaAnnotated = ImmutableProto.MetaAnnotated.of(element, name, environment);
@Nullable MetaAnnotated existing = cache.putIfAbsent(name, metaAnnotated);
if (existing != null) {
metaAnnotated = existing;
}
}
return metaAnnotated;
}
}
abstract static class Diagnosable {
/**
* Element suitable for reporting as a source of declaration which might causing problems.
*/
@Value.Auxiliary
abstract Element element();
@Value.Auxiliary
abstract Environment environment();
ProcessingEnvironment processing() {
return environment().processing();
}
@Value.Auxiliary
@Value.Derived
public String simpleName() {
return element().getSimpleName().toString();
}
public Reporter report() {
return Reporter.from(processing()).withElement(element());
}
}
@Value.Immutable
public abstract static class Environment {
@Value.Parameter
abstract ProcessingEnvironment processing();
@Value.Parameter
abstract Round round();
@Value.Derived
StyleInfo defaultStyles() {
@Nullable TypeElement element = findElement(StyleMirror.qualifiedName());
if (element == null) {
processing().getMessager()
.printMessage(Diagnostic.Kind.MANDATORY_WARNING,
"Could not found annotations on the compile classpath. It looks like annotation processor is running"
+ " in a separate annotation-processing classpath and unable to get to annotation definitions."
+ " To fix this, please add annotation-only artifact 'org.immutables:value:(version):annotations'"
+ " to 'compile' 'compileOnly' or 'provided' dependency scope.");
element = findElement(StyleMirror.mirrorQualifiedName());
verify(element != null, "Classpath should contain at least mirror annotation, otherwise library is corrupted");
}
try {
return ToStyleInfo.FUNCTION.apply(StyleMirror.from(element));
} catch (Exception ex) {
processing().getMessager()
.printMessage(Diagnostic.Kind.MANDATORY_WARNING,
"The version of the Immutables annotation on the classpath has incompatible differences"
+ " from the Immutables annotation processor used. Various problems might occur,"
+ " like this one: "
+ ex);
element = findElement(StyleMirror.mirrorQualifiedName());
verify(element != null,
"classpath should contain at least the mirror annotation, otherwise library is corrupted");
return ToStyleInfo.FUNCTION.apply(StyleMirror.from(element));
}
}
/**
* @return current Guava's MoreObjects or {@code null} if no Guava available on the classpath.
*/
@Nullable
@Value.Lazy
String typeMoreObjects() {
String typeMoreObjects = UnshadeGuava.typeString("base.MoreObjects");
String typeObjects = UnshadeGuava.typeString("base.Objects");
if (findElement(typeMoreObjects) != null) {
return typeMoreObjects;
}
if (findElement(typeMoreObjects) != null) {
return typeObjects;
}
return null;
}
public boolean hasGuavaLib() {
return typeMoreObjects() != null;
}
@Value.Lazy
public boolean hasOkJsonLib() {
return findElement("com.squareup.moshi.Moshi") != null;
}
@Value.Lazy
public boolean hasGsonLib() {
return findElement("com.google.gson.Gson") != null;
}
@Value.Lazy
public boolean hasJacksonLib() {
return findElement(Proto.JACKSON_DESERIALIZE) != null;
}
@Value.Lazy
public boolean hasMongoModule() {
return findElement(RepositoryMirror.qualifiedName()) != null;
}
@Value.Lazy
public boolean hasSerialModule() {
return findElement(VersionMirror.qualifiedName()) != null;
}
@Value.Lazy
public boolean hasTreesModule() {
return findElement(TransformMirror.qualifiedName()) != null;
}
@Value.Lazy
public boolean hasAstModule() {
return findElement(AstMirror.qualifiedName()) != null;
}
@Value.Lazy
public boolean hasOrdinalModule() {
return findElement(ORDINAL_VALUE_INTERFACE_TYPE) != null;
}
@Value.Lazy
public boolean hasBuilderModule() {
return findElement(FactoryMirror.qualifiedName()) != null;
}
@Value.Lazy
public boolean hasFuncModule() {
return findElement(FunctionalMirror.qualifiedName()) != null;
}
@Value.Lazy
public boolean hasEncodeModule() {
return findElement(EncodingMirror.qualifiedName()) != null;
}
/**
* Default type adapters should only be called if {@code Gson.TypeAdapters} annotation is
* definitely in classpath. Currenlty, it is called by for mongo repository module,
* which have {@code gson} module as a transitive dependency.
* @return default type adapters
*/
@Value.Lazy
TypeAdaptersMirror defaultTypeAdapters() {
@Nullable TypeElement typeElement =
findElement(TypeAdaptersMirror.qualifiedName());
Preconditions.checkState(typeElement != null,
"Processor internal error, @%s is not know to be on the classpath",
TypeAdaptersMirror.qualifiedName());
return TypeAdaptersMirror.from(typeElement);
}
ValueType composeValue(Protoclass protoclass) {
return round().composeValue(protoclass);
}
ImmutableList<Protoclass> protoclassesFrom(Iterable<? extends Element> elements) {
return round().protoclassesFrom(elements);
}
private @Nullable TypeElement findElement(String qualifiedName) {
if (ClasspathFence.isInhibited(qualifiedName)) {
return null;
}
try {
TypeElement typeElement = processing()
.getElementUtils()
.getTypeElement(qualifiedName);
return typeElement;
} catch (Exception ex) {
// to be visible during build
ex.printStackTrace();
// any type loading problems, which are unlikely (leftover?)
return null;
}
}
private final Map<Set<EncodingInfo>, Instantiator> instantiators = new HashMap<>();
Instantiator instantiatorFor(Set<EncodingInfo> encodings) {
@Nullable Instantiator instantiator = instantiators.get(encodings);
if (instantiator == null) {
instantiator = ENCODING_INFLATER.instantiatorFor(encodings);
instantiators.put(encodings, instantiator);
}
return instantiator;
}
}
/**
* Introspection supertype for the {@link DeclaringType} and {@link DeclaringPackage}
*/
public static abstract class AbstractDeclaring extends Diagnosable {
public abstract String name();
public abstract DeclaringPackage packageOf();
@Value.Lazy
protected Optional<IncludeMirror> include() {
return IncludeMirror.find(element());
}
@Value.Lazy
protected Optional<FIncludeMirror> builderInclude() {
return FIncludeMirror.find(element());
}
public String asPrefix() {
return name().isEmpty() ? "" : (name() + ".");
}
public Optional<DeclaringType> asType() {
return this instanceof DeclaringType
? Optional.of((DeclaringType) this)
: Optional.<DeclaringType>absent();
}
/**
* used to intern packaged created internally
*/
@Value.Auxiliary
abstract Proto.Interning interner();
@Value.Lazy
public Optional<TypeAdaptersMirror> typeAdapters() {
return TypeAdaptersMirror.find(element());
}
@Value.Lazy
public Optional<OkTypeAdaptersMirror> okTypeAdapters() {
return OkTypeAdaptersMirror.find(element());
}
@Value.Lazy
List<TypeElement> includedTypes() {
Optional<IncludeMirror> includes = include();
ImmutableList<TypeMirror> typeMirrors = includes.isPresent()
? ImmutableList.copyOf(includes.get().valueMirror())
: ImmutableList.<TypeMirror>of();
FluentIterable<TypeElement> typeElements = FluentIterable.from(typeMirrors)
.filter(DeclaredType.class)
.transform(DeclatedTypeToElement.FUNCTION);
ImmutableSet<String> uniqueTypeNames = typeElements
.filter(IsPublic.PREDICATE)
.transform(ElementToName.FUNCTION)
.toSet();
if (uniqueTypeNames.size() != typeMirrors.size()) {
report().annotationNamed(IncludeMirror.simpleName())
.warning("Some types were ignored, non-supported for inclusion: duplicates,"
+ " non declared reference types, non-public");
}
return typeElements.toList();
}
@Value.Lazy
List<TypeElement> builderIncludedTypes() {
Optional<FIncludeMirror> includes = builderInclude();
ImmutableList<TypeMirror> typeMirrors = includes.isPresent()
? ImmutableList.copyOf(includes.get().valueMirror())
: ImmutableList.<TypeMirror>of();
FluentIterable<TypeElement> typeElements = FluentIterable.from(typeMirrors)
.filter(DeclaredType.class)
.transform(DeclatedTypeToElement.FUNCTION);
ImmutableSet<String> uniqueTypeNames = typeElements
.filter(IsPublic.PREDICATE)
.transform(ElementToName.FUNCTION)
.toSet();
if (uniqueTypeNames.size() != typeMirrors.size()) {
report().annotationNamed(IncludeMirror.simpleName())
.warning("Some types were ignored, non-supported for inclusion: duplicates,"
+ " non declared reference types, non-public");
}
return typeElements.toList();
}
@Value.Lazy
List<MetaAnnotated> metaAnnotated() {
ImmutableList.Builder<MetaAnnotated> builder = ImmutableList.builder();
for (AnnotationMirror mirror : element().getAnnotationMirrors()) {
builder.add(MetaAnnotated.from(mirror, environment()));
}
return builder.build();
}
@Value.Lazy
public Optional<StyleInfo> style() {
Optional<StyleInfo> style = StyleMirror.find(element()).transform(ToStyleInfo.FUNCTION);
if (style.isPresent()) {
return style;
}
for (MetaAnnotated m : metaAnnotated()) {
Optional<StyleInfo> metaStyle = m.style();
if (metaStyle.isPresent()) {
return metaStyle;
}
}
return Optional.absent();
}
@Value.Lazy
public Optional<Long> serialVersion() {
Optional<VersionMirror> version = VersionMirror.find(element());
if (version.isPresent()) {
return Optional.of(version.get().value());
}
for (MetaAnnotated metaAnnotated : metaAnnotated()) {
Optional<Long> serialVersion = metaAnnotated.serialVersion();
if (serialVersion.isPresent()) {
return serialVersion;
}
}
return Optional.<Long>absent();
}
@Value.Lazy
public boolean isSerialStructural() {
return StructuralMirror.isPresent(element());
}
@Value.Lazy
public boolean isJacksonSerialized() {
if (jacksonSerializeMode() == JacksonMode.DELEGATED) {
// while DeclaringPackage cannot have those annotations
// directly, just checking them as a general computation path
// will not hurt much.
return true;
}
for (MetaAnnotated metaAnnotated : metaAnnotated()) {
if (metaAnnotated.isJacksonSerialized()) {
return true;
}
}
return false;
}
@Value.Lazy
public JacksonMode jacksonSerializeMode() {
return Proto.isJacksonSerializedAnnotated(element())
? JacksonMode.DELEGATED
: JacksonMode.NONE;
}
@Value.Lazy
public boolean isJacksonDeserialized() {
if (isJacksonDeserializedAnnotated()) {
// while DeclaringPackage cannot have those annotations
// directly, just checking them as a general computation path
// will not hurt much.
return true;
}
for (MetaAnnotated metaAnnotated : metaAnnotated()) {
if (metaAnnotated.isJacksonDeserialized()) {
return true;
}
}
return false;
}
protected void collectEncodings(Collection<EncodingInfo> encodings) {
for (MetaAnnotated m : metaAnnotated()) {
encodings.addAll(m.encodings());
}
}
@Value.Lazy
public Optional<String[]> depluralize() {
@Nullable String[] dictionary = null;
for (MetaAnnotated metaAnnotated : metaAnnotated()) {
Optional<String[]> depluralize = metaAnnotated.depluralize();
if (depluralize.isPresent()) {
dictionary = concat(dictionary, depluralize.get());
}
}
Optional<DepluralizeMirror> depluralize = DepluralizeMirror.find(element());
if (depluralize.isPresent()) {
dictionary = concat(dictionary, depluralize.get().dictionary());
}
return Optional.fromNullable(dictionary);
}
@Value.Lazy
public boolean isJacksonDeserializedAnnotated() {
return Proto.isJacksonDeserializedAnnotated(element());
}
@Value.Lazy
public boolean isJacksonJsonTypeInfo() {
if (isJacksonJsonTypeInfoAnnotated(element())) {
// while DeclaringPackage cannot have those annotations
// directly, just checking them as a general computation path
// will not hurt much.
return true;
}
for (MetaAnnotated metaAnnotated : metaAnnotated()) {
if (metaAnnotated.isJacksonJsonTypeInfo()) {
return true;
}
}
return false;
}
}
public enum JacksonMode {
NONE, DELEGATED, BUILDER;
}
@Value.Immutable
public static abstract class DeclaringPackage extends AbstractDeclaring {
@Override
@Value.Auxiliary
public abstract PackageElement element();
@Override
public DeclaringPackage packageOf() {
return this;
}
@Override
@Value.Auxiliary
@Value.Derived
public String simpleName() {
return element().isUnnamed() ? "" : element().getSimpleName().toString();
}
/**
* Name is the only equivalence attribute. Basically packages are interned by name.
* @return package name
*/
@Override
@Value.Derived
public String name() {
return element().isUnnamed() ? "" : element().getQualifiedName().toString();
}
@Value.Lazy
Optional<DeclaringPackage> namedParentPackage() {
String parentPackageName = SourceNames.parentPackageName(element());
if (!parentPackageName.isEmpty()) {
@Nullable PackageElement parentPackage =
environment().processing()
.getElementUtils()
.getPackageElement(parentPackageName);
if (parentPackage != null) {
return Optional.of(interner().forPackage(
ImmutableProto.DeclaringPackage.builder()
.environment(environment())
.interner(interner())
.element(parentPackage)
.build()));
}
}
return Optional.absent();
}
@Override
@Value.Lazy
public boolean isJacksonSerialized() {
boolean isJacksonSerialized = super.isJacksonSerialized();
if (isJacksonSerialized) {
return isJacksonSerialized;
}
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
return parent.get().isJacksonSerialized();
}
return false;
}
@Override
@Value.Lazy
public boolean isJacksonDeserialized() {
if (super.isJacksonDeserialized()) {
return true;
}
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
return parent.get().isJacksonDeserialized();
}
return false;
}
@Override
@Value.Lazy
public boolean isJacksonJsonTypeInfo() {
if (super.isJacksonJsonTypeInfo()) {
return true;
}
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
return parent.get().isJacksonJsonTypeInfo();
}
return false;
}
@Override
@Value.Lazy
public boolean isSerialStructural() {
boolean isSerialStructural = super.isSerialStructural();
if (isSerialStructural) {
return isSerialStructural;
}
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
return parent.get().isSerialStructural();
}
return false;
}
@Override
@Value.Lazy
public Optional<Long> serialVersion() {
Optional<Long> serialVersion = super.serialVersion();
if (serialVersion.isPresent()) {
return serialVersion;
}
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
return parent.get().serialVersion();
}
return Optional.absent();
}
@Override
@Value.Lazy
public Optional<StyleInfo> style() {
Optional<StyleInfo> style = super.style();
if (style.isPresent()) {
return style;
}
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
return parent.get().style();
}
return Optional.absent();
}
@Override
@Value.Lazy
public Optional<String[]> depluralize() {
@Nullable String[] dictionary = null;
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
Optional<String[]> depluralize = parent.get().depluralize();
if (depluralize.isPresent()) {
dictionary = concat(dictionary, depluralize.get());
}
}
Optional<String[]> depluralize = super.depluralize();
if (depluralize.isPresent()) {
dictionary = concat(dictionary, depluralize.get());
}
return Optional.fromNullable(dictionary);
}
@Override
protected void collectEncodings(Collection<EncodingInfo> encodings) {
Optional<DeclaringPackage> parent = namedParentPackage();
if (parent.isPresent()) {
parent.get().collectEncodings(encodings);
}
super.collectEncodings(encodings);
}
}
@Value.Immutable
public static abstract class DeclaringType extends AbstractDeclaring {
@Override
@Value.Auxiliary
public abstract TypeElement element();
@Override
@Value.Derived
public String name() {
return element().getQualifiedName().toString();
}
/**
* returns this class if it's top level or enclosing top level type.
* @return accossiated top level type.
*/
public DeclaringType associatedTopLevel() {
return enclosingTopLevel().or(this);
}
@Value.Lazy
public Optional<DeclaringType> enclosingTopLevel() {
TypeElement top = element();
for (Element e = top; e.getKind() != ElementKind.PACKAGE; e = e.getEnclosingElement()) {
top = (TypeElement) e;
}
if (top == element()) {
return Optional.absent();
}
return Optional.of(interner().forType(
ImmutableProto.DeclaringType.builder()
.environment(environment())
.interner(interner())
.element(top)
.build()));
}
@Value.Lazy
public Optional<RepositoryMirror> repository() {
return RepositoryMirror.find(element());
}
@Value.Lazy
public Optional<DeclaringType> enclosingOf() {
Optional<DeclaringType> topLevel = enclosingTopLevel();
if (topLevel.isPresent() && topLevel.get().isEnclosing()) {
return topLevel;
}
return Optional.absent();
}
@Override
@Value.Derived
@Value.Auxiliary
public DeclaringPackage packageOf() {
Element e = element();
for (; e.getKind() != ElementKind.PACKAGE; e = e.getEnclosingElement()) {
}
return interner().forPackage(
ImmutableProto.DeclaringPackage.builder()
.environment(environment())
.interner(interner())
.element((PackageElement) e)
.build());
}
@Value.Lazy
public Optional<ValueImmutableInfo> features() {
Optional<ValueImmutableInfo> immutableAnnotation =
ImmutableMirror.find(element()).transform(ToImmutableInfo.FUNCTION);
if (immutableAnnotation.isPresent()) {
return immutableAnnotation;
}
for (String a : environment().round().customImmutableAnnotations()) {
if (isAnnotatedWith(element(), a)) {
return Optional.of(environment().defaultStyles().defaults());
}
}
return Optional.absent();
}
@Value.Lazy
@Override
public JacksonMode jacksonSerializeMode() {
boolean wasJacksonSerialize = false;
for (AnnotationMirror a : element().getAnnotationMirrors()) {
TypeElement e = (TypeElement) a.getAnnotationType().asElement();
if (!wasJacksonSerialize && e.getQualifiedName().contentEquals(JACKSON_SERIALIZE)) {
wasJacksonSerialize = true;
}
if (e.getQualifiedName().contentEquals(JACKSON_DESERIALIZE)) {
for (ExecutableElement attr : a.getElementValues().keySet()) {
if (attr.getSimpleName().contentEquals("builder")) {
// If builder attribute is specified, we don't consider this as
// our, immutables, business to generate anything.
return JacksonMode.BUILDER;
}
}
return JacksonMode.DELEGATED;
}
}
return wasJacksonSerialize
? JacksonMode.DELEGATED
: JacksonMode.NONE;
}
@Value.Lazy
public boolean useImmutableDefaults() {
Optional<ValueImmutableInfo> immutables = features();
if (immutables.isPresent()) {
return immutables.get().isDefault();
}
return true;
}
@Value.Lazy
public boolean isEnclosing() {
if (EnclosingMirror.isPresent(element())) {
return true;
}
if (isTopLevel()) {
for (MetaAnnotated metaAnnotated : metaAnnotated()) {
if (metaAnnotated.isEnclosing()) {
return true;
}
}
}
return false;
}
@Value.Lazy
public boolean isModifiable() {
return ModifiableMirror.isPresent(element());
}
/**
* @return true, if is top level
*/
@Value.Derived
@Value.Auxiliary
public boolean isTopLevel() {
return element().getNestingKind() == NestingKind.TOP_LEVEL;
}
public boolean isImmutable() {
return features().isPresent();
}
boolean verifiedFactory(ExecutableElement element) {
if (!isTopLevel() || !suitableForBuilderFactory(element)) {
report().withElement(element)
.annotationNamed(FactoryMirror.simpleName())
.error("@%s method '%s' should be static, non-private, non-void and enclosed in top level type",
FactoryMirror.simpleName(),
element.getSimpleName());
return false;
}
return true;
}
boolean verifiedConstructor(ExecutableElement element) {
if (!isTopLevel() || !suitableForBuilderConstructor(element)) {
report().withElement(element)
.annotationNamed(FConstructorMirror.simpleName())
.error("@%s annotated element should be non-private constructor in a top level type",
FConstructorMirror.simpleName(),
element.getSimpleName());
return false;
}
return true;
}
static boolean suitableForBuilderConstructor(ExecutableElement element) {
return element.getKind() == ElementKind.CONSTRUCTOR
&& !element.getModifiers().contains(Modifier.PRIVATE);
}
static boolean suitableForBuilderFactory(ExecutableElement element) {
return element.getKind() == ElementKind.METHOD
&& element.getReturnType().getKind() != TypeKind.VOID
&& !element.getModifiers().contains(Modifier.PRIVATE)
&& element.getModifiers().contains(Modifier.STATIC);
}
/**
* Some validations, not exhaustive.
*/
@Value.Check
protected void validate() {