-
Notifications
You must be signed in to change notification settings - Fork 3
/
ModuleConverter.java
953 lines (849 loc) · 33.1 KB
/
ModuleConverter.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
/*******************************************************************************
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.migration.converters;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.acceleo.AcceleoASTNode;
import org.eclipse.acceleo.AcceleoFactory;
import org.eclipse.acceleo.AcceleoPackage;
import org.eclipse.acceleo.Binding;
import org.eclipse.acceleo.Block;
import org.eclipse.acceleo.Comment;
import org.eclipse.acceleo.CommentBody;
import org.eclipse.acceleo.Documentation;
import org.eclipse.acceleo.FileStatement;
import org.eclipse.acceleo.ForStatement;
import org.eclipse.acceleo.IfStatement;
import org.eclipse.acceleo.Import;
import org.eclipse.acceleo.LetStatement;
import org.eclipse.acceleo.Metamodel;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.ModuleElement;
import org.eclipse.acceleo.ModuleReference;
import org.eclipse.acceleo.OpenModeKind;
import org.eclipse.acceleo.ProtectedArea;
import org.eclipse.acceleo.Query;
import org.eclipse.acceleo.Statement;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.TextStatement;
import org.eclipse.acceleo.Variable;
import org.eclipse.acceleo.VisibilityKind;
import org.eclipse.acceleo.aql.migration.IModuleResolver;
import org.eclipse.acceleo.aql.migration.MigrationException;
import org.eclipse.acceleo.aql.migration.converters.utils.TypeUtils;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.model.mtl.FileBlock;
import org.eclipse.acceleo.model.mtl.ForBlock;
import org.eclipse.acceleo.model.mtl.IfBlock;
import org.eclipse.acceleo.model.mtl.LetBlock;
import org.eclipse.acceleo.model.mtl.MtlPackage;
import org.eclipse.acceleo.model.mtl.ProtectedAreaBlock;
import org.eclipse.acceleo.model.mtl.QueryInvocation;
import org.eclipse.acceleo.model.mtl.TemplateInvocation;
import org.eclipse.acceleo.model.mtl.TypedModel;
import org.eclipse.acceleo.query.ast.AstFactory;
import org.eclipse.acceleo.query.ast.AstPackage;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.ast.CallType;
import org.eclipse.acceleo.query.ast.Expression;
import org.eclipse.acceleo.query.ast.Lambda;
import org.eclipse.acceleo.query.ast.StringLiteral;
import org.eclipse.acceleo.query.ast.VariableDeclaration;
import org.eclipse.acceleo.query.parser.AstResult;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ocl.ecore.EcorePackage;
import org.eclipse.ocl.ecore.OCLExpression;
import org.eclipse.ocl.ecore.OperationCallExp;
import org.eclipse.ocl.ecore.StringLiteralExp;
import org.eclipse.ocl.utilities.PredefinedType;
/**
* A converter dedicated to MTL elements.
*
* @author <a href="mailto:william.piers@obeo.fr">William Piers</a>
*/
public final class ModuleConverter extends AbstractConverter {
public static class ImpliciteSelfFrame {
private final Object input;
private final String implicitSelfName;
/**
* Constructor.
*
* @param input
* the Acceleo 3 input
* @param implicitSelfName
* the implicit self name
*/
private ImpliciteSelfFrame(Object input, String implicitSelfName) {
this.input = input;
this.implicitSelfName = implicitSelfName;
}
public Object getInput() {
return input;
}
public String getImplicitSelfName() {
return implicitSelfName;
}
}
/**
* The new line string.
*/
private static final String NEW_LINE = "\n";
/**
* A converter dedicated to expressions.
*/
private ExpressionConverter expressionConverter;
/**
* The module resolver, for imports/extends.
*/
private IModuleResolver moduleResolver;
/**
* The {@link List} of service class to copy.
*/
private final List<String> serviceClassToCopy = new ArrayList<>();
/**
* The target folder {@link Path}.
*/
private final Path targetFolderPath;
/**
* The index of the current for implicit iterator.
*/
private int forImpliciteIteratorIndex;
/**
* The implicit self variable name stack.
*/
private final Deque<ImpliciteSelfFrame> implicitSelfStack = new ArrayDeque<>();
/**
* Creates the converter using the given module resolver.
*
* @param moduleResolver
* the module resolver
* @param targetFolderPath
* the target folder {@link Path}
*/
public ModuleConverter(IModuleResolver moduleResolver, Path targetFolderPath) {
this.moduleResolver = moduleResolver;
this.targetFolderPath = targetFolderPath;
expressionConverter = new ExpressionConverter(targetFolderPath);
}
/**
* Pushes the given self implicit variable name into the stack.
*
* @param input
* the Acceleo 3 input
* @param selfName
* the current self implicit variable name
*/
protected void pushImplicitSelf(Object input, String selfName) {
implicitSelfStack.addLast(new ImpliciteSelfFrame(input, selfName));
}
/**
* Peeks the last {@link #pushImplicitSelf(String) pushed} self implicit variable name from the stack.
*
* @return the last {@link #pushImplicitSelf(String) pushed} self implicit variable name from the stack
*/
protected ImpliciteSelfFrame peekImplicitSelf() {
return implicitSelfStack.peekLast();
}
/**
* Pops the last {@link #pushImplicitSelf(String) pushed} self implicit variable name from the stack.
*
* @return the last {@link #pushImplicitSelf(String) pushed} self implicit variable name from the stack
*/
protected ImpliciteSelfFrame popIndentation() {
return implicitSelfStack.removeLast();
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.aql.migration.converters.AbstractConverter#convert(org.eclipse.emf.ecore.EObject)
*/
@Override
public Object convert(EObject input) {
final Object res;
switch (input.eClass().getClassifierID()) {
case MtlPackage.MODULE:
res = caseModule((org.eclipse.acceleo.model.mtl.Module)input);
break;
case MtlPackage.TEMPLATE:
res = caseTemplate((org.eclipse.acceleo.model.mtl.Template)input);
break;
case MtlPackage.QUERY:
res = caseQuery((org.eclipse.acceleo.model.mtl.Query)input);
break;
case MtlPackage.DOCUMENTATION:
case MtlPackage.MODULE_DOCUMENTATION:
case MtlPackage.MODULE_ELEMENT_DOCUMENTATION:
res = caseDocumentation((org.eclipse.acceleo.model.mtl.Documentation)input);
break;
case MtlPackage.COMMENT:
res = caseComment((org.eclipse.acceleo.model.mtl.Comment)input);
break;
case MtlPackage.FILE_BLOCK:
res = caseFileBlock((FileBlock)input);
break;
case MtlPackage.FOR_BLOCK:
res = caseForBlock((ForBlock)input);
break;
case MtlPackage.IF_BLOCK:
res = caseIfBlock((IfBlock)input);
break;
case MtlPackage.LET_BLOCK:
res = caseLetBlock((LetBlock)input);
break;
case MtlPackage.PROTECTED_AREA_BLOCK:
res = caseProtectedAreaBlock((ProtectedAreaBlock)input);
break;
case EcorePackage.VARIABLE:
res = caseVariable((org.eclipse.ocl.ecore.Variable)input);
break;
case EcorePackage.STRING_LITERAL_EXP:
res = caseText((StringLiteralExp)input);
break;
case MtlPackage.TYPED_MODEL:
res = caseTypedModel((TypedModel)input);
break;
default:
if (input instanceof OCLExpression) {
res = expressionConverter.convertToStatement((OCLExpression)input, implicitSelfStack);
} else {
throw new MigrationException(input);
}
break;
}
if (res instanceof Module) {
final Iterator<EObject> it = ((Module)res).eAllContents();
while (it.hasNext()) {
final EObject current = it.next();
if (current instanceof ProtectedArea) {
setTagPrefixes((ProtectedArea)current);
}
}
}
return res;
}
/**
* Sets the {@link ProtectedArea#getStartTagPrefix() start tag} and {@link ProtectedArea#getEndTagPrefix()
* end tag}.
*
* @param protectedArea
* the {@link ProtectedArea}
*/
private void setTagPrefixes(ProtectedArea protectedArea) {
final TextStatement previousText = getPreviousTextStatement(protectedArea);
if (previousText != null && !previousText.isNewLineNeeded()) {
protectedArea.setStartTagPrefix(extractPrefix(previousText));
}
final TextStatement lastText = getLastText(protectedArea);
if (lastText != null && !lastText.isNewLineNeeded()) {
protectedArea.setEndTagPrefix(extractPrefix(lastText));
EcoreUtil.remove(lastText);
}
}
/**
* Gets the previous {@link Statement} before the given {@link ProtectedArea} if it's a
* {@link TextStatement}.
*
* @param protectedArea
* the {@link ProtectedArea}
* @return the previous {@link Statement} before the given {@link ProtectedArea} if it's a
* {@link TextStatement}, <code>null</code> otherwise
*/
private TextStatement getPreviousTextStatement(ProtectedArea protectedArea) {
final TextStatement res;
final EStructuralFeature containingFeature = protectedArea.eContainingFeature();
final EObject container = protectedArea.eContainer();
if (containingFeature != null && container != null) {
final Object values = container.eGet(containingFeature);
if (values instanceof Collection<?>) {
EObject previous = null;
for (Object value : (Collection<?>)values) {
if (value != protectedArea && value instanceof EObject) {
previous = (EObject)value;
} else {
break;
}
}
if (previous instanceof TextStatement) {
res = (TextStatement)previous;
} else {
res = null;
}
} else {
res = null;
}
} else {
res = null;
}
return res;
}
/**
* Gets the last statement of the given Pro {@link ProtectedArea} if it's a {@link TextStatement}.
*
* @param protectedArea
* the {@link ProtectedArea}
* @return the last statement of the given Pro {@link ProtectedArea} if it's a {@link TextStatement},
* <code>null</code> otherwise
*/
private TextStatement getLastText(ProtectedArea protectedArea) {
final TextStatement res;
if (!protectedArea.getBody().getStatements().isEmpty()) {
final Statement lastStatement = protectedArea.getBody().getStatements().get(protectedArea
.getBody().getStatements().size() - 1);
if (lastStatement instanceof TextStatement) {
res = (TextStatement)lastStatement;
} else {
res = null;
}
} else {
res = null;
}
return res;
}
/**
* Extracts the {@link ProtectedArea} prefix from the given {@link TextStatement}.
*
* @param textStatement
* the {@link TextStatement}
* @return the {@link ProtectedArea} prefix from the given {@link TextStatement} if any, <code>null</code>
* otherwise
*/
private org.eclipse.acceleo.Expression extractPrefix(final TextStatement textStatement) {
final org.eclipse.acceleo.Expression res;
final String text = textStatement.getValue();
int index = Math.max(text.lastIndexOf(NEW_LINE), 0);
while (index < text.length() && Character.isWhitespace(text.charAt(index))) {
index++;
}
if (index < text.length()) {
final String startPrefix = text.substring(index);
textStatement.setValue(text.substring(0, index));
final StringLiteral stringLiteral = AstPackage.eINSTANCE.getAstFactory().createStringLiteral();
stringLiteral.setValue(startPrefix);
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createExpression();
res.setAst(new AstResult(stringLiteral, null, null, Diagnostic.OK_INSTANCE));
} else {
res = null;
}
return res;
}
private Object caseModule(org.eclipse.acceleo.model.mtl.Module inputModule) {
final Module outputModule = AcceleoFactory.eINSTANCE.createModule();
outputModule.setName(getModuleName(inputModule.getName()));
map(inputModule.getInput(), outputModule.getMetamodels());
map(inputModule.getOwnedModuleElement(), outputModule.getModuleElements());
// extends
if (!inputModule.getExtends().isEmpty()) {
ModuleReference moduleReference = AcceleoFactory.eINSTANCE.createModuleReference();
// only one module can be extended
org.eclipse.acceleo.model.mtl.Module extendedModule = inputModule.getExtends().get(0);
if (extendedModule.getNsURI() != null) {
moduleReference.setQualifiedName(getModuleName(extendedModule.getNsURI()));
} else {
moduleReference.setQualifiedName(getModuleName(moduleResolver.getQualifiedName(inputModule,
extendedModule)));
}
outputModule.setExtends(moduleReference);
}
// imports
for (org.eclipse.acceleo.model.mtl.Module importedModule : inputModule.getImports()) {
Import outputImport = AcceleoFactory.eINSTANCE.createImport();
ModuleReference moduleReference = AcceleoFactory.eINSTANCE.createModuleReference();
if (importedModule.getNsURI() != null) {
moduleReference.setQualifiedName(getModuleName(importedModule.getNsURI()));
} else {
moduleReference.setQualifiedName(getModuleName(moduleResolver.getQualifiedName(inputModule,
importedModule)));
}
outputImport.setModule(moduleReference);
outputModule.getImports().add(outputImport);
}
// add imports for invoke()
addInvokeImports(inputModule, outputModule);
// add metamodel imports for called element
addCalledMetamodelImports(inputModule, outputModule);
for (Entry<Call, String> entry : expressionConverter.getJavaServiceCalls().entrySet()) {
if (isAmbiguousJavaServiceCall(outputModule, entry.getKey())) {
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
final File javaFile = new File(targetFolderPath + FileSystems.getDefault().getSeparator()
+ entry.getValue().replace(".", FileSystems.getDefault().getSeparator()) + ".java");
if (javaFile.exists()) {
try {
final IDocument document = new Document(new String(Files.readAllBytes(javaFile
.toPath())));
parser.setSource(document.get().toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit)parser.createAST(null);
cu.accept(new AmbiguousServiceMethodRefactorVisitor(document, entry.getKey()));
Files.write(javaFile.toPath(), document.get().getBytes(), StandardOpenOption.CREATE);
entry.getKey().setServiceName(entry.getKey().getServiceName()
+ ExpressionConverter.JAVA_SERVICE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return outputModule;
}
private void addCalledMetamodelImports(org.eclipse.acceleo.model.mtl.Module inputModule,
Module outputModule) {
final Set<String> knownNsURIs = new HashSet<>();
for (Metamodel metamodel : outputModule.getMetamodels()) {
knownNsURIs.add(metamodel.getReferencedPackage().getNsURI());
}
final Map<String, EPackage> missingEPackages = new LinkedHashMap<>();
final Iterator<EObject> it = inputModule.eAllContents();
while (it.hasNext()) {
final EObject eObject = it.next();
if (eObject instanceof TemplateInvocation) {
final TemplateInvocation invocation = (TemplateInvocation)eObject;
for (org.eclipse.ocl.ecore.Variable parameter : invocation.getDefinition().getParameter()) {
final EClassifier eClassifier = parameter.getEGenericType().getEClassifier();
if (!(eClassifier instanceof PredefinedType<?>)) {
final EPackage ePackage = eClassifier.getEPackage();
if (!knownNsURIs.contains(ePackage.getNsURI())) {
knownNsURIs.add(ePackage.getNsURI());
missingEPackages.put(ePackage.getNsURI(), ePackage);
}
}
}
} else if (eObject instanceof QueryInvocation) {
final QueryInvocation invocation = (QueryInvocation)eObject;
for (org.eclipse.ocl.ecore.Variable parameter : invocation.getDefinition().getParameter()) {
final EClassifier eClassifier = parameter.getEGenericType().getEClassifier();
if (!(eClassifier instanceof PredefinedType<?>)) {
final EPackage ePackage = eClassifier.getEPackage();
if (!knownNsURIs.contains(ePackage.getNsURI())) {
knownNsURIs.add(ePackage.getNsURI());
missingEPackages.put(ePackage.getNsURI(), ePackage);
}
}
}
}
}
for (EPackage missingEPackage : missingEPackages.values()) {
final Metamodel metamodel = AcceleoPackage.eINSTANCE.getAcceleoFactory().createMetamodel();
metamodel.setReferencedPackage(missingEPackage);
outputModule.getMetamodels().add(metamodel);
}
}
/**
* Gets the Acceleo 4 identifier for the given name or qualified name of a module.
*
* @param name
* the name or qualified name of a module
* @return the Acceleo 4 identifier for the given name or qualified name of a module
*/
private String getModuleName(String name) {
final StringBuilder res = new StringBuilder();
final int lastColonIndex = name.lastIndexOf("::");
final int start;
if (lastColonIndex >= 0) {
start = lastColonIndex + 2;
res.append(name.substring(0, start));
} else {
start = 0;
}
char charAt = name.charAt(start);
if (Character.isJavaIdentifierStart(charAt)) {
res.append(charAt);
} else {
res.append('_');
}
for (int i = start + 1; i < name.length(); i++) {
charAt = name.charAt(i);
if (Character.isJavaIdentifierPart(charAt)) {
res.append(charAt);
} else {
res.append('_');
}
}
return res.toString();
}
private boolean isAmbiguousJavaServiceCall(Module module, Call call) {
boolean res = false;
for (ModuleElement element : module.getModuleElements()) {
if (element instanceof Template) {
res = call.getServiceName().equals(((Template)element).getName()) && parameterMatch(call
.getArguments(), ((Template)element).getParameters());
} else if (element instanceof Query) {
res = call.getServiceName().equals(((Query)element).getName()) && parameterMatch(call
.getArguments(), ((Query)element).getParameters());
}
if (res) {
break;
}
}
return res;
}
private boolean parameterMatch(EList<Expression> arguments, EList<Variable> parameters) {
boolean res = true;
if (arguments.size() == parameters.size()) {
// TODO we should validate each expression and Variable type to see if they match
res = true;
} else {
res = false;
}
return res;
}
private void addInvokeImports(org.eclipse.acceleo.model.mtl.Module inputModule,
final Module outputModule) {
// TODO this is sub optimal, we should have access to the output Module when migrating
// OperationCallExp
final Set<String> knownImports = new HashSet<String>();
for (Import imp : outputModule.getImports()) {
knownImports.add(imp.getModule().getQualifiedName());
}
final List<String> imports = new ArrayList<String>();
final Iterator<EObject> it = inputModule.eAllContents();
while (it.hasNext()) {
final EObject eObj = it.next();
if (eObj instanceof OperationCallExp && expressionConverter.isInvokeCall(
(OperationCallExp)eObj)) {
final OperationCallExp call = (OperationCallExp)eObj;
if (call.getArgument().get(0) instanceof StringLiteralExp) {
final String serviceClassName = ((org.eclipse.ocl.expressions.StringLiteralExp<EClassifier>)call
.getArgument().get(0)).getStringSymbol();
final String toImport = serviceClassName.replace(".", AcceleoParser.QUALIFIER_SEPARATOR);
if (!knownImports.contains(toImport)) {
knownImports.add(toImport);
serviceClassToCopy.add(serviceClassName);
imports.add(toImport);
}
}
}
}
Collections.sort(imports);
for (String toImport : imports) {
final Import imp = AcceleoFactory.eINSTANCE.createImport();
final ModuleReference moduleRef = AcceleoFactory.eINSTANCE.createModuleReference();
imp.setModule(moduleRef);
moduleRef.setQualifiedName(toImport);
outputModule.getImports().add(imp);
}
}
private Object caseTemplate(org.eclipse.acceleo.model.mtl.Template inputTemplate) {
List<EObject> res = new ArrayList<>();
Template outputTemplate = AcceleoFactory.eINSTANCE.createTemplate();
if (inputTemplate.getDocumentation() != null) {
outputTemplate.setDocumentation((Documentation)convert(inputTemplate.getDocumentation()));
}
outputTemplate.setName(inputTemplate.getName());
outputTemplate.setMain(inputTemplate.isMain());
outputTemplate.setVisibility(VisibilityKind.getByName(inputTemplate.getVisibility().getName()
.toLowerCase()));
// parameters
map(inputTemplate.getParameter(), outputTemplate.getParameters());
// post
if (inputTemplate.getPost() != null) {
outputTemplate.setPost(expressionConverter.convertToExpression(inputTemplate.getPost(), null));
}
// guard
if (inputTemplate.getGuard() != null) {
outputTemplate.setGuard(expressionConverter.convertToExpression(inputTemplate.getGuard(), null));
}
// main comment
if (inputTemplate.isMain()) {
Comment mainComment = AcceleoFactory.eINSTANCE.createComment();
CommentBody commentBody = AcceleoFactory.eINSTANCE.createCommentBody();
commentBody.setValue("@main");
mainComment.setBody(commentBody);
res.add(mainComment);
}
res.add(outputTemplate);
// statements
pushImplicitSelf(inputTemplate, inputTemplate.getParameter().get(0).getName());
try {
final Block body = createBlock(outputTemplate, inputTemplate.getBody());
outputTemplate.setBody(body);
} finally {
popIndentation();
}
return res;
}
private Object caseQuery(org.eclipse.acceleo.model.mtl.Query inputQuery) {
Query outputQuery = AcceleoFactory.eINSTANCE.createQuery();
if (inputQuery.getDocumentation() != null) {
outputQuery.setDocumentation((Documentation)convert(inputQuery.getDocumentation()));
}
outputQuery.setName(inputQuery.getName());
outputQuery.setVisibility(VisibilityKind.getByName(inputQuery.getVisibility().getName()
.toLowerCase()));
map(inputQuery.getParameter(), outputQuery.getParameters());
pushImplicitSelf(inputQuery, inputQuery.getParameter().get(0).getName());
try {
outputQuery.setBody(expressionConverter.convertToExpression(inputQuery.getExpression(),
implicitSelfStack));
} finally {
popIndentation();
}
outputQuery.setType(createAstResult(TypeUtils.createTypeLiteral(inputQuery.getType())));
return outputQuery;
}
private Object caseFileBlock(FileBlock input) {
FileStatement output = AcceleoFactory.eINSTANCE.createFileStatement();
if (input.getCharset() != null) {
output.setCharset(expressionConverter.convertToExpression(input.getCharset(), implicitSelfStack));
}
output.setUrl(expressionConverter.convertToExpression(input.getFileUrl(), implicitSelfStack));
output.setMode(OpenModeKind.getByName(input.getOpenMode().getName().toLowerCase()));
// statements
final Block body = createBlock(output, input.getBody());
output.setBody(body);
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private Object caseLetBlock(LetBlock input) {
if (!input.getElseLet().isEmpty()) {
throw new MigrationException(input.getElseLet().get(0));
}
LetStatement output = AcceleoFactory.eINSTANCE.createLetStatement();
Binding binding = AcceleoFactory.eINSTANCE.createBinding();
output.getVariables().add(binding);
binding.setName(input.getLetVariable().getName());
if (!TypeUtils.containsOclAny(input.getLetVariable().getType())) {
binding.setType(createAstResult(TypeUtils.createTypeLiteral(input.getLetVariable().getType())));
}
binding.setInitExpression(expressionConverter.convertToExpression((OCLExpression)input
.getLetVariable().getInitExpression(), implicitSelfStack));
// statements
final Block body = createBlock(output, input.getBody());
output.setBody(body);
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private TextStatement newLineAfterEndBlock() {
final TextStatement res = AcceleoFactory.eINSTANCE.createTextStatement();
res.setValue("");
res.setNewLineNeeded(true);
return res;
}
private Object caseForBlock(ForBlock input) {
ForStatement output = AcceleoFactory.eINSTANCE.createForStatement();
Binding binding = AcceleoFactory.eINSTANCE.createBinding();
output.setBinding(binding);
org.eclipse.ocl.ecore.Variable loopVariable = input.getLoopVariable();
if (loopVariable != null) {
binding.setName(loopVariable.getName());
binding.setType(createAstResult(TypeUtils.createTypeLiteral(loopVariable.getType())));
} else {
binding.setName("iterator" + forImpliciteIteratorIndex++);
}
binding.setInitExpression(getInitExpression(input));
// statements
pushImplicitSelf(input, binding.getName());
try {
final Block body = createBlock(output, input.getBody());
output.setBody(body);
} finally {
popIndentation();
}
OCLExpression each = input.getEach();
if (each != null) {
output.setSeparator(expressionConverter.convertToExpression(each, implicitSelfStack));
}
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private org.eclipse.acceleo.Expression getInitExpression(ForBlock input) {
final org.eclipse.acceleo.Expression res;
final org.eclipse.acceleo.Expression initExpression = expressionConverter.convertToExpression(input
.getIterSet(), implicitSelfStack);
if (input.getGuard() != null) {
final Call selectCall = AstFactory.eINSTANCE.createCall();
selectCall.setType(CallType.COLLECTIONCALL);
selectCall.setServiceName("select");
selectCall.getArguments().add(initExpression.getAst().getAst());
final Lambda lambda = AstFactory.eINSTANCE.createLambda();
final VariableDeclaration varDeclaration = AstFactory.eINSTANCE.createVariableDeclaration();
varDeclaration.setName(input.getLoopVariable().getName());
lambda.getParameters().add(varDeclaration);
final org.eclipse.acceleo.Expression guardExpression = expressionConverter.convertToExpression(
input.getGuard(), implicitSelfStack);
lambda.setExpression(guardExpression.getAst().getAst());
selectCall.getArguments().add(lambda);
res = AcceleoFactory.eINSTANCE.createExpression();
res.setAst(new AstResult(selectCall, null, null, Diagnostic.OK_INSTANCE));
} else {
res = initExpression;
}
return res;
}
private List<Statement> caseIfBlock(IfBlock input) {
IfStatement output = AcceleoFactory.eINSTANCE.createIfStatement();
output.setCondition(expressionConverter.convertToExpression(input.getIfExpr(), implicitSelfStack));
// then statements
final Block thenBlock = createBlock(output, input.getBody());
output.setThen(thenBlock);
List<org.eclipse.acceleo.model.mtl.Block> inputElseBlocks = new ArrayList<>();
inputElseBlocks.addAll(input.getElseIf());
if (input.getElse() != null) {
inputElseBlocks.add(input.getElse());
}
if (!inputElseBlocks.isEmpty()) {
IfStatement current = output;
for (org.eclipse.acceleo.model.mtl.Block inputElseBlock : inputElseBlocks) {
// else statements
if (inputElseBlock instanceof IfBlock) {
Block elseBlock = AcceleoFactory.eINSTANCE.createBlock();
current.setElse(elseBlock);
current = (IfStatement)caseIfBlock((IfBlock)inputElseBlock).get(0);
elseBlock.getStatements().add(current);
} else {
final Block elseBlock = createBlock(current, inputElseBlock.getBody());
current.setElse(elseBlock);
}
}
}
return Arrays.asList(new Statement[] {output, newLineAfterEndBlock() });
}
private Object caseText(StringLiteralExp input) {
final List<TextStatement> outputs = new ArrayList<TextStatement>();
final String text = input.getStringSymbol().replace("[", "['['/]");
int startOfText = 0;
int endOfText;
do {
endOfText = text.indexOf(NEW_LINE, startOfText);
if (endOfText < 0) {
endOfText = text.length();
final TextStatement output = AcceleoFactory.eINSTANCE.createTextStatement();
output.setValue(text.substring(startOfText, endOfText));
output.setNewLineNeeded(false);
outputs.add(output);
} else {
final TextStatement output = AcceleoFactory.eINSTANCE.createTextStatement();
output.setValue(text.substring(startOfText, endOfText));
output.setNewLineNeeded(true);
outputs.add(output);
}
startOfText = endOfText + NEW_LINE.length();
} while (endOfText < text.length() && startOfText < text.length());
return outputs;
}
private Object caseTypedModel(TypedModel input) {
List<EObject> res = new ArrayList<>();
for (EPackage ePackage : input.getTakesTypesFrom()) {
Metamodel metamodel = AcceleoFactory.eINSTANCE.createMetamodel();
if (ePackage.eIsProxy()) {
// we create a dummy EPackage
EPackage dummy = org.eclipse.emf.ecore.EcoreFactory.eINSTANCE.createEPackage();
dummy.setNsURI(EcoreUtil.getURI(ePackage).toString().split("#")[0]);
metamodel.setReferencedPackage(dummy);
} else {
metamodel.setReferencedPackage(ePackage);
}
res.add(metamodel);
}
return res;
}
private Object caseVariable(org.eclipse.ocl.ecore.Variable inputVariable) {
Variable outputVariable = AcceleoFactory.eINSTANCE.createVariable();
outputVariable.setName(inputVariable.getName());
outputVariable.setType(createAstResult(TypeUtils.createTypeLiteral(inputVariable.getType())));
return outputVariable;
}
private Object caseDocumentation(org.eclipse.acceleo.model.mtl.Documentation input) {
Documentation output = AcceleoFactory.eINSTANCE.createModuleElementDocumentation();
CommentBody body = AcceleoFactory.eINSTANCE.createCommentBody();
StringBuilder formatValue = new StringBuilder();
formatValue.append(NEW_LINE);
for (String line : input.getBody().getValue().split(NEW_LINE)) {
if (!line.trim().isEmpty()) {
formatValue.append(" * " + line + NEW_LINE);
}
}
formatValue.append("*");
body.setValue(formatValue.toString());
output.setBody(body);
return output;
}
private Object caseComment(org.eclipse.acceleo.model.mtl.Comment inputComment) {
final String commentValue;
final Comment outputComment;
if (inputComment.getBody().getValue().contains("/]")) {
outputComment = AcceleoFactory.eINSTANCE.createBlockComment();
commentValue = inputComment.getBody().getValue();
} else {
outputComment = AcceleoFactory.eINSTANCE.createComment();
commentValue = inputComment.getBody().getValue().substring(1);
}
final CommentBody commentBody = AcceleoFactory.eINSTANCE.createCommentBody();
commentBody.setValue(commentValue);
outputComment.setBody(commentBody);
return outputComment;
}
private Object caseProtectedAreaBlock(ProtectedAreaBlock input) {
ProtectedArea output = AcceleoFactory.eINSTANCE.createProtectedArea();
output.setId(expressionConverter.convertToExpression(input.getMarker(), implicitSelfStack));
// statements
final Block body = createBlock(output, input.getBody());
output.setBody(body);
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private Block createBlock(AcceleoASTNode node, List<OCLExpression> inputStatements) {
Block res = AcceleoFactory.eINSTANCE.createBlock();
map(inputStatements, res.getStatements());
if (node instanceof Template || node instanceof File) {
res.setInlined(false);
} else if (node instanceof ProtectedArea) {
res.setInlined(false);
// remove the first new line
res.getStatements().remove(0);
} else {
boolean inlined = true;
for (Statement statement : res.getStatements()) {
if (statement instanceof TextStatement && ((TextStatement)statement).isNewLineNeeded()) {
inlined = false;
break;
}
}
res.setInlined(inlined);
}
if (res.getStatements().isEmpty()) {
res.setInlined(false);
}
return res;
}
/**
* Gets the {@link List} of service class to copy.
*
* @return the {@link List} of service class to copy
*/
public List<String> getServiceClassToCopy() {
return serviceClassToCopy;
}
}