Skip to content

Commit 01df2dd

Browse files
Merge bf47b28
2 parents d55b885 + bf47b28 commit 01df2dd

11 files changed

+72
-116
lines changed

src/Famix-Value-Entities-Extensions/FamixValueOfObjectAttribute.extension.st

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ FamixValueOfObjectAttribute >> mooseNameOn: aStream [
99
ifNil: [ '<attribute>' ]) << $=.
1010
value printValueOn: aStream
1111
]
12+
13+
{ #category : #'*Famix-Value-Entities-Extensions' }
14+
FamixValueOfObjectAttribute >> varName [
15+
16+
^ self attribute
17+
ifNotNil: [ :attribute | attribute name ]
18+
ifNil: [ value varName ]
19+
]

src/Famix-Value-Entities/FamixValueOfObjectAttribute.class.st

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,3 @@ FamixValueOfObjectAttribute >> value: anObject [
7878
<generated>
7979
value := anObject
8080
]
81-
82-
{ #category : #generate }
83-
FamixValueOfObjectAttribute >> varName [
84-
85-
^ self attribute
86-
ifNotNil: [ :attribute | attribute name ]
87-
ifNil: [ value varName ]
88-
]

src/Famix-Value-Entities/FamixValueOfType.class.st

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,6 @@ FamixValueOfType >> addValueInDictionary: anObject [
7070
^ self valueInDictionaries add: anObject
7171
]
7272

73-
{ #category : #'ston persistence' }
74-
FamixValueOfType >> asJsonString [
75-
"Currently does not work. Ideas:
76-
- Find a way to serialize any object while handling circular dependencies.
77-
- Use the json stored in the root value to read it into raw objects and traverse the path to this value."
78-
79-
self flag: #TODO.
80-
^ self asNeoJSONObject asString
81-
]
82-
83-
{ #category : #'ston persistence' }
84-
FamixValueOfType >> asNeoJSONObject [
85-
86-
self flag: #TODO. "see #asJsonString"
87-
^ self typedEntity
88-
ifNotNil: [ "at root"
89-
NeoJSONObject fromString: (self cacheAt: #json ifAbsent: '{}') ]
90-
ifNil: [ "how to iterate until root?"
91-
NeoJSONObject fromString: (self cacheAt: #json ifAbsent: '{}') ]
92-
]
93-
9473
{ #category : #accessing }
9574
FamixValueOfType >> attributeInObjects [
9675
"Relation named: #attributeInObjects type: #FamixValueOfObjectAttribute opposite: #value"

src/Famix-Value-Exporter/FASTBuilder.class.st

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
"
2+
I am a helper for the `FamixValue2ASTVisitor` hierarchy of classes to build AST.
3+
"
14
Class {
25
#name : #FASTBuilder,
36
#superclass : #Object,
47
#instVars : [
58
'model'
69
],
7-
#category : #'Famix-Value-Exporter'
10+
#category : #'Famix-Value-Exporter-Helpers'
811
}
912

1013
{ #category : #testing }

src/Famix-Value-Exporter/FASTJavaBuilder.class.st

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
"
2+
I am used to handle Java type references and imports.
3+
"
14
Class {
25
#name : #FASTJavaBuilder,
36
#superclass : #FASTBuilder,
47
#instVars : [
58
'typeNameDictionary'
69
],
7-
#category : #'Famix-Value-Exporter'
10+
#category : #'Famix-Value-Exporter-Helpers'
811
}
912

1013
{ #category : #ast }

src/Famix-Value-Exporter/FamixValue2ASTVisitor.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Class {
1010
'id',
1111
'statementBlock'
1212
],
13-
#category : #'Famix-Value-Exporter'
13+
#category : #'Famix-Value-Exporter-Visitors'
1414
}
1515

1616
{ #category : #testing }

src/Famix-Value-Exporter/FamixValue2FASTJavaVisitor.class.st

Lines changed: 23 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"
2-
A visitor for exporting a FamixValue model to FASTJava.
2+
A visitor to export a FamixValue model to FASTJava.
33
The generated code is a block statement with a variable declared for each value.
44
55
Object attributes are initialized by finding the corresponding setters.
6-
Collections and Dictionaries are constructed with the `add(element)` and `put(key, value)` methods respectively.
6+
Collections and Dictionaries are constructed with the methods `add(element)` and `put(key, value)` respectively.
77
8-
When exporting a `FamixValueOfObjectAttribute`, if the setter for its `FamixTAttribute` cannot be found, they are set using the Java Reflection API.
9-
All attributes that fall in this category are added as `markedForReflection`.
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`.
1010
"
1111
Class {
1212
#name : #FamixValue2FASTJavaVisitor,
@@ -16,10 +16,9 @@ Class {
1616
'markedForReflection',
1717
'constructorCache',
1818
'staticAttributesCache',
19-
'objectExportStrategy',
20-
'setterCache'
19+
'objectExportStrategy'
2120
],
22-
#category : #'Famix-Value-Exporter'
21+
#category : #'Famix-Value-Exporter-Visitors'
2322
}
2423

2524
{ #category : #private }
@@ -37,8 +36,10 @@ FamixValue2FASTJavaVisitor >> addAttributesFrom: object asArgumentsTo: newExpres
3736
object value
3837
detect: [ :objAttribute | "find the matching value attribute"
3938
objAttribute attribute == paramAttribute ]
40-
ifFound: [ :objAttribute |
41-
self makeVariableExpression: objAttribute value ]
39+
ifFound: [ :objAttribute | "dispatch for var naming context"
40+
objectExportStrategy
41+
makeVariableExpression: objAttribute value
42+
on: self ]
4243
ifNone: [ "the object does not have the attribute set"
4344
paramAttribute declaredType asFASTJavaDefaultValueOn:
4445
self model ] ]) ]
@@ -72,21 +73,16 @@ FamixValue2FASTJavaVisitor >> constructObject: object [
7273
FamixValue2FASTJavaVisitor >> filterAttributesToSet: attributes for: object [
7374
"No need to set attributes that are set in the constructor."
7475

75-
^ (setterCache at: object type ifAbsentPut: [
76-
| famixAttributes |
77-
famixAttributes := attributes collect: [ :attribute |
78-
attribute attribute ].
79-
constructorCache
80-
at: object type
81-
ifPresent: [ :constructor |
82-
constructorCache
83-
at: constructor
84-
ifPresent: [ :constructorAttributes |
85-
famixAttributes difference: constructorAttributes ]
86-
ifAbsent: [ famixAttributes ] ]
87-
ifAbsent: [ famixAttributes ] ]) ifNotEmpty: [ :famixAttributes |
88-
attributes select: [ :attribute |
89-
famixAttributes includes: attribute attribute ] ]
76+
^ constructorCache
77+
at: object type
78+
ifPresent: [ :constructor |
79+
constructorCache
80+
at: constructor
81+
ifPresent: [ :constructorAttributes |
82+
attributes reject: [ :attribute |
83+
constructorAttributes includes: attribute attribute ] ]
84+
ifAbsent: [ attributes ] ]
85+
ifAbsent: [ attributes ]
9086
]
9187

9288
{ #category : #private }
@@ -136,7 +132,6 @@ FamixValue2FASTJavaVisitor >> findStaticAttributeMatching: object [
136132
FamixValue2FASTJavaVisitor >> initialize [
137133

138134
constructorCache := IdentityDictionary new.
139-
setterCache := IdentityDictionary new.
140135
staticAttributesCache := IdentityDictionary new
141136
]
142137

@@ -191,49 +186,7 @@ FamixValue2FASTJavaVisitor >> makeHelperClass [
191186
expression:
192187
(model newNewExpression type:
193188
(model newClassTypeExpression typeName:
194-
(model newTypeName name: 'ObjectMapper')))) }).
195-
(model newMethodEntity
196-
typeParameters:
197-
{ (model newTypeParameterExpression name: 'T') };
198-
type:
199-
(model newClassTypeExpression typeName:
200-
(model newTypeName name: 'T'));
201-
name: 'deserialize';
202-
parameters: {
203-
(model newParameter
204-
variable: (model newVariableExpression name: 'json');
205-
type: (model newClassTypeExpression typeName:
206-
(model newTypeName name: 'String'))).
207-
(model newParameter
208-
variable: (model newVariableExpression name: 'clazz');
209-
type: (model newClassTypeExpression
210-
typeName: (model newTypeName name: 'Class');
211-
arguments: { (model newClassTypeExpression typeName:
212-
(model newTypeName name: 'T')) })) };
213-
modifiers: {
214-
(model newModifier token: 'public').
215-
(model newModifier token: 'static') };
216-
statementBlock: (model newStatementBlock statements: {
217-
(model newTryCatchStatement
218-
try: (model newStatementBlock statements:
219-
{ (model newReturnStatement expression:
220-
(model newMethodInvocation
221-
receiver: (model newIdentifier name: 'mapper');
222-
name: 'readValue';
223-
arguments: {
224-
(model newVariableExpression name: 'json').
225-
(model newVariableExpression name: 'clazz') })) });
226-
catches: { (model newCatchPartStatement
227-
catchedTypes:
228-
{ (model newClassTypeExpression typeName:
229-
(model newTypeName name: 'IOException')) };
230-
body: (model newStatementBlock statements:
231-
{ (model newExpressionStatement expression:
232-
(model newMethodInvocation
233-
receiver: (model newIdentifier name: 'e');
234-
name: 'printStackTrace')) });
235-
parameter: (model newVariableExpression name: 'e')) }).
236-
(model newReturnStatement expression: model newNullLiteral) })) }
189+
(model newTypeName name: 'ObjectMapper')))) }) }
237190
]
238191

239192
{ #category : #ast }
@@ -455,20 +408,9 @@ FamixValue2FASTJavaVisitor >> visitObjectOfRegularType: object [
455408

456409
{ #category : #visiting }
457410
FamixValue2FASTJavaVisitor >> visitObjectStub: object [
411+
"Previously tried to recreate stubs by deserializing their JSON representation, but getting a correct JSON string is a challenge."
458412

459-
self statementBlock addStatement: (self model newVarDeclStatement
460-
type: (object asFASTJavaTypeExpressionOn: self);
461-
addDeclarator: (self model newVariableDeclarator
462-
variable: (self makeVariableExpression: object);
463-
expression: (model newMethodInvocation
464-
name: 'deserialize';
465-
arguments: {
466-
(model newStringLiteral primitiveValue:
467-
object asJsonString).
468-
(model newClassProperty
469-
type: (object asFASTJavaTypeExpressionOn: self);
470-
fieldName: 'class') }));
471-
yourself)
413+
self shouldBeImplemented
472414
]
473415

474416
{ #category : #visiting }

src/Famix-Value-Exporter/FamixValue2PharoVisitor.class.st

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"
2-
A visitor for exporting a FamixValue model to the Pharo AST, see `RBEntity` and `RBNode`.
2+
A visitor for exporting a FamixValue model to the Pharo AST, see `RBNode`.
33
44
Object attributes are initialized by finding the corresponding setters.
5-
Collections and Dictionaries are constructed with the `withAll:` class method.
5+
Collections are constructed using the `withAll:` class method.
6+
Dictionaries are constructed using the `newFrom:` class method.
67
"
78
Class {
89
#name : #FamixValue2PharoVisitor,
910
#superclass : #FamixValue2ASTVisitor,
10-
#category : #'Famix-Value-Exporter'
11+
#category : #'Famix-Value-Exporter-Visitors'
1112
}
1213

1314
{ #category : #visiting }

src/Famix-Value-Exporter/FamixValueAbstractObjectExportStrategy.class.st

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"
2+
I am a strategy for exporting objects used by the `FamixValue2ASTVisitor` hierarchy of classes.
3+
Objects can be complex to recreate due to deep nesting and circular dependencies, resulting in a lot of code. My subclasses define where that code goes.
4+
"
15
Class {
26
#name : #FamixValueAbstractObjectExportStrategy,
37
#superclass : #Object,

src/Famix-Value-Exporter/FamixValueHelperObjectExportStrategy.class.st

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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+
"
15
Class {
26
#name : #FamixValueHelperObjectExportStrategy,
37
#superclass : #FamixValueAbstractObjectExportStrategy,
@@ -133,3 +137,11 @@ FamixValueHelperObjectExportStrategy >> makeSetterInvocationsFor: attributes on:
133137
named: attribute varName ]
134138
ifNil: [ visitor makeReflectionSetterInvocation: attribute ] ]
135139
]
140+
141+
{ #category : #ast }
142+
FamixValueHelperObjectExportStrategy >> makeVariableExpression: value on: visitor [
143+
"Used when constructing an object to correctly name the argument variable.
144+
For helpers, the var name is the same as the helper method parameter."
145+
146+
^ visitor model newVariableExpression name: value varName
147+
]

src/Famix-Value-Exporter/FamixValueInlineObjectExportStrategy.class.st

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"
2+
I recreate objects inline.
3+
The context of the visitor will contain the code to recreate the objects.
4+
"
15
Class {
26
#name : #FamixValueInlineObjectExportStrategy,
37
#superclass : #FamixValueAbstractObjectExportStrategy,
@@ -11,3 +15,11 @@ FamixValueInlineObjectExportStrategy >> export: object on: visitor [
1115
(visitor filterAttributesToSet: object relevantAttributes for: object)
1216
do: [ :attribute | visitor visitObjectAttribute: attribute ]
1317
]
18+
19+
{ #category : #ast }
20+
FamixValueInlineObjectExportStrategy >> makeVariableExpression: value on: visitor [
21+
"Used when constructing an object to correctly name the argument variable.
22+
When inline, the visitor can handle naming the variable on its own."
23+
24+
^ visitor makeVariableExpression: value
25+
]

0 commit comments

Comments
 (0)