/
AnnotationDescription.java
2309 lines (2054 loc) · 97.8 KB
/
AnnotationDescription.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
package net.bytebuddy.description.annotation;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.bytebuddy.description.enumeration.EnumerationDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.utility.PropertyDispatcher;
import net.bytebuddy.utility.privilege.SetAccessibleAction;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.security.AccessController;
import java.util.*;
import static net.bytebuddy.matcher.ElementMatchers.named;
/**
* An annotation description describes {@link java.lang.annotation.Annotation} meta data of a class without this class
* being required to be loaded. All values of an annotation are therefore represented in unloaded state:
* <ul>
* <li>{@link java.lang.Class} instances are represented as {@link TypeDescription}s.</li>
* <li>{@link java.lang.Enum} instances are represented as
* {@link net.bytebuddy.description.enumeration.EnumerationDescription}s.</li>
* <li>{@link java.lang.annotation.Annotation}s are described as
* {@link AnnotationDescription}s.</li>
* <li>All primitive types are represented as their wrapper types.</li>
* </ul>
* An annotation can however be loaded in order to access unwrapped values. This will cause a loading of the classes
* of these values.
*/
public interface AnnotationDescription {
/**
* Indicates an inexistent annotation in a type-safe manner.
*/
AnnotationDescription.Loadable<?> UNDEFINED = null;
/**
* Returns the value of the given method for this annotation value. Note that all return values are wrapped as
* described by {@link AnnotationDescription}.
*
* @param methodDescription The method for the value to be requested.
* @return The value for the given method.
*/
Object getValue(MethodDescription.InDefinedShape methodDescription);
/**
* Returns the value of the given method for this annotation value and performs a casting to the given value. Note
* that all return values are wrapped described by
* {@link AnnotationDescription}.
*
* @param methodDescription The method for the value to be requested.
* @param type The type to which the returned value should be casted.
* @param <T> The given type of the return value.
* @return The value for the given method casted to {@code type}.
*/
<T> T getValue(MethodDescription.InDefinedShape methodDescription, Class<T> type);
/**
* Returns a description of the annotation type of this annotation.
*
* @return A description of the annotation type of this annotation.
*/
TypeDescription getAnnotationType();
/**
* Links this annotation description to a given annotation type such that it can be loaded. This does not cause
* the values of this annotation to be loaded.
*
* @param annotationType The loaded annotation type of this annotation description.
* @param <T> The type of the annotation.
* @return A loadable version of this annotation description.
*/
<T extends Annotation> Loadable<T> prepare(Class<T> annotationType);
/**
* Returns this annotation's retention policy.
*
* @return This annotation's retention policy.
*/
RetentionPolicy getRetention();
/**
* Returns a set of all {@link ElementType}s that can declare this annotation.
*
* @return A set of all element types that can declare this annotation.
*/
Set<ElementType> getElementTypes();
/**
* Checks if this annotation is inherited.
*
* @return {@code true} if this annotation is inherited.
* @see Inherited
*/
boolean isInherited();
/**
* Checks if this annotation is documented.
*
* @return {@code true} if this annotation is documented.
* @see Documented
*/
boolean isDocumented();
/**
* A description of an annotation's value.
*
* @param <T> The type of the annotation's value when it is not loaded.
* @param <S> The type of the annotation's value when it is loaded.
*/
interface AnnotationValue<T, S> {
/**
* Resolves the unloaded value of this annotation.
*
* @return The unloaded value of this annotation.
*/
T resolve();
/**
* Returns the loaded value of this annotation.
*
* @param classLoader The class loader for loading this value.
* @return The loaded value of this annotation.
* @throws ClassNotFoundException If a type that represents a loaded value cannot be found.
*/
Loaded<S> load(ClassLoader classLoader) throws ClassNotFoundException;
/**
* A loaded variant of an {@link AnnotationValue}. While
* implementations of this value are required to be processed successfully by a
* {@link java.lang.ClassLoader} they might still be unresolved. Typical errors on loading an annotation
* value are:
* <ul>
* <li>{@link java.lang.annotation.IncompleteAnnotationException}: An annotation does not define a value
* even though no default value for a property is provided.</li>
* <li>{@link java.lang.EnumConstantNotPresentException}: An annotation defines an unknown value for
* a known enumeration.</li>
* <li>{@link java.lang.annotation.AnnotationTypeMismatchException}: An annotation property is not
* of the expected type.</li>
* </ul>
* Implementations of this interface must implement methods for {@link Object#hashCode()} and
* {@link Object#toString()} that resemble those used for the annotation values of an actual
* {@link java.lang.annotation.Annotation} implementation. Also, instances must implement
* {@link java.lang.Object#equals(Object)} to return {@code true} for other instances of
* this interface that represent the same annotation value.
*
* @param <U> The type of the loaded value of this annotation.
*/
interface Loaded<U> {
/**
* Returns the state of the represented loaded annotation value.
*
* @return The state represented by this instance.
*/
State getState();
/**
* Resolves the value to the actual value of an annotation. Calling this method might throw a runtime
* exception if this value is either not defined or not resolved.
*
* @return The actual annotation value represented by this instance.
*/
U resolve();
/**
* Represents the state of a {@link Loaded} annotation property.
*/
enum State {
/**
* A non-defined annotation value describes an annotation property which is missing such that
* an {@link java.lang.annotation.IncompleteAnnotationException} would be thrown.
*/
NON_DEFINED,
/**
* A non-resolved annotation value describes an annotation property which does not represent a
* valid value but an exceptional state.
*/
NON_RESOLVED,
/**
* A resolved annotation value describes an annotation property with an actual value.
*/
RESOLVED;
/**
* Returns {@code true} if the related annotation value is defined, i.e. either represents
* an actual value or an exceptional state.
*
* @return {@code true} if the related annotation value is defined.
*/
public boolean isDefined() {
return this != NON_DEFINED;
}
/**
* Returns {@code true} if the related annotation value is resolved, i.e. represents an actual
* value.
*
* @return {@code true} if the related annotation value is resolved.
*/
public boolean isResolved() {
return this == RESOLVED;
}
@Override
public String toString() {
return "AnnotationDescription.AnnotationValue.Loaded.State." + name();
}
}
}
/**
* Represents a primitive value, a {@link java.lang.String} or an array of the latter types.
*
* @param <U> The type where primitive values are represented by their boxed type.
*/
class Trivial<U> implements AnnotationValue<U, U> {
/**
* The represented value.
*/
private final U value;
/**
* A property dispatcher for the given value.
*/
private final PropertyDispatcher propertyDispatcher;
/**
* Creates a new representation of a trivial annotation value.
*
* @param value The value to represent.
*/
public Trivial(U value) {
this.value = value;
propertyDispatcher = PropertyDispatcher.of(value.getClass());
}
@Override
public U resolve() {
return value;
}
@Override
public AnnotationValue.Loaded<U> load(ClassLoader classLoader) {
return new Loaded<U>(value, propertyDispatcher);
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& propertyDispatcher.equals(value, ((Trivial) other).value);
}
@Override
public int hashCode() {
return propertyDispatcher.hashCode(value);
}
@Override
public String toString() {
return "AnnotationDescription.AnnotationValue.Trivial{" +
"value=" + value +
", propertyDispatcher=" + propertyDispatcher +
'}';
}
/**
* Represents a trivial loaded value.
*
* @param <V> The annotation properties type.
*/
public static class Loaded<V> implements AnnotationValue.Loaded<V> {
/**
* The represented value.
*/
private final V value;
/**
* The property dispatcher for computing this value's hash code, string and equals implementation.
*/
private final PropertyDispatcher propertyDispatcher;
/**
* Creates a new trivial loaded annotation value representation.
*
* @param value The represented value.
* @param propertyDispatcher The property dispatcher for computing this value's hash
* code, string and equals implementation.
*/
public Loaded(V value, PropertyDispatcher propertyDispatcher) {
this.value = value;
this.propertyDispatcher = propertyDispatcher;
}
@Override
public State getState() {
return State.RESOLVED;
}
@Override
public V resolve() {
return propertyDispatcher.conditionalClone(value);
}
@Override
public int hashCode() {
return propertyDispatcher.hashCode(value);
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof AnnotationValue.Loaded<?>)) return false;
AnnotationValue.Loaded<?> loadedOther = (AnnotationValue.Loaded<?>) other;
return loadedOther.getState().isResolved() && propertyDispatcher.equals(value, loadedOther.resolve());
}
@Override
public String toString() {
return propertyDispatcher.toString(value);
}
}
}
/**
* A description of an {@link java.lang.annotation.Annotation} as a value of another annotation.
*
* @param <U> The type of the annotation.
*/
class ForAnnotation<U extends Annotation> implements AnnotationValue<AnnotationDescription, U> {
/**
* The annotation description that this value represents.
*/
private final AnnotationDescription annotationDescription;
/**
* Creates a new annotation value for a given annotation description.
*
* @param annotationDescription The annotation description that this value represents.
*/
public ForAnnotation(AnnotationDescription annotationDescription) {
this.annotationDescription = annotationDescription;
}
/**
* Creates an annotation value instance for describing the given annotation type and values.
*
* @param annotationType The annotation type.
* @param annotationValues The values of the annotation.
* @param <V> The type of the annotation.
* @return An annotation value representing the given annotation.
*/
public static <V extends Annotation> AnnotationValue<AnnotationDescription, V> of(TypeDescription annotationType,
Map<String, AnnotationValue<?, ?>> annotationValues) {
return new ForAnnotation<V>(new AnnotationDescription.Latent(annotationType, annotationValues));
}
@Override
public AnnotationDescription resolve() {
return annotationDescription;
}
@Override
public AnnotationValue.Loaded<U> load(ClassLoader classLoader) throws ClassNotFoundException {
@SuppressWarnings("unchecked")
Class<U> annotationType = (Class<U>) Class.forName(annotationDescription.getAnnotationType().getName(), false, classLoader);
return new Loaded<U>(annotationDescription.prepare(annotationType).load());
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
ForAnnotation that = (ForAnnotation) other;
return annotationDescription.equals(that.annotationDescription);
}
@Override
public int hashCode() {
return annotationDescription.hashCode();
}
@Override
public String toString() {
return "AnnotationDescription.AnnotationValue.ForAnnotation{" +
"annotationDescription=" + annotationDescription +
'}';
}
/**
* A loaded version of the described annotation.
*
* @param <V> The annotation type.
*/
public static class Loaded<V extends Annotation> implements AnnotationValue.Loaded<V> {
/**
* The loaded version of the represented annotation.
*/
private final V annotation;
/**
* Creates a representation of a loaded annotation.
*
* @param annotation The represented annotation.
*/
public Loaded(V annotation) {
this.annotation = annotation;
}
@Override
public State getState() {
return State.RESOLVED;
}
@Override
public V resolve() {
return annotation;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof AnnotationValue.Loaded<?>)) return false;
AnnotationValue.Loaded<?> loadedOther = (AnnotationValue.Loaded<?>) other;
return loadedOther.getState().isResolved() && annotation.equals(loadedOther.resolve());
}
@Override
public int hashCode() {
return annotation.hashCode();
}
@Override
public String toString() {
return annotation.toString();
}
}
/**
* <p>
* Represents an annotation value which was attempted to ba loaded by a type that does not represent
* an annotation value.
* </p>
* <p>
* <b>Note</b>: Neither of {@link Object#hashCode()}, {@link Object#toString()} and
* {@link java.lang.Object#equals(Object)} are implemented specifically what resembles the way
* such exceptional states are represented in the Open JDK's annotation implementations.
* </p>
*/
public static class IncompatibleRuntimeType implements AnnotationValue.Loaded<Annotation> {
/**
* The incompatible runtime type which is not an annotation type.
*/
private final Class<?> incompatibleType;
/**
* Creates a new representation for an annotation with an incompatible runtime type.
*
* @param incompatibleType The incompatible runtime type which is not an annotation type.
*/
public IncompatibleRuntimeType(Class<?> incompatibleType) {
this.incompatibleType = incompatibleType;
}
@Override
public State getState() {
return State.NON_RESOLVED;
}
@Override
public Annotation resolve() {
throw new IncompatibleClassChangeError("Not an annotation type: " + incompatibleType.toString());
}
/* does intentionally not implement hashCode, equals and toString */
}
}
/**
* A description of an {@link java.lang.Enum} as a value of an annotation.
*
* @param <U> The type of the enumeration.
*/
class ForEnumeration<U extends Enum<U>> implements AnnotationValue<EnumerationDescription, U> {
/**
* The enumeration that is represented.
*/
private final EnumerationDescription enumerationDescription;
/**
* Creates a new description of an annotation value for a given enumeration.
*
* @param enumerationDescription The enumeration that is to be represented.
*/
public ForEnumeration(EnumerationDescription enumerationDescription) {
this.enumerationDescription = enumerationDescription;
}
/**
* Creates a new annotation value for the given enumeration description.
*
* @param value The value to represent.
* @param <V> The type of the represented enumeration.
* @return An annotation value that describes the given enumeration.
*/
public static <V extends Enum<V>> AnnotationValue<EnumerationDescription, V> of(EnumerationDescription value) {
return new ForEnumeration<V>(value);
}
@Override
public EnumerationDescription resolve() {
return enumerationDescription;
}
@Override
public AnnotationValue.Loaded<U> load(ClassLoader classLoader) throws ClassNotFoundException {
@SuppressWarnings("unchecked")
Class<U> enumerationType = (Class<U>) Class.forName(enumerationDescription.getEnumerationType().getName(), false, classLoader);
return new Loaded<U>(enumerationDescription.load(enumerationType));
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
ForEnumeration that = (ForEnumeration) other;
return enumerationDescription.equals(that.enumerationDescription);
}
@Override
public int hashCode() {
return enumerationDescription.hashCode();
}
@Override
public String toString() {
return "AnnotationDescription.AnnotationValue.ForEnumeration{" +
"enumerationDescription=" + enumerationDescription +
'}';
}
/**
* A loaded representation of an enumeration value.
*
* @param <V> The type of the represented enumeration.
*/
public static class Loaded<V extends Enum<V>> implements AnnotationValue.Loaded<V> {
/**
* The represented enumeration.
*/
private final V enumeration;
/**
* Creates a loaded version of an enumeration description.
*
* @param enumeration The represented enumeration.
*/
public Loaded(V enumeration) {
this.enumeration = enumeration;
}
@Override
public State getState() {
return State.RESOLVED;
}
@Override
public V resolve() {
return enumeration;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof AnnotationValue.Loaded<?>)) return false;
AnnotationValue.Loaded<?> loadedOther = (AnnotationValue.Loaded<?>) other;
return loadedOther.getState().isResolved() && enumeration.equals(loadedOther.resolve());
}
@Override
public int hashCode() {
return enumeration.hashCode();
}
@Override
public String toString() {
return enumeration.toString();
}
}
/**
* <p>
* Represents an annotation's enumeration value for a constant that does not exist for the runtime
* enumeration type.
* </p>
* <p>
* <b>Note</b>: Neither of {@link Object#hashCode()}, {@link Object#toString()} and
* {@link java.lang.Object#equals(Object)} are implemented specifically what resembles the way
* such exceptional states are represented in the Open JDK's annotation implementations.
* </p>
*/
public static class UnknownRuntimeEnumeration implements AnnotationValue.Loaded<Enum<?>> {
/**
* The loaded enumeration type.
*/
private final Class<? extends Enum<?>> enumType;
/**
* The value for which no enumeration constant exists at runtime.
*/
private final String value;
/**
* Creates a new representation for an unknown enumeration constant of an annotation.
*
* @param enumType The loaded enumeration type.
* @param value The value for which no enumeration constant exists at runtime.
*/
public UnknownRuntimeEnumeration(Class<? extends Enum<?>> enumType, String value) {
this.enumType = enumType;
this.value = value;
}
@Override
public State getState() {
return State.NON_RESOLVED;
}
@Override
public Enum<?> resolve() {
throw new EnumConstantNotPresentException(enumType, value);
}
/* hashCode, equals and toString are intentionally not implemented */
}
/**
* <p>
* Represents an annotation's enumeration value for a runtime type that is not an enumeration type.
* </p>
* <p>
* <b>Note</b>: Neither of {@link Object#hashCode()}, {@link Object#toString()} and
* {@link java.lang.Object#equals(Object)} are implemented specifically what resembles the way
* such exceptional states are represented in the Open JDK's annotation implementations.
* </p>
*/
public static class IncompatibleRuntimeType implements AnnotationValue.Loaded<Enum<?>> {
/**
* The runtime type which is not an enumeration type.
*/
private final Class<?> type;
/**
* Creates a new representation for an incompatible runtime type.
*
* @param type The runtime type which is not an enumeration type.
*/
public IncompatibleRuntimeType(Class<?> type) {
this.type = type;
}
@Override
public State getState() {
return State.NON_RESOLVED;
}
@Override
public Enum<?> resolve() {
throw new IncompatibleClassChangeError("Not an enumeration type: " + type.toString());
}
/* hashCode, equals and toString are intentionally not implemented */
}
}
/**
* A description of a {@link java.lang.Class} as a value of an annotation.
*
* @param <U> The type of the {@link java.lang.Class} that is described.
*/
class ForType<U extends Class<U>> implements AnnotationValue<TypeDescription, U> {
/**
* Indicates to a class loading process that class initializers are not required to be executed when loading a type.
*/
private static final boolean NO_INITIALIZATION = false;
/**
* A description of the represented type.
*/
private final TypeDescription typeDescription;
/**
* Creates a new annotation value that represents a type.
*
* @param typeDescription The represented type.
*/
public ForType(TypeDescription typeDescription) {
this.typeDescription = typeDescription;
}
/**
* Creates an annotation value for representing the given type.
*
* @param typeDescription The type to represent.
* @param <V> The represented type.
* @return An annotation value that represents the given type.
*/
public static <V extends Class<V>> AnnotationValue<TypeDescription, V> of(TypeDescription typeDescription) {
return new ForType<V>(typeDescription);
}
@Override
public TypeDescription resolve() {
return typeDescription;
}
@Override
@SuppressWarnings("unchecked")
public AnnotationValue.Loaded<U> load(ClassLoader classLoader) throws ClassNotFoundException {
return new Loaded<U>((U) Class.forName(typeDescription.getName(), NO_INITIALIZATION, classLoader));
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
ForType forType = (ForType) other;
return typeDescription.equals(forType.typeDescription);
}
@Override
public int hashCode() {
return typeDescription.hashCode();
}
@Override
public String toString() {
return "AnnotationDescription.AnnotationValue.ForType{" +
"typeDescription=" + typeDescription +
'}';
}
/**
* A loaded annotation value for a given type.
*
* @param <U> The represented type.
*/
protected static class Loaded<U extends Class<U>> implements AnnotationValue.Loaded<U> {
/**
* The represented type.
*/
private final U type;
/**
* Creates a new loaded annotation value for a given type.
*
* @param type The represented type.
*/
public Loaded(U type) {
this.type = type;
}
@Override
public State getState() {
return State.RESOLVED;
}
@Override
public U resolve() {
return type;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof AnnotationValue.Loaded<?>)) return false;
AnnotationValue.Loaded<?> loadedOther = (AnnotationValue.Loaded<?>) other;
return loadedOther.getState().isResolved() && type.equals(loadedOther.resolve());
}
@Override
public int hashCode() {
return type.hashCode();
}
@Override
public String toString() {
return PropertyDispatcher.TYPE_LOADED.toString(type);
}
}
}
/**
* Describes a complex array that is the value of an annotation. Complex arrays are arrays that might trigger the loading
* of user-defined types, i.e. {@link java.lang.Class}, {@link java.lang.annotation.Annotation} and {@link java.lang.Enum}
* instances.
*
* @param <U> The component type of the annotation's value when it is not loaded.
* @param <V> The component type of the annotation's value when it is loaded.
*/
class ForComplexArray<U, V> implements AnnotationValue<U[], V[]> {
/**
* The component type for arrays containing unloaded versions of the annotation array's values.
*/
private final Class<?> unloadedComponentType;
/**
* A description of the component type when it is loaded.
*/
private final TypeDescription componentType;
/**
* A list of values of the array elements.
*/
private final List<? extends AnnotationValue<?, ?>> annotationValues;
/**
* Creates a new complex array.
*
* @param unloadedComponentType The component type for arrays containing unloaded versions of the annotation array's values.
* @param componentType A description of the component type when it is loaded.
* @param annotationValues A list of values of the array elements.
*/
protected ForComplexArray(Class<?> unloadedComponentType,
TypeDescription componentType,
List<? extends AnnotationValue<?, ?>> annotationValues) {
this.unloadedComponentType = unloadedComponentType;
this.componentType = componentType;
this.annotationValues = annotationValues;
}
/**
* Creates a new complex array of enumeration descriptions.
*
* @param enumerationType A description of the type of the enumeration.
* @param enumerationDescription An array of enumeration descriptions.
* @param <W> The type of the enumeration.
* @return A description of the array of enumeration values.
*/
public static <W extends Enum<W>> AnnotationValue<EnumerationDescription[], W[]> of(TypeDescription enumerationType,
EnumerationDescription[] enumerationDescription) {
List<AnnotationValue<EnumerationDescription, W>> values = new ArrayList<AnnotationValue<EnumerationDescription, W>>(enumerationDescription.length);
for (EnumerationDescription value : enumerationDescription) {
if (!value.getEnumerationType().equals(enumerationType)) {
throw new IllegalArgumentException(value + " is not of " + enumerationType);
}
values.add(ForEnumeration.<W>of(value));
}
return new ForComplexArray<EnumerationDescription, W>(EnumerationDescription.class, enumerationType, values);
}
/**
* Creates a new complex array of annotation descriptions.
*
* @param annotationType A description of the type of the annotation.
* @param annotationDescription An array of annotation descriptions.
* @param <W> The type of the annotation.
* @return A description of the array of enumeration values.
*/
public static <W extends Annotation> AnnotationValue<AnnotationDescription[], W[]> of(TypeDescription annotationType,
AnnotationDescription[] annotationDescription) {
List<AnnotationValue<AnnotationDescription, W>> values = new ArrayList<AnnotationValue<AnnotationDescription, W>>(annotationDescription.length);
for (AnnotationDescription value : annotationDescription) {
if (!value.getAnnotationType().equals(annotationType)) {
throw new IllegalArgumentException(value + " is not of " + annotationType);
}
values.add(new ForAnnotation<W>(value));
}
return new ForComplexArray<AnnotationDescription, W>(AnnotationDescription.class, annotationType, values);
}
/**
* Creates a new complex array of annotation descriptions.
*
* @param typeDescription A description of the types contained in the array.
* @return A description of the array of enumeration values.
*/
@SuppressWarnings("unchecked")
public static AnnotationValue<TypeDescription[], Class<?>[]> of(TypeDescription[] typeDescription) {
List<AnnotationValue<TypeDescription, Class<?>>> values = new ArrayList<AnnotationValue<TypeDescription, Class<?>>>(typeDescription.length);
for (TypeDescription value : typeDescription) {
values.add((AnnotationValue) ForType.<Class>of(value));
}
return new ForComplexArray<TypeDescription, Class<?>>(TypeDescription.class, TypeDescription.CLASS, values);
}
@Override
public U[] resolve() {
@SuppressWarnings("unchecked")
U[] value = (U[]) Array.newInstance(unloadedComponentType, annotationValues.size());
int index = 0;
for (AnnotationValue<?, ?> annotationValue : annotationValues) {
Array.set(value, index++, annotationValue.resolve());
}
return value;
}
@Override
@SuppressWarnings("unchecked")
public AnnotationValue.Loaded<V[]> load(ClassLoader classLoader) throws ClassNotFoundException {
List<AnnotationValue.Loaded<?>> loadedValues = new ArrayList<AnnotationValue.Loaded<?>>(annotationValues.size());
for (AnnotationValue<?, ?> value : annotationValues) {
loadedValues.add(value.load(classLoader));
}
return new Loaded<V>((Class<V>) Class.forName(componentType.getName(), false, classLoader), loadedValues);
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
ForComplexArray that = (ForComplexArray) other;
return annotationValues.equals(that.annotationValues)
&& componentType.equals(that.componentType)
&& unloadedComponentType.equals(that.unloadedComponentType);
}
@Override
public int hashCode() {
int result = unloadedComponentType.hashCode();
result = 31 * result + componentType.hashCode();
result = 31 * result + annotationValues.hashCode();
return result;
}
@Override
public String toString() {
return "AnnotationDescription.AnnotationValue.ForComplexArray{" +
"unloadedComponentType=" + unloadedComponentType +
", componentType=" + componentType +
", annotationValues=" + annotationValues +
'}';
}
/**
* Represents a loaded complex array.
*
* @param <W> The component type of the loaded array.
*/
protected static class Loaded<W> implements AnnotationValue.Loaded<W[]> {
/**
* The loaded component type of the array.
*/
private final Class<W> componentType;
/**
* A list of loaded values that the represented array contains.
*/
private final List<AnnotationValue.Loaded<?>> values;
/**
* Creates a new loaded value representing a complex array.
*
* @param componentType The loaded component type of the array.
* @param values A list of loaded values that the represented array contains.
*/
protected Loaded(Class<W> componentType, List<AnnotationValue.Loaded<?>> values) {
this.componentType = componentType;
this.values = values;
}
@Override
public State getState() {
for (AnnotationValue.Loaded<?> value : values) {
if (!value.getState().isResolved()) {
return State.NON_RESOLVED;
}
}
return State.RESOLVED;
}
@Override
public W[] resolve() {
@SuppressWarnings("unchecked")
W[] array = (W[]) Array.newInstance(componentType, values.size());
int index = 0;
for (AnnotationValue.Loaded<?> annotationValue : values) {
Array.set(array, index++, annotationValue.resolve());
}
return array;