-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaToKdm.atl
More file actions
2306 lines (2138 loc) · 74.4 KB
/
javaToKdm.atl
File metadata and controls
2306 lines (2138 loc) · 74.4 KB
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
--@atlcompiler atl2006
--Copyright (c) 2009 Mia-Software.
--All rights reserved. This program and the accompanying materials
--are made available under the terms of the Eclipse Public License v1.0
--which accompanies this distribution, and is available at
--http://www.eclipse.org/legal/epl-v10.html
---
--Contributors:
-- Gabriel BARBIER (Mia-Software) - initial API and implementation
-- Fabien GIQUEL (Mia-Software) - initial API and implementation
--
-- @nsURI kdm=http://www.eclipse.org/MoDisco/kdm/action
-- @nsURI java=http://www.eclipse.org/MoDisco/Java/0.2.incubation/java
--
--Transform Java Models to KDM models
module javaToKdm; -- Module Template
create OUT : kdm from IN : java;
rule InitializerToControlElement extends BodyDeclarationToAbstractCodeElement {
from src : java!Initializer
to tgt : kdm!ControlElement (
codeElement <- src.body
)
}
-- helpers --
-- specific case to attach all single elements to root model
endpoint rule manageDetachedElements() {
do {
for (alone in kdm!AbstractCodeElement.allInstances()) {
if (alone.refImmediateComposite().oclIsUndefined()) {
thisModule.externalModel.codeElement <- alone;
}
}
}
}
helper def: externalModel : kdm!CodeModel = OclUndefined;
-- computes name of a generic type
helper context java!TypeDeclaration def : getGenericName() : String =
self.name + '<' + self.typeParameters
->collect(T | if (self.typeParameters->indexOf(T) < self.typeParameters->size()) then T.name + ', ' else T.name endif)
->sum() + '>';
--returns referenced type, or one created on the fly in case of UnresolvedItem
--this is used when creating variables or parameters which are containers for this kind of type in kdm
helper context java!TypeAccess def : getType() : kdm!Datatype =
self.type->getExtendsType();
helper context java!NamedElement def : getExtendsType() : kdm!Datatype =
if self.oclIsKindOf(java!UnresolvedItem) then
if self.refImmediateComposite().oclIsTypeOf(java!ClassDeclaration) then
thisModule->CreateClassUnit(self) -- TODO why ?
else
thisModule->CreateInterfaceUnit(self)
endif
else -- java!Type or java!Package
self
endif;
-- to be able to filter expressions that are not useful, or need to be managed manually
helper def : filterExpression(expression : java!Expression) : java!Expression =
if (expression.oclIsUndefined()) then
Sequence{}
else
if (expression.oclIsKindOf(java!SingleVariableAccess)
or expression.oclIsKindOf(java!TypeAccess)
or expression.oclIsKindOf(java!UnresolvedItemAccess)) then
Sequence{}
else
Sequence{expression}
endif
endif;
-- rules --
-- ===================================================== ---
-- Rules for the structure of a program
-- This part gives all the transformations for the structure of a Java program
-- ===================================================== ---
-- Transforms a Model into a code model
rule ModelToModel{
from
src : java!Model
to
kdmModel : kdm!CodeModel(
name <- src.name
,codeElement <- src.ownedElements->select(e| e.proxy = false)
,codeElement <- kdmLanguageUnit
)
,kdmLanguageUnit : kdm!LanguageUnit(
name <- 'Common Java datatypes',
codeElement <- src.orphanTypes->select(e| e.oclIsKindOf(java!PrimitiveType)),
codeElement <- stringType
)
,externalModel : kdm!CodeModel (
name <- 'externals'
,codeElement <- src.ownedElements->select(e| e.proxy = true)
,codeElement <- src.orphanTypes->select(e| not e.oclIsKindOf(java!PrimitiveType))
)
,sourcesModel : kdm!InventoryModel (
name <- 'source references',
inventoryElement <- src.compilationUnits,
inventoryElement <- src.archives
)
,kdmSegment : kdm!Segment mapsTo src (
model <- kdmModel
,model <- externalModel
,model <- sourcesModel
)
,stringType : kdm!StringType (
name <- 'string'
)
do {
thisModule.externalModel <- externalModel;
}
}
-- Transforms a package declaration into a package
rule PackageToPackage {
from
src:java!Package
to
tgt:kdm!Package(
name<-src.name
--get the subpackages owned by the matched package
,codeElement<-src.ownedPackages
--adds classes and interfaces
,codeElement<-src.ownedElements
)
}
-- ===================================================== ---
--
-- abstract rule to manage source reference in kdm model
-- in java, corresponding metaclass is ASTNode
-- in kdm, it is AbstractCodeElement (no better choice)
--
-- ===================================================== ---
abstract rule ASTNodeToAbstractCodeElement {
from src :java!ASTNode
to tgt :kdm!AbstractCodeElement (
-- comments
comment <- src.comments,
-- source file management
source <- sourceRef
)
,sourceRef : kdm!SourceRef (
language <- 'java'
,region <- sourceRegion
)
,sourceRegion : kdm!SourceRegion (
language <- 'java'
-- size expensive and redundant with SourceFile information
-- ,path <- if (src.originalCompilationUnit.oclIsUndefined()) then
-- 'internal'
-- else
-- src.originalCompilationUnit.originalFilePath
-- endif
,file <- if (src.originalCompilationUnit.oclIsUndefined()) then
src.originalClassFile
else
src.originalCompilationUnit
endif
,startLine<-5
,endLine <- 10
)
}
abstract rule NamedElementToAbstractCodeElement extends ASTNodeToAbstractCodeElement {
from src :java!NamedElement
to tgt :kdm!AbstractCodeElement (
name <- src.name
)
}
abstract rule BodyDeclarationToAbstractCodeElement extends NamedElementToAbstractCodeElement {
from src :java!BodyDeclaration
to tgt :kdm!AbstractCodeElement (
-- attributes to store additional information (visibility stays redundant)
attribute <- if (src.modifier.oclIsUndefined()) then
Sequence{}
else
src.modifier
endif
-- annotations
,codeRelation <- src.annotations
)
}
abstract rule AbstractTypeDeclarationToDatatype extends BodyDeclarationToAbstractCodeElement {
from src :java!AbstractTypeDeclaration
to tgt :kdm!Datatype (
-- imports
codeRelation <- if (src.originalCompilationUnit.oclIsUndefined()) then
Sequence{}
else
src.originalCompilationUnit.imports
endif
-- inheritance
,codeRelation <- src.superInterfaces->collect(e |
if (src.oclIsTypeOf(java!ClassDeclaration)) then
thisModule.CreateImplements(e)
else
thisModule.CreateExtends(e)
endif) -- end collect
-- annotations
,codeRelation <- src.annotations
-- TODO use superClass
-- comments
,comment <- src.commentsBeforeBody
,comment <- src.commentsAfterBody
,comment <- src.comments
,comment <- if (src.originalCompilationUnit.oclIsUndefined()
or not src.refImmediateComposite().oclIsTypeOf(java!Package)) then
Sequence{}
else -- top level type declaration -> retrieving CU heading comments
src.originalCompilationUnit.comments
endif
)
}
rule ModifierToAttribute {
from src : java!Modifier
to tgt : kdm!Attribute (
tag <- 'export',
value <- src.visibility.toString()
+
(if (src.inheritance = #none) then
''
else
' ' + src.inheritance.toString()
endif)
)
}
-- ===================================================== ---
-- Rules for the classes
-- This part gives all the transformations for the classes of a Java program
-- the transformations being quite different when the considered class is generic (Java meaning)
-- ===================================================== ---
-- Transfoms a class declaration into a class unit
rule ClassDeclarationToClassUnit extends AbstractTypeDeclarationToDatatype {
from src:java!ClassDeclaration (
src.typeParameters.isEmpty()
)
using{
-- For attributes, we have to separate FieldDeclaration and VariableDeclarationFragment usage
javaAttributes :java!NamedElement = src.bodyDeclarations->select(e| e.oclIsTypeOf(java!FieldDeclaration))
->collect(f| if (f.fragments->isEmpty()) then f else f.fragments endif);
}
to tgt: kdm!ClassUnit(
isAbstract <- if src.modifier.oclIsUndefined() then
OclUndefined
else
src.modifier.inheritance = #"abstract"
endif
-- attributes
,codeElement <- javaAttributes
-- other elements
,codeElement <- src.bodyDeclarations->select(e | not e.oclIsTypeOf(java!FieldDeclaration))
)
do {
-- inheritance
tgt.codeRelation <- if src.superClass.oclIsUndefined() then
Sequence{}
else
thisModule.CreateExtends(src.superClass)
endif;
}
}
-- Transfoms a class declaration into a class unit
rule AnonymousClassDeclarationToClassUnit extends ASTNodeToAbstractCodeElement {
from src:java!AnonymousClassDeclaration
using{
-- For attributes, we have to separate FieldDeclaration and VariableDeclarationFragment usage
javaAttributes :java!NamedElement = src.bodyDeclarations->select(e| e.oclIsTypeOf(java!FieldDeclaration))
->collect(f| if (f.fragments->isEmpty()) then f else f.fragments endif);
originalTypeAccess :java!TypeAccess = src.refImmediateComposite().type;
}
to tgt: kdm!ClassUnit(
name <- 'Anonymous type'
-- attributes
,codeElement <- javaAttributes
-- other elements
,codeElement <- src.bodyDeclarations->select(e | not e.oclIsTypeOf(java!FieldDeclaration))
-- imports
,codeRelation <- if (src.originalCompilationUnit.oclIsUndefined()) then
Sequence{}
else
src.originalCompilationUnit.imports
endif
-- inheritance
,codeRelation <- if (originalTypeAccess.oclIsUndefined()) then
Sequence{}
else
thisModule.CreateImplementsForTemplated(tgt, originalTypeAccess)
endif
-- TODO use superClass
-- comments
,comment <- src.comments
,comment <- if (src.originalCompilationUnit.oclIsUndefined()
or not src.refImmediateComposite().oclIsTypeOf(java!Package)) then
Sequence{}
else -- top level type declaration -> retrieving CU heading comments
src.originalCompilationUnit.comments
endif
)
}
-- ===================================================== ---
-- Rules for the interfaces
-- This part gives all the transformations for the interfaces of a java program
-- the transformations being quite different when the considered interface is generic (java meaning)
-- ===================================================== ---
-- Transfoms an interface
rule InterfaceDeclarationToInterfaceUnit extends AbstractTypeDeclarationToDatatype {
from src:java!InterfaceDeclaration (
src.typeParameters.isEmpty()
)
using{
-- For attributes, we have to separate FieldDeclaration and VariableDeclarationFragment usage
javaAttributes :java!NamedElement = src.bodyDeclarations->select(e| e.oclIsTypeOf(java!FieldDeclaration))
->collect(f| if (f.fragments->isEmpty()) then f else f.fragments endif);
}
to tgt:kdm!InterfaceUnit(
-- attributes
codeElement <- javaAttributes
-- other elements
,codeElement <- src.bodyDeclarations->select(e | not e.oclIsTypeOf(java!FieldDeclaration))
)
}
-- ===================================================== ---
-- Rules for the Enums
-- This part gives the transformation for the enums of a java programm
-- ===================================================== ---
-- Transforms a enumerated type
rule EnumDeclarationToEnumeratedType extends AbstractTypeDeclarationToDatatype {
from src : java!EnumDeclaration
using{
-- For attributes, we have to separate FieldDeclaration and VariableDeclarationFragment usage
javaAttributes :java!NamedElement = src.bodyDeclarations->select(e| e.oclIsTypeOf(java!FieldDeclaration))
->collect(f| if (f.fragments->isEmpty()) then f else f.fragments endif);
}
to tgt : kdm!EnumeratedType(
-- enumerated values
value <- src.enumConstants
-- TODO not allowed by kdm implementation !
-- attributes
,codeElement <- javaAttributes
-- other elements
,codeElement <- src.bodyDeclarations->select(e | not e.oclIsTypeOf(java!FieldDeclaration))
)
}
-- transforms an array type into an array type
rule ArrayTypeToArrayType {
from src :java!ArrayType
to tgt :kdm!ArrayType (
name <- src.name
-- size attribute will contains dimension information instead of size
,size <- src.dimensions
,itemUnit <- realType
,indexUnit <- indexUnit
)
, realType :kdm!ItemUnit (
type <- src.elementType->getType()
)
, indexUnit : kdm!IndexUnit (
type <- java!PrimitiveTypeInt.allInstances()->first()
)
}
-- transforms a wild card into a TypeUnit (DefinedType)
rule WildCardTypeToTypeUnit {
from src :java!WildCardType
to tgt :kdm!TypeUnit (
type <- if (src.bound.oclIsUndefined()) then
OclUndefined
else
src.bound->getType()
endif
)
}
-- transforms an Annotation type into an InterfaceUnit (with an annotation)
rule AnnotationTypeDeclarationToInterfaceUnit extends AbstractTypeDeclarationToDatatype {
from src :java!AnnotationTypeDeclaration
using{
-- For attributes, we have to separate FieldDeclaration and VariableDeclarationFragment usage
javaAttributes :java!NamedElement = src.bodyDeclarations->select(e| e.oclIsTypeOf(java!FieldDeclaration))
->collect(f| if (f.fragments->isEmpty()) then f else f.fragments endif);
}
to tgt :kdm!InterfaceUnit (
annotation <- annotation
-- specific for annotation types, we have to redefine these initializations ???
-- ATL mechanism of extends does not work very well ...
,codeRelation <- src.annotations
,codeRelation <- if (src.originalCompilationUnit.oclIsUndefined()) then
Sequence{}
else
src.originalCompilationUnit.imports
endif
-- attributes
,codeElement <- javaAttributes
-- other elements, it should be AnnotationTypeMemberDeclaration
,codeElement <- src.bodyDeclarations->select(e | not e.oclIsTypeOf(java!FieldDeclaration))
)
,annotation :kdm!Annotation (
text <- 'annotation'
)
}
-- transforms an AnnotationTypeMemberDeclaration into a MemberUnit
-- it is here because ATL superimposition does not allow rules extensions.
rule AnnotationTypeMemberDeclarationToMemberUnit extends BodyDeclarationToAbstractCodeElement {
from src :java!AnnotationTypeMemberDeclaration
to tgt :kdm!MemberUnit (
type <- if (src.type.oclIsUndefined()) then
OclUndefined
else
src.type->getType()
endif
)
}
-- ========================================================
-- Rules for generic types (class, interface)
-- ========================================================
-- Transfoms a type with generic declarations into a TemplateUnit containing a type unit
abstract rule TypeDeclarationToTemplateUnit {
from src : java!TypeDeclaration (
not src.typeParameters.isEmpty()
)
to tgt : kdm!TemplateUnit( -- template parameter should be first
name <- src->getGenericName()
,codeElement <- src.typeParameters
,codeElement <- type
)
, type : kdm!Datatype (
name <- src.name
-- imports
,codeRelation <- if (src.originalCompilationUnit.oclIsUndefined()) then
Sequence{}
else
src.originalCompilationUnit.imports
endif
-- inheritance
,codeRelation <- src.superInterfaces->collect(e |
if (src.oclIsTypeOf(java!ClassDeclaration)) then
thisModule.CreateImplementsForTemplated(type, e)
else
thisModule.CreateExtendsForTemplated(type, e)
endif) -- end collect
-- comments
,comment <- src.comments
,comment <- if (src.originalCompilationUnit.oclIsUndefined()
or not src.refImmediateComposite().oclIsTypeOf(java!Package)) then
Sequence{}
else -- top level type declaration -> retrieving CU heading comments
src.originalCompilationUnit.comments
endif
-- source file management
,source <- sourceRef
-- attributes to store additional informations (visibility stay redundant)
,attribute <- if (src.modifier.oclIsUndefined()) then
Sequence{}
else
src.modifier
endif
-- annotations
,codeRelation <- src.annotations
)
,sourceRef : kdm!SourceRef (
language <- 'java'
,region <- sourceRegion
)
,sourceRegion : kdm!SourceRegion (
language <- 'java'
-- size expensive and redundant with SourceFile information
-- ,path <- if (src.originalCompilationUnit.oclIsUndefined()) then
-- 'internal'
-- else
-- src.originalCompilationUnit.originalFilePath
-- endif
,file <- if (src.originalCompilationUnit.oclIsUndefined()) then
src.originalClassFile
else
src.originalCompilationUnit
endif
)
}
-- Transfoms a class with generic declarations into a TemplateUnit containing a class unit
rule ClassDeclarationToTemplateUnit extends TypeDeclarationToTemplateUnit {
from src : java!ClassDeclaration (
not src.typeParameters.isEmpty()
)
using{
-- For attributes, we have to separate FieldDeclaration and VariableDeclarationFragment usage
javaAttributes :java!NamedElement = src.bodyDeclarations->select(e| e.oclIsTypeOf(java!FieldDeclaration))
->collect(f| if (f.fragments->isEmpty()) then f else f.fragments endif);
}
to tgt : kdm!TemplateUnit()
, type : kdm!ClassUnit(
isAbstract <- if src.modifier.oclIsUndefined() then
OclUndefined
else
src.modifier.inheritance = #"abstract"
endif
-- attributes
,codeElement <- javaAttributes
-- other elements
,codeElement <- src.bodyDeclarations->select(e | not e.oclIsTypeOf(java!FieldDeclaration))
)
do {
-- inheritance
type.codeRelation <- if src.superClass.oclIsUndefined() then
Sequence{}
else
thisModule.CreateExtendsForTemplated(type, src.superClass)
endif;
}
}
-- Transfoms a generic interface
rule InterfaceDeclarationToTemplateUnit extends TypeDeclarationToTemplateUnit {
from src : java!InterfaceDeclaration(
not src.typeParameters.isEmpty()
)
using{
-- For attributes, we have to separate FieldDeclaration and VaraiableDeclarationFragment usage
javaAttributes :java!NamedElement = src.bodyDeclarations->select(e| e.oclIsTypeOf(java!FieldDeclaration))
->collect(f| if (f.fragments->isEmpty()) then f else f.fragments endif);
}
to tgt : kdm!TemplateUnit()
, type : kdm!InterfaceUnit(
-- attributes
codeElement <- javaAttributes
-- other elements
,codeElement <- src.bodyDeclarations->select(e | not e.oclIsTypeOf(java!FieldDeclaration))
)
}
-- Transforms parameters declared by a generic type
rule TypeParameterToTemplateParameter {
from src : java!TypeParameter
to parameter : kdm!TemplateParameter (
name <- src.name
)
}
-- ========================================================
-- Rules for usage of generic types
-- ========================================================
-- Transforms ParameterizedType to TemplateType
-- relationships ParameterTo link generic usage with type arguments
-- relationship InstanceOf links generic usage with generic type
rule ParameterizedTypeToTemplateType {
from src : java!ParameterizedType
to tgt : kdm!TemplateType (
name <- src.name
,codeRelation <- src.typeArguments->collect(t | thisModule->CreateParameterTo(t))
,codeRelation <- typeLink
)
, typeLink : kdm!InstanceOf (
from <- tgt
,to <- if (src.type.type->oclIsKindOf(java!UnresolvedTypeDeclaration)) then
OclUndefined -- typeParameters does not exist on UnresolvedTypeDeclaration
else
if (src.type.type.typeParameters.isEmpty()) then
OclUndefined
else
src.type->getType()
endif
endif
)
}
-- ===================================================
-- CodeRelation section --
-- here is managed 'use' relations
-- and inheritance links (extends and implements)
-- ===================================================
-- Transforms an import declaration to an import declaration
rule ImportDeclarationToImports {
from src : java!ImportDeclaration (
not src.static
)
to tgt : kdm!Imports(
from <- src.refImmediateComposite().types->at(1),
to <- src.importedElement->getExtendsType()
)
}
--create the Extends for class or interface extension
lazy rule CreateExtends{
from
javaExtends:java!TypeAccess
to
kdmExtends:kdm!Extends(
from <- javaExtends.refImmediateComposite(),
to <- javaExtends->getType()
)
}
lazy rule CreateExtendsForTemplated {
from targetFrom :kdm!CodeItem, sourceTo :java!TypeAccess
to tgt :kdm!Extends (
from <- targetFrom
,to <- sourceTo->getType()
)
}
--create the Implements for interface implementation
lazy rule CreateImplements{
from
javaImplements:java!TypeAccess
to
kdmImplements:kdm!Implements (
from <- javaImplements.refImmediateComposite(),
to <- javaImplements->getType()
)
}
lazy rule CreateImplementsForTemplated {
from targetFrom :kdm!CodeItem, sourceTo :java!TypeAccess
to tgt :kdm!Implements (
from <- targetFrom
,to <- sourceTo->getType()
)
}
-- create a class unit in case of extends relationships
unique lazy rule CreateClassUnit {
from src : java!UnresovedItem
to tgt : kdm!ClassUnit (
name <- src.name
)
}
-- create an interface unit in case of extends or implements relationships
unique lazy rule CreateInterfaceUnit {
from src : java!UnresovedItem
to tgt : kdm!InterfaceUnit (
name <- src.name
)
}
--create the ParameterTo for type arguments of generics usage
lazy rule CreateParameterTo{
from
src :java!TypeAccess
to
tgt :kdm!ParameterTo (
from <- src.refImmediateComposite(),
to <- src->getType()
)
}
-- ===================================================== ---
-- Rules for the primitive types
-- A primitive type can be translated into different types according to the "name" attribute
-- ===================================================== ---
--creates the Boolean primitive type
rule PrimitiveTypeBooleanToBooleanType{
from
javaBoolean:java!PrimitiveTypeBoolean
to
kdmBoolean:kdm!BooleanType(
name <- 'boolean'
)
}
--creates the Byte primitive type
rule PrimitiveTypeByteToByteType{
from
javaByte:java!PrimitiveTypeByte
to
kdmByte:kdm!OctetType(
name <- 'byte'
)
}
--creates the Char primitive type
rule PrimitiveTypeCharToCharType{
from
javaChar:java!PrimitiveTypeChar
to
kdmChar:kdm!CharType(
name <- 'char'
)
}
--creates the Double primitive type
rule PrimitiveTypeDoubleToDoubleType{
from
src:java!PrimitiveTypeDouble
to
kdmFLoat:kdm!FloatType(
name <- 'double'
)
}
--creates the Float primitive type
rule PrimitiveTypeFloatToFloatType{
from
javaFloat:java!PrimitiveTypeFloat
to
kdmFloat:kdm!FloatType(
name <- 'float'
)
}
--creates the int primitive type
rule PrimitiveTypeIntToIntType{
from
javaInt:java!PrimitiveTypeInt
to
kdmInteger:kdm!IntegerType(
name <- 'int'
)
}
--creates the long primitive type
rule PrimitiveTypeLongToLongType{
from
javaLong:java!PrimitiveTypeLong
to
kdmInteger:kdm!IntegerType(
name <- 'long'
)
}
--creates the short primitive type
rule PrimitiveTypeShortToShortType{
from
javaShort:java!PrimitiveTypeShort
to
kdmInteger:kdm!IntegerType(
name <- 'short'
)
}
--creates the Void primitive type
rule PrimitiveTypeVoidToVoidType{
from
javaVoid:java!PrimitiveTypeVoid
to
kdmVoid:kdm!VoidType(
name <- 'void'
)
}
-- ===================================================== ---
-- Rules for the artefacts of a program
--
-- ===================================================== ---
rule CompilationUnitToSourceFile {
from src : java!CompilationUnit
to tgt : kdm!SourceFile (
name <- src.name
,language <- 'java'
,path <- src.originalFilePath
)
}
rule ArchiveToBinaryFile {
from src : java!Archive
to tgt : kdm!BinaryFile (
path <- src.originalFilePath
-- version <- TODO retrieve version from Manifest infos ?
)
}
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- ##########################################################################
-- StructureWithMembers
-- helpers --
--returns an element of the ExportKind enumeration to set the visibility of the field or method
helper context java!BodyDeclaration def : getVisibility() : kdm!ExportKind =
if (self.modifier.oclIsUndefined()) then
#unknown
else
if self.modifier.visibility.oclIsUndefined() then
#unknown
else
if (self.modifier.visibility = #public) then
#public
else
if (self.modifier.visibility = #protected) then
#protected
else
if (self.modifier.visibility = #none) then
if (self.abstractTypeDeclaration.oclIsTypeOf(java!InterfaceDeclaration)) then
#public
else
#protected
endif
else
#private
endif
endif
endif
endif
endif;
-- rules --
-- ===================================================
-- Transformation of attributes : Field, EnumConstant, AnnotationTypeMemberDeclaration
-- ===================================================
-- ===================================================== ---
-- Rules for the Fields
-- This part gives the transformation for the fields of a java type
-- ===================================================== ---
--create the classes and interface attributes
-- Hope : kdm does not allow interface attributes ! we have to change it
rule FieldDeclarationToMemberUnit extends BodyDeclarationToAbstractCodeElement {
from src :java!FieldDeclaration (src.fragments->isEmpty())
to tgt :kdm!MemberUnit(
export <- src->getVisibility()
,type <- if (src.type.oclIsUndefined()) then
OclUndefined
else
src.type->getType()
endif
,comment<-src.comments
)
}
-- Transfoms an enumeration constant into a value
rule EnumConstantDeclarationToValue extends BodyDeclarationToAbstractCodeElement {
from src : java!EnumConstantDeclaration
to tgt : kdm!Value(
name <- 'enum literal'
,ext <- src.name
,type <- src.refImmediateComposite()
-- TODO waiting for a strategy for constant arguments
)
}
-- ===================================================== ---
-- Rules for the methods
-- This part gives all the transformations for the methods
-- the transformations being quite different when the considered method is generic
-- ===================================================== ---
-- Transforms a method into a method unit
rule MethodDeclarationToMethodUnit extends BodyDeclarationToAbstractCodeElement {
from src : java!AbstractMethodDeclaration(
src.typeParameters.isEmpty()
--and
-- Anonymous types are not translated in kdm
--src.anonymousClassDeclarationOwner.oclIsUndefined()
)
to tgt : kdm!MethodUnit(
kind <- if (src.oclIsKindOf(java!ConstructorDeclaration)) then #constructor else #method endif
,export <- src->getVisibility()
-- signature management
,type <- signature
,codeElement <- signature
-- body (block)
,codeElement <- if (src.body.oclIsUndefined()) then Sequence{} else src.body endif
)
,signature : kdm!Signature(
name <- src.name
-- return parameter
,parameterUnit <- if (src.oclIsKindOf(java!ConstructorDeclaration)) then
Sequence{}
else
if (src.returnType.oclIsUndefined()) then
Sequence{}
else
thisModule.CreateReturnParameterUnit(src.returnType)
endif
endif
-- usual parameters
,parameterUnit <- src.parameters
-- exceptions thrown
,parameterUnit <- src.thrownExceptions->collect(e | thisModule.CreateExceptionParameterUnit(e))
)
do {
-- redefinitions / redefined MethodDeclaration
tgt.codeRelation <- if (src.oclIsKindOf(java!ConstructorDeclaration)) then
Sequence{}
else
if (src.redefinedMethodDeclaration.oclIsUndefined()) then
Sequence{}
else
thisModule.CreateImplementationOf(src, src.redefinedMethodDeclaration)
endif
endif;
}
}
-- Transforms a method into a method unit
rule MethodDeclarationToTemplateUnit {
from src : java!AbstractMethodDeclaration(
(not src.typeParameters.isEmpty())
--and
-- Anonymous types are not translated in kdm
--src.anonymousClassDeclarationOwner.oclIsUndefined()
)
to tgt : kdm!TemplateUnit( -- template parameter should be first
name <- src.name
,codeElement <- src.typeParameters
,codeElement <- method
)
,method : kdm!MethodUnit(
name <- src.name
,kind <- if (src.oclIsKindOf(java!ConstructorDeclaration)) then #constructor else #method endif
,export <- src->getVisibility()
-- signature management
,type <- signature
,codeElement <- signature
-- attributes to store additional information (visibility stay redundant)
,attribute <- if (src.modifier.oclIsUndefined()) then
Sequence{}
else
src.modifier
endif
-- annotations
,codeRelation <- src.annotations
-- comments
,comment <- src.comments
-- body (block)
,codeElement <- if (src.body.oclIsUndefined()) then Sequence{} else src.body endif
-- redefinitions / redefined MethodDeclaration
,codeRelation <- if (src.oclIsKindOf(java!ConstructorDeclaration)) then
Sequence{}
else
if (src.redefinedMethodDeclaration.oclIsUndefined()) then
Sequence{}
else
thisModule.CreateImplementationOf(src, src.redefinedMethodDeclaration)
endif
endif
-- source file management
,source <- sourceRef
)
,sourceRef : kdm!SourceRef (
language <- 'java'
,region <- sourceRegion
)
,sourceRegion : kdm!SourceRegion (
language <- 'java'
-- size expensive and redundant with SourceFile information
-- ,path <- if (src.originalCompilationUnit.oclIsUndefined()) then
-- 'internal'
-- else
-- src.originalCompilationUnit.originalFilePath
-- endif
,file <- if (src.originalCompilationUnit.oclIsUndefined()) then
src.originalClassFile
else
src.originalCompilationUnit
endif
)
,signature : kdm!Signature(
name <- src.name
-- return parameter
,parameterUnit <- if (src.oclIsKindOf(java!ConstructorDeclaration)) then
Sequence{}
else
if (src.returnType.oclIsUndefined()) then