• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moosetechnology / Famix-Value / 15720287171

17 Jun 2025 11:37PM UTC coverage: 22.717% (-0.02%) from 22.736%
15720287171

push

github

Gabriel-Darbord
Adopt mooseNameWithDots deprecation

0 of 14 new or added lines in 3 files covered. (0.0%)

1 existing line in 1 file now uncovered.

796 of 3504 relevant lines covered (22.72%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.76
/src/Famix-Value-Exporter/FamixValue2FASTJavaVisitor.class.st
1
"
2
A visitor to export a FamixValue model to FASTJava.
3
The generated code is a block statement with a variable declared for each value.
4

5
Object attributes are initialized by finding the corresponding setters.
6
Collections and Dictionaries are constructed with the methods `add(element)` and `put(key, value)` respectively.
7

8
When exporting a `FamixValueOfObjectAttribute`, if the setter for its `FamixTAttribute` cannot be found, it is set using the Java Reflection API.
9
All attributes that fall into this category are added as `markedForReflection`.
10
"
11
Class {
12
        #name : 'FamixValue2FASTJavaVisitor',
13
        #superclass : 'FamixValue2ASTVisitor',
14
        #instVars : [
15
                'builder',
16
                'constructorCache',
17
                'staticAttributesCache',
18
                'objectExportStrategy',
19
                'reflections',
20
                'defaultHelperPackage'
21
        ],
22
        #category : 'Famix-Value-Exporter-Visitors',
23
        #package : 'Famix-Value-Exporter',
24
        #tag : 'Visitors'
25
}
26

