-
Notifications
You must be signed in to change notification settings - Fork 316
/
AbstractReusableActiveAnnotationTests.xtend
3457 lines (2881 loc) · 110 KB
/
AbstractReusableActiveAnnotationTests.xtend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2017, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtend.ide.tests.macros
import com.google.inject.Inject
import org.eclipse.xtend.core.compiler.XtendGenerator
import org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl
import org.eclipse.xtend.core.macro.declaration.ExpressionImpl
import org.eclipse.xtend.core.macro.declaration.MutableJvmClassDeclarationImpl
import org.eclipse.xtend.core.macro.declaration.MutableJvmFieldDeclarationImpl
import org.eclipse.xtend.core.macro.declaration.MutableJvmMethodDeclarationImpl
import org.eclipse.xtend.lib.macro.declaration.AnnotationReference
import org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration
import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration
import org.eclipse.xtend.lib.macro.declaration.Element
import org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration
import org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration
import org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration
import org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclarator
import org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration
import org.eclipse.xtend.lib.macro.declaration.TypeReference
import org.eclipse.xtext.common.types.JvmDeclaredType
import org.eclipse.xtext.ui.testing.util.LineDelimiters
import org.eclipse.xtext.testing.validation.ValidationTestHelper
import org.eclipse.xtext.xbase.compiler.IGeneratorConfigProvider
import org.eclipse.xtext.xbase.jvmmodel.ILogicalContainerProvider
import org.junit.Ignore
import org.junit.Test
import static org.junit.Assert.*
abstract class AbstractReusableActiveAnnotationTests {
@Inject XtendGenerator generator
@Inject IGeneratorConfigProvider generatorConfigProvider
@Inject ValidationTestHelper validator
@Inject ILogicalContainerProvider logicalContainerProvider
@Test def void testBug453273() {
assertProcessing(
'myannotation/AddNestedTypes.xtend' -> '''
package myannotation
import java.lang.annotation.ElementType
import java.lang.annotation.Target
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.RegisterGlobalsContext
import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration
@Target(ElementType.TYPE)
@Active(AddNestedTypesProcessor)
annotation AddNestedTypes {
}
class AddNestedTypesProcessor extends AbstractClassProcessor {
override doRegisterGlobals(ClassDeclaration it, RegisterGlobalsContext registerGlobalsContext) {
registerGlobalsContext.registerClass(qualifiedName + ".NestedType")
registerGlobalsContext.registerClass(qualifiedName + ".NestedType.NestedType2")
registerGlobalsContext.registerClass(packageName + ".OtherTopLevelClass")
registerGlobalsContext.registerClass(packageName + ".OtherTopLevelClass.NestedType")
}
def String getPackageName(ClassDeclaration it) {
qualifiedName.substring(0,qualifiedName.lastIndexOf('.'))
}
}
''',
'my/client/UserCode.xtend' -> '''
package my.client
@myannotation.AddNestedTypes
class TopLevelClass {
}
'''
) [
validator.assertNoErrors(xtendFile)
assertNotNull(typeLookup.findClass("my.client.TopLevelClass"))
assertNotNull(typeLookup.findClass("my.client.TopLevelClass.NestedType"))
assertNotNull(typeLookup.findClass("my.client.TopLevelClass.NestedType.NestedType2"))
assertNotNull(typeLookup.findClass("my.client.OtherTopLevelClass"))
assertNotNull(typeLookup.findClass("my.client.OtherTopLevelClass.NestedType"))
]
}
@Test def void testBug441081() {
assertProcessing(
'bug441081/Bug441081.xtend' -> '''
package bug441081
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
interface GenericInterface {
def <T> T m()
}
@Active(Bug441081Processor)
annotation Bug441081 {
}
class Bug441081Processor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
annotatedClass.implementedInterfaces = annotatedClass.implementedInterfaces + #[findTypeGlobally(GenericInterface).newTypeReference]
annotatedClass.addMethod("m") [
returnType = addTypeParameter("T", object).newTypeReference
body = '«»''return null;'«»''
primarySourceElement = annotatedClass
]
}
}
''',
'Bug441081Client.xtend' -> '''
import bug441081.Bug441081
@Bug441081
class Bug441081Client {
}
'''
) [
val c = typeLookup.findClass("Bug441081Client")
val typeParam = c.findDeclaredMethod('m').returnType.type as TypeParameterDeclaration
assertFalse(typeParam.upperBounds.empty)
validator.assertNoIssues(xtendFile)
]
}
@Test def void testInferredMethodReturnType() {
assertProcessing(
'myannotation/MyAnnotation.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractMethodProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration
@Active(MyAnnotationProcessor)
annotation MyAnnotation {
}
class MyAnnotationProcessor extends AbstractMethodProcessor {
override doTransform(MutableMethodDeclaration annotatedMethod, extension TransformationContext context) {
annotatedMethod.declaringType.addField(annotatedMethod.simpleName + '_field') [
type = annotatedMethod.returnType
]
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.MyAnnotation
class Client {
@MyAnnotation
def bar() {
1
}
@MyAnnotation
def create new Integer(1) foo() {
}
}
'''
) [
val foo = typeLookup.findClass("myusercode.Client")
val barType = foo.findDeclaredField('bar_field').type
assertSameType(barType, typeReferenceProvider.primitiveInt)
assertSameType(barType, foo.findDeclaredMethod('bar').returnType)
val fooType = foo.findDeclaredField('foo_field').type
assertSameType(fooType, typeReferenceProvider.newTypeReference(Integer))
assertSameType(fooType, foo.findDeclaredMethod('foo').returnType)
]
}
@Test def void testTracing() {
assertProcessing(
'myannotation/Getters.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
@Active(GettersProcessor)
annotation Getters {
}
class GettersProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
cls.declaredFields.filter[isThePrimaryGeneratedJavaElement].forEach [field|
cls.addMethod("get" + field.simpleName.toFirstUpper) [
primarySourceElement = field
returnType = field.type
body = ["return this." + field.simpleName + ";"]
field.markAsRead
]
]
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.Getters
@Getters
class Client {
val int bar = 1
def create new Integer(1) foo() {
}
}
'''
) [
val cls = typeLookup.findClass("myusercode.Client")
val barField = cls.findDeclaredField("bar")
val getters = cls.declaredMethods.filter[simpleName.startsWith("get")]
assertEquals(1, getters.size)
val getBar = getters.head
assertEquals("getBar", getBar.simpleName)
val barJvmField = (barField as MutableJvmFieldDeclarationImpl).delegate
val getBarJvmMethod = (getBar as MutableJvmMethodDeclarationImpl).delegate
val elementsAssociatedWithBarField = jvmModelAssociations.getJvmElements(jvmModelAssociations.getPrimarySourceElement(barJvmField))
assertEquals(2, elementsAssociatedWithBarField.size)
assertEquals(barJvmField, elementsAssociatedWithBarField.get(0))
assertEquals(getBarJvmMethod, elementsAssociatedWithBarField.get(1))
]
}
@Test def void testTracing2() {
assertProcessing(
'myannotation/MyAnno.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.Active
@Active(MyAnnoProcessor)
annotation MyAnno {}
class MyAnnoProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
cls.addMethod("foo") [
primarySourceElement = cls.typeParameters.head
body = ["return;"]
]
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.MyAnno
@MyAnno
class Client<A> {}
'''
) [
val cls = typeLookup.findClass("myusercode.Client")
val fooMethod = cls.findDeclaredMethod("foo")
val typeParameter = tracability.getPrimarySourceElement(cls.typeParameters.head)
assertEquals(typeParameter, tracability.getPrimarySourceElement(fooMethod))
]
}
@Test def void testTracing3() {
assertProcessing(
'myannotation/MyAnno.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.Active
@Active(MyAnnoProcessor)
annotation MyAnno {}
class MyAnnoProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
cls.addMethod("foo") [
primarySourceElement = cls.extendedClass
body = ["return;"]
]
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.MyAnno
@MyAnno
class Client extends Object{}
'''
) [
val cls = typeLookup.findClass("myusercode.Client")
val fooMethod = cls.findDeclaredMethod("foo")
val extendsClause = tracability.getPrimarySourceElement(cls.extendedClass)
assertTrue(extendsClause instanceof TypeReference)
assertEquals(extendsClause, tracability.getPrimarySourceElement(fooMethod as Element))
]
}
@Test def void testTracing4() {
assertProcessing(
'myannotation/MyAnno.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.Active
@Active(MyAnnoProcessor)
annotation MyAnno {}
class MyAnnoProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
cls.addMethod("foo") [
primarySourceElement = cls.annotations.head
body = ["return;"]
]
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.MyAnno
@MyAnno
class Client extends Object{}
'''
) [
val cls = typeLookup.findClass("myusercode.Client")
val fooMethod = cls.findDeclaredMethod("foo")
val anno = tracability.getPrimarySourceElement(cls.annotations.head)
assertTrue(anno instanceof AnnotationReference)
assertEquals(anno, tracability.getPrimarySourceElement(fooMethod as Element))
]
}
@Test def void testValidationPhase() {
assertProcessing(
'myannotation/ValidatingAnnotation.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration
import org.eclipse.xtend.lib.macro.ValidationContext
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
import org.eclipse.xtend.lib.macro.TransformationContext
@Active(ValidatingProcessor)
annotation ValidatingAnnotation {
}
class ValidatingProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
cls.addWarning("Foo")
cls.addWarning("Bar")
}
override doValidate(ClassDeclaration cls, extension ValidationContext context) {
cls.addWarning("Baz")
cls.addWarning("There were " + cls.problems.size + " problems")
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.*
@ValidatingAnnotation
class Foo {
}
'''
)[
val cls = typeLookup.findClass("myusercode.Foo")
val problems = problemSupport.getProblems(cls)
assertEquals(4, problems.size)
assertEquals("Foo", problems.get(0).message)
assertEquals("Bar", problems.get(1).message)
assertEquals("Baz", problems.get(2).message)
assertEquals("There were 3 problems", problems.get(3).message)
]
}
@Test def void testValidateLater() {
assertProcessing(
'myannotation/ValidateLater.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractFieldProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration
@Active(ValidateLaterProcessor)
annotation ValidateLater {
}
class ValidateLaterProcessor extends AbstractFieldProcessor {
override doTransform(MutableFieldDeclaration it, extension TransformationContext context) {
validateLater[ |
if (type == primitiveBoolean)
addWarning("The type was inferred and boolean")
]
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.*
class Foo {
@ValidateLater val foo = true
}
'''
)[
val cls = typeLookup.findClass("myusercode.Foo")
val problems = problemSupport.getProblems(cls.declaredFields.head)
assertEquals(1, problems.size)
assertEquals("The type was inferred and boolean", problems.get(0).message)
]
}
@Test
def void testNoMutationInValidationPhase() {
assertProcessing(
'myannotation/EvilAnnotation.xtend' -> '''
package myannotation
import java.util.List
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationParticipant
import org.eclipse.xtend.lib.macro.ValidationParticipant
import org.eclipse.xtend.lib.macro.ValidationContext
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
import org.eclipse.xtend.lib.macro.TransformationContext
@Active(EvilProcessor)
annotation EvilAnnotation {
}
class EvilProcessor implements TransformationParticipant<MutableClassDeclaration>, ValidationParticipant<MutableClassDeclaration> {
override doTransform(List<? extends MutableClassDeclaration> classes, extension TransformationContext context) {
classes.forEach[
final = false
]
}
override doValidate(List<? extends MutableClassDeclaration> classes, extension ValidationContext context) {
classes.forEach[
final = true
]
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.*
@EvilAnnotation
class Foo {
}
'''
)[
xtendFile.eResource.errors.exists[message.contains('cannot be modified')]
]
}
@Test def void testSetEmptyListAsAnnotationValue() {
assertProcessing(
'myannotation/MyAnnotation.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
@Active(MyAnnotationProcessor)
annotation MyAnnotation {
}
annotation Values {
int[] intValue
long[] longValue
short[] shortValue
double[] doubleValue
float[] floatValue
byte[] byteValue
boolean[] booleanValue
char[] charValue
String[] stringValue
Class<?>[] classValue
Enum1[] enumValue
Annotation2[] annotationValue
}
enum Enum1 {}
annotation Annotation2 {}
class MyAnnotationProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration it, extension TransformationContext context) {
removeAnnotation(annotations.head)
addAnnotation(Values.newAnnotationReference [
setIntValue('intValue', emptyList)
setLongValue('longValue', emptyList)
setShortValue('shortValue', emptyList)
setDoubleValue('doubleValue', emptyList)
setFloatValue('floatValue', emptyList)
setByteValue('byteValue', emptyList)
setBooleanValue('booleanValue', emptyList)
setCharValue('charValue', emptyList)
setStringValue('stringValue', emptyList)
setClassValue('classValue', emptyList)
setEnumValue('enumValue', emptyList)
setAnnotationValue('annotationValue', emptyList)
])
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.*
@MyAnnotation
class Foo {
}
'''
) [
val foo = typeLookup.findClass("myusercode.Foo")
val annotationType = typeLookup.findTypeGlobally("myannotation.Values")
val values = foo.findAnnotation(annotationType)
assertEquals(0, values.getIntArrayValue('intValue').size)
assertEquals(0, values.getLongArrayValue('longValue').size)
assertEquals(0, values.getShortArrayValue('shortValue').size)
assertEquals(0, values.getDoubleArrayValue('doubleValue').size)
assertEquals(0, values.getFloatArrayValue('floatValue').size)
assertEquals(0, values.getByteArrayValue('byteValue').size)
assertEquals(0, values.getBooleanArrayValue('booleanValue').size)
assertEquals(0, values.getCharArrayValue('charValue').size)
assertEquals(0, values.getStringArrayValue('stringValue').size)
assertEquals(0, values.getClassArrayValue('classValue').size)
assertEquals(0, values.getEnumArrayValue('enumValue').size)
assertEquals(0, values.getAnnotationArrayValue('annotationValue').size)
]
}
@Test def void testInferredTypeReferences() {
assertProcessing(
'myannotation/MyAnnotation.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
@Active(MyAnnotationProcessor)
annotation MyAnnotation {
Class<?> value
}
class MyAnnotationProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration it, extension TransformationContext context) {
for (field : declaredFields) {
addMethod(field.simpleName) [
returnType = field.type
body = ['return 1;']
]
}
val myAnnotation = findAnnotation(MyAnnotation.findTypeGlobally)
val type = myAnnotation.getClassValue("value").type as ClassDeclaration
for (field : type.declaredFields) {
addMethod(field.simpleName) [
returnType = field.type
body = ['return 1;']
]
}
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.*
@MyAnnotation(Bar)
class Foo {
val y = 1
}
class Bar {
val x = 1
}
'''
) [
val foo = typeLookup.findClass("myusercode.Foo")
val bar = typeLookup.findClass("myusercode.Bar")
assertSameType(foo.findDeclaredField('y').type, foo.findDeclaredMethod('y').returnType)
assertSameType(bar.findDeclaredField('x').type, foo.findDeclaredMethod('x').returnType)
]
}
@Test def void testAnnotationValueSetting_1() {
assertProcessing(
'myannotation/ConfigurableAnnotation.xtend' -> '''
package myannotation
import java.util.List
import org.eclipse.xtend.lib.macro.*
import org.eclipse.xtend.lib.macro.declaration.*
@Active(ConfigurableAnnotationProcessor)
annotation ConfigurableAnnotation {
BlackOrWhite color
BlackOrWhite[] colors
Class<?> type
Class<?>[] types
SomeAnnotation annotation
SomeAnnotation[] annotations
}
annotation SomeAnnotation {
boolean value
}
enum BlackOrWhite {
BLACK, WHITE
}
class ConfigurableAnnotationProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
val anno = annotatedClass.annotations.head
val someAnnotationType = findTypeGlobally(SomeAnnotation)
val enumType = findTypeGlobally('myannotation.BlackOrWhite') as EnumerationTypeDeclaration
val white = enumType.findDeclaredValue('WHITE')
val black = enumType.findDeclaredValue('BLACK')
val existingValue = anno.getValue('color')
if (existingValue != white)
throw new AssertionError("color")
val annoWithColor = annotatedClass.addAnnotation(newAnnotationReference(anno) [
setEnumValue('color', black)
])
annotatedClass.removeAnnotation(anno)
val existingColorsValue = annoWithColor.getValue('colors') as Object[]
if (existingColorsValue.get(0) != white && existingColorsValue.get(1) != black && existingColorsValue.length != 2)
throw new AssertionError("colors")
val annoWithColors = annotatedClass.addAnnotation(newAnnotationReference(annoWithColor) [
setEnumValue('colors', black, white)
])
annotatedClass.removeAnnotation(annoWithColor)
val existingType = annoWithColors.getValue('type')
if (existingType != string)
throw new AssertionError("type")
val annoWithType = annotatedClass.addAnnotation(newAnnotationReference(annoWithColors) [
setClassValue('type', annotatedClass.newTypeReference)
])
annotatedClass.removeAnnotation(annoWithColors)
val existingTypes = annoWithType.getValue('types') as Object[]
if (existingTypes.get(0) != primitiveInt && existingTypes.get(1) != annotatedClass.newTypeReference && existingTypes.length != 2)
throw new AssertionError("types")
val annoWithTypes = annotatedClass.addAnnotation(newAnnotationReference(annoWithType) [
setClassValue('types', primitiveBoolean)
])
annotatedClass.removeAnnotation(annoWithType)
val annotationReference = annoWithTypes.getAnnotationValue('annotation')
if (someAnnotationType != annotationReference.annotationTypeDeclaration)
throw new AssertionError("someAnnotationType != annotationReference.annotationTypeDeclaration")
val annoWithAnnotation = annotatedClass.addAnnotation(newAnnotationReference(annoWithTypes) [
setAnnotationValue('annotation',
newAnnotationReference(someAnnotationType) [
setBooleanValue('value', false)
]
)
])
annotatedClass.removeAnnotation(annoWithTypes)
val annotationReferences = annoWithAnnotation.getAnnotationArrayValue('annotations')
if (annotationReferences.size != 2)
throw new AssertionError("annotationReferences.size != 2")
annotationReferences.forEach [
if (someAnnotationType != annotationTypeDeclaration)
throw new AssertionError("someAnnotationType != annotationTypeDeclaration")
]
annotatedClass.addAnnotation(newAnnotationReference(annoWithAnnotation) [
setAnnotationValue('annotations',
newAnnotationReference(someAnnotationType) [
setBooleanValue('value', false)
],
newAnnotationReference(someAnnotationType) [
setBooleanValue('value', false)
]
)
])
annotatedClass.removeAnnotation(annoWithAnnotation)
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.*
@ConfigurableAnnotation(
color=BlackOrWhite.WHITE,
colors=#[BlackOrWhite.WHITE, BlackOrWhite.BLACK],
type = String,
types=#[Integer, MyClass],
annotation=@SomeAnnotation(true),
annotations=#[@SomeAnnotation(true), @SomeAnnotation(true)]
)
class MyClass {
}
'''
) [
val clazz = typeLookup.findClass("myusercode.MyClass")
val colorEnum = typeLookup.findTypeGlobally("myannotation.BlackOrWhite") as EnumerationTypeDeclaration
val annotationType = typeLookup.findTypeGlobally("myannotation.ConfigurableAnnotation")
val annotation = clazz.findAnnotation(annotationType)
assertEquals(colorEnum.findDeclaredValue('BLACK'), annotation.getValue('color'))
val colors = annotation.getValue('colors') as Object[]
assertEquals(2, colors.length)
assertEquals(colorEnum.findDeclaredValue('BLACK'), colors.get(0))
assertEquals(colorEnum.findDeclaredValue('WHITE'), colors.get(1))
assertEquals(typeReferenceProvider.newTypeReference(clazz), annotation.getValue('type'))
val types = annotation.getClassArrayValue('types')
assertEquals(1, types.length)
assertEquals(typeReferenceProvider.primitiveBoolean, types.get(0))
val someAnnotationType = typeLookup.findTypeGlobally('myannotation.SomeAnnotation')
val annotationValue = annotation.getAnnotationValue('annotation')
assertNotNull(annotationValue)
assertEquals(someAnnotationType, annotationValue.annotationTypeDeclaration)
assertFalse(annotationValue.getBooleanValue('value'))
val annotationsValue = annotation.getAnnotationArrayValue('annotations')
assertNotNull(annotationsValue)
assertEquals(2, annotationsValue.size)
annotationsValue.forEach[
assertEquals(someAnnotationType, annotationValue.annotationTypeDeclaration)
assertFalse(annotationValue.getBooleanValue('value'))
]
]
}
@Test def void testAnnotationValueSetting_2() {
assertProcessing(
'myannotation/ConfigurableAnnotation.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.*
import org.eclipse.xtend.lib.macro.declaration.*
@Active(ConfigurableAnnotationProcessor)
annotation ConfigurableAnnotation {
int someValue
}
class Constants {
public static val int MYCONSTANT = Integer.MAX_VALUE - 42
}
class ConfigurableAnnotationProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
val anno = annotatedClass.annotations.head
val existingValue = anno.getValue('someValue')
annotatedClass.docComment = ''+existingValue
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import myannotation.*
@ConfigurableAnnotation(someValue=MoreConstants.MY_CONSTANT * 1)
class MyClass {
}
class MoreConstants {
public static val int MY_CONSTANT = myannotation.Constants.MYCONSTANT - Integer.MAX_VALUE + 42 * 2
}
'''
) [
val clazz = typeLookup.findClass("myusercode.MyClass")
assertEquals("42",clazz.docComment)
]
}
@Test def void testAnnotationValueSetting_3() {
assertProcessing(
'myannotation/MoveValues.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
@Active(MoveAnnotationValuesProcessor)
annotation MoveValues {}
class MoveAnnotationValuesProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
val classAnnotation = annotatedClass.annotations.head
val field = annotatedClass.declaredFields.head
val fieldAnnotation = field.annotations.head
field.removeAnnotation(fieldAnnotation)
field.addAnnotation(fieldAnnotation.annotationTypeDeclaration.qualifiedName.newAnnotationReference [
set('booleanValue', classAnnotation.getValue('booleanValue'))
set('intValue', classAnnotation.getValue('intValue'))
set('longValue', classAnnotation.getValue('longValue'))
set('stringValue', classAnnotation.getValue('stringValue'))
set('booleanArrayValue', classAnnotation.getValue('booleanArrayValue'))
set('intArrayValue', classAnnotation.getValue('intArrayValue'))
set('longArrayValue', classAnnotation.getValue('longArrayValue'))
set('stringArrayValue', classAnnotation.getValue('stringArrayValue'))
set('typeValue', classAnnotation.getValue('typeValue'))
set('typeArrayValue', classAnnotation.getValue('typeArrayValue'))
set('annotation2Value', classAnnotation.getValue('annotation2Value'))
set('annotation2ArrayValue', classAnnotation.getValue('annotation2ArrayValue'))
set('enumValue', classAnnotation.getValue('enumValue'))
set('enumArrayValue', classAnnotation.getValue('enumArrayValue'))
])
}
}
''',
'myusercode/UserCode.xtend' -> '''
package myusercode
import test.Annotation
import test.Annotation2
import myannotation.MoveValues
@Annotation(
intValue = 2 / 2 + 2 * 3 - 4 % 1,
longValue = 42 + 4 + 6 * 42 - 4 / 45,
stringValue = 'foo' + 'baz',
booleanArrayValue = #[true, false],
intArrayValue = #[ -1, 34 + 45, 2 - 6 ],
longArrayValue = #[42, 5 * -3],
stringArrayValue = #['foo', 'bla' + 'buzz'],
typeValue = String,
typeArrayValue = #[String, Integer],
annotation2Value = @Annotation2('foo' + 'wuppa'),
annotation2ArrayValue = #[@Annotation2, @Annotation2('foo'+'wuppa')],
enumValue = test.Enum1.YELLOW,
enumArrayValue = #[test.Enum1.YELLOW, test.Enum1.RED]
)
@MoveValues
class UserCode {
@Annotation() String foo
}
'''
) [
val clazz = typeLookup.findClass("myusercode.UserCode")
val annoRef = clazz.declaredFields.head.annotations.head
assertEquals(2 / 2 + 2 * 3 - 4 % 1, annoRef.getValue("intValue"))
assertEquals((42 + 4 + 6 * 42 - 4 / 45) as long, annoRef.getValue("longValue"))
assertEquals('foobaz', annoRef.getValue("stringValue"))
val bools = annoRef.getValue("booleanArrayValue") as boolean[]
assertTrue(bools.get(0))
assertFalse(bools.get(1))
assertArrayEquals(#[ -1, 34 + 45, 2 - 6 ], annoRef.getValue("intArrayValue") as int[])
val type = annoRef.getValue('typeArrayValue') as TypeReference[]
assertEquals(typeReferenceProvider.newTypeReference(Integer), type.get(1))
val anno = annoRef.getValue('annotation2Value') as AnnotationReference
assertEquals('foowuppa', anno.getValue('value'))
val annoArray = annoRef.getValue('annotation2ArrayValue') as AnnotationReference[]
assertEquals("HUBBA BUBBA!", annoArray.get(0).getValue('value'))
val enum1 = annoRef.getValue('enumValue') as EnumerationValueDeclaration
assertEquals('YELLOW', enum1.simpleName)
val enumArray = annoRef.getValue('enumArrayValue') as EnumerationValueDeclaration[]
assertEquals("YELLOW", enumArray.head.simpleName)
]
}
@Test def void testAnnotationArrayValueGetting_01() {
assertProcessing(
'myannotation/MyAnnotation.xtend' -> '''
package myannotation
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.RegisterGlobalsContext
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
@Active(MyAnnotationProcessor)
annotation MyAnnotation {
boolean[] value
}
class MyAnnotationProcessor extends AbstractClassProcessor {
override doRegisterGlobals(ClassDeclaration it, extension RegisterGlobalsContext context) {
check
}
override doTransform(MutableClassDeclaration it, extension TransformationContext context) {
check
}
def check(ClassDeclaration it) {
switch qualifiedName {
case 'myusercode.UserCode',
case 'myusercode.UserCode2',
case 'myusercode.UserCode4': {
val value = annotations.head.getBooleanArrayValue("value")
if (value.size != 1) {
throw new AssertionError("value.size != 1")
}
if (!value.head) {
throw new AssertionError("!value.head")
}
try {
annotations.head.getBooleanValue("value")