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

moosetechnology / Famix-Value / 11854370139

15 Nov 2024 10:13AM UTC coverage: 22.591% (-1.4%) from 24.038%
11854370139

push

github

Gabriel-Darbord
Merge 99bc4b8f0

19 of 253 new or added lines in 12 files covered. (7.51%)

103 existing lines in 11 files now uncovered.

762 of 3373 relevant lines covered (22.59%)

0.23 hits per line

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

36.3
/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
        ],
21
        #category : 'Famix-Value-Exporter-Visitors',
22
        #package : 'Famix-Value-Exporter',
23
        #tag : 'Visitors'
24
}
25

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

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

53
{ #category : 'accessing' }
54
FamixValue2FASTJavaVisitor >> builder [
1✔
55

1✔
56
        ^ builder ifNil: [ builder := FASTJavaBuilder new model: self model ]
1✔
57
]
1✔
58

59
{ #category : 'private' }
60
FamixValue2FASTJavaVisitor >> constructObject: object [
1✔
61

1✔
62
        | constructor invocation |
1✔
63
        constructor := self findConstructorFor: object.
1✔
64
        constructor thrownExceptions ifNotEmpty: [
1✔
65
                self shouldBeImplemented "handle exceptions thrown by the chosen constructor" ].
1✔
66
        self reflections
1✔
67
                at: constructor
1✔
68
                ifPresent: [ :varName | "reflective constructor call, the variable contains the Constructor object"
1✔
69
                        invocation := model newMethodInvocation name: 'instantiate'.
1✔
70
                        invocation addArgument: (model newVariableExpression name: varName).
1✔
71
                        (self statementBlock addStatement: self model newVarDeclStatement)
1✔
72
                                type: (self builder referType: object type);
1✔
73
                                addDeclarator: (model newVariableDeclarator
1✔
74
                                                 variable: (self makeVariableExpression: object);
1✔
75
                                                 expression: invocation) ]
1✔
76
                ifAbsent: [ "regular constructor call"
1✔
77
                        | statement |
1✔
78
                        statement := self makeVarDeclStatement: object.
1✔
79
                        invocation := statement declarators first expression.
1✔
80
                        constructor thrownExceptions ifNotEmpty: [ :exceptions | "rethrow as RuntimeException"
1✔
81
                                self shouldBeImplemented.
1✔
82
                                exceptions do: [ :exception |  ] ].
1✔
83
                        self statementBlock addStatement: statement ].
1✔
84
        "call with arguments to match constructor parameters"
1✔
85
        constructor parameters ifNotEmpty: [
1✔
86
                self
1✔
87
                        addAttributesFrom: object
1✔
88
                        asArgumentsTo: invocation
1✔
89
                        usingConstructor: constructor ]
1✔
90
]
1✔
91

92
{ #category : 'private' }
UNCOV
93
FamixValue2FASTJavaVisitor >> ensureReflection [
×
UNCOV
94
        "Ensure that the infrastructure needed to use reflection is generated.
×
UNCOV
95
        A static method called `initializeReflection` is created and will be populated by calls to `ensureReflectionField:` and `ensureReflectionConstructor:`.
×
UNCOV
96
        To handle exceptions that can be thrown by these operations, a static initializer calls this method with a trycatch.
×
UNCOV
97
        Finally, the convenience methods `setField` and `instantiate` are created to handle exceptions during reflexive operations: .
×
UNCOV
98
        Return the statement block of `initializeReflection` for callers to populate."
×
UNCOV
99

×
UNCOV
100
        | declarations declaration initStatementBlock |
×
UNCOV
101
        declarations := self objectExportStrategy declarations. "does not work for inline strategy..."
×
NEW
102
        self reflections ifNotEmpty: [ "body of initializeReflection()"
×
103
                ^ (declarations at: -3) statementBlock ].
×
104

×
105
        "static {
×
106
                try {
×
107
                        initializeReflection();
×
NEW
108
                } catch (Exception e) {
×
109
                        new RuntimeException(e);
×
110
                }
×
111
        }"
×
112
        declaration := self model newInitializer isStatic: true.
×
113
        declaration attributeAt: 'order' put: -4.
×
114
        declarations at: -4 put: declaration.
×
115
        declaration statementBlock:
×
116
                (model newStatementBlock statements: { (model newTryCatchStatement
×
117
                                  try: (model newStatementBlock statements:
×
118
                                                           { (model newExpressionStatement expression:
×
119
                                                                            (model newMethodInvocation name: 'initializeReflection')) });
×
120
                                  catches: { (model newCatchPartStatement
×
NEW
121
                                                   catchedTypes: { (model newClassTypeExpression typeName:
×
NEW
122
                                                                            (model newTypeName name: 'Exception')) };
×
123
                                                   body: (model newStatementBlock statements:
×
124
                                                                            { (model newThrowStatement expression:
×
125
                                                                                             (model newNewExpression
×
126
                                                                                                      type: (model newClassTypeExpression typeName:
×
127
                                                                                                                               (model newTypeName name: 'RuntimeException'));
×
128
                                                                                                      arguments:
×
129
                                                                                                              { (model newVariableExpression name: 'e') })) });
×
130
                                                   parameter: (model newVariableExpression name: 'e')) }) }).
×
131

×
NEW
132
        "private static void initializeReflection() throws NoSuchFieldException, NoSuchMethodException, SecurityException {
×
NEW
133
                // fields and constructors will be obtained and set as accessible here
×
134
        }"
×
135
        declaration := model newMethodEntity name: 'initializeReflection'.
×
136
        declaration attributeAt: 'order' put: -3.
×
137
        declarations at: -3 put: declaration.
×
138
        declaration
×
139
                type: (model newVoidTypeExpression name: 'void');
×
140
                modifiers: {
×
141
                                (model newModifier token: 'private').
×
142
                                (model newModifier token: 'static') };
×
143
                throws: {
×
144
                                (model newClassTypeExpression typeName:
×
145
                                                 (model newTypeName name: 'NoSuchFieldException')).
×
146
                                (model newClassTypeExpression typeName:
×
NEW
147
                                                 (model newTypeName name: 'NoSuchMethodException')).
×
148
                                (model newClassTypeExpression typeName:
×
NEW
149
                                                 (model newTypeName name: 'SecurityException')) };
×
150
                statementBlock: (initStatementBlock := model newStatementBlock).
×
151

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

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

×
268
        ^ initStatementBlock
×
UNCOV
269
]
×
270

271
{ #category : 'private' }
272
FamixValue2FASTJavaVisitor >> ensureReflectionConstructor: aFamixJavaMethod [
×
273

×
274
        | declarations initStatementBlock varName |
×
275
        declarations := self objectExportStrategy declarations.
×
276
        initStatementBlock := self ensureReflection.
×
277
        varName := aFamixJavaMethod name asUppercase.
×
278
        self reflections at: aFamixJavaMethod put: varName.
×
279

×
280
        declarations
×
281
                at: varName
×
282
                ifPresent: [ "handle naming collisions, some ideas:
×
283
                                - use fully qualified type name (only when necessary?)
×
284
                                - use a HashMap to store the fields based on class and attribute name"
×
285
                        self shouldBeImplemented ]
×
286
                ifAbsentPut: [
×
287
                        model newVarDeclStatement
×
288
                                type: (model newClassTypeExpression
×
289
                                                 typeName: (model newTypeName name: 'Constructor');
×
290
                                                 arguments:
×
291
                                                         { (self builder referType: aFamixJavaMethod parentType) });
×
292
                                modifiers: {
×
293
                                                (model newModifier token: 'private').
×
294
                                                (model newModifier token: 'static') };
×
295
                                declarators: { (model newVariableDeclarator variable:
×
296
                                                 (model newVariableExpression name: varName)) } ].
×
297

×
298
        "add the code in `initializeReflection` to get the Field and set it as accessible:
×
299
        CLASSNAME = CLASS.class.getDeclaredConstructor(PARAMETER_CLASSES);
×
300
        CLASSNAME.setAccessible(true);"
×
301
        initStatementBlock addStatement:
×
302
                (model newExpressionStatement expression:
×
303
                         (model newAssignmentExpression
×
304
                                  variable: (model newVariableExpression name: varName);
×
305
                                  expression: (model newMethodInvocation
×
306
                                                   receiver: (model newClassProperty
×
307
                                                                    type:
×
308
                                                                            (self builder referType: aFamixJavaMethod parentType);
×
309
                                                                    fieldName: 'class');
×
310
                                                   name: 'getDeclaredConstructor';
×
311
                                                   arguments:
×
312
                                                           (aFamixJavaMethod sortedParameters collect: [ :parameter |
×
313
                                                                    model newClassProperty
×
314
                                                                            type: (self builder referType: parameter declaredType);
×
315
                                                                            fieldName: 'class' ]));
×
316
                                  operator: '=')).
×
317
        initStatementBlock addStatement:
×
318
                (model newExpressionStatement expression:
×
319
                         (model newMethodInvocation
×
320
                                  receiver: (model newVariableExpression name: varName);
×
321
                                  name: 'setAccessible';
×
322
                                  arguments: { (model newBooleanLiteral primitiveValue: 'true') }))
×
UNCOV
323
]
×
324

325
{ #category : 'private' }
326
FamixValue2FASTJavaVisitor >> ensureReflectionField: attribute [
×
327
        "Ensure an attribute exists to hold the java.lang.reflect.Field for the given attribute."
×
328

×
329
        ^ self reflections at: attribute attribute ifAbsentPut: [
×
330
                  | declarations initStatementBlock varName |
×
331
                  declarations := self objectExportStrategy declarations.
×
332
                  initStatementBlock := self ensureReflection.
×
333
                  varName := attribute attribute parentType name asUppercase , '_'
×
334
                             , attribute attribute name asUppercase.
×
335
                  declarations
×
336
                          at: varName
×
337
                          ifPresent: [ "handle naming collisions, some ideas:
×
338
                                - use fully qualified type name (only when necessary?)
×
339
                                - use a HashMap to store the fields based on class and attribute name"
×
340
                                  self shouldBeImplemented ]
×
341
                          ifAbsentPut: [
×
342
                                  model newVarDeclStatement
×
343
                                          type: (model newClassTypeExpression typeName:
×
344
                                                                   (model newTypeName name: 'Field'));
×
345
                                          modifiers: {
×
346
                                                          (model newModifier token: 'private').
×
347
                                                          (model newModifier token: 'static') };
×
348
                                          declarators: { (model newVariableDeclarator variable:
×
349
                                                                   (model newVariableExpression name: varName)) } ].
×
350

×
351
                  "add the code in `initializeReflection` to get the Field and set it as accessible:
×
352
                  CLASSNAME_FIELDNAME = CLASS.class.getDeclaredField(FIELDNAME);
×
353
                  CLASSNAME.setAccessible(true);"
×
354
                  initStatementBlock addStatement:
×
355
                          (model newExpressionStatement expression:
×
356
                                   (model newAssignmentExpression
×
357
                                            variable: (model newVariableExpression name: varName);
×
358
                                            expression: (model newMethodInvocation
×
359
                                                             receiver: (model newClassProperty
×
360
                                                                              type:
×
361
                                                                                      (self builder referType:
×
362
                                                                                                       attribute attribute parentType);
×
363
                                                                              fieldName: 'class');
×
364
                                                             name: 'getDeclaredField';
×
365
                                                             arguments:
×
366
                                                             { (model newStringLiteral primitiveValue:
×
367
                                                                      attribute attribute name) });
×
368
                                            operator: '=')).
×
369
                  initStatementBlock addStatement:
×
370
                          (model newExpressionStatement expression:
×
371
                                   (model newMethodInvocation
×
372
                                            receiver: (model newVariableExpression name: varName);
×
373
                                            name: 'setAccessible';
×
374
                                            arguments:
×
375
                                                    { (model newBooleanLiteral primitiveValue: 'true') })).
×
376
                  varName ]
×
UNCOV
377
]
×
378

379
{ #category : 'private' }
380
FamixValue2FASTJavaVisitor >> filterAttributesToSet: attributes for: type [
1✔
381
        "No need to set attributes that are set in the constructor."
1✔
382

1✔
383
        ^ constructorCache
1✔
384
                  at: type
1✔
385
                  ifPresent: [ :constructor |
1✔
386
                          constructorCache
1✔
387
                                  at: constructor
1✔
388
                                  ifPresent: [ :constructorAttributes |
1✔
389
                                          attributes reject: [ :attribute |
1✔
390
                                                  constructorAttributes includes: attribute attribute ] ]
1✔
391
                                  ifAbsent: [ attributes ] ]
1✔
392
                  ifAbsent: [ attributes ]
1✔
393
]
1✔
394

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

1✔
400
        | type |
1✔
401
        type := object type.
1✔
402
        ^ constructorCache at: type ifAbsentPut: [
1✔
403
                  type constructorsOrderedByScore
1✔
404
                          ifEmpty: [ "use default constructor, this acts as a null object"
1✔
405
                                  FamixJavaMethod new ]
1✔
406
                          ifNotEmpty: [ :constructors |
1✔
407
                                  constructors
1✔
408
                                          detect: [ :constructor | constructor isPublic ]
1✔
409
                                          ifNone: [ "fallback to using reflection with constructor with best score"
1✔
410
                                                  | constructor |
1✔
411
                                                  constructor := constructors first.
1✔
412
                                                  self ensureReflectionConstructor: constructor.
1✔
413
                                                  constructor ] ] ]
1✔
414
]
1✔
415

416
{ #category : 'private' }
417
FamixValue2FASTJavaVisitor >> findStaticAttributeMatching: object [
1✔
418
        "Only try to find static attributes for object composed of only primitive values, for now."
1✔
419

1✔
420
        | type staticAttributes objAttributes |
1✔
421
        type := object type.
1✔
422
        ((objAttributes := object relevantAttributes) allSatisfy: [
1✔
423
                 :attribute | attribute value isOfPrimitiveType ]) ifFalse: [ ^ nil ].
1✔
424
        "eligible attributes are public, static, of the same type, and have an initializer expression; the conditions are ordered for time efficiency"
1✔
425
        staticAttributes := staticAttributesCache at: type ifAbsentPut: [
1✔
426
                                    type attributes select: [ :attribute |
1✔
427
                                            attribute declaredType == object type and: [
1✔
428
                                                    attribute isStaticConstant ] ] ].
1✔
429
        staticAttributes ifEmpty: [ ^ nil ].
1✔
430
        "get a list of the object's attributes represented as source code literals"
1✔
431
        objAttributes := objAttributes collect: [ :objAttribute |
1✔
432
                                 objAttribute value type name = 'String'
1✔
433
                                         ifTrue: [ '"' , objAttribute value value , '"' ]
1✔
434
                                         ifFalse: [ objAttribute value value asString ] ].
1✔
435
        ^ staticAttributes
1✔
436
                  detect: [ :attribute |
1✔
437
                  attribute initializerMatchesValues: objAttributes ]
1✔
438
                  ifNone: nil
1✔
439
]
1✔
440

441
{ #category : 'initialization' }
442
FamixValue2FASTJavaVisitor >> initialize [
1✔
443

1✔
444
        constructorCache := IdentityDictionary new.
1✔
445
        staticAttributesCache := IdentityDictionary new
1✔
446
]
1✔
447

448
{ #category : 'ast' }
449
FamixValue2FASTJavaVisitor >> makeClassTypeExpression: typeName [
1✔
450

1✔
451
        ^ self model newClassTypeExpression typeName:
1✔
452
                  (model newTypeName name: typeName)
1✔
453
]
1✔
454

455
{ #category : 'ast' }
456
FamixValue2FASTJavaVisitor >> makeHelper [
×
457
        "The helper should always be generated, regardless of strategy.
×
458
        This method must be called after all of the other value exports are done."
×
459

×
460
        | helperClass compilationUnit |
×
461
        helperClass := self makeHelperClass.
×
462
        (compilationUnit := self model newCompilationUnit)
×
463
                packageDeclaration: (model newPackageDeclaration qualifiedName:
×
464
                                         (model newQualifiedName name: 'fr.evref.modest'));
×
465
                importDeclarations: self builder makeImportDeclarations;
×
466
                addClassDeclaration: helperClass.
×
467

×
468
        reflections ifNotNil: [
×
469
                compilationUnit
×
470
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
471
                                                 (model newQualifiedName name: 'java.lang.reflect.Field'));
×
472
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
473
                                                 (model newQualifiedName name: 'java.lang.reflect.Constructor'));
×
474
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
475
                                                 (model newQualifiedName name:
×
476
                                                                  'java.lang.reflect.InvocationTargetException')) ].
×
477

×
478
        self objectExportStrategy addToHelper: helperClass
×
UNCOV
479
]
×
480

481
{ #category : 'ast' }
482
FamixValue2FASTJavaVisitor >> makeHelperClass [
×
483
        "The helper class should not be instantiated so it has a private constructor."
×
484

×
485
        ^ self model newClassDeclaration
×
486
                  name: 'ModestHelper';
×
487
                  addModifier: (model newModifier token: 'public');
×
488
                  addDeclaration: (model newMethodEntity
×
489
                                   name: 'ModestHelper';
×
NEW
490
                                   addModifier: (model newModifier token: 'private');
×
491
                                   statementBlock: model newStatementBlock);
×
492
                  yourself
×
UNCOV
493
]
×
494

495
{ #category : 'ast' }
496
FamixValue2FASTJavaVisitor >> makeNewExpression: object [
1✔
497

1✔
498
        ^ self model newNewExpression type:
1✔
499
                  (object asFASTJavaTypeExpressionOn: self)
1✔
500
        "(self makeClassTypeExpression: object type concreteTypeName)"
1✔
501
]
1✔
502

503
{ #category : 'ast' }
504
FamixValue2FASTJavaVisitor >> makeReflectionFieldGetter: attribute [
×
505

×
506
        ^ self model newMethodInvocation
×
507
                  receiver: (model newClassProperty
×
508
                                   type:
×
509
                                           (self makeClassTypeExpression: attribute object type typeName);
×
510
                                   fieldName: 'class');
×
511
                  name: 'getDeclaredField';
×
512
                  addArgument:
×
513
                  (model newStringLiteral primitiveValue: attribute attribute name);
×
514
                  yourself
×
UNCOV
515
]
×
516

517
{ #category : 'ast' }
518
FamixValue2FASTJavaVisitor >> makeReflectionSetterInvocation: attribute [
×
519
        "Use reflection to set an attribute on an object."
×
520

×
521
        | fieldName |
×
522
        fieldName := self ensureReflectionField: attribute.
×
523
        self statementBlock addStatement:
×
524
                (self model newExpressionStatement expression:
×
525
                         (model newMethodInvocation
×
526
                                  name: 'setField';
×
527
                                  arguments: {
×
528
                                                  (self makeVariableExpression: attribute object).
×
529
                                                  (model newVariableExpression name: fieldName).
×
530
                                                  (model newVariableExpression name: attribute value varName) }))
×
UNCOV
531
]
×
532

533
{ #category : 'ast' }
534
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute [
1✔
535

1✔
536
        self
1✔
537
                makeSetterInvocation: setter
1✔
538
                for: attribute
1✔
539
                named: (self varNameFor: attribute value)
1✔
540
]
1✔
541

542
{ #category : 'ast' }
543
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute named: aString [
1✔
544

1✔
545
        self statementBlock addStatement:
1✔
546
                (self model newExpressionStatement expression:
1✔
547
                         (model newMethodInvocation
1✔
548
                                  receiver: (self makeVariableExpression: attribute object);
1✔
549
                                  name: setter name;
1✔
550
                                  addArgument: (model newVariableExpression name: aString);
1✔
551
                                  famixMethod: setter))
1✔
552
]
1✔
553

554
{ #category : 'ast' }
NEW
555
FamixValue2FASTJavaVisitor >> makeTry: statement catch: exceptions [
×
NEW
556
        "Wrap the statement in a try-catch for the given exceptions, and rethrow as an unchecked RuntimeException."
×
NEW
557

×
NEW
558
        ^ self model newTryCatchStatement
×
NEW
559
                  try: (model newStatementBlock statements: { statement });
×
NEW
560
                  catches: { (model newCatchPartStatement
×
NEW
561
                                   catchedTypes: (exceptions collect: [ :exception |
×
NEW
562
                                                            exception asFASTJavaTypeExpressionOn: self ]);
×
NEW
563
                                   parameter: (model newVariableExpression name: 'e');
×
NEW
564
                                   body: (model newStatementBlock statements:
×
NEW
565
                                                            { (model newThrowStatement expression:
×
NEW
566
                                                                             (model newNewExpression
×
NEW
567
                                                                                      type: (model newClassTypeExpression typeName:
×
NEW
568
                                                                                                               (model newTypeName name: 'RuntimeException'));
×
NEW
569
                                                                                      arguments: { (model newVariableExpression name: 'e') })) })) }
×
NEW
570
]
×
571

572
{ #category : 'ast' }
573
FamixValue2FASTJavaVisitor >> makeVarDeclStatement: value [
1✔
574

1✔
575
        ^ self model newVarDeclStatement
1✔
576
                  type: (value asFASTJavaTypeExpressionOn: self);
1✔
577
                  addDeclarator: (self makeVariableDeclarator: value);
1✔
578
                  yourself
1✔
579
]
1✔
580

581
{ #category : 'ast' }
582
FamixValue2FASTJavaVisitor >> makeVarDeclStatement: object usingStaticAttribute: attribute [
×
583
        "Declare a variable for object, initialized with the value of the given static attribute.
×
584
        For example: MyClass myClass = MyClass.MY_STATIC_ATTRIBUTE;"
×
585

×
586
        self statementBlock addStatement: (model newVarDeclStatement
×
587
                         type: (object asFASTJavaTypeExpressionOn: self);
×
UNCOV
588
                         addDeclarator: (model newVariableDeclarator
×
UNCOV
589
                                          variable: (self makeVariableExpression: object);
×
UNCOV
590
                                          expression: (model newClassProperty
×
UNCOV
591
                                                           type: (self makeClassTypeExpression: object type name);
×
UNCOV
592
                                                           fieldName: attribute name));
×
UNCOV
593
                         yourself)
×
UNCOV
594
]
×
595

596
{ #category : 'ast' }
597
FamixValue2FASTJavaVisitor >> makeVariableDeclarator: value [
1✔
598

1✔
599
        ^ self model newVariableDeclarator
1✔
600
                  variable: (self makeVariableExpression: value);
1✔
601
                  expression: (value asFASTJavaExpressionOn: self)
1✔
602
]
1✔
603

604
{ #category : 'ast' }
605
FamixValue2FASTJavaVisitor >> makeVariableExpression: value [
1✔
606

1✔
607
        ^ self model newVariableExpression name: (self varNameFor: value)
1✔
608
]
1✔
609

610
{ #category : 'accessing' }
611
FamixValue2FASTJavaVisitor >> model [
1✔
612

1✔
613
        ^ model ifNil: [ model := FASTJavaModel new ]
1✔
614
]
1✔
615

616
{ #category : 'accessing' }
617
FamixValue2FASTJavaVisitor >> objectExportStrategy [
1✔
618

1✔
619
        ^ objectExportStrategy ifNil: [
1✔
620
                  objectExportStrategy := FamixValueInlineObjectExportStrategy new ]
1✔
621
]
1✔
622

623
{ #category : 'accessing' }
UNCOV
624
FamixValue2FASTJavaVisitor >> objectExportStrategy: anObjectExportStrategy [
×
UNCOV
625

×
UNCOV
626
        objectExportStrategy := anObjectExportStrategy
×
UNCOV
627
]
×
628

629
{ #category : 'accessing' }
630
FamixValue2FASTJavaVisitor >> reflections [
1✔
631

1✔
632
        ^ reflections ifNil: [ reflections := IdentityDictionary new ]
1✔
633
]
1✔
634

635
{ #category : 'accessing' }
636
FamixValue2FASTJavaVisitor >> statementBlock [
1✔
637

1✔
638
        ^ statementBlock ifNil: [
1✔
639
                  statementBlock := self model newStatementBlock ]
1✔
640
]
1✔
641

642
{ #category : 'visiting' }
643
FamixValue2FASTJavaVisitor >> visitCollection: collection [
1✔
644
        "Dispatch to type to handle special cases"
1✔
645

1✔
646
        ^ collection type acceptValueVisitor: self forCollection: collection
1✔
647
]
1✔
648

649
{ #category : 'visiting' }
650
FamixValue2FASTJavaVisitor >> visitCollectionOfRegularType: collection [
1✔
651

1✔
652
        | varName |
1✔
653
        self statementBlock addStatement:
1✔
654
                (self makeVarDeclStatement: collection).
1✔
655
        varName := self varNameDict at: collection.
1✔
656
        collection value do: [ :element |
1✔
657
                self ensureVisited: element value.
1✔
658
                statementBlock addStatement:
1✔
659
                        (self model newExpressionStatement expression:
1✔
660
                                 (model newMethodInvocation
1✔
661
                                          receiver: (model newIdentifier name: varName);
1✔
662
                                          name: 'add';
1✔
663
                                          addArgument: (self makeVariableExpression: element value);
1✔
664
                                          yourself)) ].
1✔
665
        ^ statementBlock
1✔
666
]
1✔
667

668
{ #category : 'visiting' }
669
FamixValue2FASTJavaVisitor >> visitDictionary: dictionary [
1✔
670

1✔
671
        | varName |
1✔
672
        self statementBlock addStatement:
1✔
673
                (self makeVarDeclStatement: dictionary).
1✔
674
        varName := self varNameDict at: dictionary.
1✔
675
        dictionary value do: [ :association |
1✔
676
                self ensureVisited: association key.
1✔
677
                self ensureVisited: association value.
1✔
678
                statementBlock addStatement:
1✔
679
                        (self model newExpressionStatement expression:
1✔
680
                                 (model newMethodInvocation
1✔
681
                                          receiver: (model newIdentifier name: varName);
1✔
682
                                          name: 'put';
1✔
683
                                          addArgument: (self makeVariableExpression: association key);
1✔
684
                                          addArgument: (self makeVariableExpression: association value);
1✔
685
                                          yourself)) ].
1✔
686
        ^ statementBlock
1✔
687
]
1✔
688

689
{ #category : 'visiting' }
690
FamixValue2FASTJavaVisitor >> visitEnumValue: enumValue [
1✔
691

1✔
692
        ^ self statementBlock
1✔
693
                  addStatement: (self makeVarDeclStatement: enumValue);
1✔
694
                  yourself
1✔
695
]
1✔
696

697
{ #category : 'visiting' }
698
FamixValue2FASTJavaVisitor >> visitObject: object [
1✔
699
        "Dispatch to type to handle special cases"
1✔
700

1✔
701
        ^ object type acceptValueVisitor: self forObject: object
1✔
702
]
1✔
703

704
{ #category : 'visiting' }
705
FamixValue2FASTJavaVisitor >> visitObjectAttribute: attribute [
1✔
706

1✔
707
        attribute attribute ifNil: [ "ignore unknown attributes" ^ self ].
1✔
708
        self ensureVisited: attribute value.
1✔
709
        (attribute object type findSetterOf: attribute attribute)
1✔
710
                ifNotNil: [ :setter | self makeSetterInvocation: setter for: attribute ]
1✔
711
                ifNil: [ self makeReflectionSetterInvocation: attribute ]
1✔
712
]
1✔
713

714
{ #category : 'visiting' }
715
FamixValue2FASTJavaVisitor >> visitObjectOfRegularType: object [
1✔
716
        "Dispatched here from #visitObject: if the object has a regular creation schema:
1✔
717
        use constructor, then setup each attribute and use setter or reflection."
1✔
718

1✔
719
        (self findStaticAttributeMatching: object)
1✔
720
                ifNotNil: [ :attribute |
1✔
721
                        self makeVarDeclStatement: object usingStaticAttribute: attribute ]
1✔
722
                ifNil: [ self objectExportStrategy export: object on: self ].
1✔
723
        ^ statementBlock
1✔
724
]
1✔
725

726
{ #category : 'visiting' }
UNCOV
727
FamixValue2FASTJavaVisitor >> visitObjectStub: object [
×
UNCOV
728
        "Previously tried to recreate stubs by deserializing their JSON representation, but getting a correct JSON string is a challenge."
×
UNCOV
729

×
NEW
730
        FamixUTStubError signal: object type
×
UNCOV
731
]
×
732

733
{ #category : 'visiting' }
734
FamixValue2FASTJavaVisitor >> visitPrimitive: primitive [
1✔
735

1✔
736
        ^ self statementBlock
1✔
737
                  addStatement: (self makeVarDeclStatement: primitive);
1✔
738
                  yourself
1✔
739
]
1✔
740

741
{ #category : 'visiting' }
NEW
742
FamixValue2FASTJavaVisitor >> visitTypeReference: aFamixValueOfTypeReference [
×
NEW
743
        "Class xxx = MyClass.class;"
×
NEW
744

×
NEW
745
        ^ self statementBlock
×
NEW
746
                  addStatement: (model newVarDeclStatement
×
NEW
747
                                   type: (self makeClassTypeExpression: 'Class');
×
NEW
748
                                   addDeclarator: (model newVariableDeclarator
×
NEW
749
                                                    variable:
×
NEW
750
                                                            (self makeVariableExpression: aFamixValueOfTypeReference);
×
NEW
751
                                                    expression: (model newClassProperty
×
NEW
752
                                                                     type:
×
NEW
753
                                                                             (self makeClassTypeExpression:
×
NEW
754
                                                                                              aFamixValueOfTypeReference value name);
×
NEW
755
                                                                     fieldName: 'class'));
×
NEW
756
                                   yourself);
×
NEW
757
                  yourself
×
NEW
758
]
×
759

760
{ #category : 'visiting' }
761
FamixValue2FASTJavaVisitor >> visitValueOfUnknownType: value [
1✔
762

1✔
763
        ^ self statementBlock
1✔
764
                  addStatement: (self makeVarDeclStatement: value);
1✔
765
                  yourself
1✔
766
]
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