Skip to content

Commit 0e1bfee

Browse files
committed
EDIT: Removed duplicate code by merging two methods.
1 parent 3cc3c8f commit 0e1bfee

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

src/main/java/jce/generators/WrapperRepresentation.xtend

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class WrapperRepresentation {
6767
/**
6868
* Unification class for the class «eClass.name»
6969
*/
70-
«IF eClass.abstract»abstract «ENDIF»class «wrapperName + getTypeParameters» extends «createSuperType(superClass)» implements «eClass.name + genericArguments» {
70+
«IF eClass.abstract»abstract «ENDIF»class «wrapperName + getParameters(true)» extends «createSuperType(superClass)» implements «eClass.name + getParameters(false)» {
7171

7272
«delegateAnnotation»
73-
protected var «eClass.name + genericArguments» ecoreImplementation
73+
protected var «eClass.name + getParameters(false)» ecoreImplementation
7474

7575
«IF superClass === null»
7676
// Methods of InternalEObject must also be delegated to the wrapped class
@@ -99,7 +99,7 @@ class WrapperRepresentation {
9999
def String getPackage() {
100100
return packageName
101101
}
102-
102+
103103
def private createContent(IJavaProject project) {
104104
packageName = getPackage(eClass)
105105
wrapperName = WRAPPER_PREFIX.get + eClass.name + WRAPPER_SUFFIX.get // name of the wrapper class
@@ -110,7 +110,7 @@ class WrapperRepresentation {
110110
ecoreImplementation = append(ECORE_PACKAGE.get, packageName, "impl", eClass.name + "Impl")
111111
typeParameters = TypeParameterGenerator.generate(eClass.ETypeParameters, ecoreImplementation, project, properties)
112112
importDeclarations = new HashSet // add import declarations:
113-
if(superClass === null) {
113+
if (superClass === null) {
114114
importDeclarations += InternalEObject.name
115115
importDeclarations += EObject.name
116116
importDeclarations += Notifier.name
@@ -123,7 +123,7 @@ class WrapperRepresentation {
123123
* Builds the super type declaration of a wrapper from a String that is either the super type or null.
124124
*/
125125
def private String createSuperType(String superClass) {
126-
if(superClass === null) {
126+
if (superClass === null) {
127127
return append(typeof(MinimalEObjectImpl).simpleName, typeof(MinimalEObjectImpl.Container).simpleName)
128128
}
129129
return superClass.getLastSegment
@@ -188,9 +188,9 @@ class WrapperRepresentation {
188188
*/
189189
def private String getInstanceMethod() '''
190190
«IF eClass.abstract»
191-
«methodKeyword» protected abstract «eClass.name + genericArguments» getInstance()
191+
«methodKeyword» protected abstract «eClass.name + getParameters(false)» getInstance()
192192
«ELSE»
193-
«methodKeyword» protected «eClass.name + genericArguments» getInstance() {
193+
«methodKeyword» protected «eClass.name + getParameters(false)» getInstance() {
194194
return «factoryName».eINSTANCE.create«eClass.name»
195195
}
196196
«ENDIF»
@@ -214,7 +214,7 @@ class WrapperRepresentation {
214214
«ENDIF»
215215
«ENDFOR»
216216
''' // TODO (HIGH) replace raw type parameter and add imports
217-
217+
218218
def private String getGenericArguments(EStructuralFeature feature) {
219219
var String result = ""
220220
for (argument : IntermediateModelSearcher.findField(feature, model).genericArguments) {
@@ -228,43 +228,37 @@ class WrapperRepresentation {
228228
*/
229229
def private String getSuperClassName(EClass eClass) {
230230
val EClass superType = getSuperClass(eClass)
231-
if(superType !== null) {
231+
if (superType !== null) {
232232
return append(getPackage(superType), superType.name)
233233
}
234234
return null
235235
}
236236
237237
/**
238-
* Generates the String of type type parameters with their respective bounds, e.g. "<T extends List<EString> & IFace<List<EString>>>"
238+
* Generates the String of type parameters.
239+
* @param includeBounds determines whether the String contains the parameters with their respective bounds, e.g. "<T extends List<EString> & IFace<List<EString>>>"
239240
*/
240-
def private String getTypeParameters() { // TODO (HIGH) remove duplicate code.
241-
if(typeParameters.empty) {
242-
return ""
243-
}
244-
val StringJoiner joiner = new StringJoiner(", ")
245-
for (parameter : typeParameters) {
246-
joiner.add(parameter.content)
241+
def private String getParameters(boolean includeBounds) {
242+
if (typeParameters.empty) {
243+
return "" // no type parameters at all
247244
}
248-
return '''<«joiner»>'''
249-
}
250-
251-
def private String getGenericArguments() { // TODO (HIGH) remove duplicate code.
252-
if(typeParameters.empty) {
253-
return ""
254-
}
255-
val StringJoiner joiner = new StringJoiner(", ")
245+
val StringJoiner joiner = new StringJoiner(", ") // join parameters with commas
256246
for (parameter : typeParameters) {
257-
joiner.add(parameter.name)
247+
if (includeBounds) {
248+
joiner.add(parameter.content) // name and bounds with their arguments
249+
} else {
250+
joiner.add(parameter.name) // only name
251+
}
258252
}
259-
return '''<«joiner»>'''
253+
return '''<«joiner»>''' // return parameters with generic brackets
260254
}
261255
262256
/**
263257
* Returns super class of an EClass or null if it has none.
264258
*/
265259
def private EClass getSuperClass(EClass eClass) {
266260
for (superType : eClass.ESuperTypes) {
267-
if(!superType.interface) {
261+
if (!superType.interface) {
268262
return superType
269263
}
270264
}
@@ -277,7 +271,7 @@ class WrapperRepresentation {
277271
def private String getPackage(EClass eClass) {
278272
var String package = ""
279273
var EPackage current = eClass.EPackage
280-
while(current !== null) { // iterate through package hierarchy
274+
while (current !== null) { // iterate through package hierarchy
281275
package = append(current.name, package) // concatenate package with super package name
282276
current = current.ESuperPackage
283277
}

0 commit comments

Comments
 (0)