Skip to content

Commit

Permalink
first version of carrefour exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
badetitou committed May 2, 2024
1 parent 70aaa95 commit f18f54b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 12 deletions.
33 changes: 22 additions & 11 deletions src/BaselineOfCarrefour/BaselineOfCarrefour.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,33 @@ BaselineOfCarrefour >> defineGroups: spec [

{ #category : #baselines }
BaselineOfCarrefour >> definePackages: spec [

spec
repository: 'https://github.com/badetitou/Carrefour';
package: 'Carrefour-Model' with: [ spec requires: #('FASTJava') ];
package: 'Carrefour-Model' with: [ spec requires: #( 'FASTJava' ) ];
package: 'Carrefour-Model-Generator'
with: [ spec requires: #('FASTJava') ];
with: [ spec requires: #( 'FASTJava' ) ];
package: 'Carrefour-FastAndBindingGenerator'
with: [ spec requires: #('FASTJava') ];
with: [ spec requires: #( 'FASTJava' ) ];
package: 'Carrefour-Tests'
with: [ spec requires: #('FASTJava' 'Carrefour-FastAndBindingGenerator') ];
package: 'Carrefour-FastAndBindingGenerator-Tests'
with: [ spec
requires: #('FASTJava' 'Carrefour-FastAndBindingGenerator' 'Carrefour-Tests') ];
with: [
spec requires: #( 'FASTJava' 'Carrefour-FastAndBindingGenerator' ) ];
package: 'Carrefour-FastAndBindingGenerator-Tests' with: [
spec requires: #( 'FASTJava' 'Carrefour-FastAndBindingGenerator'
'Carrefour-Tests' ) ];
package: 'Carrefour-Extension'
with: [ spec requires: #('FASTJava') ];
with: [ spec requires: #( 'FASTJava' ) ];
package: 'Carrefour-RemoveBinding'
with: [ spec requires: #('FASTJava' 'Carrefour-FastAndBindingGenerator') ];
with: [
spec requires: #( 'FASTJava' 'Carrefour-FastAndBindingGenerator' ) ];
package: 'Carrefour-RemoveBinding-Tests'
with: [ spec requires: #('Carrefour-RemoveBinding' 'Carrefour-Tests') ]
with: [
spec requires: #( 'Carrefour-RemoveBinding'
'Carrefour-Tests' ) ].
"Exporter"
spec
package: 'Carrefour-Exporter'
with: [ spec requires: #( 'Carrefour-Model' ) ].
spec
package: 'Carrefour-Exporter-Tests'
with: [ spec requires: #( 'Carrefour-Exporter' ) ]
]
63 changes: 63 additions & 0 deletions src/Carrefour-Exporter-Tests/CRFExporterTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"
A CRFExporterTest is a test class for testing the behavior of CRFExporter
"
Class {
#name : #CRFExporterTest,
#superclass : #TestCase,
#instVars : [
'exporter'
],
#category : #'Carrefour-Exporter-Tests'
}

{ #category : #running }
CRFExporterTest >> astFor: sourceCode withRule: rule [
"same as in `FASTJavaExportVisitorTest`"

^ JavaSmaCCProgramNodeImporterVisitor new accept: (JavaParser
createParserOnStream: (ReadStream on: sourceCode)
startingAt: (JavaParser perform: rule)) parse
]

{ #category : #running }
CRFExporterTest >> methodAST: sourceCode [
"same as in `FASTJavaExportVisitorTest`"
^self astFor: sourceCode withRule: #startingStateForclass_or_interface_body_declaration

]

{ #category : #running }
CRFExporterTest >> setUp [
super setUp.
exporter := CRFExporter new.
exporter endOfLine: String cr
]

{ #category : #tests }
CRFExporterTest >> testExportOneClassWithOneMethodBinded [

| model resultString aClass method ast primitiveInt |
model := FamixJavaModel new.
aClass := model newClassNamed: 'DemoClass'.
method := model newMethodNamed: 'aMethod'.
primitiveInt := model newPrimitiveTypeNamed: 'int'.
method declaredType: primitiveInt.
aClass addMethod: method.

ast := self methodAST: 'int aMethod() { return 1; }'.

method fast: ast.

resultString := String streamContents: [ :stream |
exporter currentStream: stream.
aClass accept: exporter ].
self assert: resultString equals: 'public class DemoClass {
int aMethod() {
return 1;
}
}'
]
1 change: 1 addition & 0 deletions src/Carrefour-Exporter-Tests/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Carrefour-Exporter-Tests' }
55 changes: 54 additions & 1 deletion src/Carrefour-Exporter/CRFExporter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,59 @@ The basic idea is to modify the way the code of `FamixTMethod` is exported to sw
"
Class {
#name : #CRFExporter,
#superclass : #Object,
#superclass : #FAMIX2JavaVisitor,
#category : #'Carrefour-Exporter'
}

{ #category : #accessing }
CRFExporter >> visitMethod: aMethod [

aMethod isStub ifTrue: [ ^ self ].
self printMethodAnnotations: aMethod.
self indent.
aMethod isPrivate ifTrue: [ self <<< 'private ' ].
aMethod isProtected ifTrue: [ self <<< 'protected ' ].
aMethod isPublic ifTrue: [ self <<< 'public ' ].
aMethod isClassSide ifTrue: [ self <<< 'static ' ].
"Printing return type for method"
aMethod declaredType ifNotNil: [ :declaredType |
self printDeclaredType: declaredType.
currentStream << String space ].
"Printing name + parameters of method"
(aMethod name = '<Initializer>' or: [
aMethod isAnInitializer and: [ aMethod isConstructor not ] ])
ifFalse: [
self
<<< aMethod name;
<<< '('.
(aMethod parameters sorted: [ :p :p2 |
p sourceAnchor startPos < p2 sourceAnchor startPos ])
do: [ :parameter | parameter accept: self clone ]
separatedBy: [ self <<< ', ' ].
self <<< ')' ]
ifTrue: [ self << 'static' ].
"print exception"
((aMethod withMethodsOverriding collect: [ :m |
m thrownExceptions , m declaredExceptions ]) flattened asSet
asOrderedCollection sorted: #name ascending) ifNotEmpty: [
:exceptions |
self <<< ' throws '.
exceptions
do: [ :exception | self <<< exception name ]
separatedBy: [ self <<< ', ' ] ].

"Printing body of method if class is not abstract or an interface"
((aMethod atScope: FamixTClass) anyOne isInterface or: [
aMethod isAbstract isNotNil and: [ aMethod isAbstract ] ])
ifTrue: [ self <<< ';' ]
ifFalse: [
aMethod fast ifNotNil: [ :fastMethod |
| fastJavaExporterVisitor |
fastJavaExporterVisitor := FASTJavaExportVisitor new
outputStream: self currentStream;
indentSize: tabulationSize;
yourself.
self <<< ' '.
1 to: tabs do: [ :tab | fastJavaExporterVisitor indent ].
fastMethod statementBlock accept: fastJavaExporterVisitor ] ]
]

0 comments on commit f18f54b

Please sign in to comment.