Skip to content

Commit 3d58568

Browse files
committed
Refactor functions for creating FAMIX elements to reduce redundancy
1 parent 5c28505 commit 3d58568

1 file changed

Lines changed: 58 additions & 53 deletions

File tree

generator/org.svis.generator/src/org/svis/generator/famix/Famix2Famix.xtend

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import org.neo4j.graphdb.Node
4747
import org.neo4j.graphdb.Label
4848
import org.neo4j.graphdb.Direction
4949
import org.neo4j.graphdb.traversal.Uniqueness
50+
import org.svis.xtext.famix.FAMIXElement
5051

5152
class Famix2Famix extends WorkflowComponentWithConfig {
5253
var GraphDatabaseService graph
@@ -119,73 +120,39 @@ class Famix2Famix extends WorkflowComponentWithConfig {
119120
}
120121
switch (type) {
121122
case "Package": {
122-
var namespace = createNamespace(node)
123-
namespaces.put(node.id, namespace)
124-
famixDocument.elements += namespace
123+
var FAMIXNamespace parentNamespace = null
125124
if(parent !== null) {
126-
val parentNamespace = namespaces.get(parent.id)
127-
var ref = famixFactory.createIntegerReference
128-
ref.ref = parentNamespace
129-
namespace.parentScope = ref
125+
parentNamespace = namespaces.get(parent.id)
130126
}
127+
val namespace = createNamespace(node, parentNamespace)
128+
namespaces.put(node.id, namespace)
129+
famixDocument.elements += namespace
131130
}
132131
case "Class": {
133-
val class = createClass(node)
132+
var FAMIXElement container
134133
if(parent.hasLabel(packageLabel)) {
135-
val parentNamespace = namespaces.get(parent.id)
136-
var ref = famixFactory.createIntegerReference
137-
ref.ref = parentNamespace
138-
class.container = ref
139-
classes.put(node.id, class)
140-
famixDocument.elements += class
134+
container = namespaces.get(parent.id) as FAMIXNamespace
141135
}
136+
142137
if(parent.hasLabel(classLabel)) {
143-
val parentClass = classes.get(parent.id)
144-
var ref = famixFactory.createIntegerReference
145-
ref.ref = parentClass
146-
class.container = ref
147-
famixDocument.elements += class
148-
classes.put(node.id, class)
138+
container = classes.get(parent.id) as FAMIXClass
149139
}
140+
val class = createClass(node, container)
141+
classes.put(node.id, class)
142+
famixDocument.elements += class
150143
}
151144
case "Method": {
152-
val method = createMethod(node)
153145
val parentClass = classes.get(parent.id)
154-
var ref = famixFactory.createIntegerReference
155-
ref.ref = parentClass
156-
method.parentType = ref
157146
val fileAnchor = famixFactory.createFAMIXFileAnchor
158-
if(node.hasProperty("firstLineNumber")) {
159-
val firstLineNumber = node.getProperty("firstLineNumber") as Long
160-
fileAnchor.startline = firstLineNumber.intValue
161-
}
162-
if(node.hasProperty("lastLineNumber")) {
163-
val lastLineNumber = node.getProperty("lastLineNumber") as Long
164-
fileAnchor.endline = lastLineNumber.intValue
165-
}
166147
fileAnchor.filename = parent.getProperty("sourceFileName") as String
167-
var anchorRef = famixFactory.createIntegerReference
168-
anchorRef.ref = method
169-
var methodRef = famixFactory.createIntegerReference
170-
methodRef.ref = fileAnchor
171-
fileAnchor.element = methodRef
172-
method.sourceAnchor = anchorRef
148+
val method = createMethod(node, parentClass, fileAnchor)
173149
famixDocument.elements += method
174150
}
175151
case "Field": {
176-
val attribute = createAttribute(node)
177152
val parentClass = classes.get(parent.id)
178-
var ref = famixFactory.createIntegerReference
179-
ref.ref = parentClass
180-
attribute.parentType = ref
181153
val fileAnchor = famixFactory.createFAMIXFileAnchor
182-
val anchorRef = famixFactory.createIntegerReference
183-
val attributeRef = famixFactory.createIntegerReference
184154
fileAnchor.filename = parent.getProperty("sourceFileName") as String
185-
anchorRef.ref = attribute
186-
attributeRef.ref = fileAnchor
187-
fileAnchor.element = attributeRef
188-
attribute.sourceAnchor = anchorRef
155+
val attribute = createAttribute(node, parentClass, fileAnchor)
189156
famixDocument.elements+= attribute
190157
}
191158
}
@@ -694,17 +661,22 @@ class Famix2Famix extends WorkflowComponentWithConfig {
694661
structures.removeAll(duplicateAnnotationTypes)
695662
}
696663

697-
def createNamespace(Node node) {
664+
def createNamespace(Node node, FAMIXNamespace parent) {
698665
val namespace = famixFactory.createFAMIXNamespace
699666
val id = node.id.toString
700667
namespace.name = id
701668
namespace.id = id
702669
namespace.value = node.getProperty("name") as String
703670
namespace.fqn = node.getProperty("fqn") as String
671+
if(parent !== null) {
672+
var ref = famixFactory.createIntegerReference
673+
ref.ref = parent
674+
namespace.parentScope = ref
675+
}
704676
return namespace
705677
}
706678

707-
def createClass(Node node) {
679+
def createClass(Node node, FAMIXElement parent) {
708680
val class = famixFactory.createFAMIXClass
709681
class.name = node.id.toString
710682
if(node.hasProperty("name")) {
@@ -716,6 +688,14 @@ class Famix2Famix extends WorkflowComponentWithConfig {
716688
if(node.hasProperty("md5")) {
717689
class.id = node.getProperty("md5") as String
718690
}
691+
var ref = famixFactory.createIntegerReference
692+
if(parent instanceof FAMIXNamespace) {
693+
ref.ref = parent as FAMIXNamespace
694+
}
695+
else {
696+
ref.ref = parent as FAMIXClass
697+
}
698+
class.container = ref
719699
val anchorRef = famixFactory.createIntegerReference
720700
val classRef = famixFactory.createIntegerReference
721701
val fileAnchor = famixFactory.createFAMIXFileAnchor
@@ -727,7 +707,7 @@ class Famix2Famix extends WorkflowComponentWithConfig {
727707
return class
728708
}
729709

730-
def createMethod(Node node) {
710+
def createMethod(Node node, FAMIXClass parent, FAMIXFileAnchor fileAnchor) {
731711
val method = famixFactory.createFAMIXMethod
732712
method.name = node.id.toString
733713
if(node.hasProperty("name")) {
@@ -737,16 +717,32 @@ class Famix2Famix extends WorkflowComponentWithConfig {
737717
val cyclomaticComplexity = node.getProperty("cyclomaticComplexity") as Long
738718
method.cyclomaticComplexity = cyclomaticComplexity.intValue
739719
}
740-
741720
if(node.hasProperty("effectiveLineCount")) {
742721
val numberOfStatements = node.getProperty("effectiveLineCount") as Long
743722
method.numberOfStatements = numberOfStatements.intValue
744723
}
745724
method.signature = node.getProperty("signature") as String
725+
var ref = famixFactory.createIntegerReference
726+
ref.ref = parent
727+
method.parentType = ref
728+
if(node.hasProperty("firstLineNumber")) {
729+
val firstLineNumber = node.getProperty("firstLineNumber") as Long
730+
fileAnchor.startline = firstLineNumber.intValue
731+
}
732+
if(node.hasProperty("lastLineNumber")) {
733+
val lastLineNumber = node.getProperty("lastLineNumber") as Long
734+
fileAnchor.endline = lastLineNumber.intValue
735+
}
736+
var anchorRef = famixFactory.createIntegerReference
737+
anchorRef.ref = method
738+
var methodRef = famixFactory.createIntegerReference
739+
methodRef.ref = fileAnchor
740+
fileAnchor.element = methodRef
741+
method.sourceAnchor = anchorRef
746742
return method
747743
}
748744

749-
def createAttribute(Node node) {
745+
def createAttribute(Node node, FAMIXClass parent, FAMIXFileAnchor fileAnchor) {
750746
val attribute = famixFactory.createFAMIXAttribute
751747
attribute.name = node.id.toString
752748
if(node.hasProperty("name")) {
@@ -755,6 +751,15 @@ class Famix2Famix extends WorkflowComponentWithConfig {
755751
if(node.hasProperty("visibility")) {
756752
attribute.modifiers+= node.getProperty("visibility") as String
757753
}
754+
var ref = famixFactory.createIntegerReference
755+
ref.ref = parent
756+
attribute.parentType = ref
757+
val anchorRef = famixFactory.createIntegerReference
758+
val attributeRef = famixFactory.createIntegerReference
759+
anchorRef.ref = attribute
760+
attributeRef.ref = fileAnchor
761+
fileAnchor.element = attributeRef
762+
attribute.sourceAnchor = anchorRef
758763
return attribute
759764
}
760765
}

0 commit comments

Comments
 (0)