-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
TypePool.java
7810 lines (6730 loc) · 355 KB
/
TypePool.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.pool;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
import net.bytebuddy.description.TypeVariableSource;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.annotation.AnnotationValue;
import net.bytebuddy.description.enumeration.EnumerationDescription;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.method.ParameterList;
import net.bytebuddy.description.type.PackageDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.utility.OpenedClassReader;
import org.objectweb.asm.*;
import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.signature.SignatureVisitor;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.GenericSignatureFormatError;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static net.bytebuddy.matcher.ElementMatchers.*;
/**
* A type pool allows the retrieval of {@link TypeDescription} by its name.
*/
public interface TypePool {
/**
* Locates and describes the given type by its name.
*
* @param name The name of the type to describe. The name is to be written as when calling {@link Object#toString()}
* on a loaded {@link java.lang.Class}.
* @return A resolution of the type to describe. If the type to be described was found, the returned
* {@link net.bytebuddy.pool.TypePool.Resolution} represents this type. Otherwise, an illegal resolution is returned.
*/
Resolution describe(String name);
/**
* Clears this type pool's cache.
*/
void clear();
/**
* A resolution of a {@link net.bytebuddy.pool.TypePool} which was queried for a description.
*/
interface Resolution {
/**
* Determines if this resolution represents a fully-resolved {@link TypeDescription}.
*
* @return {@code true} if the queried type could be resolved.
*/
boolean isResolved();
/**
* Resolves this resolution to a {@link TypeDescription}. If this resolution is unresolved, this
* method throws an exception either upon invoking this method or upon invoking at least one method
* of the returned type description.
*
* @return The type description that is represented by this resolution.
*/
TypeDescription resolve();
/**
* A simple resolution that represents a given {@link TypeDescription}.
*/
@HashCodeAndEqualsPlugin.Enhance
class Simple implements Resolution {
/**
* The represented type description.
*/
private final TypeDescription typeDescription;
/**
* Creates a new successful resolution of a given type description.
*
* @param typeDescription The represented type description.
*/
public Simple(TypeDescription typeDescription) {
this.typeDescription = typeDescription;
}
@Override
public boolean isResolved() {
return true;
}
@Override
public TypeDescription resolve() {
return typeDescription;
}
}
/**
* A canonical representation of a non-successful resolution of a {@link net.bytebuddy.pool.TypePool}.
*/
@HashCodeAndEqualsPlugin.Enhance
class Illegal implements Resolution {
/**
* The name of the unresolved type.
*/
private final String name;
/**
* Creates a new illegal resolution.
*
* @param name The name of the unresolved type.
*/
public Illegal(String name) {
this.name = name;
}
@Override
public boolean isResolved() {
return false;
}
@Override
public TypeDescription resolve() {
throw new IllegalStateException("Cannot resolve type description for " + name);
}
}
}
/**
* A cache provider for a {@link net.bytebuddy.pool.TypePool}.
*/
interface CacheProvider {
/**
* The value that is returned on a cache-miss.
*/
Resolution UNRESOLVED = null;
/**
* Attempts to find a resolution in this cache.
*
* @param name The name of the type to describe.
* @return A resolution of the type or {@code null} if no such resolution can be found in the cache..
*/
Resolution find(String name);
/**
* Registers a resolution in this cache. If a resolution to the given name already exists in the
* cache, it should be discarded.
*
* @param name The name of the type that is to be registered.
* @param resolution The resolution to register.
* @return The oldest version of a resolution that is currently registered in the cache which might
* be the given resolution or another resolution that was previously registered.
*/
Resolution register(String name, Resolution resolution);
/**
* Clears this cache.
*/
void clear();
/**
* A non-operational cache that does not store any type descriptions.
*/
enum NoOp implements CacheProvider {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public Resolution find(String name) {
return UNRESOLVED;
}
@Override
public Resolution register(String name, Resolution resolution) {
return resolution;
}
@Override
public void clear() {
/* do nothing */
}
}
/**
* A simple, thread-safe type cache based on a {@link java.util.concurrent.ConcurrentHashMap}.
*/
class Simple implements CacheProvider {
/**
* A map containing all cached resolutions by their names.
*/
private final ConcurrentMap<String, Resolution> cache;
/**
* Creates a new simple cache.
*/
public Simple() {
cache = new ConcurrentHashMap<String, Resolution>();
}
/**
* Returns a simple cache provider that is prepopulated with the {@link Object} type.
*
* @return A simple cache provider that is prepopulated with the {@link Object} type.
*/
public static CacheProvider withObjectType() {
CacheProvider cacheProvider = new Simple();
cacheProvider.register(Object.class.getName(), new Resolution.Simple(TypeDescription.OBJECT));
return cacheProvider;
}
@Override
public Resolution find(String name) {
return cache.get(name);
}
@Override
public Resolution register(String name, Resolution resolution) {
Resolution cached = cache.putIfAbsent(name, resolution);
return cached == null
? resolution
: cached;
}
@Override
public void clear() {
cache.clear();
}
}
}
/**
* An empty type pool that cannot describe any type.
*/
enum Empty implements TypePool {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public Resolution describe(String name) {
return new Resolution.Illegal(name);
}
@Override
public void clear() {
/* do nothing */
}
}
/**
* A base implementation of a {@link net.bytebuddy.pool.TypePool} that is managing a cache provider and
* that handles the description of array and primitive types.
*/
@HashCodeAndEqualsPlugin.Enhance
abstract class AbstractBase implements TypePool {
/**
* A map of primitive types by their name.
*/
protected static final Map<String, TypeDescription> PRIMITIVE_TYPES;
/**
* A map of primitive types by their descriptor.
*/
protected static final Map<String, String> PRIMITIVE_DESCRIPTORS;
/**
* The array symbol as used by Java descriptors.
*/
private static final String ARRAY_SYMBOL = "[";
/*
* Initializes the maps of primitive type names and descriptors.
*/
static {
Map<String, TypeDescription> primitiveTypes = new HashMap<String, TypeDescription>();
Map<String, String> primitiveDescriptors = new HashMap<String, String>();
for (Class<?> primitiveType : new Class<?>[]{boolean.class,
byte.class,
short.class,
char.class,
int.class,
long.class,
float.class,
double.class,
void.class}) {
primitiveTypes.put(primitiveType.getName(), TypeDescription.ForLoadedType.of(primitiveType));
primitiveDescriptors.put(Type.getDescriptor(primitiveType), primitiveType.getName());
}
PRIMITIVE_TYPES = Collections.unmodifiableMap(primitiveTypes);
PRIMITIVE_DESCRIPTORS = Collections.unmodifiableMap(primitiveDescriptors);
}
/**
* The cache provider of this instance.
*/
protected final CacheProvider cacheProvider;
/**
* Creates a new instance.
*
* @param cacheProvider The cache provider to be used.
*/
protected AbstractBase(CacheProvider cacheProvider) {
this.cacheProvider = cacheProvider;
}
@Override
public Resolution describe(String name) {
if (name.contains("/")) {
throw new IllegalArgumentException(name + " contains the illegal character '/'");
}
int arity = 0;
while (name.startsWith(ARRAY_SYMBOL)) {
arity++;
name = name.substring(1);
}
if (arity > 0) {
String primitiveName = PRIMITIVE_DESCRIPTORS.get(name);
name = primitiveName == null
? name.substring(1, name.length() - 1)
: primitiveName;
}
TypeDescription typeDescription = PRIMITIVE_TYPES.get(name);
Resolution resolution = typeDescription == null
? cacheProvider.find(name)
: new Resolution.Simple(typeDescription);
if (resolution == null) {
resolution = doCache(name, doDescribe(name));
}
return ArrayTypeResolution.of(resolution, arity);
}
/**
* Writes the resolution to the cache. This method should be overridden if the directly
* resolved instance should not be added to the cache.
*
* @param name The name of the type.
* @param resolution The resolution for this type.
* @return The actual resolution for the type of this name that is stored in the cache.
*/
protected Resolution doCache(String name, Resolution resolution) {
return cacheProvider.register(name, resolution);
}
@Override
public void clear() {
cacheProvider.clear();
}
/**
* Determines a resolution to a non-primitive, non-array type.
*
* @param name The name of the type to describe.
* @return A resolution to the type to describe.
*/
protected abstract Resolution doDescribe(String name);
/**
* Implements a hierarchical view of type pools, similarly to class loader hierarchies. For every lookup, the parent type pool
* is asked first if it can resolve a type. Only if the parent (and potentially its parents) are unable to resolve a type,
* this instance is queried for a type description.
*/
@HashCodeAndEqualsPlugin.Enhance
public abstract static class Hierarchical extends AbstractBase {
/**
* The parent type pool.
*/
private final TypePool parent;
/**
* Creates a hierarchical type pool.
*
* @param cacheProvider The cache provider to be used.
* @param parent The parent type pool to be used.
*/
protected Hierarchical(CacheProvider cacheProvider, TypePool parent) {
super(cacheProvider);
this.parent = parent;
}
@Override
public Resolution describe(String name) {
Resolution resolution = parent.describe(name);
return resolution.isResolved()
? resolution
: super.describe(name);
}
@Override
public void clear() {
try {
parent.clear();
} finally {
super.clear();
}
}
}
/**
* A resolution for a type that, if resolved, represents an array type.
*/
@HashCodeAndEqualsPlugin.Enhance
protected static class ArrayTypeResolution implements Resolution {
/**
* The underlying resolution that is represented by this instance.
*/
private final Resolution resolution;
/**
* The arity of the represented array.
*/
private final int arity;
/**
* Creates a wrapper for another resolution that, if resolved, represents an array type.
*
* @param resolution The underlying resolution that is represented by this instance.
* @param arity The arity of the represented array.
*/
protected ArrayTypeResolution(Resolution resolution, int arity) {
this.resolution = resolution;
this.arity = arity;
}
/**
* Creates a wrapper for another resolution that, if resolved, represents an array type. The wrapper
* is only created if the arity is not zero. If the arity is zero, the given resolution is simply
* returned instead.
*
* @param resolution The underlying resolution that is represented by this instance.
* @param arity The arity of the represented array.
* @return A wrapper for another resolution that, if resolved, represents an array type or the
* given resolution if the given arity is zero.
*/
protected static Resolution of(Resolution resolution, int arity) {
return arity == 0
? resolution
: new ArrayTypeResolution(resolution, arity);
}
@Override
public boolean isResolved() {
return resolution.isResolved();
}
@Override
public TypeDescription resolve() {
return TypeDescription.ArrayProjection.of(resolution.resolve(), arity);
}
}
/**
* Represents a nested annotation value.
*/
protected static class RawAnnotationValue extends AnnotationValue.AbstractBase<AnnotationDescription, Annotation> {
/**
* The type pool to use for looking up types.
*/
private final TypePool typePool;
/**
* The annotation token that represents the nested invocation.
*/
private final Default.LazyTypeDescription.AnnotationToken annotationToken;
/**
* Creates a new annotation value for a nested annotation.
*
* @param typePool The type pool to use for looking up types.
* @param annotationToken The token that represents the annotation.
*/
public RawAnnotationValue(TypePool typePool, Default.LazyTypeDescription.AnnotationToken annotationToken) {
this.typePool = typePool;
this.annotationToken = annotationToken;
}
@Override
public AnnotationDescription resolve() {
return annotationToken.toAnnotationDescription(typePool).resolve();
}
@Override
@SuppressWarnings("unchecked")
public Loaded<Annotation> load(ClassLoader classLoader) throws ClassNotFoundException {
Class<?> type = Class.forName(annotationToken.getBinaryName(), false, classLoader);
if (type.isAnnotation()) {
return new ForAnnotationDescription.Loaded<Annotation>(AnnotationDescription.AnnotationInvocationHandler.of(classLoader,
(Class<? extends Annotation>) type,
annotationToken.getValues()));
} else {
return new ForAnnotationDescription.IncompatibleRuntimeType(type);
}
}
@Override
public boolean equals(Object other) {
return this == other || other instanceof AnnotationValue<?, ?> && resolve().equals(((AnnotationValue<?, ?>) other).resolve());
}
@Override
public int hashCode() {
return resolve().hashCode();
}
@Override
public String toString() {
return resolve().toString();
}
}
/**
* Represents an enumeration value of an annotation.
*/
protected static class RawEnumerationValue extends AnnotationValue.AbstractBase<EnumerationDescription, Enum<?>> {
/**
* The type pool to use for looking up types.
*/
private final TypePool typePool;
/**
* The descriptor of the enumeration type.
*/
private final String descriptor;
/**
* The name of the enumeration.
*/
private final String value;
/**
* Creates a new enumeration value representation.
*
* @param typePool The type pool to use for looking up types.
* @param descriptor The descriptor of the enumeration type.
* @param value The name of the enumeration.
*/
public RawEnumerationValue(TypePool typePool, String descriptor, String value) {
this.typePool = typePool;
this.descriptor = descriptor;
this.value = value;
}
@Override
public EnumerationDescription resolve() {
return new LazyEnumerationDescription();
}
@Override
@SuppressWarnings("unchecked")
public Loaded<Enum<?>> load(ClassLoader classLoader) throws ClassNotFoundException {
Class<?> type = Class.forName(descriptor.substring(1, descriptor.length() - 1).replace('/', '.'), false, classLoader);
try {
return type.isEnum()
? new ForEnumerationDescription.Loaded(Enum.valueOf((Class) type, value))
: new ForEnumerationDescription.IncompatibleRuntimeType(type);
} catch (IllegalArgumentException ignored) {
return new ForEnumerationDescription.UnknownRuntimeEnumeration((Class) type, value);
}
}
@Override
public boolean equals(Object other) {
return this == other || other instanceof AnnotationValue<?, ?> && resolve().equals(((AnnotationValue<?, ?>) other).resolve());
}
@Override
public int hashCode() {
return resolve().hashCode();
}
@Override
public String toString() {
return resolve().toString();
}
/**
* An enumeration description where any type references are only resolved on demand.
*/
protected class LazyEnumerationDescription extends EnumerationDescription.AbstractBase {
@Override
public String getValue() {
return value;
}
@Override
public TypeDescription getEnumerationType() {
return typePool.describe(descriptor.substring(1, descriptor.length() - 1).replace('/', '.')).resolve();
}
@Override
public <T extends Enum<T>> T load(Class<T> type) {
return Enum.valueOf(type, value);
}
}
}
/**
* Represents a type value of an annotation.
*/
protected static class RawTypeValue extends AnnotationValue.AbstractBase<TypeDescription, Class<?>> {
/**
* A convenience reference indicating that a loaded type should not be initialized.
*/
private static final boolean NO_INITIALIZATION = false;
/**
* The type pool to use for looking up types.
*/
private final TypePool typePool;
/**
* The binary name of the type.
*/
private final String name;
/**
* Represents a type value of an annotation.
*
* @param typePool The type pool to use for looking up types.
* @param type A type representation of the type that is referenced by the annotation..
*/
protected RawTypeValue(TypePool typePool, Type type) {
this.typePool = typePool;
name = type.getSort() == Type.ARRAY
? type.getInternalName().replace('/', '.')
: type.getClassName();
}
@Override
public TypeDescription resolve() {
return typePool.describe(name).resolve();
}
@Override
public AnnotationValue.Loaded<Class<?>> load(ClassLoader classLoader) throws ClassNotFoundException {
return new Loaded(Class.forName(name, NO_INITIALIZATION, classLoader));
}
@Override
public boolean equals(Object other) {
return this == other || other instanceof AnnotationValue<?, ?> && resolve().equals(((AnnotationValue<?, ?>) other).resolve());
}
@Override
public int hashCode() {
return resolve().hashCode();
}
@Override
public String toString() {
return RenderingDispatcher.CURRENT.toSourceString(resolve());
}
/**
* Represents a loaded annotation property that represents a type.
*/
protected static class Loaded extends AnnotationValue.Loaded.AbstractBase<Class<?>> {
/**
* The type that is represented by an annotation property.
*/
private final Class<?> type;
/**
* Creates a new representation for an annotation property referencing a type.
*
* @param type The type that is represented by an annotation property.
*/
public Loaded(Class<?> type) {
this.type = type;
}
@Override
public State getState() {
return State.RESOLVED;
}
@Override
public Class<?> resolve() {
return type;
}
@Override
public boolean represents(Object value) {
return type.equals(value);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof AnnotationValue.Loaded<?>)) {
return false;
}
AnnotationValue.Loaded<?> annotationValue = (AnnotationValue.Loaded<?>) other;
return annotationValue.getState().isResolved() && type.equals(annotationValue.resolve());
}
@Override
public int hashCode() {
return type.hashCode();
}
@Override
public String toString() {
return RenderingDispatcher.CURRENT.toSourceString(TypeDescription.ForLoadedType.of(type));
}
}
}
/**
* Represents an array that is referenced by an annotation which does not contain primitive values or {@link String}s.
*/
protected static class RawDescriptionArray extends AnnotationValue.AbstractBase<Object[], Object[]> {
/**
* The type pool to use for looking up types.
*/
private final TypePool typePool;
/**
* A reference to the component type.
*/
private final ComponentTypeReference componentTypeReference;
/**
* A list of all values of this array value in their order.
*/
private List<AnnotationValue<?, ?>> values;
/**
* Creates a new array value representation of a complex array.
*
* @param typePool The type pool to use for looking up types.
* @param componentTypeReference A lazy reference to the component type of this array.
* @param values A list of all values of this annotation.
*/
public RawDescriptionArray(TypePool typePool,
ComponentTypeReference componentTypeReference,
List<AnnotationValue<?, ?>> values) {
this.typePool = typePool;
this.values = values;
this.componentTypeReference = componentTypeReference;
}
@Override
public Object[] resolve() {
TypeDescription componentTypeDescription = typePool.describe(componentTypeReference.lookup()).resolve();
Class<?> componentType;
if (componentTypeDescription.represents(Class.class)) {
componentType = TypeDescription.class;
} else if (componentTypeDescription.isAssignableTo(Enum.class)) { // Enums can implement annotation interfaces, check this first.
componentType = EnumerationDescription.class;
} else if (componentTypeDescription.isAssignableTo(Annotation.class)) {
componentType = AnnotationDescription.class;
} else if (componentTypeDescription.represents(String.class)) {
componentType = String.class;
} else {
throw new IllegalStateException("Unexpected complex array component type " + componentTypeDescription);
}
Object[] array = (Object[]) Array.newInstance(componentType, values.size());
int index = 0;
for (AnnotationValue<?, ?> annotationValue : values) {
Array.set(array, index++, annotationValue.resolve());
}
return array;
}
@Override
public AnnotationValue.Loaded<Object[]> load(ClassLoader classLoader) throws ClassNotFoundException {
List<AnnotationValue.Loaded<?>> loadedValues = new ArrayList<AnnotationValue.Loaded<?>>(values.size());
for (AnnotationValue<?, ?> value : values) {
loadedValues.add(value.load(classLoader));
}
return new Loaded(Class.forName(componentTypeReference.lookup(), false, classLoader), loadedValues);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof AnnotationValue<?, ?>)) {
return false;
}
Object value = ((AnnotationValue<?, ?>) other).resolve();
return value instanceof Object[] && Arrays.equals(resolve(), (Object[]) value);
}
@Override
public int hashCode() {
return Arrays.hashCode(resolve());
}
@Override
public String toString() {
return RenderingDispatcher.CURRENT.toSourceString(values);
}
/**
* A lazy representation of the component type of an array.
*/
public interface ComponentTypeReference {
/**
* Lazily returns the binary name of the array component type of an annotation value.
*
* @return The binary name of the component type.
*/
String lookup();
}
/**
* Represents a loaded annotation property representing a complex array.
*/
protected static class Loaded extends AnnotationValue.Loaded.AbstractBase<Object[]> {
/**
* The array's loaded component type.
*/
private final Class<?> componentType;
/**
* A list of loaded values of the represented complex array.
*/
private final List<AnnotationValue.Loaded<?>> values;
/**
* Creates a new representation of an annotation property representing an array of
* non-trivial values.
*
* @param componentType The array's loaded component type.
* @param values A list of loaded values of the represented complex array.
*/
public Loaded(Class<?> 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.UNRESOLVED;
}
}
return State.RESOLVED;
}
@Override
public Object[] resolve() {
Object[] array = (Object[]) Array.newInstance(componentType, values.size());
int index = 0;
for (AnnotationValue.Loaded<?> annotationValue : values) {
Array.set(array, index++, annotationValue.resolve());
}
return array;
}
@Override
public boolean represents(Object value) {
if (!(value instanceof Object[])) return false;
if (value.getClass().getComponentType() != componentType) return false;
Object[] array = (Object[]) value;
if (values.size() != array.length) return false;
Iterator<AnnotationValue.Loaded<?>> iterator = values.iterator();
for (Object aValue : array) {
AnnotationValue.Loaded<?> self = iterator.next();
if (!self.getState().isResolved() || !self.represents(aValue)) {
return false;
}
}
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof AnnotationValue.Loaded<?>)) {
return false;
}
AnnotationValue.Loaded<?> annotationValue = (AnnotationValue.Loaded<?>) other;
if (!annotationValue.getState().isResolved()) {
return false;
}
Object value = annotationValue.resolve();
if (!(value instanceof Object[])) {
return false;
}
Object[] arrayValue = (Object[]) value;
if (values.size() != arrayValue.length) {
return false;
}
Iterator<AnnotationValue.Loaded<?>> iterator = values.iterator();
for (Object aValue : arrayValue) {
AnnotationValue.Loaded<?> self = iterator.next();
if (!self.resolve().equals(aValue)) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 1;
for (AnnotationValue.Loaded<?> value : values) {
result = 31 * result + value.hashCode();
}
return result;
}
@Override
public String toString() {
return RenderingDispatcher.CURRENT.toSourceString(values);
}
}
}
}
/**
* <p>
* A default implementation of a {@link net.bytebuddy.pool.TypePool} that models binary data in the Java byte code format
* into a {@link TypeDescription}. The data lookup is delegated to a {@link net.bytebuddy.dynamic.ClassFileLocator}.
* </p>
* <p>
* {@link Resolution}s that are produced by this type pool are either fully resolved or not resolved at all.
* </p>
*/
@HashCodeAndEqualsPlugin.Enhance
class Default extends AbstractBase.Hierarchical {
/**
* Indicates that a visited method should be ignored.
*/
private static final MethodVisitor IGNORE_METHOD = null;
/**
* The locator to query for finding binary data of a type.
*/
protected final ClassFileLocator classFileLocator;
/**
* The reader mode to apply by this default type pool.
*/
protected final ReaderMode readerMode;
/**
* Creates a new default type pool without a parent pool.
*
* @param cacheProvider The cache provider to be used.
* @param classFileLocator The class file locator to be used.
* @param readerMode The reader mode to apply by this default type pool.
*/
public Default(CacheProvider cacheProvider, ClassFileLocator classFileLocator, ReaderMode readerMode) {
this(cacheProvider, classFileLocator, readerMode, Empty.INSTANCE);
}
/**
* Creates a new default type pool.
*
* @param cacheProvider The cache provider to be used.
* @param classFileLocator The class file locator to be used.
* @param readerMode The reader mode to apply by this default type pool.
* @param parentPool The parent type pool.
*/
public Default(CacheProvider cacheProvider, ClassFileLocator classFileLocator, ReaderMode readerMode, TypePool parentPool) {
super(cacheProvider, parentPool);
this.classFileLocator = classFileLocator;
this.readerMode = readerMode;
}
/**
* Creates a default {@link net.bytebuddy.pool.TypePool} that looks up data by querying the system class