1 7 package org.eclipse.emf.codegen.ecore.genmodel.impl; 8 9 import org.eclipse.emf.codegen.ecore.genmodel.GenClass; 10 import org.eclipse.emf.codegen.ecore.genmodel.GenDataType; 11 import org.eclipse.emf.codegen.ecore.genmodel.GenEnum; 12 import org.eclipse.emf.codegen.ecore.genmodel.GenFeature; 13 import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage; 14 import org.eclipse.emf.codegen.ecore.genmodel.GenPackage; 15 import org.eclipse.emf.codegen.ecore.genmodel.GenTypedElement; 16 17 import org.eclipse.emf.ecore.EClass; 18 import org.eclipse.emf.ecore.EClassifier; 19 import org.eclipse.emf.ecore.EDataType; 20 import org.eclipse.emf.ecore.EEnum; 21 import org.eclipse.emf.ecore.EModelElement; 22 import org.eclipse.emf.ecore.ETypedElement; 23 24 33 public abstract class GenTypedElementImpl extends GenBaseImpl implements GenTypedElement 34 { 35 40 protected GenTypedElementImpl() 41 { 42 super(); 43 } 44 45 50 protected EClass eStaticClass() 51 { 52 return GenModelPackage.eINSTANCE.getGenTypedElement(); 53 } 54 55 public abstract ETypedElement getEcoreTypedElement(); 56 57 public EModelElement getEcoreModelElement() 58 { 59 return getEcoreTypedElement(); 60 } 61 62 public GenPackage getTypeGenPackage() 63 { 64 return findGenPackage(getEcoreTypedElement().getEType().getEPackage()); 65 } 66 67 public String getTypeClassifierAccessorName() 68 { 69 return findGenClassifier(getEcoreTypedElement().getEType()).getClassifierAccessorName(); 70 } 71 72 75 public String getTypeClassifier() 76 { 77 return getTypeClassifierAccessorName(); 78 } 79 80 public String getType() 81 { 82 if (isFeatureMapType()) return getEffectiveFeatureMapWrapperInterface(); 83 if (isMapType()) return getEffectiveMapType(); 84 if (isListType()) return getEffectiveListType(); 85 if (isEObjectType()) return getEffectiveEObjectType(); 86 return getType(getEcoreTypedElement().getEType(), false); 87 } 88 89 public String getImportedType() 90 { 91 if (isFeatureMapType()) return getGenModel().getImportedName(getEffectiveFeatureMapWrapperInterface()); 92 if (isMapType()) return getGenModel().getImportedName(getEffectiveMapType()); 93 if (isListType()) return getGenModel().getImportedName(getEffectiveListType()); 94 if (isEObjectType()) return getGenModel().getImportedName(getEffectiveEObjectType()); 95 return getImportedType(getEcoreTypedElement().getEType(), false); 96 } 97 98 public String getObjectType() 99 { 100 if (isFeatureMapType()) return getGenModel().getImportedName(getEffectiveFeatureMapWrapperInterface()); 101 if (isMapType()) return getGenModel().getImportedName(getEffectiveMapType()); 102 if (isListType()) return getGenModel().getImportedName(getEffectiveListType()); 103 if (isEObjectType()) return getGenModel().getImportedName(getEffectiveEObjectType()); 104 return getImportedType(getEcoreTypedElement().getEType(), true); 105 } 106 107 public String getImportedInternalType() 108 { 109 if (isFeatureMapType()) return getImportedEffectiveFeatureMapWrapperInternalInterface(); 110 if (isMapType()) return getGenModel().getImportedName("org.eclipse.emf.common.util.EMap"); 111 if (isListType()) return getGenModel().getImportedName("org.eclipse.emf.common.util.EList"); 112 return getImportedType(getEcoreTypedElement().getEType(), false); 113 } 114 115 public boolean isFeatureMapType() 116 { 117 EClassifier type = getEcoreTypedElement().getEType(); 118 return type != null && isFeatureMapEntry(getEcoreTypedElement().getEType().getInstanceClassName()); 119 } 120 121 protected static boolean isFeatureMapEntry(String name) 122 { 123 return "org.eclipse.emf.ecore.util.FeatureMap.Entry".equals(name) || "org.eclipse.emf.ecore.util.FeatureMap$Entry".equals(name); 124 } 125 126 public boolean isWrappedFeatureMapType() 127 { 128 return isFeatureMapType() && 129 !isBlank(getGenModel().getFeatureMapWrapperInterface()) && 130 !isBlank(getGenModel().getFeatureMapWrapperInternalInterface()) && 131 !isBlank(getGenModel().getFeatureMapWrapperClass()); 132 } 133 134 137 public boolean isFeatureMapWrapped() 138 { 139 return isWrappedFeatureMapType(); 140 } 141 142 public boolean isEffectiveSuppressEMFTypes() 143 { 144 return super.isEffectiveSuppressEMFTypes(); 145 } 146 147 public String getImportedEffectiveFeatureMapWrapperInternalInterface() 148 { 149 return super.getImportedEffectiveFeatureMapWrapperInternalInterface(); 150 } 151 152 public String getImportedEffectiveFeatureMapWrapperClass() 153 { 154 return super.getImportedEffectiveFeatureMapWrapperClass(); 155 } 156 157 public boolean isListType() 158 { 159 return getEcoreTypedElement().isMany() || isFeatureMapType(); 160 } 161 162 public String getListItemType() 163 { 164 return getImportedType(getEcoreTypedElement().getEType(), true); 165 } 166 167 public String getQualifiedListItemType() 168 { 169 return getType(getEcoreTypedElement().getEType(), true).replace('$', '.'); 170 } 171 172 public boolean isMapType() 173 { 174 return isListType() && getMapGenClass() != null; 175 } 176 177 public GenClass getMapEntryTypeGenClass() 178 { 179 EClassifier eType = getEcoreTypedElement().getEType(); 180 if (eType instanceof EClass && isJavaUtilMapEntry(eType.getInstanceClassName())) 181 { 182 GenClass genClass = findGenClass((EClass)eType); 183 if (genClass.isMapEntry()) 184 { 185 return genClass; 186 } 187 } 188 return null; 189 } 190 191 public String getImportedMapEntryType() 192 { 193 GenClass genClass = getMapEntryTypeGenClass(); 194 return genClass == null ? null : genClass.getImportedClassName(); 195 } 196 197 200 public GenClass getMapGenClass() 201 { 202 return getMapEntryTypeGenClass(); 203 } 204 205 208 public String getMapItemType() 209 { 210 return getImportedMapEntryType(); 211 } 212 213 protected boolean isEObjectType() 214 { 215 EClassifier type = getEcoreTypedElement().getEType(); 216 return type instanceof EClass && findGenClass((EClass)type).isEObject(); 217 } 218 219 protected boolean isEObjectExtensionType() 220 { 221 EClassifier type = getEcoreTypedElement().getEType(); 222 return type instanceof EClass && findGenClass((EClass)type).isEObjectExtension(); 223 } 224 225 public String getEObjectCast() 226 { 227 return isEObjectExtensionType() && !isEffectiveSuppressEMFTypes() ? 228 "" : "(" + getGenModel().getImportedName("org.eclipse.emf.ecore.EObject") + ")"; 229 } 230 231 public String getInternalTypeCast() 232 { 233 return isEObjectType() && isEffectiveSuppressEMFTypes() ? 234 "(" + getGenModel().getImportedName("org.eclipse.emf.ecore.EObject") + ")" : ""; 235 } 236 237 public boolean isPrimitiveType() 238 { 239 return !isListType() && isPrimitiveType(getEcoreTypedElement().getEType()); 240 } 241 242 public String getPrimitiveValueFunction() 243 { 244 Class instanceClass = getInstanceClass(getEcoreTypedElement().getEType()); 245 if (instanceClass == java.lang.Boolean.TYPE) 246 return "booleanValue"; 247 if (instanceClass == java.lang.Byte.TYPE) 248 return "byteValue"; 249 if (instanceClass == java.lang.Character.TYPE) 250 return "charValue"; 251 if (instanceClass == java.lang.Double.TYPE) 252 return "doubleValue"; 253 if (instanceClass == java.lang.Float.TYPE) 254 return "floatValue"; 255 if (instanceClass == java.lang.Integer.TYPE) 256 return "intValue"; 257 if (instanceClass == java.lang.Long.TYPE) 258 return "longValue"; 259 if (instanceClass == java.lang.Short.TYPE) 260 return "shortValue"; 261 return null; 262 } 263 264 public boolean isBooleanType() 265 { 266 return isPrimitiveType() && getInstanceClass(getEcoreTypedElement().getEType()) == java.lang.Boolean.TYPE; 267 } 268 269 public boolean isStringType() 270 { 271 return !isListType() && getInstanceClass(getEcoreTypedElement().getEType()) == java.lang.String .class; 272 } 273 274 public boolean isStringBasedType() 277 { 278 return getInstanceClass(getEcoreTypedElement().getEType()) == java.lang.String .class; 279 } 280 281 public boolean isEnumType() 282 { 283 return !isListType() && getEcoreTypedElement().getEType() instanceof EEnum; 284 } 285 286 public boolean isEnumBasedType() 287 { 288 return getEcoreTypedElement().getEType() instanceof EEnum; 289 } 290 291 public GenEnum getTypeGenEnum() 292 { 293 EClassifier eType = getEcoreTypedElement().getEType(); 294 return eType instanceof EEnum ? findGenEnum((EEnum)eType) : null; 295 } 296 297 public GenDataType getTypeGenDataType() 298 { 299 EClassifier eType = getEcoreTypedElement().getEType(); 300 return eType instanceof EDataType ? findGenDataType((EDataType)eType) : null; 301 } 302 303 public GenClass getTypeGenClass() 304 { 305 EClassifier eType = getEcoreTypedElement().getEType(); 306 return eType instanceof EClass ? findGenClass((EClass)eType) : null; 307 } 308 309 312 public GenEnum getGenEnumType() 313 { 314 return getTypeGenEnum(); 315 } 316 317 320 public GenDataType getGenDataTypeType() 321 { 322 return getTypeGenDataType(); 323 } 324 325 328 public GenClass getGenClassType() 329 { 330 return getTypeGenClass(); 331 } 332 333 public String getLowerBound() 334 { 335 return String.valueOf(getEcoreTypedElement().getLowerBound()); 336 } 337 338 public String getUpperBound() 339 { 340 return String.valueOf(getEcoreTypedElement().getUpperBound()); 341 } 342 343 public boolean isUnique() 344 { 345 return getEcoreTypedElement().isUnique(); 346 } 347 348 public String getUniqueFlag() 349 { 350 String result = !isUnique() ? "!" : ""; 351 return result + "IS_UNIQUE"; 352 } 353 354 public String getOrderedFlag() 355 { 356 String result = !getEcoreTypedElement().isOrdered() ? "!" : ""; 357 return result + "IS_ORDERED"; 358 } 359 360 protected void appendModelSetting(StringBuffer result, boolean qualified, String name, String value) 361 { 362 appendModelSetting(result, qualified ? getEcoreTypedElement().getName() : null, name, value); 363 } 364 365 protected String getMapModelInfo(boolean qualified, boolean includeFeatures) 366 { 367 GenClass mapGenClass = getMapEntryTypeGenClass(); 368 if (mapGenClass != null) 369 { 370 StringBuffer result = new StringBuffer (); 371 372 appendModelSetting(result, qualified, "mapType", 373 mapGenClass.getGenPackage().getInterfacePackageName() + '.' + mapGenClass.getEcoreClass().getName()); 374 375 if (includeFeatures) 376 { 377 GenFeature keyFeature = mapGenClass.getMapEntryKeyFeature(); 378 appendModelSetting(result, qualified, "keyType", getType(keyFeature.getEcoreFeature().getEType(), false)); 379 380 GenFeature valueFeature = mapGenClass.getMapEntryValueFeature(); 381 appendModelSetting(result, qualified, "valueType", getType(valueFeature.getEcoreFeature().getEType(), false)); 382 } 383 384 return result.toString(); 385 } 386 387 return null; 388 } 389 390 protected String getMultiplicityModelInfo(boolean qualified) 391 { 392 ETypedElement eTypedElement = getEcoreTypedElement(); 393 StringBuffer result = new StringBuffer (); 394 395 int lowerBound = eTypedElement.getLowerBound(); 396 if (lowerBound == 1) 397 { 398 appendModelSetting(result, qualified, "required", "true"); 399 } 400 else if (lowerBound > 1) 401 { 402 appendModelSetting(result, qualified, "lower", Integer.toString(lowerBound)); 403 } 404 405 int upperBound = eTypedElement.getUpperBound(); 406 if (upperBound > 1 || upperBound < -1) 407 { 408 appendModelSetting(result, qualified, "upper", Integer.toString(eTypedElement.getUpperBound())); 409 } 410 else if (upperBound == 1) 411 { 412 String typeName = getType(eTypedElement.getEType(), false); 413 if ("org.eclipse.emf.common.util.EList".equals(typeName) || "java.util.List".equals(typeName)) 414 { 415 appendModelSetting(result, qualified, "many", "false"); 416 } 417 } 418 else if ((qualified || isFeatureMapType()) && upperBound == -1) 419 { 420 appendModelSetting(result, qualified, "many", "true"); 421 } 422 423 return result.toString(); 424 } 425 426 } | Popular Tags |