27
{ #category : 'private' }
28
FamixValue2FASTJavaVisitor >> addAttributesFrom: object asArgumentsTo: invocation usingConstructor: constructor [
×
29

×
30
        (constructorCache
×
31
                 at: constructor
×
32
                 ifAbsentPut: [ constructor mapConstructorParametersToAttributes ])
×
33
                withIndexDo: [ :paramAttribute :index |
×
34
                        invocation addArgument: (paramAttribute
×
35
                                         ifNil: [ "constructor parameter is not mapped to an attribute
×
36
                                                => give it a default value"
×
37
                                                 (constructor parameters at: index) declaredType
×
38
                                                         ifNil: [ "TODO fix Java parser
×
39
                                                                if it's unknown then it must be a class, otherwise the parser would have found it
×
40
                                                                => the default for class instances is `null`"
×
41
                                                                 self model newNullLiteral ]
×
42
                                                         ifNotNil: [ :declaredType |
×
43
                                                         declaredType asFASTJavaDefaultValueOn: self model ] ]
×
44
                                         ifNotNil: [ "parameter is mapped to attribute"
×
45
                                                 object value
×
46
                                                         detect: [ :objAttribute | "find the matching value attribute"
×
47
                                                                 objAttribute attribute == paramAttribute ]
×
48
                                                         ifFound: [ :objAttribute | "dispatch for var naming context"
×
49
                                                                 objectExportStrategy
×
50
                                                                         makeVariableExpression: objAttribute value
×
51
                                                                         on: self ]
×
52
                                                         ifNone: [ "the object does not have the attribute set"
×
53
                                                                 paramAttribute declaredType asFASTJavaDefaultValueOn:
×
54
                                                                         self model ] ]) ]
×
55
]
×
56

57
{ #category : 'accessing' }
58
FamixValue2FASTJavaVisitor >> builder [
1✔
59

1✔
60
        ^ builder ifNil: [ builder := FASTJavaBuilder new model: self model ]
1✔
61
]
1✔
62

63
{ #category : 'private' }
64
FamixValue2FASTJavaVisitor >> constructObject: object [
1✔
65
        "Build code to instantiate the object and store it in a variable"
1✔
66

1✔
67
        | constructor invocation |
1✔
68
        constructor := self findConstructorFor: object.
1✔
69

1✔
70
        "make the constructor invocation"
1✔
71
        invocation := self reflections
1✔
72
                              at: constructor
1✔
73
                              ifPresent: [ :varName | "use reflection: var contains the Constructor object"
1✔
74
                                      self
1✔
75
                                              makeReflectionConstructorInvocation: varName
1✔
76
                                              for: object ]
1✔
77
                              ifAbsent: [ "regular use of the constructor"
1✔
78
                                      self
1✔
79
                                              makeConstructorInvocation: constructor
1✔
80
                                              for: object ].
1✔
81

1✔
82
        "add arguments to match constructor parameters"
1✔
83
        constructor parameters ifNotEmpty: [
1✔
84
                self
1✔
85
                        addAttributesFrom: object
1✔
86
                        asArgumentsTo: invocation
1✔
87
                        usingConstructor: constructor ]
1✔
88
]
1✔
89

90
{ #category : 'exporting' }
91
FamixValue2FASTJavaVisitor >> defaultHelperPackage [
×
92

×
93
        ^ defaultHelperPackage ifNil: [ defaultHelperPackage := 'org.modest' ]
×
94
]
×
95

96
{ #category : 'exporting' }
97
FamixValue2FASTJavaVisitor >> defaultHelperPackage: aString [
×
98

×
99
        defaultHelperPackage := aString
×
100
]
×
101

102
{ #category : 'private' }
103
FamixValue2FASTJavaVisitor >> ensureReflection [
×
104
        "Ensure that the infrastructure needed to use reflection is generated.
×
105
        A static method called `initializeReflection` is created and will be populated by calls to `ensureReflectionField:` and `ensureReflectionConstructor:`.
×
106
        To handle exceptions that can be thrown by these operations, a static initializer calls this method with a trycatch.
×
107
        Finally, the convenience methods `setField` and `instantiate` are created to handle exceptions during reflexive operations: .
×
108
        Return the statement block of `initializeReflection` for callers to populate."
×
109

×
110
        | declarations declaration initStatementBlock |
×
111
        declarations := self objectExportStrategy currentDeclarations. "does not work for inline strategy..."
×
112

×
113
        "check if already defined and return the body of initializeReflection()"
×
114
        declarations at: -3 ifPresent: [ :decl | ^ decl statementBlock ].
×
115

×
116
        "static {
×
117
                try {
×
118
                        initializeReflection();
×
119
                } catch (Exception e) {
×
120
                        new RuntimeException(e);
×
121
                }
×
122
        }"
×
123
        declaration := self model newInitializer isStatic: true.
×
124
        declaration cacheAt: #order put: -4.
×
125
        declarations at: -4 put: declaration.
×
126
        declaration statementBlock:
×
127
                (model newStatementBlock statements: { (model newTryCatchStatement
×
128
                                  try: (model newStatementBlock statements:
×
129
                                                           { (model newExpressionStatement expression:
×
130
                                                                            (model newMethodInvocation name: 'initializeReflection')) });
×
131
                                  catches: { (model newCatchPartStatement
×
132
                                                   catchedTypes: { (model newClassTypeExpression typeName:
×
133
                                                                            (model newTypeName name: 'Exception')) };
×
134
                                                   body: (model newStatementBlock statements:
×
135
                                                                            { (model newThrowStatement expression:
×
136
                                                                                             (model newNewExpression
×
137
                                                                                                      type: (model newClassTypeExpression typeName:
×
138
                                                                                                                               (model newTypeName name: 'RuntimeException'));
×
139
                                                                                                      arguments:
×
140
                                                                                                              { (model newVariableExpression name: 'e') })) });
×
141
                                                   parameter: (model newVariableExpression name: 'e')) }) }).
×
142

×
143
        "private static void initializeReflection() throws NoSuchFieldException, NoSuchMethodException, SecurityException {
×
144
                // fields and constructors will be obtained and set as accessible here
×
145
        }"
×
146
        declaration := model newMethodEntity name: 'initializeReflection'.
×
147
        declaration cacheAt: #order put: -3.
×
148
        declarations at: -3 put: declaration.
×
149
        declaration
×
150
                type: (model newVoidTypeExpression name: 'void');
×
151
                modifiers: {
×
152
                                (model newModifier token: 'private').
×
153
                                (model newModifier token: 'static') };
×
154
                throws: {
×
155
                                (model newClassTypeExpression typeName:
×
156
                                                 (model newTypeName name: 'NoSuchFieldException')).
×
157
                                (model newClassTypeExpression typeName:
×
158
                                                 (model newTypeName name: 'NoSuchMethodException')).
×
159
                                (model newClassTypeExpression typeName:
×
160
                                                 (model newTypeName name: 'SecurityException')) };
×
161
                statementBlock: (initStatementBlock := model newStatementBlock).
×
162

×
163
        "public static void setField(Object object, Field field, Object value) {
×
164
                try {
×
165
                        field.set(object, value);
×
166
                } catch (IllegalAccessException e) {
×
167
                        throw new RuntimeException(e);
×
168
                }
×
169
        }"
×
170
        declaration := model newMethodEntity name: 'setField'.
×
171
        declaration cacheAt: #order put: -2.
×
172
        declarations at: -2 put: declaration.
×
173
        declaration
×
174
                type: (model newVoidTypeExpression name: 'void');
×
175
                parameters: {
×
176
                                (model newParameter
×
177
                                         variable: (model newVariableExpression name: 'object');
×
178
                                         type: (model newClassTypeExpression typeName:
×
179
                                                                  (model newTypeName name: 'Object'))).
×
180
                                (model newParameter
×
181
                                         variable: (model newVariableExpression name: 'field');
×
182
                                         type: (model newClassTypeExpression typeName:
×
183
                                                                  (model newTypeName name: 'Field'))).
×
184
                                (model newParameter
×
185
                                         variable: (model newVariableExpression name: 'value');
×
186
                                         type: (model newClassTypeExpression typeName:
×
187
                                                                  (model newTypeName name: 'Object'))) };
×
188
                modifiers: {
×
189
                                (model newModifier token: 'public').
×
190
                                (model newModifier token: 'static') };
×
191
                statementBlock:
×
192
                        (model newStatementBlock statements: { (model newTryCatchStatement
×
193
                                          try: (model newStatementBlock statements:
×
194
                                                                   { (model newExpressionStatement expression:
×
195
                                                                                    (model newMethodInvocation
×
196
                                                                                             receiver: (model newVariableExpression name: 'field');
×
197
                                                                                             name: 'set';
×
198
                                                                                             arguments: {
×
199
                                                                                                             (model newVariableExpression name: 'object').
×
200
                                                                                                             (model newVariableExpression name: 'value') })) });
×
201
                                          catches: { (model newCatchPartStatement
×
202
                                                           catchedTypes: { (model newClassTypeExpression typeName:
×
203
                                                                                    (model newTypeName name: 'IllegalAccessException')) };
×
204
                                                           body: (model newStatementBlock statements:
×
205
                                                                                    { (model newThrowStatement expression:
×
206
                                                                                                     (model newNewExpression
×
207
                                                                                                              type: (model newClassTypeExpression typeName:
×
208
                                                                                                                                       (model newTypeName name: 'RuntimeException'));
×
209
                                                                                                              arguments:
×
210
                                                                                                                      { (model newVariableExpression name: 'e') })) });
×
211
                                                           parameter: (model newVariableExpression name: 'e')) }) }).
×
212

×
213
        "private static <T> T instantiate(Constructor<T> constructor, Object... arguments) {
×
214
                T instance;
×
215
                try {
×
216
                        instance = constructor.newInstance(arguments);
×
217
                } catch (Exception e) {
×
218
                        throw new RuntimeException(e);
×
219
                }
×
220
                return instance;
×
221
        }"
×
222
        declaration := model newMethodEntity name: 'instantiate'.
×
223
        declaration cacheAt: #order put: -1.
×
224
        declarations at: -1 put: declaration.
×
225
        declaration
×
226
                typeParameters: { (model newTypeParameterExpression name: 'T') };
×
227
                type:
×
228
                        (model newClassTypeExpression typeName:
×
229
                                         (model newTypeName name: 'T'));
×
230
                parameters: {
×
231
                                (model newParameter
×
232
                                         variable: (model newVariableExpression name: 'constructor');
×
233
                                         type: (model newClassTypeExpression
×
234
                                                          arguments: { (model newClassTypeExpression typeName:
×
235
                                                                                   (model newTypeName name: 'T')) };
×
236
                                                          typeName: (model newTypeName name: 'Constructor'))).
×
237
                                (model newParameter
×
238
                                         variable: (model newVariableExpression name: 'arguments');
×
239
                                         type: (model newClassTypeExpression typeName:
×
240
                                                                  (model newTypeName name: 'Object'));
×
241
                                         hasVariableArguments: true) };
×
242
                modifiers: {
×
243
                                (model newModifier token: 'private').
×
244
                                (model newModifier token: 'static') };
×
245
                statementBlock: (model newStatementBlock statements: {
×
246
                                         (model newVarDeclStatement
×
247
                                                  type:
×
248
                                                          (model newClassTypeExpression typeName:
×
249
                                                                           (model newTypeName name: 'T'));
×
250
                                                  declarators: { (model newVariableDeclarator variable:
×
251
                                                                           (model newVariableExpression name: 'instance')) }).
×
252
                                         (model newTryCatchStatement
×
253
                                                  try: (model newStatementBlock statements:
×
254
                                                                           { (model newExpressionStatement expression:
×
255
                                                                                            (model newAssignmentExpression
×
256
                                                                                                     variable:
×
257
                                                                                                             (model newVariableExpression name: 'instance');
×
258
                                                                                                     expression: (model newMethodInvocation
×
259
                                                                                                                      receiver:
×
260
                                                                                                                              (model newIdentifier name: 'constructor');
×
261
                                                                                                                      name: 'newInstance';
×
262
                                                                                                                      arguments:
×
263
                                                                                                                              { (model newVariableExpression name: 'arguments') });
×
264
                                                                                                     operator: '=')) });
×
265
                                                  catches: { (model newCatchPartStatement
×
266
                                                                   catchedTypes: { (model newClassTypeExpression typeName:
×
267
                                                                                            (model newTypeName name: 'Exception')) };
×
268
                                                                   body: (model newStatementBlock statements:
×
269
                                                                                            { (model newThrowStatement expression:
×
270
                                                                                                             (model newNewExpression
×
271
                                                                                                                      type: (model newClassTypeExpression typeName:
×
272
                                                                                                                                               (model newTypeName name: 'RuntimeException'));
×
273
                                                                                                                      arguments:
×
274
                                                                                                                              { (model newVariableExpression name: 'e') })) });
×
275
                                                                   parameter: (model newVariableExpression name: 'e')) }).
×
276
                                         (model newReturnStatement expression:
×
277
                                                  (model newVariableExpression name: 'instance')) }).
×
278

×
279
        ^ initStatementBlock
×
280
]
×
281

282
{ #category : 'private' }
283
FamixValue2FASTJavaVisitor >> ensureReflectionConstructor: aFamixJavaMethod [
×
284

×
285
        | declarations initStatementBlock varName |
×
286
        declarations := self objectExportStrategy currentDeclarations.
×
287
        initStatementBlock := self ensureReflection.
×
288
        varName := aFamixJavaMethod name asUppercase.
×
289
        self reflections at: aFamixJavaMethod put: varName.
×
290

×
291
        declarations
×
292
                at: varName
×
293
                ifPresent: [ "handle naming collisions, some ideas:
×
294
                                - use fully qualified type name (only when necessary?)
×
295
                                - use a HashMap to store the fields based on class and attribute name"
×
296
                        self shouldBeImplemented ]
×
297
                ifAbsentPut: [
×
298
                        model newVarDeclStatement
×
299
                                type: (model newClassTypeExpression
×
300
                                                 typeName: (model newTypeName name: 'Constructor');
×
301
                                                 arguments:
×
302
                                                         { (self builder referType: aFamixJavaMethod parentType) });
×
303
                                modifiers: {
×
304
                                                (model newModifier token: 'private').
×
305
                                                (model newModifier token: 'static') };
×
306
                                declarators: { (model newVariableDeclarator variable:
×
307
                                                 (model newVariableExpression name: varName)) } ].
×
308

×
309
        "add the code in `initializeReflection` to get the Field and set it as accessible:
×
310
        CLASSNAME = CLASS.class.getDeclaredConstructor(PARAMETER_CLASSES);
×
311
        CLASSNAME.setAccessible(true);"
×
312
        initStatementBlock addStatement:
×
313
                (model newExpressionStatement expression:
×
314
                         (model newAssignmentExpression
×
315
                                  variable: (model newVariableExpression name: varName);
×
316
                                  expression: (model newMethodInvocation
×
317
                                                   receiver: (model newClassProperty
×
318
                                                                    type:
×
319
                                                                            (self builder referType: aFamixJavaMethod parentType);
×
320
                                                                    fieldName: 'class');
×
321
                                                   name: 'getDeclaredConstructor';
×
322
                                                   arguments:
×
323
                                                           (aFamixJavaMethod sortedParameters collect: [ :parameter |
×
324
                                                                    model newClassProperty
×
325
                                                                            type: (self builder referType: parameter declaredType);
×
326
                                                                            fieldName: 'class' ]));
×
327
                                  operator: '=')).
×
328
        initStatementBlock addStatement:
×
329
                (model newExpressionStatement expression:
×
330
                         (model newMethodInvocation
×
331
                                  receiver: (model newVariableExpression name: varName);
×
332
                                  name: 'setAccessible';
×
333
                                  arguments: { (model newBooleanLiteral primitiveValue: 'true') }))
×
334
]
×
335

336
{ #category : 'private' }
337
FamixValue2FASTJavaVisitor >> ensureReflectionField: aFamixJavaAttribute [
×
338
        "Ensure an attribute exists to hold the java.lang.reflect.Field for the given attribute."
×
339

×
340
        ^ self reflections at: aFamixJavaAttribute ifAbsentPut: [
×
341
                  | declarations initStatementBlock varName |
×
342
                  declarations := self objectExportStrategy currentDeclarations.
×
343
                  initStatementBlock := self ensureReflection.
×
344
                  varName := aFamixJavaAttribute parentType name asUppercase , '_'
×
345
                             , aFamixJavaAttribute name asUppercase.
×
346

×
347
                  declarations
×
348
                          at: varName
×
349
                          ifPresent: [ "handle naming collisions, some ideas:
×
350
                                - use fully qualified type name (only when necessary?)
×
351
                                - use a HashMap to store the fields based on class and attribute name"
×
352
                                  self shouldBeImplemented ]
×
353
                          ifAbsentPut: [
×
354
                                  model newVarDeclStatement
×
355
                                          type: (model newClassTypeExpression typeName:
×
356
                                                                   (model newTypeName name: 'Field'));
×
357
                                          modifiers: {
×
358
                                                          (model newModifier token: 'private').
×
359
                                                          (model newModifier token: 'static') };
×
360
                                          declarators: { (model newVariableDeclarator variable:
×
361
                                                                   (model newVariableExpression name: varName)) } ].
×
362

×
363
                  "add the code in `initializeReflection` to get the Field and set it as accessible:
×
364
                  CLASSNAME_FIELDNAME = CLASS.class.getDeclaredField(FIELDNAME);
×
365
                  CLASSNAME.setAccessible(true);"
×
366
                  initStatementBlock addStatement:
×
367
                          (model newExpressionStatement expression:
×
368
                                   (model newAssignmentExpression
×
369
                                            variable: (model newVariableExpression name: varName);
×
370
                                            expression: (model newMethodInvocation
×
371
                                                             receiver: (model newClassProperty
×
372
                                                                              type:
×
373
                                                                                      (self builder referType:
×
374
                                                                                                       aFamixJavaAttribute parentType);
×
375
                                                                              fieldName: 'class');
×
376
                                                             name: 'getDeclaredField';
×
377
                                                             arguments:
×
378
                                                             { (model newStringLiteral primitiveValue:
×
379
                                                                      aFamixJavaAttribute name) });
×
380
                                            operator: '=')).
×
381
                  initStatementBlock addStatement:
×
382
                          (model newExpressionStatement expression:
×
383
                                   (model newMethodInvocation
×
384
                                            receiver: (model newVariableExpression name: varName);
×
385
                                            name: 'setAccessible';
×
386
                                            arguments:
×
387
                                                    { (model newBooleanLiteral primitiveValue: 'true') })).
×
388
                  varName ]
×
389
]
×
390

391
{ #category : 'private' }
392
FamixValue2FASTJavaVisitor >> filterAttributesToSet: attributes for: type [
1✔
393
        "No need to set attributes that are set in the constructor."
1✔
394

1✔
395
        | attributesToIgnore |
1✔
396
        "there can be more attributes to ignore in the future"
1✔
397
        attributesToIgnore := type entityCache
1✔
398
                                      at: #syntheticOuterInstance
1✔
399
                                      ifPresent: [ :field | { field } ]
1✔
400
                                      ifAbsent: [ #(  ) ].
1✔
401

1✔
402
        ^ constructorCache
1✔
403
                  at: type
1✔
404
                  ifPresent: [ :constructor |
1✔
405
                          constructorCache
1✔
406
                                  at: constructor
1✔
407
                                  ifPresent: [ :constructorAttributes |
1✔
408
                                          attributes reject: [ :attribute |
1✔
409
                                                  | codeAttribute |
1✔
410
                                                  codeAttribute := attribute attribute.
1✔
411
                                                  (constructorAttributes includes: codeAttribute) or: [
1✔
412
                                                          attributesToIgnore includes: codeAttribute ] ] ]
1✔
413
                                  ifAbsent: [ attributes ] ]
1✔
414
                  ifAbsent: [ attributes ]
1✔
415
]
1✔
416

417
{ #category : 'ast' }
418
FamixValue2FASTJavaVisitor >> finalizeHelpers [
×
419
        "This method must be called after all of the other value exports are done."
×
420

×
421
        self objectExportStrategy makeHelpersOn: self
×
422
]
×
423

424
{ #category : 'private' }
425
FamixValue2FASTJavaVisitor >> findConstructorFor: object [
1✔
426
        "Order all constructors according to attributes set and number of parameters, then choose best public one.
1✔
427
        If no constructor is explicitly declared, use the default constructor. Otherwise, rely on reflection."
1✔
428

1✔
429
        | type |
1✔
430
        type := object type.
1✔
431
        ^ constructorCache at: type ifAbsentPut: [
1✔
432
                  type constructorsOrderedByScore
1✔
433
                          ifEmpty: [ "use default constructor, this acts as a null object"
1✔
434
                                  FamixJavaMethod new ]
1✔
435
                          ifNotEmpty: [ :constructors |
1✔
436
                                  constructors
1✔
437
                                          detect: [ :constructor | constructor isPublic ]
1✔
438
                                          ifNone: [ "fallback to using reflection with constructor with best score"
1✔
439
                                                  | constructor |
1✔
440
                                                  constructor := constructors first.
1✔
441
                                                  self ensureReflectionConstructor: constructor.
1✔
442
                                                  constructor ] ] ]
1✔
443
]
1✔
444

445
{ #category : 'private' }
446
FamixValue2FASTJavaVisitor >> findStaticAttributeMatching: object [
1✔
447
        "Only try to find static attributes for object composed of only primitive values, for now."
1✔
448

1✔
449
        | type staticAttributes objAttributes |
1✔
450
        type := object type.
1✔
451
        ((objAttributes := object relevantAttributes) allSatisfy: [
1✔
452
                 :attribute | attribute value isOfPrimitiveType ]) ifFalse: [ ^ nil ].
1✔
453
        "eligible attributes are public, static, of the same type, and have an initializer expression; the conditions are ordered for time efficiency"
1✔
454
        staticAttributes := staticAttributesCache at: type ifAbsentPut: [
1✔
455
                                    type attributes select: [ :attribute |
1✔
456
                                            attribute declaredType == object type and: [
1✔
457
                                                    attribute isStaticConstant ] ] ].
1✔
458
        staticAttributes ifEmpty: [ ^ nil ].
1✔
459
        "get a list of the object's attributes represented as source code literals"
1✔
460
        objAttributes := objAttributes collect: [ :objAttribute |
1✔
461
                                 objAttribute value type name = 'String'
1✔
462
                                         ifTrue: [ '"' , objAttribute value value , '"' ]
1✔
463
                                         ifFalse: [ objAttribute value value asString ] ].
1✔
464
        ^ staticAttributes
1✔
465
                  detect: [ :attribute |
1✔
466
                  attribute initializerMatchesValues: objAttributes ]
1✔
467
                  ifNone: nil
1✔
468
]
1✔
469

470
{ #category : 'exporting' }
471
FamixValue2FASTJavaVisitor >> helperPackageFor: type [
×
472
        "Most helper methods will be put in a common helper class in a default location.
×
473
        However, types with protected or package-private visibility need to be created in the same package they are declared in.
×
474
        This means a helper class should be created in the same package."
×
475

×
476
        | container |
×
477
        type isPublic ifTrue: [ ^ self defaultHelperPackage ].
×
478

×
479
        (type isProtected or: [ type isPackageVisibility ]) ifFalse: [
×
480
                FamixValueExporterError signal:
×
NEW
481
                        'Cannot recreate object of private class: ' , type mooseName ].
×
482

×
483
        "requires the helper to be in the same package"
×
484
        container := type.
×
485
        [ (container := container typeContainer) isPackage ] whileFalse.
×
NEW
486
        ^ container mooseName
×
487
]
×
488

489
{ #category : 'initialization' }
490
FamixValue2FASTJavaVisitor >> initialize [
1✔
491

1✔
492
        constructorCache := IdentityDictionary new.
1✔
493
        staticAttributesCache := IdentityDictionary new
1✔
494
]
1✔
495

496
{ #category : 'ast' }
497
FamixValue2FASTJavaVisitor >> makeClassTypeExpression: typeName [
1✔
498

1✔
499
        ^ self model newClassTypeExpression typeName:
1✔
500
                  (model newTypeName name: typeName)
1✔
501
]
1✔
502

503
{ #category : 'ast' }
504
FamixValue2FASTJavaVisitor >> makeConstructorInvocation: constructor for: object [
1✔
505
        "Make the statement calling the constructor and return the invocation expression."
1✔
506

1✔
507
        | invocation statement |
1✔
508
        statement := self makeVarDeclStatement: object.
1✔
509
        invocation := statement declarators first expression.
1✔
510

1✔
511
        "If the object is a non-static inner class, it has a reference to its enclosing instance.
1✔
512
        It must be exported beforehand, and it must be the receiver of the invocation."
1✔
513
        object type cacheAt: #syntheticOuterInstance ifPresent: [ :field |
1✔
514
                | outerInstance |
1✔
515
                outerInstance := object value detect: [ :attribute |
1✔
516
                                         attribute attribute == field ].
1✔
517
                self ensureVisited: outerInstance value.
1✔
518
                invocation receiver: (self objectExportStrategy
1✔
519
                                 makeVariableExpression: outerInstance
1✔
520
                                 on: self) ].
1✔
521

1✔
522
        "handle exceptions thrown by the constructor and rethrow as an unchecked RuntimeException"
1✔
523
        constructor thrownExceptions ifNotEmpty: [ :exceptions |
1✔
524
                statement := self makeTry: statement catch: exceptions ].
1✔
525

1✔
526
        self statementBlock addStatement: statement.
1✔
527
        ^ invocation
1✔
528
]
1✔
529

530
{ #category : 'helpers' }
531
FamixValue2FASTJavaVisitor >> makeDefaultHelper [
×
532
        "The default helper should always be generated, regardless of strategy."
×
533

×
534
        self makeHelperInPackage: self defaultHelperPackage
×
535
]
×
536

537
{ #category : 'ast' }
538
FamixValue2FASTJavaVisitor >> makeHelperClass [
×
539
        "The helper class should not be instantiated so it has a private constructor."
×
540

×
541
        ^ self model newClassDeclaration
×
542
                  name: 'ModestHelper';
×
543
                  addModifier: (model newModifier token: 'public');
×
544
                  addDeclaration: (model newMethodEntity
×
545
                                   name: 'ModestHelper';
×
546
                                   addModifier: (model newModifier token: 'private');
×
547
                                   statementBlock: model newStatementBlock);
×
548
                  yourself
×
549
]
×
550

551
{ #category : 'ast' }
552
FamixValue2FASTJavaVisitor >> makeHelperInPackage: aPackageName [
×
553

×
554
        | helperClass compilationUnit |
×
555
        helperClass := self makeHelperClass.
×
556
        (compilationUnit := self model newCompilationUnit)
×
557
                packageDeclaration: (model newPackageDeclaration qualifiedName:
×
558
                                         (model newQualifiedName name: aPackageName));
×
559
                importDeclarations: self builder makeImportDeclarations;
×
560
                addClassDeclaration: helperClass.
×
561

×
562
        reflections ifNotNil: [ "TODO reflections per package"
×
563
                compilationUnit
×
564
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
565
                                                 (model newQualifiedName name: 'java.lang.reflect.Field'));
×
566
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
567
                                                 (model newQualifiedName name: 'java.lang.reflect.Constructor'));
×
568
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
569
                                                 (model newQualifiedName name:
×
570
                                                                  'java.lang.reflect.InvocationTargetException')) ].
×
571

×
572
        ^ helperClass
×
573
]
×
574

575
{ #category : 'ast' }
576
FamixValue2FASTJavaVisitor >> makeNewExpression: object [
1✔
577

1✔
578
        ^ self model newNewExpression type:
1✔
579
                  (object asFASTJavaTypeExpressionOn: self)
1✔
580
        "(self makeClassTypeExpression: object type concreteTypeName)"
1✔
581
]
1✔
582

583
{ #category : 'ast' }
584
FamixValue2FASTJavaVisitor >> makeReflectionConstructorInvocation: constructorVarName for: object [
×
585
        "Use `T instantiate(Constructor<T> constructor, Object... arguments)` from the UT helper.
×
586
        constructorVarName is the name of the variable that contains the Constructor object."
×
587

×
588
        | invocation |
×
589
        (invocation := model newMethodInvocation name: 'instantiate')
×
590
                addArgument: (model newVariableExpression name: constructorVarName).
×
591

×
592
        "If the object is a non-static inner class, it has a reference to its enclosing instance.
×
593
        It must be exported beforehand, and it must be the first argument of the invocation."
×
594
        object type cacheAt: #syntheticOuterInstance ifPresent: [ :field |
×
595
                | outerInstance |
×
596
                outerInstance := object value detect: [ :attribute |
×
597
                                         attribute attribute == field ].
×
598
                self ensureVisited: outerInstance value.
×
599
                invocation addArgument: (self objectExportStrategy
×
600
                                 makeVariableExpression: outerInstance
×
601
                                 on: self) ].
×
602

×
603
        "invoke the method and store the result in a variable"
×
604
        (self statementBlock addStatement: self model newVarDeclStatement)
×
605
                type: (self builder referType: object type);
×
606
                addDeclarator: (model newVariableDeclarator
×
607
                                 variable: (self makeVariableExpression: object);
×
608
                                 expression: invocation).
×
609
        ^ invocation
×
610
]
×
611

612
{ #category : 'ast' }
613
FamixValue2FASTJavaVisitor >> makeReflectionFieldGetter: attribute [
×
614

×
615
        ^ self model newMethodInvocation
×
616
                  receiver: (model newClassProperty
×
617
                                   type:
×
618
                                           (self makeClassTypeExpression: attribute object type typeName);
×
619
                                   fieldName: 'class');
×
620
                  name: 'getDeclaredField';
×
621
                  addArgument:
×
622
                  (model newStringLiteral primitiveValue: attribute attribute name);
×
623
                  yourself
×
624
]
×
625

626
{ #category : 'ast' }
627
FamixValue2FASTJavaVisitor >> makeReflectionSetterInvocation: attribute [
×
628
        "Use reflection to set an attribute on an object."
×
629

×
630
        | fieldName |
×
631
        fieldName := self ensureReflectionField: attribute attribute.
×
632
        self statementBlock addStatement:
×
633
                (self model newExpressionStatement expression:
×
634
                         (model newMethodInvocation
×
635
                                  name: 'setField';
×
636
                                  arguments: {
×
637
                                                  (self makeVariableExpression: attribute object).
×
638
                                                  (model newVariableExpression name: fieldName).
×
639
                                                  (model newVariableExpression name: attribute value varName) }))
×
640
]
×
641

642
{ #category : 'ast' }
643
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute [
1✔
644

1✔
645
        self
1✔
646
                makeSetterInvocation: setter
1✔
647
                for: attribute
1✔
648
                named: (self varNameFor: attribute value)
1✔
649
]
1✔
650

651
{ #category : 'ast' }
652
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute named: aString [
1✔
653

1✔
654
        | invocation |
1✔
655
        (invocation := self model newExpressionStatement) expression:
1✔
656
                (model newMethodInvocation
1✔
657
                         receiver: (self makeVariableExpression: attribute object);
1✔
658
                         name: setter name;
1✔
659
                         addArgument: (model newVariableExpression name: aString);
1✔
660
                         famixMethod: setter).
1✔
661

1✔
662
        self statementBlock addStatement: (setter thrownExceptions
1✔
663
                         ifEmpty: [ invocation ]
1✔
664
                         ifNotEmpty: [ :exceptions | "handle setters that can throw exceptions to rethrow an unchecked exception"
1✔
665
                                 self makeTry: invocation catch: exceptions ])
1✔
666
]
1✔
667

668
{ #category : 'ast' }
669
FamixValue2FASTJavaVisitor >> makeTry: statement catch: exceptions [
×
670
        "Wrap the statement in a try-catch for the given exceptions, and rethrow as an unchecked RuntimeException."
×
671

×
672
        ^ self model newTryCatchStatement
×
673
                  try: (model newStatementBlock
×
674
                                   addStatement: statement;
×
675
                                   yourself);
×
676
                  addCatch: (model newCatchPartStatement
×
677
                                   catchedTypes: (exceptions collect: [ :exception |
×
678
                                                            exception asFASTJavaTypeExpressionOn: self ]);
×
679
                                   parameter: (model newVariableExpression name: 'e');
×
680
                                   body: (model newStatementBlock
×
681
                                                    addStatement:
×
682
                                                            (model newThrowStatement expression:
×
683
                                                                             (model newNewExpression
×
684
                                                                                      type: (model newClassTypeExpression typeName:
×
685
                                                                                                               (model newTypeName name: 'RuntimeException'));
×
686
                                                                                      addArgument: (model newVariableExpression name: 'e');
×
687
                                                                                      yourself));
×
688
                                                    yourself));
×
689
                  yourself
×
690
]
×
691

692
{ #category : 'ast' }
693
FamixValue2FASTJavaVisitor >> makeVarDeclStatement: value [
1✔
694

1✔
695
        ^ self model newVarDeclStatement
1✔
696
                  type: (value asFASTJavaTypeExpressionOn: self);
1✔
697
                  addDeclarator: (self makeVariableDeclarator: value);
1✔
698
                  yourself
1✔
699
]
1✔
700

701
{ #category : 'ast' }
702
FamixValue2FASTJavaVisitor >> makeVarDeclStatement: object usingStaticAttribute: attribute [
×
703
        "Declare a variable for object, initialized with the value of the given static attribute.
×
704
        For example: MyClass myClass = MyClass.MY_STATIC_ATTRIBUTE;"
×
705

×
706
        self statementBlock addStatement: (model newVarDeclStatement
×
707
                         type: (object asFASTJavaTypeExpressionOn: self);
×
708
                         addDeclarator: (model newVariableDeclarator
×
709
                                          variable: (self makeVariableExpression: object);
×
710
                                          expression: (model newClassProperty
×
711
                                                           type: (self makeClassTypeExpression: object type name);
×
712
                                                           fieldName: attribute name));
×
713
                         yourself)
×
714
]
×
715

716
{ #category : 'ast' }
717
FamixValue2FASTJavaVisitor >> makeVariableDeclarator: value [
1✔
718

1✔
719
        ^ self model newVariableDeclarator
1✔
720
                  variable: (self makeVariableExpression: value);
1✔
721
                  expression: (value asFASTJavaExpressionOn: self)
1✔
722
]
1✔
723

724
{ #category : 'ast' }
725
FamixValue2FASTJavaVisitor >> makeVariableExpression: value [
1✔
726

1✔
727
        ^ self model newVariableExpression name: (self varNameFor: value)
1✔
728
]
1✔
729

730
{ #category : 'accessing' }
731
FamixValue2FASTJavaVisitor >> model [
1✔
732

1✔
733
        ^ model ifNil: [ model := FASTJavaModel new ]
1✔
734
]
1✔
735

736
{ #category : 'accessing' }
737
FamixValue2FASTJavaVisitor >> objectExportStrategy [
1✔
738

1✔
739
        ^ objectExportStrategy ifNil: [
1✔
740
                  objectExportStrategy := FamixValueInlineObjectExportStrategy new ]
1✔
741
]
1✔
742

743
{ #category : 'accessing' }
744
FamixValue2FASTJavaVisitor >> objectExportStrategy: anObjectExportStrategy [
×
745

×
746
        objectExportStrategy := anObjectExportStrategy
×
747
]
×
748

749
{ #category : 'accessing' }
750
FamixValue2FASTJavaVisitor >> reflections [
1✔
751

1✔
752
        ^ reflections ifNil: [ reflections := IdentityDictionary new ]
1✔
753
]
1✔
754

755
{ #category : 'accessing' }
756
FamixValue2FASTJavaVisitor >> statementBlock [
1✔
757

1✔
758
        ^ statementBlock ifNil: [
1✔
759
                  statementBlock := self model newStatementBlock ]
1✔
760
]
1✔
761

762
{ #category : 'visiting' }
763
FamixValue2FASTJavaVisitor >> visitCollection: collection [
1✔
764
        "Dispatch to type to handle special cases"
1✔
765

1✔
766
        ^ collection type acceptValueVisitor: self forCollection: collection
1✔
767
]
1✔
768

769
{ #category : 'visiting' }
770
FamixValue2FASTJavaVisitor >> visitCollectionOfRegularType: collection [
1✔
771

1✔
772
        | varName |
1✔
773
        self statementBlock addStatement:
1✔
774
                (self makeVarDeclStatement: collection).
1✔
775
        varName := self varNameDict at: collection.
1✔
776
        collection value do: [ :element |
1✔
777
                self ensureVisited: element value.
1✔
778
                statementBlock addStatement:
1✔
779
                        (self model newExpressionStatement expression:
1✔
780
                                 (model newMethodInvocation
1✔
781
                                          receiver: (model newIdentifier name: varName);
1✔
782
                                          name: 'add';
1✔
783
                                          addArgument: (self makeVariableExpression: element value);
1✔
784
                                          yourself)) ].
1✔
785
        ^ statementBlock
1✔
786
]
1✔
787

788
{ #category : 'visiting' }
789
FamixValue2FASTJavaVisitor >> visitDictionary: dictionary [
1✔
790

1✔
791
        | varName |
1✔
792
        self statementBlock addStatement:
1✔
793
                (self makeVarDeclStatement: dictionary).
1✔
794
        varName := self varNameDict at: dictionary.
1✔
795
        dictionary value do: [ :association |
1✔
796
                self ensureVisited: association key.
1✔
797
                self ensureVisited: association value.
1✔
798
                statementBlock addStatement:
1✔
799
                        (self model newExpressionStatement expression:
1✔
800
                                 (model newMethodInvocation
1✔
801
                                          receiver: (model newIdentifier name: varName);
1✔
802
                                          name: 'put';
1✔
803
                                          addArgument: (self makeVariableExpression: association key);
1✔
804
                                          addArgument: (self makeVariableExpression: association value);
1✔
805
                                          yourself)) ].
1✔
806
        ^ statementBlock
1✔
807
]
1✔
808

809
{ #category : 'visiting' }
810
FamixValue2FASTJavaVisitor >> visitEnumValue: enumValue [
1✔
811

1✔
812
        ^ self statementBlock
1✔
813
                  addStatement: (self makeVarDeclStatement: enumValue);
1✔
814
                  yourself
1✔
815
]
1✔
816

817
{ #category : 'visiting' }
818
FamixValue2FASTJavaVisitor >> visitObject: object [
1✔
819
        "Dispatch to type to handle special cases"
1✔
820

1✔
821
        ^ object type acceptValueVisitor: self forObject: object
1✔
822
]
1✔
823

824
{ #category : 'visiting' }
825
FamixValue2FASTJavaVisitor >> visitObjectAttribute: attribute [
1✔
826

1✔
827
        attribute attribute ifNil: [ "ignore unknown attributes" ^ self ].
1✔
828
        self ensureVisited: attribute value.
1✔
829
        (attribute object type findSetterOf: attribute attribute)
1✔
830
                ifNotNil: [ :setter | self makeSetterInvocation: setter for: attribute ]
1✔
831
                ifNil: [ self makeReflectionSetterInvocation: attribute ]
1✔
832
]
1✔
833

834
{ #category : 'visiting' }
835
FamixValue2FASTJavaVisitor >> visitObjectOfRegularType: object [
1✔
836
        "Dispatched here from #visitObject: if the object has a regular creation schema:
1✔
837
        use constructor, then setup each attribute and use setter or reflection."
1✔
838

1✔
839
        (self findStaticAttributeMatching: object)
1✔
840
                ifNotNil: [ :attribute |
1✔
841
                        self makeVarDeclStatement: object usingStaticAttribute: attribute ]
1✔
842
                ifNil: [ self objectExportStrategy export: object on: self ].
1✔
843
        ^ statementBlock
1✔
844
]
1✔
845

846
{ #category : 'visiting' }
847
FamixValue2FASTJavaVisitor >> visitObjectStub: object [
×
848
        "Previously tried to recreate stubs by deserializing their JSON representation, but getting a correct JSON string is a challenge."
×
849

×
850
        FamixUTStubError signal: object type
×
851
]
×
852

853
{ #category : 'visiting' }
854
FamixValue2FASTJavaVisitor >> visitPrimitive: primitive [
1✔
855

1✔
856
        ^ self statementBlock
1✔
857
                  addStatement: (self makeVarDeclStatement: primitive);
1✔
858
                  yourself
1✔
859
]
1✔
860

861
{ #category : 'visiting' }
862
FamixValue2FASTJavaVisitor >> visitTypeReference: aFamixValueOfTypeReference [
×
863
        "Class xxx = MyClass.class;"
×
864

×
865
        ^ self statementBlock
×
866
                  addStatement: (model newVarDeclStatement
×
867
                                   type: (self makeClassTypeExpression: 'Class');
×
868
                                   addDeclarator: (model newVariableDeclarator
×
869
                                                    variable:
×
870
                                                            (self makeVariableExpression: aFamixValueOfTypeReference);
×
871
                                                    expression: (model newClassProperty
×
872
                                                                     type:
×
873
                                                                             (self makeClassTypeExpression:
×
874
                                                                                              aFamixValueOfTypeReference value name);
×
875
                                                                     fieldName: 'class'));
×
876
                                   yourself);
×
877
                  yourself
×
878
]
×
879

880
{ #category : 'visiting' }
881
FamixValue2FASTJavaVisitor >> visitValueOfUnknownType: value [
1✔
882

1✔
883
        ^ self statementBlock
1✔
884
                  addStatement: (self makeVarDeclStatement: value);
1✔
885
                  yourself
1✔
886
]
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc