aboutsummaryrefslogtreecommitdiffstats
blob: 2a522884f0d752af80664fb6525eebc6db892bac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (c) 2008, 2010 Borland Software Corporation and others
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Artem Tikhomirov (Borland) - initial API and implementation
 */
import xpt.StringOperations;
import xpt.GenModelUtils;
import gmf.CodeGenerationUtils;
import gmf.GenPackageUtils;

modeltype genmodel uses "http://www.eclipse.org/emf/2002/GenModel";
modeltype ecore uses "http://www.eclipse.org/emf/2002/Ecore";

/*
 * NOTE: Extensions from this file are supposed to be used 
 * ONLY from MetaModel.xpt. No other .xpt or .ext should import
 * this file. Common GenModel functionality, unrelated to meta-model 
 * access and access to generated model code should go into GenModelUtils.ext. 
 */

library MetaModel;


--
-- GenPackageImpl#getQualifiedPackageInterfaceName()
--
helper getQualifiedPackageInterfaceName(gp : genmodel::GenPackage) : String {
	return getGenPackageQualifiedPackageInterfaceName(gp)
}

--
-- GenPackageImpl#getQualifiedFactoryInterfaceName()
--
helper getQualifiedFactoryInterfaceName(gp : genmodel::GenPackage) : String {
	return getGenPackageQualifiedFactoryInterfaceName(gp)
}

--
-- GenPackage#getFactoryInstanceName
--
helper getFactoryInstanceName(gp : genmodel::GenPackage) : String {
	return (if gp.genModel.suppressEMFMetaData then 'INSTANCE' else 'eINSTANCE' endif)
}

--
-- GenPackageImpl#getQualifiedItemProviderAdapterFactoryClassName()
--
helper getQualifiedItemProviderAdapterFactoryClassName(gp : genmodel::GenPackage) : String {
	return getQualifiedPackageName(gp, gp.providerPackageSuffix) + '.' + getItemProviderAdapterFactoryClassName(gp)
}

--
-- GenPackageImpl#getItemProviderAdapterFactoryClassName()
--
-- MODIFIERS: private
helper getItemProviderAdapterFactoryClassName(gp : genmodel::GenPackage) : String {
	return getPrefixedName(gp, 'ItemProviderAdapterFactory')
}

--
-- GenClass#getQualifiedInterfaceName
--
helper getQualifiedInterfaceName(gc : genmodel::GenClass) : String {
	return getGenPackageQualifiedInterfaceName(gc)
}

--
-- GenPackageImpl#getInterfacePackageName()
--
-- MODIFIERS: private
helper getInterfacePackageName(gp : genmodel::GenPackage) : String {
	return getQualifiedPackageName(gp, gp.interfacePackageSuffix)
}

-- NOTE GenDataType#getQualifiedInstanceClassName returns primitive types as is,
-- while we return their java.lang wrappers
helper genmodel::GenClassifier::getQualifiedClassName() : String {
	return null
}

helper genmodel::GenDataType::getQualifiedClassName() : String {
	return getObjectCompatibleClassName(self.ecoreDataType)
}

helper genmodel::GenEnum::getQualifiedClassName() : String {
	return getInterfacePackageName(self.genPackage) + '.' + self.ecoreEnum.name
}

--
-- GenClass#getClassifierAccessorName
--
helper getClassifierAccessorName(gc : genmodel::GenClassifier) : String {
	return switch { case (gc.getEcoreClassifier().name = 'Class') 'Class_'; case (gc.getEcoreClassifier().name = 'Name') 'Name_'; else gc.getEcoreClassifier().name; }
}

--
-- GenFeature#getFeatureAccessorName
--
helper getFeatureAccessorName(gf : genmodel::GenFeature) : String {
	return gf.genClass.ecoreClass.name + '_' + gf.ecoreFeature.name.firstToUpper()
}

-- GenFeatureImpl#getAccessorName
helper getAccessorName(gf : genmodel::GenFeature) : String {
	return (if isMapEntryFeature(gf) then 'Typed' + gf.ecoreFeature.name.firstToUpper() else gf.ecoreFeature.name.firstToUpper() endif)
}

-- GenClassImpl#isMapEntry
helper isMapEntry(gc : genmodel::GenClass) : Boolean {
	return 'java.util.Map.Entry' = gc.ecoreClass.instanceClassName or 'java.util.Map$Entry' = gc.ecoreClass.instanceClassName and (gc.ecoreClass.getEStructuralFeature('key') <> null and gc.ecoreClass.getEStructuralFeature('value') <> null)
}

