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

moosetechnology / Famix-Value / 15827198882

23 Jun 2025 02:34PM UTC coverage: 22.949% (+0.2%) from 22.717%
15827198882

push

github

Gabriel-Darbord
Exporter: Enhance constructor discovery

811 of 3534 relevant lines covered (22.95%)

0.23 hits per line

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

36.01
/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) isInterface ifTrue: [
1✔
431
                ^ FamixValueExporterError signal:
1✔
432
                          'Cannot reconstruct object of interface type: ' , type mooseName ].
1✔
433

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

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

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

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

×
479
        | container |
×
480
        type isPublic ifTrue: [ ^ self defaultHelperPackage ].
×
481

×
482
        (type isProtected or: [ type isPackageVisibility ]) ifFalse: [
×
483
                FamixValueExporterError signal:
×
484
                        'Cannot recreate object of private class: ' , type mooseName ].
×
485

×
486
        "requires the helper to be in the same package"
×
487
        container := type.
×
488
        [ (container := container typeContainer) isPackage ] whileFalse.
×
489
        ^ container mooseName
×
490
]
×
491

492
{ #category : 'initialization' }
493
FamixValue2FASTJavaVisitor >> initialize [
1✔
494

1✔
495
        constructorCache := IdentityDictionary new.
1✔
496
        staticAttributesCache := IdentityDictionary new
1✔
497
]
1✔
498

499
{ #category : 'ast' }
500
FamixValue2FASTJavaVisitor >> makeClassTypeExpression: typeName [
1✔
501

1✔
502
        ^ self model newClassTypeExpression typeName:
1✔
503
                  (model newTypeName name: typeName)
1✔
504
]
1✔
505

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

1✔
510
        | invocation statement |
1✔
511
        statement := self makeVarDeclStatement: object.
1✔
512
        invocation := statement declarators first expression.
1✔
513

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

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

1✔
529
        self statementBlock addStatement: statement.
1✔
530
        ^ invocation
1✔
531
]
1✔
532

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

×
537
        self makeHelperInPackage: self defaultHelperPackage
×
538
]
×
539

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

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

