• 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

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
                'currentDeclarations'
11
        ],
12
        #category : 'Famix-Value-Exporter-Strategies',
13
        #package : 'Famix-Value-Exporter',
14
        #tag : 'Strategies'
15
}
16

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

×
22
        | model oldStatementBlock helper |
×
UNCOV
23
        model := visitor model.
×
UNCOV
24
        "swap visitor context: the 'sheet' on which the visitor writes"
×
25
        oldStatementBlock := visitor statementBlock.
×
26
        visitor statementBlock: model newStatementBlock.
×
27
        visitor builder processType: object type.
×
28
        "make the method entity"
×
29
        helper := self
×
30
                          makeMethodFor: object type
×
31
                          withParametersFor: attributes
×
32
                          on: visitor.
×
33
        "make the method body: instantiate object, set attributes and return it"
×
34
        visitor constructObject: object.
×
35
        self
×
36
                makeSetterInvocationsFor:
×
37
                (visitor filterAttributesToSet: attributes for: object type)
×
38
                on: visitor.
×
39
        visitor statementBlock addStatement:
×
40
                (model newReturnStatement expression:
×
41
                         (model newVariableExpression name: (visitor varNameFor: object))).
×
42
        "restore original visitor context"
×
43
        visitor statementBlock: oldStatementBlock.
×
44
        ^ helper
×
45
]
×
46

47
{ #category : 'accessing' }
48
FamixValueHelperObjectExportStrategy >> currentDeclarations [
×
49

×
50
        ^ currentDeclarations
×
51
]
×
52

53
{ #category : 'ast' }
54
FamixValueHelperObjectExportStrategy >> dependencyImportsOn: aFASTModel [
×
55

×
56
        ^ declarations keys collect: [ :package |
×
UNCOV
57
                  (aFASTModel newImportDeclaration qualifiedName:
×
UNCOV
58
                                   (aFASTModel newQualifiedName name: package , '.ModestHelper'))
×
59
                          isStatic: true;
×
60
                          isOnDemand: true ]
×
61
]
×
62

63
{ #category : 'exporting' }
UNCOV
64
FamixValueHelperObjectExportStrategy >> export: object on: visitor [
×
65
        "Use a helper method to instantiate the object and set its attributes."
×
66

×
67
        | attributes helper model |
×
68
        "ensure all relevant attributes are set up"
×
69
        (attributes := object relevantAttributes) do: [ :attribute |
×
70
                visitor ensureVisited: attribute value ].
×
71
        "check if the object was not set up in the previous step"
×
72
        (visitor varNameDict includesKey: object) ifTrue: [ ^ self ].
×
UNCOV
73

×
UNCOV
74
        "get the helper method, or build it if it does not exist"
×
75
        currentDeclarations := declarations
×
76
                                       at: (visitor helperPackageFor: object type)
×
77
                                       ifAbsentPut: [ Dictionary new ].
×
78
        helper := currentDeclarations
×
79
                          at: object constructorSignature
×
80
                          ifAbsentPut: [
×
81
                                  self
×
82
                                          buildMethodFor: object
×
83
                                          withParametersFor: attributes
×
84
                                          on: visitor ].
×
85

×
86
        "call the helper method to build the object and store it in a variable"
×
87
        model := visitor model.
×
88
        visitor statementBlock addStatement: (model newVarDeclStatement
×
89
                         type: (object asFASTJavaTypeExpressionOn: visitor);
×
90
                         addDeclarator: (model newVariableDeclarator
×
91
                                          variable:
×
92
                                                  (model newVariableExpression name:
×
93
                                                                   (visitor varNameFor: object));
×
94
                                          expression:
×
95
                                                  (self
×
96
                                                           makeHelperInvocation: helper
×
97
                                                           arguments: attributes
×
98
                                                           on: visitor));
×
99
                         yourself)
×
100
]
×
101

102
{ #category : 'initialization' }
103
FamixValueHelperObjectExportStrategy >> initialize [
×
104

×
105
        declarations := Dictionary new
×
106
]
×
107

108
{ #category : 'exporting' }
109
FamixValueHelperObjectExportStrategy >> makeHelperInvocation: helper arguments: arguments on: visitor [
×
110

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

131
{ #category : 'ast' }
132
FamixValueHelperObjectExportStrategy >> makeHelpersOn: visitor [
×
133

×
134
        declarations keysAndValuesDo: [ :package :decls |
×
135
                | helperClass |
×
UNCOV
136
                helperClass := visitor makeHelperInPackage: package.
×
UNCOV
137
                decls do: [ :declaration | helperClass addDeclaration: declaration ] ]
×
138
]
×
139

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

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

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

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

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

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

×
UNCOV
193
        ^ visitor model newVariableExpression name: value varName
×
UNCOV
194
]
×
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