-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
InstrumentedTypeDefaultTest.java
1307 lines (1176 loc) · 69.6 KB
/
InstrumentedTypeDefaultTest.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.dynamic.scaffold;
import net.bytebuddy.description.ModifierReviewable;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.TypeVariableSource;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationSource;
import net.bytebuddy.description.annotation.AnnotationValue;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.modifier.ModifierContributor;
import net.bytebuddy.description.type.*;
import net.bytebuddy.dynamic.TargetType;
import net.bytebuddy.dynamic.Transformer;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.LoadedTypeInitializer;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.test.packaging.PackagePrivateType;
import net.bytebuddy.test.utility.MockitoRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.io.Serializable;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.Collections;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.test.utility.FieldByFieldComparison.hasPrototype;
import static net.bytebuddy.test.utility.FieldByFieldComparison.matchesPrototype;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*;
public class InstrumentedTypeDefaultTest {
private static final String FOO = "foo", BAR = "bar", QUX = "qux", BAZ = "baz", ILLEGAL_NAME = "<>";
private static final int ILLEGAL_MODIFIERS = -1, OTHER_MODIFIERS = 42;
@Rule
public TestRule mockitoRule = new MockitoRule(this);
@Mock
private MethodVisitor methodVisitor;
@Mock
private Implementation.Context implementationContext;
@Mock
private AnnotationDescription annotationDescription;
protected static InstrumentedType.WithFlexibleName makePlainInstrumentedType() {
return new InstrumentedType.Default(FOO + "." + BAZ,
ModifierReviewable.EMPTY_MASK,
TypeDescription.Generic.OBJECT,
Collections.<TypeVariableToken>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<FieldDescription.Token>emptyList(),
Collections.<MethodDescription.Token>emptyList(),
Collections.<AnnotationDescription>emptyList(),
TypeInitializer.None.INSTANCE,
LoadedTypeInitializer.NoOp.INSTANCE,
TypeDescription.UNDEFINED,
MethodDescription.UNDEFINED,
TypeDescription.UNDEFINED,
Collections.<TypeDescription>emptyList(),
false,
false,
false,
TargetType.DESCRIPTION,
Collections.singletonList(TargetType.DESCRIPTION));
}
@Test
@SuppressWarnings("unchecked")
public void testWithTypeVariable() throws Exception {
TypeDescription.Generic boundType = mock(TypeDescription.Generic.class);
when(boundType.asGenericType()).thenReturn(boundType);
when(boundType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(boundType);
TypeDescription rawBoundType = mock(TypeDescription.class);
when(boundType.asErasure()).thenReturn(rawBoundType);
when(rawBoundType.getName()).thenReturn(FOO);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getTypeVariables().size(), is(0));
instrumentedType = instrumentedType.withTypeVariable(new TypeVariableToken(BAR, Collections.singletonList(boundType)));
assertThat(instrumentedType.getTypeVariables().size(), is(1));
TypeDescription.Generic typeVariable = instrumentedType.getTypeVariables().get(0);
assertThat(typeVariable.getTypeName(), is(BAR));
assertThat(typeVariable.getTypeVariableSource(), sameInstance((TypeVariableSource) instrumentedType));
assertThat(typeVariable.getUpperBounds(), is(Collections.singletonList(boundType)));
}
@Test
public void testWithTypeVariableWithInstrumentedType() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getTypeVariables().size(), is(0));
instrumentedType = instrumentedType.withTypeVariable(new TypeVariableToken(BAR, Collections.singletonList(TargetType.DESCRIPTION.asGenericType())));
assertThat(instrumentedType.getTypeVariables().size(), is(1));
TypeDescription.Generic typeVariable = instrumentedType.getTypeVariables().get(0);
assertThat(typeVariable.getTypeName(), is(BAR));
assertThat(typeVariable.getTypeVariableSource(), sameInstance((TypeVariableSource) instrumentedType));
assertThat(typeVariable.getUpperBounds(), is(Collections.singletonList(instrumentedType.asGenericType())));
}
@Test
@SuppressWarnings("unchecked")
public void testWithTypeVariableTransformed() throws Exception {
TypeDescription.Generic boundType = mock(TypeDescription.Generic.class);
when(boundType.asGenericType()).thenReturn(boundType);
when(boundType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(boundType);
TypeDescription rawBoundType = mock(TypeDescription.class);
when(boundType.asErasure()).thenReturn(rawBoundType);
when(rawBoundType.getName()).thenReturn(FOO);
InstrumentedType.WithFlexibleName instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getTypeVariables().size(), is(0));
instrumentedType = instrumentedType.withTypeVariable(new TypeVariableToken(BAR, Collections.singletonList(boundType)));
Transformer<TypeVariableToken> transformer = mock(Transformer.class);
TypeDescription.Generic otherBoundType = mock(TypeDescription.Generic.class);
when(otherBoundType.asGenericType()).thenReturn(otherBoundType);
when(otherBoundType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(otherBoundType);
TypeDescription rawOtherBoundType = mock(TypeDescription.class);
when(otherBoundType.asErasure()).thenReturn(rawOtherBoundType);
when(transformer.transform(instrumentedType, new TypeVariableToken(BAR, Collections.singletonList(boundType))))
.thenReturn(new TypeVariableToken(QUX, Collections.singletonList(otherBoundType)));
instrumentedType = instrumentedType.withTypeVariables(named(BAR), transformer);
assertThat(instrumentedType.getTypeVariables().size(), is(1));
TypeDescription.Generic typeVariable = instrumentedType.getTypeVariables().get(0);
assertThat(typeVariable.getTypeName(), is(QUX));
assertThat(typeVariable.getTypeVariableSource(), sameInstance((TypeVariableSource) instrumentedType));
assertThat(typeVariable.getUpperBounds(), is(Collections.singletonList(otherBoundType)));
}
@Test
@SuppressWarnings("unchecked")
public void testWithField() throws Exception {
TypeDescription.Generic fieldType = mock(TypeDescription.Generic.class);
when(fieldType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(fieldType);
TypeDescription rawFieldType = mock(TypeDescription.class);
when(fieldType.asErasure()).thenReturn(rawFieldType);
when(rawFieldType.getName()).thenReturn(FOO);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
instrumentedType = instrumentedType.withField(new FieldDescription.Token(BAR, Opcodes.ACC_PUBLIC, fieldType));
assertThat(instrumentedType.getDeclaredFields().size(), is(1));
FieldDescription.InDefinedShape fieldDescription = instrumentedType.getDeclaredFields().get(0);
assertThat(fieldDescription.getType(), is(fieldType));
assertThat(fieldDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
assertThat(fieldDescription.getName(), is(BAR));
assertThat(fieldDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}
@Test
public void testWithFieldOfInstrumentedType() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
instrumentedType = instrumentedType.withField(new FieldDescription.Token(BAR, Opcodes.ACC_PUBLIC, TargetType.DESCRIPTION.asGenericType()));
assertThat(instrumentedType.getDeclaredFields().size(), is(1));
FieldDescription.InDefinedShape fieldDescription = instrumentedType.getDeclaredFields().get(0);
assertThat(fieldDescription.getType().asErasure(), sameInstance((TypeDescription) instrumentedType));
assertThat(fieldDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
assertThat(fieldDescription.getName(), is(BAR));
assertThat(fieldDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}
@Test
public void testWithFieldOfInstrumentedTypeAsArray() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
instrumentedType = instrumentedType.withField(new FieldDescription.Token(BAR, Opcodes.ACC_PUBLIC,
new TypeDescription.Generic.OfGenericArray.Latent(TargetType.DESCRIPTION.asGenericType(), new AnnotationSource.Explicit(annotationDescription))));
assertThat(instrumentedType.getDeclaredFields().size(), is(1));
FieldDescription.InDefinedShape fieldDescription = instrumentedType.getDeclaredFields().get(0);
assertThat(fieldDescription.getType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(fieldDescription.getType().asErasure().isArray(), is(true));
assertThat(fieldDescription.getType().asErasure().getComponentType(), sameInstance((TypeDescription) instrumentedType));
assertThat(fieldDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
assertThat(fieldDescription.getName(), is(BAR));
assertThat(fieldDescription.getType().getDeclaredAnnotations().size(), is(1));
assertThat(fieldDescription.getType().getDeclaredAnnotations().getOnly(), is(annotationDescription));
assertThat(fieldDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}
@Test
@SuppressWarnings("unchecked")
public void testWithMethod() throws Exception {
TypeDescription.Generic returnType = mock(TypeDescription.Generic.class);
when(returnType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(returnType);
TypeDescription rawReturnType = mock(TypeDescription.class);
when(returnType.asErasure()).thenReturn(rawReturnType);
when(rawReturnType.getName()).thenReturn(FOO);
TypeDescription.Generic parameterType = mock(TypeDescription.Generic.class);
when(parameterType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(parameterType);
when(parameterType.asGenericType()).thenReturn(parameterType);
TypeDescription rawParameterType = mock(TypeDescription.class);
when(parameterType.asErasure()).thenReturn(rawParameterType);
when(rawParameterType.getName()).thenReturn(QUX);
when(rawParameterType.getStackSize()).thenReturn(StackSize.ZERO);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
instrumentedType = instrumentedType.withMethod(new MethodDescription.Token(BAR,
Opcodes.ACC_PUBLIC,
returnType,
Collections.singletonList(parameterType)));
assertThat(instrumentedType.getDeclaredMethods().size(), is(1));
MethodDescription.InDefinedShape methodDescription = instrumentedType.getDeclaredMethods().get(0);
assertThat(methodDescription.getReturnType(), is(returnType));
assertThat(methodDescription.getParameters().size(), is(1));
assertThat(methodDescription.getParameters().asTypeList(), is(Collections.singletonList(parameterType)));
assertThat(methodDescription.getExceptionTypes().size(), is(0));
assertThat(methodDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
assertThat(methodDescription.getName(), is(BAR));
assertThat(methodDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}
@Test
public void testWithMethodOfInstrumentedType() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
instrumentedType = instrumentedType.withMethod(new MethodDescription.Token(BAR,
Opcodes.ACC_PUBLIC,
TargetType.DESCRIPTION.asGenericType(),
Collections.singletonList(TargetType.DESCRIPTION.asGenericType())));
assertThat(instrumentedType.getDeclaredMethods().size(), is(1));
MethodDescription.InDefinedShape methodDescription = instrumentedType.getDeclaredMethods().get(0);
assertThat(methodDescription.getReturnType().asErasure(), sameInstance((TypeDescription) instrumentedType));
assertThat(methodDescription.getParameters().size(), is(1));
assertThat(methodDescription.getParameters().asTypeList().get(0).asErasure(), sameInstance((TypeDescription) instrumentedType));
assertThat(methodDescription.getExceptionTypes().size(), is(0));
assertThat(methodDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
assertThat(methodDescription.getName(), is(BAR));
assertThat(methodDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}
@Test
public void testWithMethodOfInstrumentedTypeAsArray() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
instrumentedType = instrumentedType.withMethod(new MethodDescription.Token(BAR,
Opcodes.ACC_PUBLIC,
new TypeDescription.Generic.OfGenericArray.Latent(TargetType.DESCRIPTION.asGenericType(), new AnnotationSource.Explicit(annotationDescription)),
Collections.singletonList(new TypeDescription.Generic.OfGenericArray.Latent(TargetType.DESCRIPTION.asGenericType(), new AnnotationSource.Explicit(annotationDescription)))));
assertThat(instrumentedType.getDeclaredMethods().size(), is(1));
MethodDescription.InDefinedShape methodDescription = instrumentedType.getDeclaredMethods().get(0);
assertThat(methodDescription.getReturnType().asErasure().isArray(), is(true));
assertThat(methodDescription.getReturnType().getComponentType().asErasure(), sameInstance((TypeDescription) instrumentedType));
assertThat(methodDescription.getParameters().size(), is(1));
assertThat(methodDescription.getParameters().asTypeList().asErasures().get(0).isArray(), is(true));
assertThat(methodDescription.getParameters().asTypeList().get(0).getComponentType().asErasure(), sameInstance((TypeDescription) instrumentedType));
assertThat(methodDescription.getExceptionTypes().size(), is(0));
assertThat(methodDescription.getReturnType().getDeclaredAnnotations().size(), is(1));
assertThat(methodDescription.getReturnType().getDeclaredAnnotations().getOnly(), is(annotationDescription));
assertThat(methodDescription.getParameters().getOnly().getType().getDeclaredAnnotations().size(), is(1));
assertThat(methodDescription.getParameters().getOnly().getType().getDeclaredAnnotations().getOnly(), is(annotationDescription));
assertThat(methodDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
assertThat(methodDescription.getName(), is(BAR));
assertThat(methodDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}
@Test
@SuppressWarnings("unchecked")
public void testWithInterface() throws Exception {
TypeDescription.Generic interfaceType = mock(TypeDescription.Generic.class);
when(interfaceType.asGenericType()).thenReturn(interfaceType);
when(interfaceType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(interfaceType);
TypeDescription rawBoundType = mock(TypeDescription.class);
when(interfaceType.asErasure()).thenReturn(rawBoundType);
when(rawBoundType.getName()).thenReturn(FOO);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getInterfaces().size(), is(0));
instrumentedType = instrumentedType.withInterfaces(new TypeList.Generic.Explicit(interfaceType));
assertThat(instrumentedType.getInterfaces().size(), is(1));
assertThat(instrumentedType.getInterfaces(), is(Collections.singletonList(interfaceType)));
}
@Test
public void testWithInterfaceOfInstrumentedType() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getInterfaces().size(), is(0));
instrumentedType = instrumentedType.withInterfaces(new TypeList.Generic.Explicit(TargetType.DESCRIPTION));
assertThat(instrumentedType.getInterfaces().size(), is(1));
assertThat(instrumentedType.getInterfaces(), is(Collections.singletonList(instrumentedType.asGenericType())));
}
@Test
public void testWithAnnotation() throws Exception {
AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredAnnotations().size(), is(0));
instrumentedType = instrumentedType.withAnnotations(Collections.singletonList(annotationDescription));
assertThat(instrumentedType.getDeclaredAnnotations(), is(Collections.singletonList(annotationDescription)));
}
@Test
public void testWithName() throws Exception {
InstrumentedType.WithFlexibleName instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getName(), is(FOO + "." + BAZ));
instrumentedType = instrumentedType.withName(BAR);
assertThat(instrumentedType.getName(), is(BAR));
}
@Test
public void testModifiers() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getModifiers(), is(ModifierContributor.EMPTY_MASK));
instrumentedType = instrumentedType.withModifiers(OTHER_MODIFIERS);
assertThat(instrumentedType.getModifiers(), is(OTHER_MODIFIERS));
}
@Test
public void testWithLoadedTypeInitializerInitial() throws Exception {
LoadedTypeInitializer loadedTypeInitializer = makePlainInstrumentedType().getLoadedTypeInitializer();
assertThat(loadedTypeInitializer.isAlive(), is(false));
}
@Test
public void testWithLoadedTypeInitializerSingle() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
LoadedTypeInitializer loadedTypeInitializer = mock(LoadedTypeInitializer.class);
instrumentedType = instrumentedType.withInitializer(loadedTypeInitializer);
assertThat(instrumentedType.getLoadedTypeInitializer(),
hasPrototype((LoadedTypeInitializer) new LoadedTypeInitializer.Compound(LoadedTypeInitializer.NoOp.INSTANCE, loadedTypeInitializer)));
}
@Test
public void testWithLoadedTypeInitializerDouble() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
LoadedTypeInitializer first = mock(LoadedTypeInitializer.class), second = mock(LoadedTypeInitializer.class);
instrumentedType = instrumentedType.withInitializer(first).withInitializer(second);
assertThat(instrumentedType.getLoadedTypeInitializer(),
hasPrototype((LoadedTypeInitializer) new LoadedTypeInitializer.Compound(new LoadedTypeInitializer
.Compound(LoadedTypeInitializer.NoOp.INSTANCE, first), second)));
}
@Test
public void testWithTypeInitializerInitial() throws Exception {
TypeInitializer typeInitializer = makePlainInstrumentedType().getTypeInitializer();
assertThat(typeInitializer.isDefined(), is(false));
}
@Test
public void testWithTypeInitializerSingle() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
ByteCodeAppender byteCodeAppender = mock(ByteCodeAppender.class);
instrumentedType = instrumentedType.withInitializer(byteCodeAppender);
TypeInitializer typeInitializer = instrumentedType.getTypeInitializer();
assertThat(typeInitializer.isDefined(), is(true));
MethodDescription methodDescription = mock(MethodDescription.class);
typeInitializer.apply(methodVisitor, implementationContext, methodDescription);
verify(byteCodeAppender).apply(methodVisitor, implementationContext, methodDescription);
}
@Test
public void testWithTypeInitializerDouble() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
ByteCodeAppender first = mock(ByteCodeAppender.class), second = mock(ByteCodeAppender.class);
MethodDescription methodDescription = mock(MethodDescription.class);
when(first.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
when(second.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
instrumentedType = instrumentedType.withInitializer(first).withInitializer(second);
TypeInitializer typeInitializer = instrumentedType.getTypeInitializer();
assertThat(typeInitializer.isDefined(), is(true));
typeInitializer.apply(methodVisitor, implementationContext, methodDescription);
verify(first).apply(methodVisitor, implementationContext, methodDescription);
verify(second).apply(methodVisitor, implementationContext, methodDescription);
}
@Test
public void testGetStackSize() throws Exception {
assertThat(makePlainInstrumentedType().getStackSize(), is(StackSize.SINGLE));
}
@Test
public void testHashCode() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.hashCode(), is(instrumentedType.getName().hashCode()));
}
@Test
public void testEquals() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
TypeDescription other = mock(TypeDescription.class);
when(other.getName()).thenReturn(instrumentedType.getName());
when(other.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
when(other.asErasure()).thenReturn(other);
assertThat(instrumentedType, is(other));
verify(other, atLeast(1)).getName();
}
@Test
public void testIsAssignableFrom() {
assertThat(makePlainInstrumentedType().isAssignableFrom(Object.class), is(false));
assertThat(makePlainInstrumentedType().isAssignableFrom(Serializable.class), is(false));
assertThat(makePlainInstrumentedType().isAssignableFrom(Integer.class), is(false));
TypeDescription objectTypeDescription = TypeDescription.OBJECT;
assertThat(makePlainInstrumentedType().isAssignableFrom(objectTypeDescription), is(false));
TypeDescription serializableTypeDescription = TypeDescription.ForLoadedType.of(Serializable.class);
assertThat(makePlainInstrumentedType().isAssignableFrom(serializableTypeDescription), is(false));
TypeDescription integerTypeDescription = TypeDescription.ForLoadedType.of(Integer.class);
assertThat(makePlainInstrumentedType().isAssignableFrom(integerTypeDescription), is(false));
}
@Test
public void testIsAssignableTo() {
assertThat(makePlainInstrumentedType().isAssignableTo(Object.class), is(true));
assertThat(makePlainInstrumentedType().isAssignableTo(makePlainInstrumentedType()), is(true));
assertThat(makePlainInstrumentedType().isAssignableTo(Integer.class), is(false));
assertThat(makePlainInstrumentedType().isAssignableTo(TypeDescription.OBJECT), is(true));
}
@Test
public void testRepresents() {
assertThat(makePlainInstrumentedType().represents(Object.class), is(false));
assertThat(makePlainInstrumentedType().represents(Serializable.class), is(false));
assertThat(makePlainInstrumentedType().represents(Integer.class), is(false));
}
@Test
public void testSuperClass() {
assertThat(makePlainInstrumentedType().getSuperClass(), is(TypeDescription.Generic.OBJECT));
assertThat(makePlainInstrumentedType().getSuperClass(), not((TypeDescription.Generic) TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Integer.class)));
assertThat(makePlainInstrumentedType().getSuperClass(), not((TypeDescription.Generic) TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Serializable.class)));
}
@Test
public void testInterfaces() {
assertThat(makePlainInstrumentedType().getInterfaces().size(), is(0));
}
@Test
public void testPackage() {
assertThat(makePlainInstrumentedType().getPackage().getName(), is(FOO));
}
@Test
public void testSimpleName() {
assertThat(makePlainInstrumentedType().getSimpleName(), is(BAZ));
}
@Test
public void testEnclosingMethod() throws Exception {
assertThat(makePlainInstrumentedType().getEnclosingMethod(), nullValue());
}
@Test
public void testEnclosingType() throws Exception {
assertThat(makePlainInstrumentedType().getEnclosingType(), nullValue());
}
@Test
public void testDeclaringType() throws Exception {
assertThat(makePlainInstrumentedType().getDeclaringType(), nullValue());
}
@Test
public void testIsAnonymous() throws Exception {
assertThat(makePlainInstrumentedType().isAnonymousClass(), is(false));
}
@Test
public void testCanonicalName() throws Exception {
TypeDescription typeDescription = makePlainInstrumentedType();
assertThat(typeDescription.getCanonicalName(), is(typeDescription.getName()));
}
@Test
public void testIsMemberClass() throws Exception {
assertThat(makePlainInstrumentedType().isMemberClass(), is(false));
}
@Test
public void testDeclaredTypes() throws Exception {
assertThat(makePlainInstrumentedType().getDeclaredTypes().size(), is(0));
}
@Test
public void testFieldTokenIsVisited() throws Exception {
FieldDescription.Token token = mock(FieldDescription.Token.class);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.withField(token), is(instrumentedType));
verify(token).accept(matchesPrototype(TypeDescription.Generic.Visitor.Substitutor.ForDetachment.of(instrumentedType)));
verifyNoMoreInteractions(token);
}
@Test
public void testMethodTokenIsVisited() throws Exception {
MethodDescription.Token token = mock(MethodDescription.Token.class);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.withMethod(token), is(instrumentedType));
verify(token).accept(matchesPrototype(TypeDescription.Generic.Visitor.Substitutor.ForDetachment.of(instrumentedType)));
verifyNoMoreInteractions(token);
}
@Test
public void testTypeVariableIsVisited() throws Exception {
TypeVariableToken token = mock(TypeVariableToken.class);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.withTypeVariable(token), is(instrumentedType));
verify(token).accept(matchesPrototype(TypeDescription.Generic.Visitor.Substitutor.ForDetachment.of(instrumentedType)));
verifyNoMoreInteractions(token);
}
@Test
@SuppressWarnings("unchecked")
public void testInterfaceTypesVisited() throws Exception {
TypeDescription.Generic typeDescription = mock(TypeDescription.Generic.class);
when(typeDescription.asGenericType()).thenReturn(typeDescription);
when(typeDescription.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(typeDescription);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.withInterfaces(new TypeList.Generic.Explicit(typeDescription)), is(instrumentedType));
verify(typeDescription).accept(matchesPrototype(TypeDescription.Generic.Visitor.Substitutor.ForDetachment.of(instrumentedType)));
verify(typeDescription, times(2)).asGenericType();
verifyNoMoreInteractions(typeDescription);
}
@Test(expected = IllegalStateException.class)
public void testTypeIllegalName() throws Exception {
makePlainInstrumentedType().withName(ILLEGAL_NAME).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeIllegalEndName() throws Exception {
makePlainInstrumentedType().withName(FOO + ILLEGAL_NAME).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeEmptyEndName() throws Exception {
makePlainInstrumentedType().withName(NamedElement.EMPTY_NAME).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeChainedEmptyEndName() throws Exception {
makePlainInstrumentedType().withName("." + FOO).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeIllegalKeywordName() throws Exception {
makePlainInstrumentedType().withName(void.class.getName()).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeIllegalSubType() throws Exception {
InstrumentedType.Factory.Default.MODIFIABLE.subclass(FOO, ModifierContributor.EMPTY_MASK, TypeDefinition.Sort.describe(Serializable.class)).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeInvisibleSubType() throws Exception {
InstrumentedType.Factory.Default.MODIFIABLE.subclass(FOO, ModifierContributor.EMPTY_MASK, TypeDefinition.Sort.describe(PackagePrivateType.TYPE)).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeIllegalModifiers() throws Exception {
InstrumentedType.Factory.Default.MODIFIABLE.subclass(FOO, ILLEGAL_MODIFIERS, TypeDefinition.Sort.describe(Object.class)).validated();
}
@Test(expected = IllegalStateException.class)
public void testPackageTypeIllegalModifiers() throws Exception {
InstrumentedType.Factory.Default.MODIFIABLE.subclass(FOO + "." + PackageDescription.PACKAGE_CLASS_NAME, ModifierContributor.EMPTY_MASK, TypeDefinition.Sort.describe(Object.class))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeIllegalInterfaceType() throws Exception {
makePlainInstrumentedType().withInterfaces(new TypeList.Generic.Explicit(TypeDescription.Generic.OBJECT)).validated();
}
@Test(expected = IllegalStateException.class)
public void testInvisibleInterfaceType() throws Exception {
makePlainInstrumentedType().withInterfaces(new TypeList.Generic.Explicit(TypeDefinition.Sort.describe(PackagePrivateType.INTERFACE_TYPE))).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeDuplicateInterface() throws Exception {
makePlainInstrumentedType().withInterfaces(new TypeList.Generic.Explicit(
TypeDescription.ForLoadedType.of(Serializable.class),
TypeDescription.ForLoadedType.of(Serializable.class)
)).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeThrowableWithGenerics() throws Exception {
InstrumentedType.Factory.Default.MODIFIABLE.represent(TypeDescription.ForLoadedType.of(Exception.class))
.withTypeVariable(new TypeVariableToken(FOO, Collections.singletonList(TypeDescription.Generic.OBJECT)))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeDuplicateTypeVariableName() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(FOO, Collections.singletonList(TypeDescription.Generic.OBJECT)))
.withTypeVariable(new TypeVariableToken(FOO, Collections.singletonList(TypeDescription.Generic.OBJECT)))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeTypeVariableIllegalName() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(ILLEGAL_NAME, Collections.singletonList(TypeDescription.Generic.OBJECT)))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeTypeVariableMissingBound() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(FOO, Collections.<TypeDescription.Generic>emptyList()))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeTypeVariableDuplicateBound() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(FOO, Arrays.asList(TypeDescription.Sort.describe(Serializable.class), TypeDefinition.Sort.describe(Serializable.class))))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeTypeVariableIllegalBound() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(FOO, Collections.singletonList(TypeDescription.Generic.VOID)))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeTypeVariableDoubleClassBound() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(FOO, Arrays.asList(TypeDescription.Generic.OBJECT, TypeDefinition.Sort.describe(String.class))))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeDuplicateAnnotation() throws Exception {
makePlainInstrumentedType()
.withAnnotations(Arrays.asList(
AnnotationDescription.Builder.ofType(SampleAnnotation.class).build(),
AnnotationDescription.Builder.ofType(SampleAnnotation.class).build()
)).validated();
}
@Test(expected = IllegalStateException.class)
public void testTypeIncompatibleAnnotation() throws Exception {
makePlainInstrumentedType()
.withAnnotations(Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testPackageIncompatibleAnnotation() throws Exception {
makePlainInstrumentedType()
.withName(FOO + "." + PackageDescription.PACKAGE_CLASS_NAME)
.withAnnotations(Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testAnnotationTypeIncompatibleAnnotation() throws Exception {
makePlainInstrumentedType()
.withModifiers(Opcodes.ACC_ANNOTATION | Opcodes.ACC_ABSTRACT)
.withAnnotations(Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testAnnotationTypeIncompatibleSuperClassTypeAnnotation() throws Exception {
InstrumentedType.Factory.Default.MODIFIABLE.subclass(FOO, ModifierReviewable.EMPTY_MASK, TypeDescription.Generic.Builder.rawType(Object.class)
.build(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testAnnotationTypeIncompatibleInterfaceTypeAnnotation() throws Exception {
makePlainInstrumentedType()
.withInterfaces(new TypeList.Generic.Explicit(TypeDescription.Generic.Builder.rawType(Runnable.class)
.build(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build())))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testAnnotationTypeIncompatibleTypeVariableTypeAnnotation() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(FOO,
Collections.singletonList(TypeDescription.Generic.OBJECT),
Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build())))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testAnnotationTypeIncompatibleTypeVariableBoundTypeAnnotation() throws Exception {
makePlainInstrumentedType()
.withTypeVariable(new TypeVariableToken(FOO,
Collections.singletonList(TypeDescription.Generic.Builder.rawType(Object.class)
.build(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()))))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testFieldDuplicateName() throws Exception {
makePlainInstrumentedType()
.withField(new FieldDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT))
.withField(new FieldDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testFieldIllegalName() throws Exception {
makePlainInstrumentedType().withField(new FieldDescription.Token(ILLEGAL_NAME, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT)).validated();
}
@Test(expected = IllegalStateException.class)
public void testFieldIllegalModifiers() throws Exception {
makePlainInstrumentedType().withField(new FieldDescription.Token(FOO, ILLEGAL_MODIFIERS, TypeDescription.Generic.OBJECT)).validated();
}
@Test(expected = IllegalStateException.class)
public void testFieldIllegalType() throws Exception {
makePlainInstrumentedType().withField(new FieldDescription.Token(ILLEGAL_NAME, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.VOID)).validated();
}
@Test(expected = IllegalStateException.class)
public void testFieldInvisibleType() throws Exception {
makePlainInstrumentedType()
.withField(new FieldDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDefinition.Sort.describe(PackagePrivateType.TYPE)))
.validated();
}
@Test(expected = IllegalStateException.class)
public void tesFieldDuplicateAnnotation() throws Exception {
makePlainInstrumentedType()
.withField(new FieldDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT, Arrays.asList(
AnnotationDescription.Builder.ofType(SampleAnnotation.class).build(),
AnnotationDescription.Builder.ofType(SampleAnnotation.class).build()
))).validated();
}
@Test(expected = IllegalStateException.class)
public void tesFieldIncompatibleAnnotation() throws Exception {
makePlainInstrumentedType()
.withField(new FieldDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT, Collections.singletonList(
AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()
))).validated();
}
@Test(expected = IllegalStateException.class)
public void testFieldIncompatibleTypeAnnotation() throws Exception {
makePlainInstrumentedType()
.withField(new FieldDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
TypeDescription.Generic.Builder.rawType(Runnable.class)
.build(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build())))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodDuplicateErasure() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT))
.withMethod(new MethodDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodTypeInitializer() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(MethodDescription.TYPE_INITIALIZER_INTERNAL_NAME, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.VOID))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testConstructorNonVoidReturnType() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(MethodDescription.CONSTRUCTOR_INTERNAL_NAME, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodInvisibleReturnType() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO, ModifierContributor.EMPTY_MASK, TypeDefinition.Sort.describe(PackagePrivateType.TYPE)))
.validated();
}
@Test
public void testMethodInvisibleReturnTypeSynthetic() throws Exception {
assertThat(makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO, Opcodes.ACC_SYNTHETIC, TypeDefinition.Sort.describe(PackagePrivateType.TYPE)))
.validated(), instanceOf(TypeDescription.class));
}
@Test(expected = IllegalStateException.class)
public void testMethodIllegalName() throws Exception {
makePlainInstrumentedType().withMethod(new MethodDescription.Token(ILLEGAL_NAME, ModifierContributor.EMPTY_MASK, TypeDescription.Generic.OBJECT)).validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodIllegalModifiers() throws Exception {
makePlainInstrumentedType().withMethod(new MethodDescription.Token(FOO, ILLEGAL_MODIFIERS, TypeDescription.Generic.OBJECT)).validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodDuplicateAnnotation() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.<TypeVariableToken>emptyList(),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Arrays.asList(
AnnotationDescription.Builder.ofType(SampleAnnotation.class).build(),
AnnotationDescription.Builder.ofType(SampleAnnotation.class).build()
), AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodIncompatibleAnnotation() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.<TypeVariableToken>emptyList(),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodIncompatibleReturnTypeTypeAnnotation() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
TypeDescription.Generic.Builder.rawType(Runnable.class)
.build(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build())))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodIllegalTypeVariableTypeAnnotation() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.singletonList(new TypeVariableToken(FOO,
Collections.singletonList(TypeDescription.Generic.OBJECT),
Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()))),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodIllegalTypeVariableBoundTypeAnnotation() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.singletonList(new TypeVariableToken(FOO,
Collections.singletonList(TypeDescription.Generic.Builder.rawType(Object.class).build(
AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build())))),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodIllegalTypeVariableName() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.singletonList(new TypeVariableToken(ILLEGAL_NAME, Collections.singletonList(TypeDescription.Generic.OBJECT))),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodDuplicateTypeVariableName() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Arrays.asList(
new TypeVariableToken(FOO, Collections.singletonList(TypeDescription.Generic.OBJECT)),
new TypeVariableToken(FOO, Collections.singletonList(TypeDescription.Generic.OBJECT))
),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodTypeVariableMissingBound() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.singletonList(new TypeVariableToken(FOO, Collections.<TypeDescription.Generic>emptyList())),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodTypeVariableIllegalBound() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.singletonList(new TypeVariableToken(FOO, Collections.singletonList(TypeDescription.Generic.VOID))),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodTypeVariableDuplicateBound() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.singletonList(new TypeVariableToken(FOO, Arrays.asList(TypeDefinition.Sort.describe(Serializable.class),
TypeDefinition.Sort.describe(Serializable.class)))),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodTypeVariableDoubleClassBound() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.singletonList(new TypeVariableToken(FOO, Arrays.asList(TypeDescription.Generic.OBJECT, TypeDefinition.Sort.describe(String.class)))),
TypeDescription.Generic.OBJECT,
Collections.<ParameterDescription.Token>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodParameterIllegalName() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.<TypeVariableToken>emptyList(),
TypeDescription.Generic.OBJECT,
Collections.singletonList(new ParameterDescription.Token(TypeDescription.Generic.OBJECT, ILLEGAL_NAME, 0)),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<AnnotationDescription>emptyList(),
AnnotationValue.UNDEFINED,
TypeDescription.Generic.UNDEFINED))
.validated();
}
@Test(expected = IllegalStateException.class)
public void testMethodParameterIllegalType() throws Exception {
makePlainInstrumentedType()
.withMethod(new MethodDescription.Token(FOO,
ModifierContributor.EMPTY_MASK,
Collections.<TypeVariableToken>emptyList(),
TypeDescription.Generic.OBJECT,