/
AnnotationTypeMappingsTests.java
953 lines (770 loc) · 32.6 KB
/
AnnotationTypeMappingsTests.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
/*
* Copyright 2002-2020 the original author or authors.
*
* 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 org.springframework.core.annotation;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AnnotationTypeMapping.MirrorSets;
import org.springframework.core.annotation.AnnotationTypeMapping.MirrorSets.MirrorSet;
import org.springframework.lang.UsesSunMisc;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link AnnotationTypeMappings} and {@link AnnotationTypeMapping}.
*
* @author Phillip Webb
* @author Sam Brannen
*/
class AnnotationTypeMappingsTests {
@Test
void forAnnotationTypeWhenNoMetaAnnotationsReturnsMappings() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(SimpleAnnotation.class);
assertThat(mappings.size()).isEqualTo(1);
assertThat(mappings.get(0).getAnnotationType()).isEqualTo(SimpleAnnotation.class);
assertThat(getAll(mappings)).flatExtracting(
AnnotationTypeMapping::getAnnotationType).containsExactly(SimpleAnnotation.class);
}
@Test
void forAnnotationWhenHasSpringAnnotationReturnsFilteredMappings() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(WithSpringLangAnnotation.class);
assertThat(mappings.size()).isEqualTo(1);
}
@Test
void forAnnotationTypeWhenMetaAnnotationsReturnsMappings() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(MetaAnnotated.class);
assertThat(mappings.size()).isEqualTo(6);
assertThat(getAll(mappings)).flatExtracting(
AnnotationTypeMapping::getAnnotationType).containsExactly(
MetaAnnotated.class, A.class, B.class, AA.class, AB.class,
ABC.class);
}
@Test
void forAnnotationTypeWhenHasRepeatingMetaAnnotationReturnsMapping() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(WithRepeatedMetaAnnotations.class);
assertThat(mappings.size()).isEqualTo(3);
assertThat(getAll(mappings)).flatExtracting(
AnnotationTypeMapping::getAnnotationType).containsExactly(
WithRepeatedMetaAnnotations.class, Repeating.class, Repeating.class);
}
@Test
void forAnnotationTypeWhenSelfAnnotatedReturnsMapping() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(SelfAnnotated.class);
assertThat(mappings.size()).isEqualTo(1);
assertThat(getAll(mappings)).flatExtracting(
AnnotationTypeMapping::getAnnotationType).containsExactly(SelfAnnotated.class);
}
@Test
void forAnnotationTypeWhenFormsLoopReturnsMapping() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(LoopA.class);
assertThat(mappings.size()).isEqualTo(2);
assertThat(getAll(mappings)).flatExtracting(
AnnotationTypeMapping::getAnnotationType).containsExactly(LoopA.class, LoopB.class);
}
@Test
void forAnnotationTypeWhenHasAliasForWithBothValueAndAttributeThrowsException() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForWithBothValueAndAttribute.class))
.withMessage("In @AliasFor declared on attribute 'test' in annotation ["
+ AliasForWithBothValueAndAttribute.class.getName()
+ "], attribute 'attribute' and its alias 'value' are present with values of 'foo' and 'bar', but only one is permitted.");
}
@Test
void forAnnotationTypeWhenAliasForToSelfNonExistingAttribute() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForToSelfNonExistingAttribute.class))
.withMessage("@AliasFor declaration on attribute 'test' in annotation ["
+ AliasForToSelfNonExistingAttribute.class.getName()
+ "] declares an alias for 'missing' which is not present.");
}
@Test
void forAnnotationTypeWhenAliasForToOtherNonExistingAttribute() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForToOtherNonExistingAttribute.class))
.withMessage("Attribute 'test' in annotation ["
+ AliasForToOtherNonExistingAttribute.class.getName()
+ "] is declared as an @AliasFor nonexistent "
+ "attribute 'missing' in annotation ["
+ AliasForToOtherNonExistingAttributeTarget.class.getName()
+ "].");
}
@Test
void forAnnotationTypeWhenAliasForToSelf() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForToSelf.class))
.withMessage("@AliasFor declaration on attribute 'test' in annotation ["
+ AliasForToSelf.class.getName()
+ "] points to itself. Specify 'annotation' to point to "
+ "a same-named attribute on a meta-annotation.");
}
@Test
void forAnnotationTypeWhenAliasForWithArrayCompatibleReturnTypes() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(
AliasForWithArrayCompatibleReturnTypes.class);
AnnotationTypeMapping mapping = getMapping(mappings,
AliasForWithArrayCompatibleReturnTypesTarget.class);
assertThat(getAliasMapping(mapping, 0).getName()).isEqualTo("test");
}
@Test
void forAnnotationTypeWhenAliasForWithIncompatibleReturnTypes() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForWithIncompatibleReturnTypes.class))
.withMessage("Misconfigured aliases: attribute 'test' in annotation ["
+ AliasForWithIncompatibleReturnTypes.class.getName()
+ "] and attribute 'test' in annotation ["
+ AliasForWithIncompatibleReturnTypesTarget.class.getName()
+ "] must declare the same return type.");
}
@Test
void forAnnotationTypeWhenAliasForToSelfAnnotatedToOtherAttribute() {
String annotationType = AliasForToSelfAnnotatedToOtherAttribute.class.getName();
assertThatExceptionOfType(AnnotationConfigurationException.class)
.isThrownBy(() -> AnnotationTypeMappings.forAnnotationType(AliasForToSelfAnnotatedToOtherAttribute.class))
.withMessage("Attribute 'b' in annotation [" + annotationType
+ "] must be declared as an @AliasFor attribute 'a' in annotation [" + annotationType
+ "], not attribute 'c' in annotation [" + annotationType + "].");
}
@Test
void forAnnotationTypeWhenAliasForHasMixedImplicitAndExplicitAliases() {
assertMixedImplicitAndExplicitAliases(AliasForWithMixedImplicitAndExplicitAliasesV1.class, "b");
assertMixedImplicitAndExplicitAliases(AliasForWithMixedImplicitAndExplicitAliasesV2.class, "a");
}
private void assertMixedImplicitAndExplicitAliases(Class<? extends Annotation> annotationType, String overriddenAttribute) {
String annotationName = annotationType.getName();
String metaAnnotationName = AliasPair.class.getName();
assertThatExceptionOfType(AnnotationConfigurationException.class)
.isThrownBy(() -> AnnotationTypeMappings.forAnnotationType(annotationType))
.withMessage("Attribute 'b' in annotation [" + annotationName
+ "] must be declared as an @AliasFor attribute 'a' in annotation [" + annotationName
+ "], not attribute '" + overriddenAttribute + "' in annotation [" + metaAnnotationName + "].");
}
@Test
void forAnnotationTypeWhenAliasForNonMetaAnnotated() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForNonMetaAnnotated.class))
.withMessage("@AliasFor declaration on attribute 'test' in annotation ["
+ AliasForNonMetaAnnotated.class.getName()
+ "] declares an alias for attribute 'test' in annotation ["
+ AliasForNonMetaAnnotatedTarget.class.getName()
+ "] which is not meta-present.");
}
@Test
void forAnnotationTypeWhenAliasForSelfWithDifferentDefaults() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForSelfWithDifferentDefaults.class))
.withMessage("Misconfigured aliases: attribute 'a' in annotation ["
+ AliasForSelfWithDifferentDefaults.class.getName()
+ "] and attribute 'b' in annotation ["
+ AliasForSelfWithDifferentDefaults.class.getName()
+ "] must declare the same default value.");
}
@Test
void forAnnotationTypeWhenAliasForSelfWithMissingDefault() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasForSelfWithMissingDefault.class))
.withMessage("Misconfigured aliases: attribute 'a' in annotation ["
+ AliasForSelfWithMissingDefault.class.getName()
+ "] and attribute 'b' in annotation ["
+ AliasForSelfWithMissingDefault.class.getName()
+ "] must declare default values.");
}
@Test
void forAnnotationTypeWhenAliasWithExplicitMirrorAndDifferentDefaults() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
AnnotationTypeMappings.forAnnotationType(AliasWithExplicitMirrorAndDifferentDefaults.class))
.withMessage("Misconfigured aliases: attribute 'a' in annotation ["
+ AliasWithExplicitMirrorAndDifferentDefaults.class.getName()
+ "] and attribute 'c' in annotation ["
+ AliasWithExplicitMirrorAndDifferentDefaults.class.getName()
+ "] must declare the same default value.");
}
@Test
void getDistanceReturnsDistance() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(Mapped.class);
assertThat(mappings.get(0).getDistance()).isEqualTo(0);
assertThat(mappings.get(1).getDistance()).isEqualTo(1);
}
@Test
void getAnnotationTypeReturnsAnnotationType() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(Mapped.class);
assertThat(mappings.get(0).getAnnotationType()).isEqualTo(Mapped.class);
assertThat(mappings.get(1).getAnnotationType()).isEqualTo(MappedTarget.class);
}
@Test
void getMetaTypeReturnsTypes() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(ThreeDeepA.class);
AnnotationTypeMapping mappingC = mappings.get(2);
assertThat(mappingC.getMetaTypes()).containsExactly(ThreeDeepA.class, ThreeDeepB.class, ThreeDeepC.class);
}
@Test
void getAnnotationWhenRootReturnsNull() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(Mapped.class);
assertThat(mappings.get(0).getAnnotation()).isNull();
}
@Test
void getAnnotationWhenMetaAnnotationReturnsAnnotation() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(Mapped.class);
assertThat(mappings.get(1).getAnnotation()).isEqualTo(Mapped.class.getAnnotation(MappedTarget.class));
}
@Test
void getAttributesReturnsAttributes() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(Mapped.class).get(0);
AttributeMethods attributes = mapping.getAttributes();
assertThat(attributes.size()).isEqualTo(2);
assertThat(attributes.get(0).getName()).isEqualTo("alias");
assertThat(attributes.get(1).getName()).isEqualTo("convention");
}
@Test
void getAliasMappingReturnsAttributes() throws Exception {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(Mapped.class).get(1);
assertThat(getAliasMapping(mapping, 0)).isEqualTo(Mapped.class.getDeclaredMethod("alias"));
}
@Test
void getConventionMappingReturnsAttributes() throws Exception {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(Mapped.class).get(1);
assertThat(getConventionMapping(mapping, 1)).isEqualTo(Mapped.class.getDeclaredMethod("convention"));
}
@Test
void getMirrorSetWhenAliasPairReturnsMirrors() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(AliasPair.class).get(0);
MirrorSets mirrorSets = mapping.getMirrorSets();
assertThat(mirrorSets.size()).isEqualTo(1);
assertThat(mirrorSets.get(0).size()).isEqualTo(2);
assertThat(mirrorSets.get(0).get(0).getName()).isEqualTo("a");
assertThat(mirrorSets.get(0).get(1).getName()).isEqualTo("b");
}
@Test
void getMirrorSetWhenImplicitMirrorsReturnsMirrors() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(ImplicitMirrors.class).get(0);
MirrorSets mirrorSets = mapping.getMirrorSets();
assertThat(mirrorSets.size()).isEqualTo(1);
assertThat(mirrorSets.get(0).size()).isEqualTo(2);
assertThat(mirrorSets.get(0).get(0).getName()).isEqualTo("a");
assertThat(mirrorSets.get(0).get(1).getName()).isEqualTo("b");
}
@Test
void getMirrorSetWhenThreeDeepReturnsMirrors() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(ThreeDeepA.class);
AnnotationTypeMapping mappingA = mappings.get(0);
MirrorSets mirrorSetsA = mappingA.getMirrorSets();
assertThat(mirrorSetsA.size()).isEqualTo(2);
assertThat(getNames(mirrorSetsA.get(0))).containsExactly("a1", "a2", "a3");
AnnotationTypeMapping mappingB = mappings.get(1);
MirrorSets mirrorSetsB = mappingB.getMirrorSets();
assertThat(mirrorSetsB.size()).isEqualTo(1);
assertThat(getNames(mirrorSetsB.get(0))).containsExactly("b1", "b2");
AnnotationTypeMapping mappingC = mappings.get(2);
MirrorSets mirrorSetsC = mappingC.getMirrorSets();
assertThat(mirrorSetsC.size()).isEqualTo(0);
}
@Test
void getAliasMappingWhenThreeDeepReturnsMappedAttributes() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(ThreeDeepA.class);
AnnotationTypeMapping mappingA = mappings.get(0);
assertThat(getAliasMapping(mappingA, 0)).isNull();
assertThat(getAliasMapping(mappingA, 1)).isNull();
assertThat(getAliasMapping(mappingA, 2)).isNull();
assertThat(getAliasMapping(mappingA, 3)).isNull();
assertThat(getAliasMapping(mappingA, 4)).isNull();
AnnotationTypeMapping mappingB = mappings.get(1);
assertThat(getAliasMapping(mappingB, 0).getName()).isEqualTo("a1");
assertThat(getAliasMapping(mappingB, 1).getName()).isEqualTo("a1");
AnnotationTypeMapping mappingC = mappings.get(2);
assertThat(getAliasMapping(mappingC, 0).getName()).isEqualTo("a1");
assertThat(getAliasMapping(mappingC, 1).getName()).isEqualTo("a4");
}
@Test
void getAliasMappingsWhenHasDefinedAttributesReturnsMappedAttributes() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(DefinedAttributes.class).get(1);
assertThat(getAliasMapping(mapping, 0)).isNull();
assertThat(getAliasMapping(mapping, 1).getName()).isEqualTo("value");
}
@Test
void resolveMirrorsWhenAliasPairResolves() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(AliasPair.class).get(0);
Method[] resolvedA = resolveMirrorSets(mapping, WithAliasPairA.class, AliasPair.class);
assertThat(resolvedA[0].getName()).isEqualTo("a");
assertThat(resolvedA[1].getName()).isEqualTo("a");
Method[] resolvedB = resolveMirrorSets(mapping, WithAliasPairB.class, AliasPair.class);
assertThat(resolvedB[0].getName()).isEqualTo("b");
assertThat(resolvedB[1].getName()).isEqualTo("b");
}
@Test
void resolveMirrorsWhenHasSameValuesUsesFirst() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(AliasPair.class).get(0);
Method[] resolved = resolveMirrorSets(mapping, WithSameValueAliasPair.class, AliasPair.class);
assertThat(resolved[0].getName()).isEqualTo("a");
assertThat(resolved[1].getName()).isEqualTo("a");
}
@Test
void resolveMirrorsWhenOnlyHasDefaultValuesUsesFirst() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(AliasPair.class).get(0);
Method[] resolved = resolveMirrorSets(mapping, WithDefaultValueAliasPair.class, AliasPair.class);
assertThat(resolved[0].getName()).isEqualTo("a");
assertThat(resolved[1].getName()).isEqualTo("a");
}
@Test
void resolveMirrorsWhenHasDifferentValuesThrowsException() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(AliasPair.class).get(0);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
resolveMirrorSets(mapping, WithDifferentValueAliasPair.class, AliasPair.class))
.withMessage("Different @AliasFor mirror values for annotation ["
+ AliasPair.class.getName() + "] declared on "
+ WithDifferentValueAliasPair.class.getName()
+ "; attribute 'a' and its alias 'b' are declared with values of [test1] and [test2].");
}
@Test
void resolveMirrorsWhenHasWithMultipleRoutesToAliasReturnsMirrors() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(
MultipleRoutesToAliasA.class);
AnnotationTypeMapping mappingsA = getMapping(mappings, MultipleRoutesToAliasA.class);
assertThat(mappingsA.getMirrorSets().size()).isZero();
AnnotationTypeMapping mappingsB = getMapping(mappings, MultipleRoutesToAliasB.class);
assertThat(getNames(mappingsB.getMirrorSets().get(0))).containsExactly("b1", "b2", "b3");
AnnotationTypeMapping mappingsC = getMapping(mappings, MultipleRoutesToAliasC.class);
assertThat(getNames(mappingsC.getMirrorSets().get(0))).containsExactly("c1", "c2");
}
@Test
void getAliasMappingWhenHasWithMultipleRoutesToAliasReturnsMappedAttributes() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(
MultipleRoutesToAliasA.class);
AnnotationTypeMapping mappingsA = getMapping(mappings, MultipleRoutesToAliasA.class);
assertThat(getAliasMapping(mappingsA, 0)).isNull();
AnnotationTypeMapping mappingsB = getMapping(mappings, MultipleRoutesToAliasB.class);
assertThat(getAliasMapping(mappingsB, 0).getName()).isEqualTo("a1");
assertThat(getAliasMapping(mappingsB, 1).getName()).isEqualTo("a1");
assertThat(getAliasMapping(mappingsB, 2).getName()).isEqualTo("a1");
AnnotationTypeMapping mappingsC = getMapping(mappings, MultipleRoutesToAliasC.class);
assertThat(getAliasMapping(mappingsC, 0).getName()).isEqualTo("a1");
assertThat(getAliasMapping(mappingsC, 1).getName()).isEqualTo("a1");
}
@Test
void getConventionMappingWhenConventionToExplicitAliasesReturnsMappedAttributes() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(ConventionToExplicitAliases.class);
AnnotationTypeMapping mapping = getMapping(mappings, ConventionToExplicitAliasesTarget.class);
assertThat(mapping.getConventionMapping(0)).isEqualTo(0);
assertThat(mapping.getConventionMapping(1)).isEqualTo(0);
}
@Test
void isEquivalentToDefaultValueWhenValueAndDefaultAreNullReturnsTrue() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(ClassValue.class).get(0);
assertThat(mapping.isEquivalentToDefaultValue(0, null, ReflectionUtils::invokeMethod)).isTrue();
}
@Test
void isEquivalentToDefaultValueWhenValueAndDefaultMatchReturnsTrue() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(ClassValueWithDefault.class).get(0);
assertThat(mapping.isEquivalentToDefaultValue(0, InputStream.class, ReflectionUtils::invokeMethod)).isTrue();
}
@Test
void isEquivalentToDefaultValueWhenClassAndStringNamesMatchReturnsTrue() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(ClassValueWithDefault.class).get(0);
assertThat(mapping.isEquivalentToDefaultValue(0, "java.io.InputStream", ReflectionUtils::invokeMethod)).isTrue();
}
@Test
void isEquivalentToDefaultValueWhenClassArrayAndStringArrayNamesMatchReturnsTrue() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(ClassArrayValueWithDefault.class).get(0);
assertThat(mapping.isEquivalentToDefaultValue(0,
new String[] { "java.io.InputStream", "java.io.OutputStream" },
ReflectionUtils::invokeMethod)).isTrue();
}
@Test
void isEquivalentToDefaultValueWhenNestedAnnotationAndExtractedValuesMatchReturnsTrueAndValueSuppliedAsMap() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(NestedValue.class).get(0);
Map<String, Object> value = Collections.singletonMap("value", "java.io.InputStream");
assertThat(mapping.isEquivalentToDefaultValue(0, value, TypeMappedAnnotation::extractFromMap)).isTrue();
}
@Test // gh-24375
void isEquivalentToDefaultValueWhenNestedAnnotationAndExtractedValuesMatchReturnsTrueAndValueSuppliedAsTypeMappedAnnotation() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(NestedValue.class).get(0);
Map<String, String> attributes = Collections.singletonMap("value", "java.io.InputStream");
MergedAnnotation<ClassValue> value = TypeMappedAnnotation.of(getClass().getClassLoader(), null, ClassValue.class, attributes);
assertThat(mapping.isEquivalentToDefaultValue(0, value, TypeMappedAnnotation::extractFromMap)).isTrue();
}
@Test
void isEquivalentToDefaultValueWhenNotMatchingReturnsFalse() {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(ClassValueWithDefault.class).get(0);
assertThat(mapping.isEquivalentToDefaultValue(0, OutputStream.class, ReflectionUtils::invokeMethod)).isFalse();
}
private Method[] resolveMirrorSets(AnnotationTypeMapping mapping, Class<?> element,
Class<? extends Annotation> annotationClass) {
Annotation annotation = element.getAnnotation(annotationClass);
int[] resolved = mapping.getMirrorSets().resolve(element.getName(), annotation, ReflectionUtils::invokeMethod);
Method[] result = new Method[resolved.length];
for (int i = 0; i < resolved.length; i++) {
result[i] = resolved[i] != -1 ? mapping.getAttributes().get(resolved[i]) : null;
}
return result;
}
@Test
void forAnnotationTypeWhenRepeatableMetaAnnotationFilterd() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(WithRepeatedMetaAnnotations.class,
annotationType ->
ObjectUtils.nullSafeEquals(annotationType, Repeating.class.getName()));
assertThat(getAll(mappings)).flatExtracting(
AnnotationTypeMapping::getAnnotationType).containsExactly(
WithRepeatedMetaAnnotations.class);
}
@Nullable
private Method getAliasMapping(AnnotationTypeMapping mapping, int attributeIndex) {
int mapped = mapping.getAliasMapping(attributeIndex);
return mapped != -1 ? mapping.getRoot().getAttributes().get(mapped) : null;
}
@Nullable
private Method getConventionMapping(AnnotationTypeMapping mapping, int attributeIndex) {
int mapped = mapping.getConventionMapping(attributeIndex);
return mapped != -1 ? mapping.getRoot().getAttributes().get(mapped) : null;
}
private AnnotationTypeMapping getMapping(AnnotationTypeMappings mappings,
Class<? extends Annotation> annotationType) {
for (AnnotationTypeMapping candidate : getAll(mappings)) {
if (candidate.getAnnotationType() == annotationType) {
return candidate;
}
}
return null;
}
private List<AnnotationTypeMapping> getAll(AnnotationTypeMappings mappings) {
// AnnotationTypeMappings does not implement Iterable so we don't create
// too many garbage Iterators
return IntStream.range(0, mappings.size()).mapToObj(mappings::get).collect(toList());
}
private List<String> getNames(MirrorSet mirrorSet) {
List<String> names = new ArrayList<>(mirrorSet.size());
for (int i = 0; i < mirrorSet.size(); i++) {
names.add(mirrorSet.get(i).getName());
}
return names;
}
@Retention(RetentionPolicy.RUNTIME)
@interface SimpleAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@UsesSunMisc
@interface WithSpringLangAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
@interface AA {
}
@Retention(RetentionPolicy.RUNTIME)
@interface ABC {
}
@Retention(RetentionPolicy.RUNTIME)
@ABC
@interface AB {
}
@Retention(RetentionPolicy.RUNTIME)
@AA
@AB
@interface A {
}
@Retention(RetentionPolicy.RUNTIME)
@interface B {
}
@Retention(RetentionPolicy.RUNTIME)
@A
@B
@interface MetaAnnotated {
}
@Retention(RetentionPolicy.RUNTIME)
@interface Repeatings {
Repeating[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Repeatings.class)
@interface Repeating {
}
@Retention(RetentionPolicy.RUNTIME)
@Repeating
@Repeating
@interface WithRepeatedMetaAnnotations {
}
@Retention(RetentionPolicy.RUNTIME)
@SelfAnnotated
@interface SelfAnnotated {
}
@Retention(RetentionPolicy.RUNTIME)
@LoopB
@interface LoopA {
}
@Retention(RetentionPolicy.RUNTIME)
@LoopA
@interface LoopB {
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForWithBothValueAndAttribute {
@AliasFor(value = "bar", attribute = "foo")
String test();
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForToSelfNonExistingAttribute {
@AliasFor("missing")
String test() default "";
String other() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForToOtherNonExistingAttributeTarget {
String other() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@AliasForToOtherNonExistingAttributeTarget
@interface AliasForToOtherNonExistingAttribute {
@AliasFor(annotation = AliasForToOtherNonExistingAttributeTarget.class, attribute = "missing")
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForToSelf {
@AliasFor("test")
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForWithArrayCompatibleReturnTypesTarget {
String[] test() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@AliasForWithArrayCompatibleReturnTypesTarget
@interface AliasForWithArrayCompatibleReturnTypes {
@AliasFor(annotation = AliasForWithArrayCompatibleReturnTypesTarget.class)
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForWithIncompatibleReturnTypesTarget {
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForWithIncompatibleReturnTypes {
@AliasFor(annotation = AliasForWithIncompatibleReturnTypesTarget.class)
String[] test() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForToSelfAnnotatedToOtherAttribute {
@AliasFor("b")
String a() default "";
@AliasFor("c")
String b() default "";
@AliasFor("a")
String c() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasPair {
@AliasFor("b")
String a() default "";
@AliasFor("a")
String b() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@AliasPair
@interface AliasForWithMixedImplicitAndExplicitAliasesV1 {
// attempted implicit alias via attribute override
@AliasFor(annotation = AliasPair.class, attribute = "b")
String b() default "";
// explicit local alias
@AliasFor("b")
String a() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@AliasPair
@interface AliasForWithMixedImplicitAndExplicitAliasesV2 {
// attempted implicit alias via attribute override
@AliasFor(annotation = AliasPair.class, attribute = "a")
String b() default "";
// explicit local alias
@AliasFor("b")
String a() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForNonMetaAnnotated {
@AliasFor(annotation = AliasForNonMetaAnnotatedTarget.class)
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForNonMetaAnnotatedTarget {
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForSelfWithDifferentDefaults {
@AliasFor("b")
String a() default "a";
@AliasFor("a")
String b() default "b";
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasForSelfWithMissingDefault {
@AliasFor("b")
String a() default "a";
@AliasFor("a")
String b();
}
@Retention(RetentionPolicy.RUNTIME)
@interface AliasWithExplicitMirrorAndDifferentDefaultsTarget {
String a() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@AliasWithExplicitMirrorAndDifferentDefaultsTarget
@interface AliasWithExplicitMirrorAndDifferentDefaults {
@AliasFor(annotation = AliasWithExplicitMirrorAndDifferentDefaultsTarget.class, attribute = "a")
String a() default "x";
@AliasFor(annotation = AliasWithExplicitMirrorAndDifferentDefaultsTarget.class, attribute = "a")
String b() default "x";
@AliasFor(annotation = AliasWithExplicitMirrorAndDifferentDefaultsTarget.class, attribute = "a")
String c() default "y";
}
@Retention(RetentionPolicy.RUNTIME)
@interface MappedTarget {
String convention() default "";
String aliasTarget() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@MappedTarget
@interface Mapped {
String convention() default "";
@AliasFor(annotation = MappedTarget.class, attribute = "aliasTarget")
String alias() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface ImplicitMirrorsTarget {
@AliasFor("d")
String c() default "";
@AliasFor("c")
String d() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@ImplicitMirrorsTarget
@interface ImplicitMirrors {
@AliasFor(annotation = ImplicitMirrorsTarget.class, attribute = "c")
String a() default "";
@AliasFor(annotation = ImplicitMirrorsTarget.class, attribute = "c")
String b() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface ThreeDeepC {
String c1() default "";
String c2() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@ThreeDeepC
@interface ThreeDeepB {
@AliasFor(annotation = ThreeDeepC.class, attribute = "c1")
String b1() default "";
@AliasFor(annotation = ThreeDeepC.class, attribute = "c1")
String b2() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@ThreeDeepB
@interface ThreeDeepA {
@AliasFor(annotation = ThreeDeepB.class, attribute = "b1")
String a1() default "";
@AliasFor(annotation = ThreeDeepB.class, attribute = "b2")
String a2() default "";
@AliasFor(annotation = ThreeDeepC.class, attribute = "c1")
String a3() default "";
@AliasFor(annotation = ThreeDeepC.class, attribute = "c2")
String a4() default "";
@AliasFor(annotation = ThreeDeepC.class, attribute = "c2")
String a5() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface DefinedAttributesTarget {
String a();
String b() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@DefinedAttributesTarget(a = "test")
@interface DefinedAttributes {
@AliasFor(annotation = DefinedAttributesTarget.class, attribute = "b")
String value();
}
@AliasPair(a = "test")
static class WithAliasPairA {
}
@AliasPair(b = "test")
static class WithAliasPairB {
}
@AliasPair(a = "test", b = "test")
static class WithSameValueAliasPair {
}
@AliasPair(a = "test1", b = "test2")
static class WithDifferentValueAliasPair {
}
@AliasPair
static class WithDefaultValueAliasPair {
}
@Retention(RetentionPolicy.RUNTIME)
@interface MultipleRoutesToAliasC {
@AliasFor("c2")
String c1() default "";
@AliasFor("c1")
String c2() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@MultipleRoutesToAliasC
@interface MultipleRoutesToAliasB {
@AliasFor(annotation = MultipleRoutesToAliasC.class, attribute = "c2")
String b1() default "";
@AliasFor(annotation = MultipleRoutesToAliasC.class, attribute = "c2")
String b2() default "";
@AliasFor(annotation = MultipleRoutesToAliasC.class, attribute = "c1")
String b3() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@MultipleRoutesToAliasB
@interface MultipleRoutesToAliasA {
@AliasFor(annotation = MultipleRoutesToAliasB.class, attribute = "b2")
String a1() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface ConventionToExplicitAliasesTarget {
@AliasFor("test")
String value() default "";
@AliasFor("value")
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@ConventionToExplicitAliasesTarget
@interface ConventionToExplicitAliases {
String test() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@interface ClassValue {
Class<?> value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface ClassValueWithDefault {
Class<?> value() default InputStream.class;
}
@Retention(RetentionPolicy.RUNTIME)
@interface ClassArrayValueWithDefault {
Class<?>[] value() default { InputStream.class, OutputStream.class };
}
@Retention(RetentionPolicy.RUNTIME)
@interface NestedValue {
ClassValue value() default @ClassValue(InputStream.class);
}
}