554
{ #category : 'ast' }
555
FamixValue2FASTJavaVisitor >> makeHelperInPackage: aPackageName [
×
556

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

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

×
575
        ^ helperClass
×
576
]
×
577

578
{ #category : 'ast' }
579
FamixValue2FASTJavaVisitor >> makeNewExpression: object [
1✔
580

1✔
581
        ^ self model newNewExpression type:
1✔
582
                  (object asFASTJavaTypeExpressionOn: self)
1✔
583
        "(self makeClassTypeExpression: object type concreteTypeName)"
1✔
584
]
1✔
585

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

×
591
        | invocation |
×
592
        (invocation := model newMethodInvocation name: 'instantiate')
×
593
                addArgument: (model newVariableExpression name: constructorVarName).
×
594

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

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

615
{ #category : 'ast' }
616
FamixValue2FASTJavaVisitor >> makeReflectionFieldGetter: attribute [
×
617

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

629
{ #category : 'ast' }
630
FamixValue2FASTJavaVisitor >> makeReflectionSetterInvocation: attribute [
×
631
        "Use reflection to set an attribute on an object."
×
632

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

645
{ #category : 'ast' }
646
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute [
1✔
647

1✔
648
        self
1✔
649
                makeSetterInvocation: setter
1✔
650
                for: attribute
1✔
651
                named: (self varNameFor: attribute value)
1✔
652
]
1✔
653

654
{ #category : 'ast' }
655
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute named: aString [
1✔
656

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

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

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

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

695
{ #category : 'ast' }
696
FamixValue2FASTJavaVisitor >> makeVarDeclStatement: value [
1✔
697

1✔
698
        ^ self model newVarDeclStatement
1✔
699
                  type: (value asFASTJavaTypeExpressionOn: self);
1✔
700
                  addDeclarator: (self makeVariableDeclarator: value);
1✔
701
                  yourself
1✔
702
]
1✔
703

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

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

719
{ #category : 'ast' }
720
FamixValue2FASTJavaVisitor >> makeVariableDeclarator: value [
1✔
721

1✔
722
        ^ self model newVariableDeclarator
1✔
723
                  variable: (self makeVariableExpression: value);
1✔
724
                  expression: (value asFASTJavaExpressionOn: self)
1✔
725
]
1✔
726

727
{ #category : 'ast' }
728
FamixValue2FASTJavaVisitor >> makeVariableExpression: value [
1✔
729

1✔
730
        ^ self model newVariableExpression name: (self varNameFor: value)
1✔
731
]
1✔
732

733
{ #category : 'accessing' }
734
FamixValue2FASTJavaVisitor >> model [
1✔
735

1✔
736
        ^ model ifNil: [ model := FASTJavaModel new ]
1✔
737
]
1✔
738

739
{ #category : 'accessing' }
740
FamixValue2FASTJavaVisitor >> objectExportStrategy [
1✔
741

1✔
742
        ^ objectExportStrategy ifNil: [
1✔
743
                  objectExportStrategy := FamixValueInlineObjectExportStrategy new ]
1✔
744
]
1✔
745

746
{ #category : 'accessing' }
747
FamixValue2FASTJavaVisitor >> objectExportStrategy: anObjectExportStrategy [
×
748

×
749
        objectExportStrategy := anObjectExportStrategy
×
750
]
×
751

752
{ #category : 'accessing' }
753
FamixValue2FASTJavaVisitor >> reflections [
1✔
754

1✔
755
        ^ reflections ifNil: [ reflections := IdentityDictionary new ]
1✔
756
]
1✔
757

758
{ #category : 'accessing' }
759
FamixValue2FASTJavaVisitor >> statementBlock [
1✔
760

1✔
761
        ^ statementBlock ifNil: [
1✔
762
                  statementBlock := self model newStatementBlock ]
1✔
763
]
1✔
764

765
{ #category : 'visiting' }
766
FamixValue2FASTJavaVisitor >> visitCollection: collection [
1✔
767
        "Dispatch to type to handle special cases"
1✔
768

1✔
769
        ^ collection type acceptValueVisitor: self forCollection: collection
1✔
770
]
1✔
771

772
{ #category : 'visiting' }
773
FamixValue2FASTJavaVisitor >> visitCollectionOfRegularType: collection [
1✔
774

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

791
{ #category : 'visiting' }
792
FamixValue2FASTJavaVisitor >> visitDictionary: dictionary [
1✔
793

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

812
{ #category : 'visiting' }
813
FamixValue2FASTJavaVisitor >> visitEnumValue: enumValue [
1✔
814

1✔
815
        ^ self statementBlock
1✔
816
                  addStatement: (self makeVarDeclStatement: enumValue);
1✔
817
                  yourself
1✔
818
]
1✔
819

820
{ #category : 'visiting' }
821
FamixValue2FASTJavaVisitor >> visitObject: object [
1✔
822
        "Dispatch to type to handle special cases"
1✔
823

1✔
824
        ^ object type acceptValueVisitor: self forObject: object
1✔
825
]
1✔
826

827
{ #category : 'visiting' }
828
FamixValue2FASTJavaVisitor >> visitObjectAttribute: attribute [
1✔
829

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

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

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

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

×
853
        FamixValueStubError signalFor: object type
×
854
]
×
855

856
{ #category : 'visiting' }
857
FamixValue2FASTJavaVisitor >> visitPrimitive: primitive [
1✔
858

1✔
859
        ^ self statementBlock
1✔
860
                  addStatement: (self makeVarDeclStatement: primitive);
1✔
861
                  yourself
1✔
862
]
1✔
863

864
{ #category : 'visiting' }
865
FamixValue2FASTJavaVisitor >> visitTypeReference: aFamixValueOfTypeReference [
×
866
        "Class xxx = MyClass.class;"
×
867

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

883
{ #category : 'visiting' }
884
FamixValue2FASTJavaVisitor >> visitValueOfUnknownType: value [
1✔
885

1✔
886
        ^ self statementBlock
1✔
887
                  addStatement: (self makeVarDeclStatement: value);
1✔
888
                  yourself
1✔
889
]
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