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

moosetechnology / Famix-Value / 11937060415

20 Nov 2024 03:58PM UTC coverage: 22.326% (-0.3%) from 22.591%
11937060415

push

github

Gabriel-Darbord
Merge c3b748a92

0 of 203 new or added lines in 1 file covered. (0.0%)

133 existing lines in 3 files now uncovered.

762 of 3413 relevant lines covered (22.33%)

0.22 hits per line

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

34.43
/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
                                                 (constructor parameters at: index) declaredType
×
37
                                                         ifNil: [ "TODO fix Java parser; if it's unknown then it must be a class, otherwise the parser would have found it"
×
38
                                                                 FamixJavaClass new asFASTJavaDefaultValueOn: self model ]
×
39
                                                         ifNotNil: [ :declaredType |
×
40
                                                         declaredType asFASTJavaDefaultValueOn: self model ] ]
×
41
                                         ifNotNil: [
×
42
                                                 object value
×
43
                                                         detect: [ :objAttribute | "find the matching value attribute"
×
44
                                                                 objAttribute attribute == paramAttribute ]
×
45
                                                         ifFound: [ :objAttribute | "dispatch for var naming context"
×
46
                                                                 objectExportStrategy
×
47
                                                                         makeVariableExpression: objAttribute value
×
48
                                                                         on: self ]
×
49
                                                         ifNone: [ "the object does not have the attribute set"
×
50
                                                                 paramAttribute declaredType asFASTJavaDefaultValueOn:
×
51
                                                                         self model ] ]) ]
×
UNCOV
52
]
×
53

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

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

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

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

