Skip to content

Commit d498fe1

Browse files
committed
EDIT: Implemented an early version of special setters.
1 parent 635dd6d commit d498fe1

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import static jce.properties.TextProperty.WRAPPER_SUFFIX
1919
import org.eclipse.emf.ecore.InternalEObject
2020
import org.eclipse.emf.ecore.EObject
2121
import org.eclipse.emf.common.notify.Notifier
22+
import jce.util.EcoreToJavaUtil
2223

2324
/**
2425
* This class models a wrapper class which unifies an origin code type with its Ecore counterparts in the Ecore model
@@ -57,7 +58,7 @@ class WrapperRepresentation {
5758
ecoreImplementation = append(ECORE_PACKAGE.get, packageName, "impl", eClass.name + "Impl")
5859
typeParameters = TypeParameterGenerator.generate(eClass.ETypeParameters, ecoreImplementation, project, properties)
5960
importDeclarations = new HashSet // add import declarations:
60-
if (superClass === null) {
61+
if(superClass === null) {
6162
importDeclarations += InternalEObject.name
6263
importDeclarations += EObject.name
6364
importDeclarations += Notifier.name
@@ -87,10 +88,12 @@ class WrapperRepresentation {
8788
@DelegateExceptNotifier.simpleName», «EObject.simpleName»)
8889
protected var «InternalEObject.simpleName» internalEcoreImplementation
8990
«ENDIF»
90-
91+
9192
«constructors»
9293

9394
«instanceMethod»
95+
96+
«specialSetters»
9497
}
9598
'''
9699
@@ -166,6 +169,10 @@ class WrapperRepresentation {
166169
«FOR importDeclaration : importDeclarations»
167170
import «importDeclaration»
168171
«ENDFOR»
172+
173+
«IF eClass.EStructuralFeatures.exists[field | field.upperBound == -1
174+
import java.util.List
175+
«ENDIF»
169176
'''
170177
171178
/**
@@ -186,6 +193,21 @@ class WrapperRepresentation {
186193
*/
187194
def private String getMethodKeyword() '''«IF superClass === null»def«ELSE»override«ENDIF»'''
188195
196+
/**
197+
* Returns a special setter for every field which was extracted using multiplicities.
198+
*/
199+
def private String getSpecialSetters() '''
200+
«FOR field : eClass.EStructuralFeatures»
201+
«IF field.upperBound == -1»
202+
def protected void set«field.name.toFirstUpper» (List<«EcoreToJavaUtil.getFeatureType(field.EGenericType)»> «field.name») {
203+
get«field.name.toFirstUpper».clear
204+
get«field.name.toFirstUpper».addAll(«field.name»)
205+
}
206+
207+
«ENDIF»
208+
«ENDFOR»
209+
''' // TODO (HIGH) imports
210+
189211
/**
190212
* Returns the fully qualified name of the super class of an EClass.
191213
*/
@@ -200,7 +222,7 @@ class WrapperRepresentation {
200222
/**
201223
* Generates the String of type type parameters with their respective bounds, e.g. "<T extends List<EString> & IFace<List<EString>>>"
202224
*/
203-
def private String getTypeParameters() { // TODO (MEDIUM) remove duplicate code.
225+
def private String getTypeParameters() { // TODO (HIGH) remove duplicate code.
204226
if(typeParameters.empty) {
205227
return ""
206228
}
@@ -211,7 +233,7 @@ class WrapperRepresentation {
211233
return '''<«joiner»>'''
212234
}
213235
214-
def private String getGenericArguments() { // TODO (MEDIUM) remove duplicate code.
236+
def private String getGenericArguments() { // TODO (HIGH) remove duplicate code.
215237
if(typeParameters.empty) {
216238
return ""
217239
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package jce.util
2+
3+
import org.eclipse.emf.ecore.EcorePackage
4+
import java.util.Map
5+
import org.eclipse.emf.ecore.EDataType
6+
import java.util.HashMap
7+
import org.eclipse.emf.ecore.EGenericType
8+
import org.eclipse.emf.ecore.EStructuralFeature
9+
import eme.model.IntermediateModel
10+
import org.eclipse.emf.ecore.EClass
11+
12+
/**
13+
* Utility for the extraction of Java features from Ecore metamodel elements.
14+
*/
15+
final class EcoreToJavaUtil {
16+
17+
private new() {
18+
throw new AssertionError("Suppress default constructor for noninstantiability")
19+
}
20+
21+
/**
22+
* Returns the Java name of an EGenericType.
23+
*/
24+
def static String getFeatureType(EGenericType eGenericType) {
25+
val token = "(expression: "
26+
val dataTypeMap = dataTypeMap
27+
var String type = eGenericType.toString
28+
type = type.substring(type.indexOf(token) + token.length, type.length - 1)
29+
for (dataType : dataTypeMap.keySet) {
30+
type = type.replaceAll(dataType.name, dataTypeMap.get(dataType))
31+
}
32+
/*
33+
* Slightly botched way to get the Java representation through the toString() method of EGenericType.
34+
* It returns the Java representation at the end of the string in brackets with the comment "expression:".
35+
* Therefore we drop everything except the bracket content without the "expression:" comment.
36+
* TODO (MEDIUM) implement this in a less botched way.
37+
*/
38+
return type
39+
}
40+
41+
// TODO (MEDIUM) maybe use this instead some time.
42+
def static String getFeatureType(EClass eClass, EStructuralFeature feature, IntermediateModel model) {
43+
for (field : model.getType(eClass.name).fields) { // find correlating extracted class
44+
if(field.identifier.equals(feature.name)) { // find correlating extracted field
45+
return field.fullType
46+
}
47+
}
48+
return null;
49+
}
50+
51+
def private static Map<EDataType, String> getDataTypeMap() {
52+
val dataTypeMap = new HashMap<EDataType, String>
53+
dataTypeMap.put(EcorePackage.eINSTANCE.EBoolean, "boolean")
54+
dataTypeMap.put(EcorePackage.eINSTANCE.EByte, "byte")
55+
dataTypeMap.put(EcorePackage.eINSTANCE.EChar, "char")
56+
dataTypeMap.put(EcorePackage.eINSTANCE.getEDouble, "double")
57+
dataTypeMap.put(EcorePackage.eINSTANCE.EFloat, "float")
58+
dataTypeMap.put(EcorePackage.eINSTANCE.EInt, "int")
59+
dataTypeMap.put(EcorePackage.eINSTANCE.ELong, "long")
60+
dataTypeMap.put(EcorePackage.eINSTANCE.EShort, "short")
61+
return dataTypeMap
62+
}
63+
}

0 commit comments

Comments
 (0)