/*
 * Casts the argument (which is assumed to be of the given source class) to the given target class. 
 * A null value may be used for both source and target class to specify the EObject class.
 * If the cast is required, it will be parenthesized as a whole, producing, e.g., ((EObject) o). 
 * If the parentheses are not required, use unparenthesizedCast() extension.
 */
helper parenthesizedCast(what : String, source : genmodel::GenClass, target : genmodel::GenClass) : String {
	return (if needsCast(source, target) then '(' + unparenthesizedCast(what, source, target) + ')' else what endif)
}

/*
 * Casts the argument (which is assumed to be of the given source class) to the given target class. 
 * A null value may be used for both source and target class to specify the EObject class.
 * If the cast is required, it will NOT be parenthesized as a whole, producing, e.g., (EObject) o. 
 * If the parentheses are required, use parenthesizedCast() extension.
 */
helper unparenthesizedCast(what : String, source : genmodel::GenClass, target : genmodel::GenClass) : String {
	return (if needsCast(source, target) then (if target = null then '(org.eclipse.emf.ecore.EObject)' + what else '(' + getQualifiedInterfaceName(target) + ')' + what endif) else what endif)
}

--See cast()
-- MODIFIERS: private
helper needsCast(source : genmodel::GenClass, target : genmodel::GenClass) : Boolean {
	return (if source = target then false else (if target = null then source <> null and isExternalInterface(source) else source = null or (isExternalInterface(target) or (isExternalInterface(source) or not target.isSuperTypeOf(source))) endif) endif)
}
-- MODIFIERS: private
helper genmodel::GenClassifier::getEcoreClassifier() : ecore::EClassifier {	return null }
-- MODIFIERS: private
helper genmodel::GenClass::getEcoreClassifier() : ecore::EClassifier { return self.ecoreClass }
-- MODIFIERS: private
helper genmodel::GenDataType::getEcoreClassifier() : ecore::EClassifier { return self.ecoreDataType }

-- GenPackageImpl#getReflectionPackageName()
-- MODIFIERS: private
helper getReflectionPackageName(gp : genmodel::GenPackage) : String {
	return getQualifiedPackageName(gp, gp.metaDataPackageSuffix)
}

-- GenPackageImpl#getQualifiedPackageClassName()
-- MODIFIERS: private
helper getQualifiedPackageClassName(gp : genmodel::GenPackage) : String {
	return getReflectionClassPackageName(gp) + '.' + getPackageClassName(gp)
}

-- GenPackageImpl#getReflectionClassPackageName()
-- MODIFIERS: private
helper getReflectionClassPackageName(gp : genmodel::GenPackage) : String {
	return getQualifiedPackageName(gp, (if gp.genModel.suppressInterfaces then gp.metaDataPackageSuffix else gp.classPackageSuffix endif))
}

-- GenPackageImpl#getPackageClassName()
-- MODIFIERS: private
helper getPackageClassName(gp : genmodel::GenPackage) : String {
	return (if not gp.genModel.suppressEMFMetaData and not gp.genModel.suppressInterfaces then getPackageInterfaceName(gp) + 'Impl' else getPackageInterfaceName(gp) endif)
}

-- GenPackageImpl#getPackageInterfaceName()
-- MODIFIERS: private
helper getPackageInterfaceName(gp : genmodel::GenPackage) : String {
	return (if gp.genModel.suppressEMFMetaData and not gp.genModel.suppressInterfaces then getPrefixedName(gp, 'PackageImpl') else getPrefixedName(gp, 'Package') endif)
}

-- GenPackageImpl#getPrefixedName()
-- MODIFIERS: private
helper getPrefixedName(gp : genmodel::GenPackage, name : String) : String {
	return (if null <> gp.prefix then gp.prefix + name else name endif)
}

-- GenPackageImpl#getQualifiedPackageName(String)
-- MODIFIERS: private
helper getQualifiedPackageName(gp : genmodel::GenPackage, suffix : String) : String {
	return (if isBlank(suffix) then getQualifiedPackageName(gp) else getQualifiedPackageName(gp) + '.' + suffix endif)
}

-- GenPackageImpl#getQualifiedPackageName()
-- MODIFIERS: private
helper getQualifiedPackageName(gp : genmodel::GenPackage) : String {
	return (if isBlank(gp.basePackage) then safeName(gp.ecorePackage.name) else gp.basePackage + '.' + safeName(gp.ecorePackage.name) endif)
}

-- GenFeatureImpl#isMapEntryFeature
-- MODIFIERS: private
helper isMapEntryFeature(gf : genmodel::GenFeature) : Boolean {
	return isMapEntry(gf.genClass) and ('key' = gf.ecoreFeature.name or 'value' = gf.ecoreFeature.name)
}

-- MODIFIERS: private
helper isBlank(s : String) : Boolean {
	return null = s or s.size() = 0
}