93
{ #category : 'exporting' }
94
FamixValue2FASTJavaVisitor >> defaultHelperPackage [
×
95

×
96
        ^ defaultHelperPackage ifNil: [ defaultHelperPackage := 'org.modest' ]
×
97
]
×
98

99
{ #category : 'exporting' }
100
FamixValue2FASTJavaVisitor >> defaultHelperPackage: aString [
×
101

×
102
        defaultHelperPackage := aString
×
103
]
×
104

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

×
113
        | declarations declaration initStatementBlock |
×
114
        declarations := self objectExportStrategy currentDeclarations. "does not work for inline strategy..."
×
115

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

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

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

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

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

×
282
        ^ initStatementBlock
×
283
]
×
284

285
{ #category : 'private' }
286
FamixValue2FASTJavaVisitor >> ensureReflectionConstructor: aFamixJavaMethod [
×
287

×
288
        | declarations initStatementBlock varName |
×
289
        declarations := self objectExportStrategy currentDeclarations.
×
290
        initStatementBlock := self ensureReflection.
×
291
        varName := aFamixJavaMethod name asUppercase.
×
292
        self reflections at: aFamixJavaMethod put: varName.
×
293

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

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

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

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

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

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

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

1✔
398
        ^ constructorCache
1✔
399
                  at: type
1✔
400
                  ifPresent: [ :constructor |
1✔
401
                          constructorCache
1✔
402
                                  at: constructor
1✔
403
                                  ifPresent: [ :constructorAttributes |
1✔
404
                                          attributes reject: [ :attribute |
1✔
405
                                                  constructorAttributes includes: attribute attribute ] ]
1✔
406
                                  ifAbsent: [ attributes ] ]
1✔
407
                  ifAbsent: [ attributes ]
1✔
408
]
1✔
409

410
{ #category : 'ast' }
UNCOV
411
FamixValue2FASTJavaVisitor >> finalizeHelpers [
×
UNCOV
412
        "This method must be called after all of the other value exports are done."
×
UNCOV
413

×
UNCOV
414
        self objectExportStrategy makeHelpersOn: self
×
UNCOV
415
]
×
416

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

1✔
422
        | type |
1✔
423
        type := object type.
1✔
424
        ^ constructorCache at: type ifAbsentPut: [
1✔
425
                  type constructorsOrderedByScore
1✔
426
                          ifEmpty: [ "use default constructor, this acts as a null object"
1✔
427
                                  FamixJavaMethod new ]
1✔
428
                          ifNotEmpty: [ :constructors |
1✔
429
                                  constructors
1✔
430
                                          detect: [ :constructor | constructor isPublic ]
1✔
431
                                          ifNone: [ "fallback to using reflection with constructor with best score"
1✔
432
                                                  | constructor |
1✔
433
                                                  constructor := constructors first.
1✔
434
                                                  self ensureReflectionConstructor: constructor.
1✔
435
                                                  constructor ] ] ]
1✔
436
]
1✔
437

438
{ #category : 'private' }
439
FamixValue2FASTJavaVisitor >> findStaticAttributeMatching: object [
1✔
440
        "Only try to find static attributes for object composed of only primitive values, for now."
1✔
441

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

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

×
469
        | container |
×
470
        type isPublic ifTrue: [ ^ self defaultHelperPackage ].
×
471

×
472
        (type isProtected or: [ type isPackageVisibility ]) ifFalse: [
×
473
                FamixValueExporterError signal:
×
474
                        'Cannot recreate object of private class: '
×
475
                        , type mooseNameWithDots ].
×
476

×
477
        "requires the helper to be in the same package"
×
478
        container := type.
×
479
        [ (container := container typeContainer) isPackage ] whileFalse.
×
UNCOV
480
        ^ container mooseNameWithDots
×
UNCOV
481
]
×
482

483
{ #category : 'initialization' }
484
FamixValue2FASTJavaVisitor >> initialize [
1✔
485

1✔
486
        constructorCache := IdentityDictionary new.
1✔
487
        staticAttributesCache := IdentityDictionary new
1✔
488
]
1✔
489

490
{ #category : 'ast' }
491
FamixValue2FASTJavaVisitor >> makeClassTypeExpression: typeName [
1✔
492

1✔
493
        ^ self model newClassTypeExpression typeName:
1✔
494
                  (model newTypeName name: typeName)
1✔
495
]
1✔
496

497
{ #category : 'helpers' }
UNCOV
498
FamixValue2FASTJavaVisitor >> makeDefaultHelper [
×
UNCOV
499
        "The default helper should always be generated, regardless of strategy."
×
UNCOV
500

×
UNCOV
501
        self makeHelperInPackage: self defaultHelperPackage
×
UNCOV
502
]
×
503

504
{ #category : 'ast' }
505
FamixValue2FASTJavaVisitor >> makeHelperClass [
×
506
        "The helper class should not be instantiated so it has a private constructor."
×
507

×
508
        ^ self model newClassDeclaration
×
509
                  name: 'ModestHelper';
×
510
                  addModifier: (model newModifier token: 'public');
×
511
                  addDeclaration: (model newMethodEntity
×
512
                                   name: 'ModestHelper';
×
513
                                   addModifier: (model newModifier token: 'private');
×
514
                                   statementBlock: model newStatementBlock);
×
515
                  yourself
×
UNCOV
516
]
×
517

518
{ #category : 'ast' }
519
FamixValue2FASTJavaVisitor >> makeHelperInPackage: aPackageName [
×
520

×
521
        | helperClass compilationUnit |
×
522
        helperClass := self makeHelperClass.
×
523
        (compilationUnit := self model newCompilationUnit)
×
524
                packageDeclaration: (model newPackageDeclaration qualifiedName:
×
525
                                         (model newQualifiedName name: aPackageName));
×
526
                importDeclarations: self builder makeImportDeclarations;
×
527
                addClassDeclaration: helperClass.
×
528

×
529
        reflections ifNotNil: [ "TODO reflections per package"
×
530
                compilationUnit
×
531
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
UNCOV
532
                                                 (model newQualifiedName name: 'java.lang.reflect.Field'));
×
UNCOV
533
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
UNCOV
534
                                                 (model newQualifiedName name: 'java.lang.reflect.Constructor'));
×
UNCOV
535
                        addImportDeclaration: (model newImportDeclaration qualifiedName:
×
UNCOV
536
                                                 (model newQualifiedName name:
×
UNCOV
537
                                                                  'java.lang.reflect.InvocationTargetException')) ].
×
UNCOV
538

×
UNCOV
539
        ^ helperClass
×
UNCOV
540
]
×
541

542
{ #category : 'ast' }
543
FamixValue2FASTJavaVisitor >> makeNewExpression: object [
1✔
544

1✔
545
        ^ self model newNewExpression type:
1✔
546
                  (object asFASTJavaTypeExpressionOn: self)
1✔
547
        "(self makeClassTypeExpression: object type concreteTypeName)"
1✔
548
]
1✔
549

550
{ #category : 'ast' }
UNCOV
551
FamixValue2FASTJavaVisitor >> makeReflectionFieldGetter: attribute [
×
UNCOV
552

×
UNCOV
553
        ^ self model newMethodInvocation
×
UNCOV
554
                  receiver: (model newClassProperty
×
555
                                   type:
×
556
                                           (self makeClassTypeExpression: attribute object type typeName);
×
557
                                   fieldName: 'class');
×
558
                  name: 'getDeclaredField';
×
559
                  addArgument:
×
560
                  (model newStringLiteral primitiveValue: attribute attribute name);
×
561
                  yourself
×
562
]
×
563

564
{ #category : 'ast' }
565
FamixValue2FASTJavaVisitor >> makeReflectionSetterInvocation: attribute [
×
566
        "Use reflection to set an attribute on an object."
×
567

×
568
        | fieldName |
×
569
        fieldName := self ensureReflectionField: attribute attribute.
×
570
        self statementBlock addStatement:
×
UNCOV
571
                (self model newExpressionStatement expression:
×
UNCOV
572
                         (model newMethodInvocation
×
UNCOV
573
                                  name: 'setField';
×
UNCOV
574
                                  arguments: {
×
UNCOV
575
                                                  (self makeVariableExpression: attribute object).
×
UNCOV
576
                                                  (model newVariableExpression name: fieldName).
×
UNCOV
577
                                                  (model newVariableExpression name: attribute value varName) }))
×
UNCOV
578
]
×
579

580
{ #category : 'ast' }
581
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute [
1✔
582

1✔
583
        self
1✔
584
                makeSetterInvocation: setter
1✔
585
                for: attribute
1✔
586
                named: (self varNameFor: attribute value)
1✔
587
]
1✔
588

589
{ #category : 'ast' }
590
FamixValue2FASTJavaVisitor >> makeSetterInvocation: setter for: attribute named: aString [
1✔
591

1✔
592
        self statementBlock addStatement:
1✔
593
                (self model newExpressionStatement expression:
1✔
594
                         (model newMethodInvocation
1✔
595
                                  receiver: (self makeVariableExpression: attribute object);
1✔
596
                                  name: setter name;
1✔
597
                                  addArgument: (model newVariableExpression name: aString);
1✔
598
                                  famixMethod: setter))
1✔
599
]
1✔
600

601
{ #category : 'ast' }
UNCOV
602
FamixValue2FASTJavaVisitor >> makeTry: statement catch: exceptions [
×
UNCOV
603
        "Wrap the statement in a try-catch for the given exceptions, and rethrow as an unchecked RuntimeException."
×
UNCOV
604

×
UNCOV
605
        ^ self model newTryCatchStatement
×
UNCOV
606
                  try: (model newStatementBlock statements: { statement });
×
UNCOV
607
                  catches: { (model newCatchPartStatement
×
UNCOV
608
                                   catchedTypes: (exceptions collect: [ :exception |
×
UNCOV
609
                                                            exception asFASTJavaTypeExpressionOn: self ]);
×
UNCOV
610
                                   parameter: (model newVariableExpression name: 'e');
×
UNCOV
611
                                   body: (model newStatementBlock statements:
×
UNCOV
612
                                                            { (model newThrowStatement expression:
×
UNCOV
613
                                                                             (model newNewExpression
×
UNCOV
614
                                                                                      type: (model newClassTypeExpression typeName:
×
UNCOV
615
                                                                                                               (model newTypeName name: 'RuntimeException'));
×
UNCOV
616
                                                                                      arguments: { (model newVariableExpression name: 'e') })) })) }
×
UNCOV
617
]
×
618

619
{ #category : 'ast' }
620
FamixValue2FASTJavaVisitor >> makeVarDeclStatement: value [
1✔
621

1✔
622
        ^ self model newVarDeclStatement
1✔
623
                  type: (value asFASTJavaTypeExpressionOn: self);
1✔
624
                  addDeclarator: (self makeVariableDeclarator: value);
1✔
625
                  yourself
1✔
626
]
1✔
627

628
{ #category : 'ast' }
UNCOV
629
FamixValue2FASTJavaVisitor >> makeVarDeclStatement: object usingStaticAttribute: attribute [
×
UNCOV
630
        "Declare a variable for object, initialized with the value of the given static attribute.
×
UNCOV
631
        For example: MyClass myClass = MyClass.MY_STATIC_ATTRIBUTE;"
×
UNCOV
632

×
UNCOV
633
        self statementBlock addStatement: (model newVarDeclStatement
×
UNCOV
634
                         type: (object asFASTJavaTypeExpressionOn: self);
×
UNCOV
635
                         addDeclarator: (model newVariableDeclarator
×
UNCOV
636
                                          variable: (self makeVariableExpression: object);
×
UNCOV
637
                                          expression: (model newClassProperty
×
UNCOV
638
                                                           type: (self makeClassTypeExpression: object type name);
×
UNCOV
639
                                                           fieldName: attribute name));
×
UNCOV
640
                         yourself)
×
UNCOV
641
]
×
642

643
{ #category : 'ast' }
644
FamixValue2FASTJavaVisitor >> makeVariableDeclarator: value [
1✔
645

1✔
646
        ^ self model newVariableDeclarator
1✔
647
                  variable: (self makeVariableExpression: value);
1✔
648
                  expression: (value asFASTJavaExpressionOn: self)
1✔
649
]
1✔
650

651
{ #category : 'ast' }
652
FamixValue2FASTJavaVisitor >> makeVariableExpression: value [
1✔
653

1✔
654
        ^ self model newVariableExpression name: (self varNameFor: value)
1✔
655
]
1✔
656

657
{ #category : 'accessing' }
658
FamixValue2FASTJavaVisitor >> model [
1✔
659

1✔
660
        ^ model ifNil: [ model := FASTJavaModel new ]
1✔
661
]
1✔
662

663
{ #category : 'accessing' }
664
FamixValue2FASTJavaVisitor >> objectExportStrategy [
1✔
665

1✔
666
        ^ objectExportStrategy ifNil: [
1✔
667
                  objectExportStrategy := FamixValueInlineObjectExportStrategy new ]
1✔
668
]
1✔
669

670
{ #category : 'accessing' }
UNCOV
671
FamixValue2FASTJavaVisitor >> objectExportStrategy: anObjectExportStrategy [
×
UNCOV
672

×
UNCOV
673
        objectExportStrategy := anObjectExportStrategy
×
UNCOV
674
]
×
675

676
{ #category : 'accessing' }
677
FamixValue2FASTJavaVisitor >> reflections [
1✔
678

1✔
679
        ^ reflections ifNil: [ reflections := IdentityDictionary new ]
1✔
680
]
1✔
681

682
{ #category : 'accessing' }
683
FamixValue2FASTJavaVisitor >> statementBlock [
1✔
684

1✔
685
        ^ statementBlock ifNil: [
1✔
686
                  statementBlock := self model newStatementBlock ]
1✔
687
]
1✔
688

689
{ #category : 'visiting' }
690
FamixValue2FASTJavaVisitor >> visitCollection: collection [
1✔
691
        "Dispatch to type to handle special cases"
1✔
692

1✔
693
        ^ collection type acceptValueVisitor: self forCollection: collection
1✔
694
]
1✔
695

696
{ #category : 'visiting' }
697
FamixValue2FASTJavaVisitor >> visitCollectionOfRegularType: collection [
1✔
698

1✔
699
        | varName |
1✔
700
        self statementBlock addStatement:
1✔
701
                (self makeVarDeclStatement: collection).
1✔
702
        varName := self varNameDict at: collection.
1✔
703
        collection value do: [ :element |
1✔
704
                self ensureVisited: element value.
1✔
705
                statementBlock addStatement:
1✔
706
                        (self model newExpressionStatement expression:
1✔
707
                                 (model newMethodInvocation
1✔
708
                                          receiver: (model newIdentifier name: varName);
1✔
709
                                          name: 'add';
1✔
710
                                          addArgument: (self makeVariableExpression: element value);
1✔
711
                                          yourself)) ].
1✔
712
        ^ statementBlock
1✔
713
]
1✔
714

715
{ #category : 'visiting' }
716
FamixValue2FASTJavaVisitor >> visitDictionary: dictionary [
1✔
717

1✔
718
        | varName |
1✔
719
        self statementBlock addStatement:
1✔
720
                (self makeVarDeclStatement: dictionary).
1✔
721
        varName := self varNameDict at: dictionary.
1✔
722
        dictionary value do: [ :association |
1✔
723
                self ensureVisited: association key.
1✔
724
                self ensureVisited: association value.
1✔
725
                statementBlock addStatement:
1✔
726
                        (self model newExpressionStatement expression:
1✔
727
                                 (model newMethodInvocation
1✔
728
                                          receiver: (model newIdentifier name: varName);
1✔
729
                                          name: 'put';
1✔
730
                                          addArgument: (self makeVariableExpression: association key);
1✔
731
                                          addArgument: (self makeVariableExpression: association value);
1✔
732
                                          yourself)) ].
1✔
733
        ^ statementBlock
1✔
734
]
1✔
735

736
{ #category : 'visiting' }
737
FamixValue2FASTJavaVisitor >> visitEnumValue: enumValue [
1✔
738

1✔
739
        ^ self statementBlock
1✔
740
                  addStatement: (self makeVarDeclStatement: enumValue);
1✔
741
                  yourself
1✔
742
]
1✔
743

744
{ #category : 'visiting' }
745
FamixValue2FASTJavaVisitor >> visitObject: object [
1✔
746
        "Dispatch to type to handle special cases"
1✔
747

1✔
748
        ^ object type acceptValueVisitor: self forObject: object
1✔
749
]
1✔
750

751
{ #category : 'visiting' }
752
FamixValue2FASTJavaVisitor >> visitObjectAttribute: attribute [
1✔
753

1✔
754
        attribute attribute ifNil: [ "ignore unknown attributes" ^ self ].
1✔
755
        self ensureVisited: attribute value.
1✔
756
        (attribute object type findSetterOf: attribute attribute)
1✔
757
                ifNotNil: [ :setter | self makeSetterInvocation: setter for: attribute ]
1✔
758
                ifNil: [ self makeReflectionSetterInvocation: attribute ]
1✔
759
]
1✔
760

761
{ #category : 'visiting' }
762
FamixValue2FASTJavaVisitor >> visitObjectOfRegularType: object [
1✔
763
        "Dispatched here from #visitObject: if the object has a regular creation schema:
1✔
764
        use constructor, then setup each attribute and use setter or reflection."
1✔
765

1✔
766
        (self findStaticAttributeMatching: object)
1✔
767
                ifNotNil: [ :attribute |
1✔
768
                        self makeVarDeclStatement: object usingStaticAttribute: attribute ]
1✔
769
                ifNil: [ self objectExportStrategy export: object on: self ].
1✔
770
        ^ statementBlock
1✔
771
]
1✔
772

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

×
UNCOV
777
        FamixUTStubError signal: object type
×
UNCOV
778
]
×
779

780
{ #category : 'visiting' }
781
FamixValue2FASTJavaVisitor >> visitPrimitive: primitive [
1✔
782

1✔
783
        ^ self statementBlock
1✔
784
                  addStatement: (self makeVarDeclStatement: primitive);
1✔
785
                  yourself
1✔
786
]
1✔
787

788
{ #category : 'visiting' }
UNCOV
789
FamixValue2FASTJavaVisitor >> visitTypeReference: aFamixValueOfTypeReference [
×
UNCOV
790
        "Class xxx = MyClass.class;"
×
UNCOV
791

×
UNCOV
792
        ^ self statementBlock
×
UNCOV
793
                  addStatement: (model newVarDeclStatement
×
UNCOV
794
                                   type: (self makeClassTypeExpression: 'Class');
×
UNCOV
795
                                   addDeclarator: (model newVariableDeclarator
×
UNCOV
796
                                                    variable:
×
UNCOV
797
                                                            (self makeVariableExpression: aFamixValueOfTypeReference);
×
UNCOV
798
                                                    expression: (model newClassProperty
×
UNCOV
799
                                                                     type:
×
UNCOV
800
                                                                             (self makeClassTypeExpression:
×
UNCOV
801
                                                                                              aFamixValueOfTypeReference value name);
×
UNCOV
802
                                                                     fieldName: 'class'));
×
UNCOV
803
                                   yourself);
×
UNCOV
804
                  yourself
×
UNCOV
805
]
×
806

807
{ #category : 'visiting' }
808
FamixValue2FASTJavaVisitor >> visitValueOfUnknownType: value [
1✔
809

1✔
810
        ^ self statementBlock
1✔
811
                  addStatement: (self makeVarDeclStatement: value);
1✔
812
                  yourself
1✔
813
]
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