-
Notifications
You must be signed in to change notification settings - Fork 317
/
JvmModelGenerator.xtend
1063 lines (975 loc) · 39.8 KB
/
JvmModelGenerator.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) 2012, 2021, 2022 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.xtext.xbase.compiler
import com.google.inject.Inject
import java.util.List
import java.util.Map
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend2.lib.StringConcatenation
import org.eclipse.xtext.EcoreUtil2
import org.eclipse.xtext.common.types.JvmAnnotationAnnotationValue
import org.eclipse.xtext.common.types.JvmAnnotationReference
import org.eclipse.xtext.common.types.JvmAnnotationType
import org.eclipse.xtext.common.types.JvmAnnotationValue
import org.eclipse.xtext.common.types.JvmBooleanAnnotationValue
import org.eclipse.xtext.common.types.JvmByteAnnotationValue
import org.eclipse.xtext.common.types.JvmCharAnnotationValue
import org.eclipse.xtext.common.types.JvmConstructor
import org.eclipse.xtext.common.types.JvmCustomAnnotationValue
import org.eclipse.xtext.common.types.JvmDeclaredType
import org.eclipse.xtext.common.types.JvmDoubleAnnotationValue
import org.eclipse.xtext.common.types.JvmEnumAnnotationValue
import org.eclipse.xtext.common.types.JvmEnumerationLiteral
import org.eclipse.xtext.common.types.JvmEnumerationType
import org.eclipse.xtext.common.types.JvmExecutable
import org.eclipse.xtext.common.types.JvmFeature
import org.eclipse.xtext.common.types.JvmField
import org.eclipse.xtext.common.types.JvmFloatAnnotationValue
import org.eclipse.xtext.common.types.JvmFormalParameter
import org.eclipse.xtext.common.types.JvmGenericArrayTypeReference
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.common.types.JvmIdentifiableElement
import org.eclipse.xtext.common.types.JvmIntAnnotationValue
import org.eclipse.xtext.common.types.JvmLongAnnotationValue
import org.eclipse.xtext.common.types.JvmMember
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.common.types.JvmShortAnnotationValue
import org.eclipse.xtext.common.types.JvmStringAnnotationValue
import org.eclipse.xtext.common.types.JvmType
import org.eclipse.xtext.common.types.JvmTypeAnnotationValue
import org.eclipse.xtext.common.types.JvmTypeParameter
import org.eclipse.xtext.common.types.JvmTypeParameterDeclarator
import org.eclipse.xtext.common.types.JvmTypeReference
import org.eclipse.xtext.common.types.JvmUpperBound
import org.eclipse.xtext.common.types.JvmVisibility
import org.eclipse.xtext.common.types.util.TypeReferences
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider
import org.eclipse.xtext.documentation.IEObjectDocumentationProviderExtension
import org.eclipse.xtext.documentation.IFileHeaderProvider
import org.eclipse.xtext.documentation.IJavaDocTypeReferenceProvider
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.trace.AbsoluteURI
import org.eclipse.xtext.generator.trace.ITraceURIConverter
import org.eclipse.xtext.generator.trace.LocationData
import org.eclipse.xtext.generator.trace.SourceRelativeURI
import org.eclipse.xtext.naming.IQualifiedNameConverter
import org.eclipse.xtext.nodemodel.INode
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.eclipse.xtext.resource.ILocationInFileProvider
import org.eclipse.xtext.scoping.IScopeProvider
import org.eclipse.xtext.util.ITextRegionWithLineInformation
import org.eclipse.xtext.util.Strings
import org.eclipse.xtext.validation.Issue
import org.eclipse.xtext.workspace.IProjectConfig
import org.eclipse.xtext.xbase.XExpression
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable
import org.eclipse.xtext.xbase.compiler.output.ImportingStringConcatenation
import org.eclipse.xtext.xbase.compiler.output.SharedAppendableState
import org.eclipse.xtext.xbase.compiler.output.TreeAppendable
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.ILogicalContainerProvider
import org.eclipse.xtext.xbase.jvmmodel.JvmTypeExtensions
import org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner
import org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner
import org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices
import static com.google.common.collect.Maps.newHashMap
import static org.eclipse.xtext.common.types.TypesPackage.Literals.*
import static org.eclipse.xtext.util.JavaVersion.*
/**
* A generator implementation that processes the
* derived {@link IJvmModelInferrer JVM model}
* and produces the respective java code.
*/
class JvmModelGenerator implements IGenerator {
@Inject protected extension ILogicalContainerProvider
@Inject protected extension TypeReferences
@Inject protected extension TreeAppendableUtil
@Inject protected extension JvmTypeExtensions
@Inject protected extension LoopExtensions
@Inject protected extension ErrorSafeExtensions
@Inject CommonTypeComputationServices commonServices
@Inject XbaseCompiler compiler
@Inject ILocationInFileProvider locationProvider
@Inject IEObjectDocumentationProvider documentationProvider
@Inject IFileHeaderProvider fileHeaderProvider
@Inject IJvmModelAssociations jvmModelAssociations
@Inject JavaKeywords keywords
@Inject IGeneratorConfigProvider generatorConfigProvider
@Inject ITraceURIConverter converter
@Inject IJavaDocTypeReferenceProvider javaDocTypeReferenceProvider
@Inject IScopeProvider scopeProvider
@Inject IQualifiedNameConverter qualifiedNameConverter
override void doGenerate(Resource input, IFileSystemAccess fsa) {
for (obj : input.contents) {
obj.internalDoGenerate(fsa)
}
}
def dispatch void internalDoGenerate(EObject obj, IFileSystemAccess fsa) {}
def dispatch void internalDoGenerate(JvmDeclaredType type, IFileSystemAccess fsa) {
if (DisableCodeGenerationAdapter.isDisabled(type))
return;
if(type.qualifiedName !== null)
fsa.generateFile(type.qualifiedName.replace('.', '/') + '.java', type.generateType(generatorConfigProvider.get(type)))
}
protected def createImportManager(JvmDeclaredType type) {
new ImportManager(true, type)
}
def CharSequence generateType(JvmDeclaredType type, GeneratorConfig config) {
val importManager = createImportManager(type)
val bodyAppendable = createAppendable(type, importManager, config)
bodyAppendable.openScope
bodyAppendable.assignThisAndSuper(type, config)
generateBody(type, bodyAppendable, config)
bodyAppendable.closeScope
val importAppendable = createAppendable(type, importManager, config)
generateFileHeader(type, importAppendable, config)
if (type.packageName !== null) {
importAppendable.append("package ").append(type.packageName).append(";");
importAppendable.newLine.newLine
}
for(i: importManager.imports) {
importAppendable.append("import ").append(i).append(";").newLine
}
if (!importManager.imports.empty)
importAppendable.newLine
importAppendable.append(bodyAppendable)
return importAppendable
}
def dispatch ITreeAppendable generateBody(JvmGenericType it, ITreeAppendable appendable, GeneratorConfig config) {
generateJavaDoc(appendable, config)
val childAppendable = appendable.trace(it)
annotations.generateAnnotations(childAppendable, true, config)
generateModifier(childAppendable, config)
if (isInterface) {
childAppendable.append("interface ")
} else {
childAppendable.append("class ")
}
childAppendable.traceSignificant(it).append(simpleName.makeJavaIdentifier)
generateTypeParameterDeclaration(childAppendable, config)
if (typeParameters.empty)
childAppendable.append(" ")
generateExtendsClause(childAppendable, config)
generateMembersInBody(childAppendable, config)
if(!anonymous && !(it.eContainer instanceof JvmType))
appendable.newLine
}
def generateMembersInBody(JvmDeclaredType it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.append('{').increaseIndentation
appendable.forEach(membersToBeCompiled, [
separator = memberSeparator()
], [
val memberAppendable = appendable.traceWithComments(it)
memberAppendable.openScope
generateMember(memberAppendable, config)
memberAppendable.closeScope
])
appendable.decreaseIndentation.newLine.append('}')
}
/**
* @since 2.27
*/
protected def (ITreeAppendable)=>ITreeAppendable memberSeparator() {
[
ITreeAppendable it |
blankLine
]
}
/**
* @deprecated Additional annotations should be created in the JVM model.
*/
@Deprecated
def generateAnnotationsWithSyntheticSuppressWarnings(JvmDeclaredType it, ITreeAppendable appendable, GeneratorConfig config) {
val noSuppressWarningsFilter = [JvmAnnotationReference it | annotation?.identifier != SuppressWarnings.name]
annotations.filter(noSuppressWarningsFilter).generateAnnotations(appendable, true, config)
if (it.eContainer === null)
appendable.append("@SuppressWarnings(\"all\")").newLine
}
def dispatch ITreeAppendable generateBody(JvmEnumerationType it, ITreeAppendable appendable, GeneratorConfig config) {
generateJavaDoc(appendable, config)
val childAppendable = appendable.trace(it)
annotations.generateAnnotations(childAppendable, true, config)
generateModifier(childAppendable, config)
childAppendable.append("enum ")
childAppendable.traceSignificant(it).append(simpleName.makeJavaIdentifier)
childAppendable.append(" ")
generateExtendsClause(childAppendable, config)
childAppendable.append("{").increaseIndentation
childAppendable.forEach(literals, [
separator = [
ITreeAppendable it |
append(',').blankLine
]
suffix = ';'
], [
generateEnumLiteral(childAppendable.trace(it), config)
])
childAppendable.forEach(membersToBeCompiled.filter[!(it instanceof JvmEnumerationLiteral)], [
separator = memberSeparator()
], [
generateMember(childAppendable.trace(it), config)
])
childAppendable.decreaseIndentation.newLine.append("}")
if(!(it.eContainer instanceof JvmType))
appendable.newLine
}
def void generateEnumLiteral(JvmEnumerationLiteral it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.newLine
generateJavaDoc(appendable, config)
annotations.generateAnnotations(appendable, true, config)
appendable.append(simpleName.makeJavaIdentifier)
// TODO: constructor args
}
def dispatch ITreeAppendable generateBody(JvmAnnotationType it, ITreeAppendable appendable, GeneratorConfig config) {
generateJavaDoc(appendable, config)
val childAppendable = appendable.trace(it)
annotations.generateAnnotations(childAppendable, true, config)
generateModifier(childAppendable, config)
childAppendable.append("@interface ")
childAppendable.traceSignificant(it).append(simpleName.makeJavaIdentifier)
childAppendable.append(" {")
for (innerType: membersToBeCompiled.filter(JvmDeclaredType)) {
val innerTypeAppendable = childAppendable.trace(innerType)
innerTypeAppendable.increaseIndentation
generateMember(innerType, innerTypeAppendable, config)
innerTypeAppendable.decreaseIndentation
}
for (operation : membersToBeCompiled.filter(JvmOperation)) {
generateAnnotationMethod(operation, childAppendable, config)
}
childAppendable.newLine.append("}")
if(!(it.eContainer instanceof JvmType))
appendable.newLine
}
def generateAnnotationMethod(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.increaseIndentation.newLine
appendable.openScope
generateJavaDoc(appendable, config)
val tracedAppendable = appendable.trace(it)
annotations.generateAnnotations(tracedAppendable, true, config)
generateModifier(tracedAppendable, config)
returnType.serializeSafely("Object", tracedAppendable)
tracedAppendable.append(" ")
tracedAppendable.traceSignificant(it).append(simpleName.makeJavaIdentifier)
tracedAppendable.append("()")
generateDefaultExpression(tracedAppendable, config)
tracedAppendable.append(";")
appendable.decreaseIndentation
appendable.closeScope
}
def void generateDefaultExpression(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
if (compilationStrategy !== null) {
appendable.append(" default ")
appendable.increaseIndentation
compilationStrategy.apply(appendable)
appendable.decreaseIndentation
} else if (compilationTemplate !== null) {
appendable.append(" default ").increaseIndentation
appendCompilationTemplate(appendable, it)
appendable.decreaseIndentation
} else if (config.generateExpressions) {
val body = associatedExpression
if(body !== null) {
if(body.hasErrors()) {
appendable.append("/* skipped default expression with errors */")
} else {
appendable.append(" default ")
compiler.compileAsJavaExpression(body, appendable, returnType)
}
} else if (defaultValue !== null) {
if(defaultValue.hasErrors()) {
appendable.append("/* skipped default expression with errors */")
} else {
appendable.append(" default ")
defaultValue.toJavaLiteral(appendable, config)
}
}
}
}
/**
* @since 2.16
*/
protected def void appendCompilationTemplate(ITreeAppendable appendable, JvmIdentifiableElement it) {
switch appendable {
TreeAppendable: {
val target = createImportingStringConcatenation(appendable.state, new StandardTypeReferenceOwner(commonServices, it));
target.append(compilationTemplate)
appendable.append(target)
}
default: {
throw new IllegalStateException("unexpected appendable: " + appendable.getClass.name)
}
}
}
protected def ImportingStringConcatenation createImportingStringConcatenation(SharedAppendableState state, ITypeReferenceOwner owner) {
return new ImportingStringConcatenation(state, owner)
}
def dispatch generateModifier(JvmGenericType it, ITreeAppendable appendable, GeneratorConfig config) {
generateVisibilityModifier(it, appendable)
if (!interface) {
if (isStatic && !isDeclaredWithinInterface)
appendable.append("static ")
if (isAbstract)
appendable.append("abstract ")
}
if (isFinal)
appendable.append("final ")
if (isStrictFloatingPoint)
appendable.append("strictfp ")
}
def private boolean isDeclaredWithinInterface (JvmMember it) {
return declaringType instanceof JvmGenericType && (declaringType as JvmGenericType).isInterface
}
def dispatch generateModifier(JvmDeclaredType it, ITreeAppendable appendable, GeneratorConfig config) {
generateVisibilityModifier(it, appendable)
}
def dispatch generateModifier(JvmField it, ITreeAppendable appendable, GeneratorConfig config) {
generateVisibilityModifier(it, appendable)
if (isStatic)
appendable.append("static ")
if (isFinal)
appendable.append("final ")
if (isTransient)
appendable.append("transient ")
if (isVolatile)
appendable.append("volatile ")
}
def dispatch generateModifier(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
generateVisibilityModifier(it, appendable)
if (isAbstract && !isDeclaredWithinInterface)
appendable.append("abstract ")
if (isStatic)
appendable.append("static ")
if (!isAbstract && !isStatic && config.getJavaSourceVersion.isAtLeast(JAVA8)
&& isDeclaredWithinInterface)
appendable.append("default ")
if (isFinal)
appendable.append("final ")
if (isSynchronized)
appendable.append("synchronized ")
if (isStrictFloatingPoint)
appendable.append("strictfp ")
if (isNative)
appendable.append("native ")
}
def generateVisibilityModifier(JvmMember it, ITreeAppendable result) {
result.append(visibility.javaName)
}
def dispatch generateModifier(JvmConstructor it, ITreeAppendable appendable, GeneratorConfig config) {
generateVisibilityModifier(it, appendable)
}
/**
* Returns the visibility modifier and a space as suffix if not empty
*/
def javaName(JvmVisibility visibility) {
if (visibility !== null)
return switch visibility {
case JvmVisibility.PRIVATE : 'private '
case JvmVisibility.PUBLIC : 'public '
case JvmVisibility.PROTECTED : 'protected '
case JvmVisibility.DEFAULT : ''
}
else
return ''
}
def void generateExtendsClause(JvmDeclaredType it, ITreeAppendable appendable, GeneratorConfig config) {
val implicitSuperType = switch it {
JvmAnnotationType: 'java.lang.Annotation'
JvmEnumerationType: "java.lang.Enum<"+identifier+">"
default: 'java.lang.Object'
}
if (it instanceof JvmAnnotationType || (it instanceof JvmGenericType && (it as JvmGenericType).isInterface)) {
val withoutObject = superTypes.filter [ typeRef | typeRef.identifier != implicitSuperType ]
appendable.forEachSafely(withoutObject, [
prefix = 'extends ' separator = ', ' suffix = ' '
], [
it, app | serializeSafely(app)
])
} else {
val withoutObject = superTypes.filter [ typeRef | typeRef.identifier != implicitSuperType ]
val superClazz = withoutObject.filter(typeRef | typeRef.type instanceof JvmGenericType && !(typeRef.type as JvmGenericType).isInterface).head
val superInterfaces = withoutObject.filter(typeRef | typeRef != superClazz)
if (superClazz !== null) {
val hasErrors = superClazz.hasErrors()
if(hasErrors)
appendable.append('/* ')
try {
appendable.append('extends ')
superClazz.serializeSafely(appendable)
appendable.append(' ')
} catch (Exception ignoreMe) {}
if(hasErrors)
appendable.append(' */')
}
appendable.forEachSafely(superInterfaces, [
prefix = 'implements ' separator = ', ' suffix = ' '
], [
it, app | serializeSafely(app)
])
}
}
def dispatch ITreeAppendable generateMember(JvmMember it, ITreeAppendable appendable, GeneratorConfig config) {
throw new UnsupportedOperationException("generateMember not implemented for elements of type "
+ it?.^class?.name
)
}
def dispatch generateMember(JvmDeclaredType it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.newLine
appendable.openScope
appendable.assignThisAndSuper(it, config)
try {
generateBody(it, appendable, config)
} finally {
appendable.closeScope
}
}
def dispatch generateMember(JvmField it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.newLine
generateJavaDoc(appendable, config)
val tracedAppendable = appendable.trace(it)
annotations.generateAnnotations(tracedAppendable, true, config)
generateModifier(tracedAppendable, config)
type.serializeSafely("Object", tracedAppendable)
tracedAppendable.append(" ")
val name = tracedAppendable.declareVariable(it, simpleName.makeJavaIdentifier)
tracedAppendable.traceSignificant(it).append(name)
generateInitialization(tracedAppendable, config)
tracedAppendable.append(";")
}
def dispatch generateMember(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.newLine
appendable.openScope
generateJavaDoc(appendable, config)
val tracedAppendable = appendable.trace(it)
annotations.generateAnnotations(tracedAppendable, true, config)
generateModifier(tracedAppendable, config)
generateTypeParameterDeclaration(tracedAppendable, config)
if (returnType === null) {
tracedAppendable.append("void")
} else {
returnType.serializeSafely("Object", tracedAppendable)
}
tracedAppendable.append(" ")
tracedAppendable.traceSignificant(it).append(simpleName.makeJavaIdentifier)
tracedAppendable.append("(")
generateParameters(tracedAppendable, config)
tracedAppendable.append(")")
generateThrowsClause(tracedAppendable, config)
if (isAbstract || !hasBody) {
tracedAppendable.append(";")
} else {
tracedAppendable.append(" ")
generateExecutableBody(tracedAppendable, config)
}
appendable.closeScope
appendable
}
def dispatch generateMember(JvmConstructor it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.newLine
appendable.openScope
generateJavaDoc(appendable, config)
val tracedAppendable = appendable.trace(it)
annotations.generateAnnotations(tracedAppendable, true, config)
generateModifier(tracedAppendable, config)
generateTypeParameterDeclaration(tracedAppendable, config)
tracedAppendable.traceSignificant(it).append(simpleName.makeJavaIdentifier)
tracedAppendable.append("(")
generateParameters(tracedAppendable, config)
tracedAppendable.append(")")
generateThrowsClause(tracedAppendable, config)
tracedAppendable.append(" ")
generateExecutableBody(tracedAppendable, config)
appendable.closeScope
appendable
}
def void generateInitialization(JvmField it, ITreeAppendable appendable, GeneratorConfig config) {
if (compilationStrategy !== null) {
val errors = directErrorsOrLogicallyContainedErrors
if (errors.isEmpty) {
appendable.append(" = ")
appendable.increaseIndentation
compilationStrategy.apply(appendable)
appendable.decreaseIndentation
} else {
appendable.append(" /* Skipped initializer because of errors */")
}
} else if (compilationTemplate !== null) {
val errors = directErrorsOrLogicallyContainedErrors
if (errors.isEmpty) {
appendable.append(" = ").increaseIndentation
appendCompilationTemplate(appendable, it)
appendable.decreaseIndentation
} else {
appendable.append(" /* Skipped initializer because of errors */")
}
} else {
val expression = associatedExpression
if (expression !== null && config.generateExpressions) {
if(expression.hasErrors()) {
appendable.append(" /* Skipped initializer because of errors */")
} else {
appendable.append(" = ")
compiler.compileAsJavaExpression(expression, appendable, type)
}
}
}
}
def void generateTypeParameterDeclaration(JvmTypeParameterDeclarator it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEach(typeParameters, [
prefix = '<' separator = ', ' suffix = '> '
], [
it| generateTypeParameterDeclaration(appendable, config)
])
}
def void generateTypeParameterDeclaration(JvmTypeParameter it, ITreeAppendable appendable, GeneratorConfig config) {
val tracedAppendable = appendable.trace(it)
tracedAppendable.traceSignificant(it).append(name)
generateTypeParameterConstraints(tracedAppendable, config)
}
def void generateTypeParameterConstraints(JvmTypeParameter it, ITreeAppendable appendable, GeneratorConfig config) {
val upperBounds = constraints.filter(JvmUpperBound)
appendable.forEachSafely(upperBounds, [
prefix = ' extends ' separator = ' & '
], [
it, app | typeReference.serializeSafely(app)
])
}
def void generateThrowsClause(JvmExecutable it, ITreeAppendable appendable, GeneratorConfig config) {
val toBeGenerated = <JvmType, JvmTypeReference> newLinkedHashMap
exceptions.forEach[if(!toBeGenerated.containsKey(type)) toBeGenerated.put(type, it)]
appendable.forEachSafely(toBeGenerated.values, [
prefix = ' throws ' separator = ', '
], [
it, app | app.trace(it).append(it.type)
])
}
def void generateParameters(JvmExecutable it, ITreeAppendable appendable, GeneratorConfig config) {
if (!parameters.isEmpty) {
for (i : 0 .. parameters.size - 1) {
val last = i + 1 == parameters.size
val p = parameters.get(i)
p.generateParameter(appendable, last && it.varArgs, config)
if (!last)
appendable.append(", ")
}
}
}
def void generateParameter(JvmFormalParameter it, ITreeAppendable appendable, boolean vararg, GeneratorConfig config) {
val tracedAppendable = appendable.trace(it)
annotations.generateAnnotations(tracedAppendable, false, config)
tracedAppendable.append("final ")
if (vararg) {
if (! (parameterType instanceof JvmGenericArrayTypeReference)) {
tracedAppendable.append("/* Internal Error: Parameter was vararg but not an array type. */");
} else {
(parameterType as JvmGenericArrayTypeReference).componentType.serializeSafely("Object", tracedAppendable)
}
tracedAppendable.append("...")
} else {
parameterType.serializeSafely("Object", tracedAppendable)
}
tracedAppendable.append(" ")
val name = tracedAppendable.declareVariable(it, makeJavaIdentifier(simpleName))
tracedAppendable.traceSignificant(it).append(name)
}
def hasBody(JvmExecutable it) {
compilationTemplate !== null || compilationStrategy !== null || associatedExpression !== null
}
/**
* Returns the errors that are produced for elements that are directly contained
* in this feature (e.g. unresolved type proxies) or that are associated with
* the expression that may be logically contained in the given feature.
*/
private def getDirectErrorsOrLogicallyContainedErrors(JvmFeature feature) {
var errors = feature.errors
if(errors.empty) {
val expression = feature.associatedExpression
if (expression !== null) {
errors = expression.errors
}
}
return errors
}
def void generateExecutableBody(JvmExecutable op, ITreeAppendable appendable, GeneratorConfig config) {
if (op.compilationStrategy !== null) {
var errors = op.directErrorsOrLogicallyContainedErrors
if(errors.empty) {
appendable.increaseIndentation.append("{").newLine
op.compilationStrategy.apply(appendable)
appendable.decreaseIndentation.newLine.append("}")
} else {
op.generateBodyWithIssues(appendable, errors)
}
} else if (op.compilationTemplate !== null) {
val errors = op.directErrorsOrLogicallyContainedErrors
if(errors.empty) {
appendable.increaseIndentation.append("{").newLine
appendCompilationTemplate(appendable, op)
appendable.decreaseIndentation.newLine.append("}")
} else {
op.generateBodyWithIssues(appendable, errors)
}
} else {
val expression = op.getAssociatedExpression
if (expression !== null && config.generateExpressions) {
val errors = expression.errors
if(errors.empty) {
val returnType = switch(op) {
JvmOperation: op.returnType
JvmConstructor: Void.TYPE.getTypeForName(op)
default: null
}
appendable.append("{").increaseIndentation
compile(op, expression, returnType, appendable, config)
appendable.decreaseIndentation.newLine.append("}")
} else {
op.generateBodyWithIssues(appendable, errors)
}
} else if(op instanceof JvmOperation) {
appendable.increaseIndentation.append("{").newLine
appendable.append('throw new UnsupportedOperationException("')
appendable.append(op.simpleName)
appendable.append(' is not implemented");')
appendable.decreaseIndentation.newLine.append("}")
} else if (op instanceof JvmConstructor) {
appendable.append("{").newLine.append("}")
}
}
}
def compile(JvmExecutable executable, XExpression expression, JvmTypeReference returnType, ITreeAppendable appendable, GeneratorConfig config) {
compiler.compile(expression, appendable, returnType, executable.exceptions.toSet)
}
def void assignThisAndSuper(ITreeAppendable b, JvmDeclaredType declaredType, GeneratorConfig config) {
b.reassignSuperType(declaredType, config)
b.reassignThisType(declaredType)
}
private def reassignSuperType(ITreeAppendable b, JvmDeclaredType declaredType, GeneratorConfig config) {
val superType = declaredType.extendedClass?.type
if (b.hasObject('super')) {
/*
* class A {}
* class B extends A { B.super.toString() }
* B.super resolves to A though the name is computed from the current 'this'
*/
val element = b.getObject('this') // if we have a super, there must be a 'this'
if (element instanceof JvmDeclaredType) {
val superElement = b.getObject('super')
// Don't reassign the super of the enclosing type if it has already been reassigned
val superVariable = b.getName(superElement)
if ('super'.equals(superVariable)) {
val proposedName = element.simpleName + '.super'
b.declareVariable(superElement, proposedName)
}
}
}
if (config.getJavaSourceVersion.isAtLeast(JAVA8)) {
for (JvmTypeReference interfaceRef : declaredType.extendedInterfaces) {
val interfaze = interfaceRef.type
val simpleVarName = interfaze.simpleName + '.super'
if (b.hasObject(simpleVarName)) {
val element = b.getObject(simpleVarName)
if (element != interfaceRef) {
// This can happen when the type extends multiple interfaces with the same simple name
val qualifiedVarName = interfaze.qualifiedName + '.super'
b.declareVariable(interfaze, qualifiedVarName)
}
} else {
b.declareVariable(interfaze, simpleVarName)
}
}
}
if (superType !== null)
b.declareVariable(superType, 'super');
}
protected def reassignThisType(ITreeAppendable b, JvmDeclaredType declaredType) {
if (b.hasObject('this')) {
val element = b.getObject('this')
if (element instanceof JvmDeclaredType) {
if (element.local) {
b.declareVariable(element, '')
} else {
val proposedName = element.simpleName+'.this'
b.declareVariable(element, proposedName)
}
}
}
if (declaredType !== null)
b.declareVariable(declaredType, 'this');
}
/**
* @since 2.16
*/
def generateBodyWithIssues(JvmExecutable op, ITreeAppendable appendable, Iterable<Issue> errors) {
generateBodyWithIssues(appendable, errors)
}
def generateBodyWithIssues(ITreeAppendable appendable, Iterable<Issue> errors) {
appendable.append('{').increaseIndentation.newLine
.append('throw new Error("Unresolved compilation problems:"')
appendable.increaseIndentation
errors.forEach[appendable.newLine.append('+ "\\n').append(doConvertToJavaString(message)).append('"')]
appendable.append(');').decreaseIndentation.decreaseIndentation.newLine
.append('}')
}
/**
* Convert a given input string to a Java string. Non-ascii characters will
* be replaced by a unicode escape sequence by default.
*/
protected def doConvertToJavaString(String input) {
Strings.convertToJavaString(input, true)
}
def void generateFileHeader(JvmDeclaredType it, ITreeAppendable appendable, GeneratorConfig config) {
val fileHeaderAdapter = it.eAdapters.filter(FileHeaderAdapter).head
if (fileHeaderAdapter !== null && !fileHeaderAdapter.headerText.nullOrEmpty) {
generateDocumentation(fileHeaderAdapter.headerText, fileHeaderProvider.getFileHeaderNodes(eResource),
appendable, config)
}
}
def void generateJavaDoc(EObject it, ITreeAppendable appendable, GeneratorConfig config) {
val adapter = it.eAdapters.filter(DocumentationAdapter).head
if(adapter !== null && !adapter.documentation.nullOrEmpty) {
// TODO we should track the source of the documentation in the documentation adapter
val sourceElements = it.sourceElements
if (sourceElements.size == 1 && documentationProvider instanceof IEObjectDocumentationProviderExtension) {
val documentationNodes = (documentationProvider as IEObjectDocumentationProviderExtension).getDocumentationNodes(sourceElements.head)
addJavaDocImports(appendable, documentationNodes)
generateDocumentation(adapter.documentation, documentationNodes, appendable, config)
} else {
generateDocumentation(adapter.documentation, emptyList, appendable, config)
}
}
}
def addJavaDocImports(EObject it, ITreeAppendable appendable,List<INode> documentationNodes) {
for(node : documentationNodes){
for(region : javaDocTypeReferenceProvider.computeTypeRefRegions(node)) {
val text = region.text
if(text !== null && text.length > 0){
val fqn = qualifiedNameConverter.toQualifiedName(text)
val context = NodeModelUtils.findActualSemanticObjectFor(node)
if(fqn.segmentCount == 1 && context !== null){
val scope = scopeProvider.getScope(context, JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE)
val candidate = scope.getSingleElement(fqn)
if(candidate !== null) {
val jvmType = (
if(candidate.EObjectOrProxy.eIsProxy)
EcoreUtil.resolve(candidate.EObjectOrProxy, context)
else
candidate.EObjectOrProxy
) as JvmType
if(jvmType instanceof JvmDeclaredType && !jvmType.eIsProxy) {
val referencedType = jvmType as JvmDeclaredType
val contextDeclarator = EcoreUtil2.getContainerOfType(it,JvmDeclaredType)
if(referencedType.packageName != contextDeclarator.packageName){
val importManager = getImportManager(appendable)
importManager.addImportFor(jvmType)
}
}
}
}
}
}
}
}
def getImportManager(ITreeAppendable appendable) {
(appendable as TreeAppendable).importManager
}
def protected generateDocumentation(String text, List<INode> documentationNodes, ITreeAppendable appendable, GeneratorConfig config) {
val doc = new StringConcatenation
doc.append("/**")
doc.newLine
doc.append(" * ")
doc.append(text, " * ")
doc.newLine
doc.append(" */")
if (!documentationNodes.empty) {
var documentationTrace = ITextRegionWithLineInformation.EMPTY_REGION
for(node: documentationNodes) {
documentationTrace = documentationTrace.merge(node.textRegionWithLineInformation)
}
appendable.trace(new LocationData(documentationTrace, null)).append(doc.toString)
appendable.newLine
} else {
appendable.append(doc.toString).newLine
}
}
def void generateAnnotations(Iterable<JvmAnnotationReference> annotations, ITreeAppendable appendable, boolean withLineBreak, GeneratorConfig config) {
val sep = [ITreeAppendable it | if(withLineBreak) newLine else append(' ') ]
appendable.forEachSafely(annotations, [
separator = sep suffix = sep
], [
it, app | it.generateAnnotation(app, config)
])
}
def void generateAnnotation(JvmAnnotationReference it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.append("@")
appendable.append(annotation)
appendable.forEach(explicitValues, [
prefix = '(' separator = ', ' suffix = ')'
], [
toJava(appendable, config)
])
}
def void toJava(JvmAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
if (operation !== null) {
if (operation.simpleName === null) {
return
}
appendable.append(operation.simpleName);
appendable.append(" = ")
} else if ((eContainer as JvmAnnotationReference).explicitValues.size > 1) { // more than one value
appendable.append("value = ")
}
toJavaLiteral(appendable, config)
}
def dispatch void toJavaLiteral(JvmAnnotationAnnotationValue value, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(value.values, [generateAnnotation(appendable, config)])
}
def dispatch void toJavaLiteral(JvmShortAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [appendable.append(toString)])
}
def dispatch void toJavaLiteral(JvmIntAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [appendable.append(toString)])
}
def dispatch void toJavaLiteral(JvmLongAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [appendable.append(toString)])
}
def dispatch void toJavaLiteral(JvmByteAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [appendable.append(toString)])
}
def dispatch void toJavaLiteral(JvmDoubleAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(
switch(it) {
case Double.isNaN(it) : "Double.NaN"
case Double.POSITIVE_INFINITY : "Double.POSITIVE_INFINITY"
case Double.NEGATIVE_INFINITY : "Double.NEGATIVE_INFINITY"
default: toString + "d"
})
])
}
def dispatch void toJavaLiteral(JvmFloatAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(
switch(it) {
case Float.isNaN(it) : "Float.NaN"
case Float.POSITIVE_INFINITY : "Float.POSITIVE_INFINITY"
case Float.NEGATIVE_INFINITY : "Float.NEGATIVE_INFINITY"
default: toString + "f"
})
])
}
def dispatch void toJavaLiteral(JvmCharAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append("'" + doConvertToJavaString(toString) + "'")
])
}
def dispatch void toJavaLiteral(JvmStringAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append('"' + doConvertToJavaString(toString) + '"')
])
}
def dispatch void toJavaLiteral(JvmTypeAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(type).append(".class")
])
}
def dispatch void toJavaLiteral(JvmEnumAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(declaringType)
appendable.append(".")
appendable.append(simpleName.makeJavaIdentifier)
])
}
def dispatch void toJavaLiteral(JvmBooleanAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(toString)
])
}
def dispatch void toJavaLiteral(JvmCustomAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
if(values.isEmpty)
appendable.append('{}')
else
appendable.forEachWithShortcut(values.filter(XExpression), [
compiler.toJavaExpression(it, appendable)
])
}
def TreeAppendable createAppendable(EObject context, ImportManager importManager, GeneratorConfig config) {
val cachingConverter = new ITraceURIConverter() {
Map<URI, SourceRelativeURI> uriForTraceCache = newHashMap();
override SourceRelativeURI getURIForTrace(IProjectConfig config, AbsoluteURI uri) {
if (!uriForTraceCache.containsKey(uri.getURI)) {
val result = converter.getURIForTrace(config, uri);
uriForTraceCache.put(uri.getURI, result);
}
return uriForTraceCache.get(uri.getURI);
}
override SourceRelativeURI getURIForTrace(Resource resource) {
if (!uriForTraceCache.containsKey(resource.getURI)) {
val result = converter.getURIForTrace(resource);
uriForTraceCache.put(resource.getURI, result);
}