diff --git a/src/BaselineOfCarrefour/BaselineOfCarrefour.class.st b/src/BaselineOfCarrefour/BaselineOfCarrefour.class.st index 0cc805e..65477d5 100644 --- a/src/BaselineOfCarrefour/BaselineOfCarrefour.class.st +++ b/src/BaselineOfCarrefour/BaselineOfCarrefour.class.st @@ -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' ) ] ] diff --git a/src/Carrefour-Exporter-Tests/CRFExporterTest.class.st b/src/Carrefour-Exporter-Tests/CRFExporterTest.class.st new file mode 100644 index 0000000..ded9d3e --- /dev/null +++ b/src/Carrefour-Exporter-Tests/CRFExporterTest.class.st @@ -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; + } + + +}' +] diff --git a/src/Carrefour-Exporter-Tests/package.st b/src/Carrefour-Exporter-Tests/package.st new file mode 100644 index 0000000..fea4ab8 --- /dev/null +++ b/src/Carrefour-Exporter-Tests/package.st @@ -0,0 +1 @@ +Package { #name : #'Carrefour-Exporter-Tests' } diff --git a/src/Carrefour-Exporter/CRFExporter.class.st b/src/Carrefour-Exporter/CRFExporter.class.st index 4b0ace2..e472861 100644 --- a/src/Carrefour-Exporter/CRFExporter.class.st +++ b/src/Carrefour-Exporter/CRFExporter.class.st @@ -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 = '' 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 ] ] +]