• 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

0.0
/src/Famix-Value-Exporter/FamixValueHelperObjectExportStrategy.class.st
1
"
2
I generate the code to recreate objects in helper functions.
3
For each object, a dedicated function is generated that takes no arguments and returns the recreated object.
4
"
5
Class {
6
        #name : 'FamixValueHelperObjectExportStrategy',
7
        #superclass : 'FamixValueAbstractObjectExportStrategy',
8
        #instVars : [
9
                'declarations'
10
        ],
11
        #category : 'Famix-Value-Exporter-Strategies',
12
        #package : 'Famix-Value-Exporter',
13
        #tag : 'Strategies'
14
}
15

16
{ #category : 'ast' }
17
FamixValueHelperObjectExportStrategy >> addToHelper: helperClass [
×
18
        "Must be called when building the helper class to add the generated helper declarations."
×
19

×
20
        declarations valuesDo: [ :declaration |
×
21
                helperClass addDeclaration: declaration ]
×
22
]
×
23

24
{ #category : 'ast' }
25
FamixValueHelperObjectExportStrategy >> buildMethodFor: object withParametersFor: attributes on: visitor [
×
26
        "Build the helper method to construct objects of the same type as the one given in argument.
×
27
        Use only the relevant attributes that are given, rather than all attributes."
×
28

×
29
        | model oldStatementBlock helper |
×
NEW
30
        object type isPublic ifFalse: [
×
NEW
31
                FamixValueExporterError signal:
×
NEW
32
                        'Cannot (yet) recreate object of non-public class: '
×
NEW
33
                        , object type name ].
×
34
        model := visitor model.
×
35
        "swap visitor context: the 'sheet' on which the visitor writes"
×
36
        oldStatementBlock := visitor statementBlock.
×
37
        visitor statementBlock: model newStatementBlock.
×
38
        visitor builder processType: object type.
×
39
        "make the method entity"
×
40
        helper := self
×
41
                          makeMethodFor: object type
×
42
                          withParametersFor: attributes
×
43
                          on: visitor.
×
44
        "make the method body: instantiate object, set attributes and return it"
×
45
        visitor constructObject: object.
×
46
        self
×
47
                makeSetterInvocationsFor:
×
NEW
48
                (visitor filterAttributesToSet: attributes for: object type)
×
49
                on: visitor.
×
50
        visitor statementBlock addStatement:
×
51
                (model newReturnStatement expression:
×
52
                         (model newVariableExpression name: (visitor varNameFor: object))).
×
53
        "restore original visitor context"
×
54
        visitor statementBlock: oldStatementBlock.
×
55
        ^ helper
×
56
]
×
57

58
{ #category : 'initialization' }
59
FamixValueHelperObjectExportStrategy >> declarations [
×
60

×
UNCOV
61
        ^ declarations
×
UNCOV
62
]
×
63

64
{ #category : 'ast' }
65
FamixValueHelperObjectExportStrategy >> dependencyImportsOn: aFASTModel [
×
66

×
UNCOV
67
        ^ { ((aFASTModel newImportDeclaration qualifiedName:
×
UNCOV
68
                            (aFASTModel newQualifiedName name:
×
69
                                             'fr.evref.modest.ModestHelper'))
×
70
                   isStatic: true;
×
71
                   isOnDemand: true) }
×
72
]
×
73

74
{ #category : 'exporting' }
75
FamixValueHelperObjectExportStrategy >> export: object on: visitor [
×
76
        "Use a helper method to instantiate the object and set its attributes."
×
UNCOV
77

×
UNCOV
78
        | attributes helper model |
×
79
        "ensure all relevant attributes are set up"
×
80
        (attributes := object relevantAttributes) do: [ :attribute |
×
81
                visitor ensureVisited: attribute value ].
×
82
        "check if the object was not set up in the previous step"
×
83
        (visitor varNameDict includesKey: object) ifTrue: [ ^ self ].
×
84
        "get the helper method, or build it if it does not exist"
×
85
        helper := declarations
×
86
                          at: object constructorSignature
×
87
                          ifAbsentPut: [
×
88
                                  self
×
89
                                          buildMethodFor: object
×
90
                                          withParametersFor: attributes
×
91
                                          on: visitor ].
×
92
        "call the helper method to build the object and store it in a variable"
×
93
        model := visitor model.
×
94
        visitor statementBlock addStatement: (model newVarDeclStatement
×
95
                         type: (object asFASTJavaTypeExpressionOn: visitor);
×
96
                         addDeclarator: (model newVariableDeclarator
×
97
                                          variable:
×
98
                                                  (model newVariableExpression name:
×
99
                                                                   (visitor varNameFor: object));
×
NEW
100
                                          expression:
×
NEW
101
                                                  (self
×
NEW
102
                                                           makeHelperInvocation: helper
×
NEW
103
                                                           arguments: attributes
×
NEW
104
                                                           on: visitor));
×
105
                         yourself)
×
106
]
×
107

108
{ #category : 'initialization' }
109
FamixValueHelperObjectExportStrategy >> initialize [
×
110

×
111
        declarations := Dictionary new
×
UNCOV
112
]
×
113

114
{ #category : 'exporting' }
NEW
115
FamixValueHelperObjectExportStrategy >> makeHelperInvocation: helper arguments: arguments on: visitor [
×
NEW
116

×
NEW
117
        | model |
×
NEW
118
        model := visitor model.
×
NEW
119
        ^ model newMethodInvocation
×
NEW
120
                  name: helper name;
×
NEW
121
                  arguments: (arguments
×
NEW
122
                                   with: helper parameters
×
NEW
123
                                   collect: [ :argument :parameter | "reference the value with a variable"
×
NEW
124
                                           | ref |
×
NEW
125
                                           ref := visitor makeVariableExpression: argument value.
×
NEW
126
                                           ((argument value type name sameAs: 'long') not and: [
×
NEW
127
                                                            (FASTJavaExportVisitor new export: parameter type) sameAs:
×
NEW
128
                                                                            'long' ])
×
NEW
129
                                                   ifTrue: [ "try casting"
×
NEW
130
                                                           model newCastExpression
×
NEW
131
                                                                   type: (model newClassTypeExpression typeName:
×
NEW
132
                                                                                            (model newTypeName name: 'long'));
×
NEW
133
                                                                   expression: ref ]
×
NEW
134
                                                   ifFalse: [ ref ] ])
×
NEW
135
]
×
136

137
{ #category : 'ast' }
138
FamixValueHelperObjectExportStrategy >> makeMethodFor: aFamixType withParametersFor: attributes on: visitor [
×
139
        "Make the method entity, handles its signature and modifiers."
×
140

×
UNCOV
141
        | model |
×
UNCOV
142
        model := visitor model.
×
143
        ^ model newMethodEntity
×
144
                  name: 'new' , aFamixType baseName;
×
145
                  addModifier: (model newModifier token: 'public');
×
146
                  addModifier: (model newModifier token: 'static');
×
147
                  type: (aFamixType asFASTJavaTypeExpressionOn: visitor);
×
148
                  parameters: (attributes collect: [ :attribute |
×
149
                                           model newParameter
×
150
                                                   type: (attribute attribute declaredType
×
151
                                                                    ifNotNil: [ :declaredType |
×
152
                                                                            attribute value type
×
153
                                                                                    decorate: declaredType
×
154
                                                                                    asFASTJavaTypeExpressionOn: visitor ]
×
155
                                                                    ifNil: [
×
156
                                                                            model newClassTypeExpression typeName:
×
157
                                                                                            (model newTypeName name: 'Object') ]);
×
158
                                                   variable:
×
159
                                                           (model newVariableExpression name: attribute varName) ]);
×
160
                  statementBlock: visitor statementBlock
×
161
]
×
162

163
{ #category : 'ast' }
UNCOV
164
FamixValueHelperObjectExportStrategy >> makeSetterInvocationsFor: attributes on: visitor [
×
UNCOV
165
        "Build the helper method's body: set the value of each argument to the corresponding attribute using setters or reflection."
×
166

×
167
        attributes do: [ :attribute |
×
168
                attribute attribute declaredType ifNotNil: [ :declaredType |
×
169
                        visitor builder processType: declaredType ].
×
170
                (attribute object type findSetterOf: attribute attribute)
×
171
                        ifNotNil: [ :setter |
×
172
                                visitor
×
173
                                        makeSetterInvocation: setter
×
174
                                        for: attribute
×
NEW
175
                                        named: attribute varName.
×
NEW
176

×
NEW
177
                                "handle setters that can throw exceptions to rethrow an unchecked exception"
×
NEW
178
                                setter thrownExceptions ifNotEmpty: [ :exceptions |
×
NEW
179
                                        visitor statementBlock addStatement: (visitor
×
NEW
180
                                                         makeTry: visitor statementBlock statements last
×
NEW
181
                                                         catch: exceptions) ] ]
×
182
                        ifNil: [ visitor makeReflectionSetterInvocation: attribute ] ]
×
183
]
×
184

185
{ #category : 'ast' }
186
FamixValueHelperObjectExportStrategy >> makeVariableExpression: value on: visitor [
×
187
        "Used when constructing an object to correctly name the argument variable.
×
188
        For helpers, the var name is the same as the helper method parameter."
×
189

×
190
        ^ visitor model newVariableExpression name: value varName
×
191
]
×
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