1 17 package org.eclipse.emf.codegen.ecore.genmodel.impl; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.eclipse.emf.codegen.ecore.genmodel.GenClass; 26 import org.eclipse.emf.codegen.ecore.genmodel.GenClassifier; 27 import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage; 28 import org.eclipse.emf.codegen.ecore.genmodel.GenOperation; 29 import org.eclipse.emf.codegen.ecore.genmodel.GenPackage; 30 import org.eclipse.emf.codegen.ecore.genmodel.GenParameter; 31 import org.eclipse.emf.common.notify.Notification; 32 import org.eclipse.emf.common.notify.NotificationChain; 33 import org.eclipse.emf.common.util.EList; 34 import org.eclipse.emf.ecore.EAnnotation; 35 import org.eclipse.emf.ecore.EClass; 36 import org.eclipse.emf.ecore.EClassifier; 37 import org.eclipse.emf.ecore.EDataType; 38 import org.eclipse.emf.ecore.EEnum; 39 import org.eclipse.emf.ecore.EOperation; 40 import org.eclipse.emf.ecore.EParameter; 41 import org.eclipse.emf.ecore.EStructuralFeature; 42 import org.eclipse.emf.ecore.ETypedElement; 43 import org.eclipse.emf.ecore.InternalEObject; 44 import org.eclipse.emf.ecore.impl.ENotificationImpl; 45 import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList; 46 import org.eclipse.emf.ecore.util.EcoreUtil; 47 import org.eclipse.emf.ecore.util.InternalEList; 48 49 50 65 public class GenOperationImpl extends GenTypedElementImpl implements GenOperation 66 { 67 75 protected EOperation ecoreOperation = null; 76 77 85 protected EList genParameters = null; 86 87 92 protected GenOperationImpl() 93 { 94 super(); 95 } 96 97 102 protected EClass eStaticClass() 103 { 104 return GenModelPackage.eINSTANCE.getGenOperation(); 105 } 106 107 112 public GenClass getGenClass() 113 { 114 if (eContainerFeatureID != GenModelPackage.GEN_OPERATION__GEN_CLASS) return null; 115 return (GenClass)eContainer; 116 } 117 118 123 public void setGenClass(GenClass newGenClass) 124 { 125 if (newGenClass != eContainer || (eContainerFeatureID != GenModelPackage.GEN_OPERATION__GEN_CLASS && newGenClass != null)) 126 { 127 if (EcoreUtil.isAncestor(this, newGenClass)) 128 throw new IllegalArgumentException ("Recursive containment not allowed for " + toString()); 129 NotificationChain msgs = null; 130 if (eContainer != null) 131 msgs = eBasicRemoveFromContainer(msgs); 132 if (newGenClass != null) 133 msgs = ((InternalEObject)newGenClass).eInverseAdd(this, GenModelPackage.GEN_CLASS__GEN_OPERATIONS, GenClass.class, msgs); 134 msgs = eBasicSetContainer((InternalEObject)newGenClass, GenModelPackage.GEN_OPERATION__GEN_CLASS, msgs); 135 if (msgs != null) msgs.dispatch(); 136 } 137 else if (eNotificationRequired()) 138 eNotify(new ENotificationImpl(this, Notification.SET, GenModelPackage.GEN_OPERATION__GEN_CLASS, newGenClass, newGenClass)); 139 } 140 141 146 public EOperation getEcoreOperation() 147 { 148 if (ecoreOperation != null && ecoreOperation.eIsProxy()) 149 { 150 EOperation oldEcoreOperation = ecoreOperation; 151 ecoreOperation = (EOperation)eResolveProxy((InternalEObject)ecoreOperation); 152 if (ecoreOperation != oldEcoreOperation) 153 { 154 if (eNotificationRequired()) 155 eNotify(new ENotificationImpl(this, Notification.RESOLVE, GenModelPackage.GEN_OPERATION__ECORE_OPERATION, oldEcoreOperation, ecoreOperation)); 156 } 157 } 158 return ecoreOperation; 159 } 160 161 166 public EOperation basicGetEcoreOperation() 167 { 168 return ecoreOperation; 169 } 170 171 176 public void setEcoreOperation(EOperation newEcoreOperation) 177 { 178 EOperation oldEcoreOperation = ecoreOperation; 179 ecoreOperation = newEcoreOperation; 180 if (eNotificationRequired()) 181 eNotify(new ENotificationImpl(this, Notification.SET, GenModelPackage.GEN_OPERATION__ECORE_OPERATION, oldEcoreOperation, ecoreOperation)); 182 } 183 184 189 public EList getGenParameters() 190 { 191 if (genParameters == null) 192 { 193 genParameters = new EObjectContainmentWithInverseEList(GenParameter.class, this, GenModelPackage.GEN_OPERATION__GEN_PARAMETERS, GenModelPackage.GEN_PARAMETER__GEN_OPERATION); 194 } 195 return genParameters; 196 } 197 198 203 public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 204 { 205 if (featureID >= 0) 206 { 207 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 208 { 209 case GenModelPackage.GEN_OPERATION__GEN_CLASS: 210 if (eContainer != null) 211 msgs = eBasicRemoveFromContainer(msgs); 212 return eBasicSetContainer(otherEnd, GenModelPackage.GEN_OPERATION__GEN_CLASS, msgs); 213 case GenModelPackage.GEN_OPERATION__GEN_PARAMETERS: 214 return ((InternalEList)getGenParameters()).basicAdd(otherEnd, msgs); 215 default: 216 return eDynamicInverseAdd(otherEnd, featureID, baseClass, msgs); 217 } 218 } 219 if (eContainer != null) 220 msgs = eBasicRemoveFromContainer(msgs); 221 return eBasicSetContainer(otherEnd, featureID, msgs); 222 } 223 224 229 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 230 { 231 if (featureID >= 0) 232 { 233 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 234 { 235 case GenModelPackage.GEN_OPERATION__GEN_CLASS: 236 return eBasicSetContainer(null, GenModelPackage.GEN_OPERATION__GEN_CLASS, msgs); 237 case GenModelPackage.GEN_OPERATION__GEN_PARAMETERS: 238 return ((InternalEList)getGenParameters()).basicRemove(otherEnd, msgs); 239 default: 240 return eDynamicInverseRemove(otherEnd, featureID, baseClass, msgs); 241 } 242 } 243 return eBasicSetContainer(null, featureID, msgs); 244 } 245 246 251 public NotificationChain eBasicRemoveFromContainer(NotificationChain msgs) 252 { 253 if (eContainerFeatureID >= 0) 254 { 255 switch (eContainerFeatureID) 256 { 257 case GenModelPackage.GEN_OPERATION__GEN_CLASS: 258 return eContainer.eInverseRemove(this, GenModelPackage.GEN_CLASS__GEN_OPERATIONS, GenClass.class, msgs); 259 default: 260 return eDynamicBasicRemoveFromContainer(msgs); 261 } 262 } 263 return eContainer.eInverseRemove(this, EOPPOSITE_FEATURE_BASE - eContainerFeatureID, null, msgs); 264 } 265 266 271 public Object eGet(EStructuralFeature eFeature, boolean resolve) 272 { 273 switch (eDerivedStructuralFeatureID(eFeature)) 274 { 275 case GenModelPackage.GEN_OPERATION__GEN_CLASS: 276 return getGenClass(); 277 case GenModelPackage.GEN_OPERATION__ECORE_OPERATION: 278 if (resolve) return getEcoreOperation(); 279 return basicGetEcoreOperation(); 280 case GenModelPackage.GEN_OPERATION__GEN_PARAMETERS: 281 return getGenParameters(); 282 } 283 return eDynamicGet(eFeature, resolve); 284 } 285 286 291 public boolean eIsSet(EStructuralFeature eFeature) 292 { 293 switch (eDerivedStructuralFeatureID(eFeature)) 294 { 295 case GenModelPackage.GEN_OPERATION__GEN_CLASS: 296 return getGenClass() != null; 297 case GenModelPackage.GEN_OPERATION__ECORE_OPERATION: 298 return ecoreOperation != null; 299 case GenModelPackage.GEN_OPERATION__GEN_PARAMETERS: 300 return genParameters != null && !genParameters.isEmpty(); 301 } 302 return eDynamicIsSet(eFeature); 303 } 304 305 310 public void eSet(EStructuralFeature eFeature, Object newValue) 311 { 312 switch (eDerivedStructuralFeatureID(eFeature)) 313 { 314 case GenModelPackage.GEN_OPERATION__GEN_CLASS: 315 setGenClass((GenClass)newValue); 316 return; 317 case GenModelPackage.GEN_OPERATION__ECORE_OPERATION: 318 setEcoreOperation((EOperation)newValue); 319 return; 320 case GenModelPackage.GEN_OPERATION__GEN_PARAMETERS: 321 getGenParameters().clear(); 322 getGenParameters().addAll((Collection )newValue); 323 return; 324 } 325 eDynamicSet(eFeature, newValue); 326 } 327 328 333 public void eUnset(EStructuralFeature eFeature) 334 { 335 switch (eDerivedStructuralFeatureID(eFeature)) 336 { 337 case GenModelPackage.GEN_OPERATION__GEN_CLASS: 338 setGenClass((GenClass)null); 339 return; 340 case GenModelPackage.GEN_OPERATION__ECORE_OPERATION: 341 setEcoreOperation((EOperation)null); 342 return; 343 case GenModelPackage.GEN_OPERATION__GEN_PARAMETERS: 344 getGenParameters().clear(); 345 return; 346 } 347 eDynamicUnset(eFeature); 348 } 349 350 public ETypedElement getEcoreTypedElement() 351 { 352 return getEcoreOperation(); 353 } 354 355 public String getName() 356 { 357 return getEcoreOperation().getName(); 358 } 359 360 public String getCapName() 361 { 362 return capName(getName()); 363 } 364 365 public String getFormattedName() 366 { 367 return format(getCapName(), ' ', null, false); 368 } 369 370 public boolean isVoid() 373 { 374 return getEcoreOperation().getEType() == null; 375 } 376 377 380 protected EClassifier getReturn() 381 { 382 return getEcoreOperation().getEType(); 383 } 384 385 public String getTypeClassifierAccessorName() 386 { 387 return isVoid() ? "null" : super.getTypeClassifierAccessorName(); 388 } 389 390 public GenPackage getTypeGenPackage() 391 { 392 return isVoid() ? null : super.getTypeGenPackage(); 393 } 394 395 public String getType() 396 { 397 return isVoid() ? "void" : super.getType(); 398 } 399 400 public String getImportedType() 401 { 402 return isVoid() ? "void" : super.getImportedType(); 403 } 404 405 public String getObjectType() 406 { 407 return isVoid() ? "void" : super.getObjectType(); 408 } 409 410 public String getImportedInternalType() 411 { 412 return isVoid() ? "void" : super.getImportedInternalType(); 413 } 414 415 public boolean isFeatureMapType() 416 { 417 return !isVoid() && super.isFeatureMapType(); 418 } 419 420 public String getListItemType() 421 { 422 return isVoid() ? "void" : super.getListItemType(); 423 } 424 425 public String getQualifiedListItemType() 426 { 427 return isVoid() ? "void" : super.getQualifiedListItemType(); 428 } 429 430 public boolean isPrimitiveType() 431 { 432 return !isVoid() && super.isPrimitiveType(); 433 } 434 435 public String getPrimitiveValueFunction() 436 { 437 return isVoid() ? null : super.getPrimitiveValueFunction(); 438 } 439 440 public boolean isStringType() 441 { 442 return !isVoid() && super.isStringType(); 443 } 444 445 public boolean isStringBasedType() 446 { 447 return !isVoid() && super.isStringBasedType(); 448 } 449 450 453 public String getReturnTypeClassifier() 454 { 455 return getTypeClassifierAccessorName(); 456 } 457 458 461 public GenPackage getReturnTypeGenPackage() 462 { 463 return getTypeGenPackage(); 464 } 465 466 469 public String getReturnType() 470 { 471 return getType(); 472 } 473 474 477 public String getImportedReturnType() 478 { 479 return getImportedType(); 480 } 481 482 485 public String getObjectReturnType() 486 { 487 return getObjectType(); 488 } 489 490 493 public boolean isPrimitiveReturnType() 494 { 495 return isPrimitiveType(); 496 } 497 498 public String getParameters() 499 { 500 return getParameters(true); 501 } 502 503 protected String getParameters(boolean formal) 504 { 505 StringBuffer result = new StringBuffer (); 506 for (Iterator iter = getGenParameters().iterator(); iter.hasNext(); ) 507 { 508 GenParameter genParameter = (GenParameter)iter.next(); 509 if (formal) 510 { 511 result.append(genParameter.getImportedType()); 512 result.append(' '); 513 } 514 result.append(genParameter.getName()); 515 516 if (iter.hasNext()) result.append(", "); 517 } 518 return result.toString(); 519 } 520 521 public String getParameterTypes(String separator) 522 { 523 return getParameterTypes(separator, true); 524 } 525 526 public String getParameterTypes(String separator, boolean qualified) 527 { 528 StringBuffer parameterTypes = new StringBuffer (); 529 530 for (Iterator genParameters = getGenParameters().iterator(); genParameters.hasNext();) 531 { 532 GenParameter genParameter = (GenParameter)genParameters.next(); 533 String type = genParameter.getType(); 534 535 if (!qualified) 536 { 537 int firstBracket = type.indexOf("["); 538 if (firstBracket != -1) 539 { 540 type = type.substring(0, firstBracket); 541 } 542 543 type = type.substring(type.lastIndexOf(".") + 1); 544 545 int firstDollar = type.indexOf("$"); 546 if (firstDollar != -1) 547 { 548 type = type.substring(0, firstDollar); 549 } 550 } 551 552 parameterTypes.append(type); 553 554 if (genParameters.hasNext()) 555 { 556 parameterTypes.append(separator); 557 } 558 } 559 560 return parameterTypes.toString(); 561 } 562 563 public String getImportedMetaType() 564 { 565 return getGenModel().getImportedName("org.eclipse.emf.ecore.EOperation"); 566 } 567 568 public GenPackage getGenPackage() 569 { 570 return getGenClass().getGenPackage(); 571 } 572 573 public void initialize(EOperation eOperation) 574 { 575 setEcoreOperation(eOperation); 576 577 List parameters = eOperation.getEParameters(); 578 LOOP: 579 for (int i = 0; i < parameters.size(); ++i) 580 { 581 EParameter parameter = (EParameter)parameters.get(i); 582 583 for (int j = 0; j < getGenParameters().size(); ++j) 584 { 585 GenParameter genParameter = (GenParameter)getGenParameters().get(j); 586 if (genParameter.getEcoreParameter() == parameter) 587 { 588 genParameter.initialize(parameter); 589 if (i != j) 590 { 591 getGenParameters().move(i, j); 592 } 593 594 continue LOOP; 595 } 596 } 597 598 GenParameter genParameter = getGenModel().createGenParameter(); 599 getGenParameters().add(genParameter); 600 genParameter.initialize(parameter); 601 } 602 } 603 604 public String getModelInfo() 605 { 606 EOperation eOperation = getEcoreOperation(); 607 StringBuffer result = new StringBuffer (); 608 609 if (getGenParameters().isEmpty() && 612 ((getName().startsWith("get") && getName().length() > 3 && Character.isUpperCase(getName().charAt(3))) || 613 (getName().startsWith("is") && getName().length() > 2 && Character.isUpperCase(getName().charAt(2))))) 614 { 615 appendModelSetting(result, "kind", "operation"); 616 } 617 618 if (!isVoid()) 619 { 620 String mapModelInfo = getMapModelInfo(false, true); 624 if (mapModelInfo != null) 625 { 626 result.append(mapModelInfo); 627 } 628 else 629 { 630 if (eOperation.isMany() && !isFeatureMapType()) 631 { 632 appendModelSetting(result, "type", getType(eOperation.getEType(), false)); 633 } 634 635 EClassifier type = eOperation.getEType(); 636 if (type instanceof EDataType && !(type instanceof EEnum)) 637 { 638 GenPackage genPackage = findGenPackage(type.getEPackage()); 639 if (genPackage != null && (isFeatureMapType() || !genPackage.isEcorePackage())) 640 { 641 appendModelSetting(result, "dataType", genPackage.getInterfacePackageName() + '.' + type.getName()); 642 } 643 } 644 645 if (!eOperation.isUnique()) 646 { 647 appendModelSetting(result, "unique", "false"); 648 } 649 650 result.append(getMultiplicityModelInfo(false)); 651 } 652 653 if (!eOperation.isOrdered()) 654 { 655 appendModelSetting(result, "ordered", "false"); 656 } 657 } 658 659 for (Iterator i = getGenParameters().iterator(); i.hasNext(); ) 660 { 661 String parameterResult = ((GenParameter)i.next()).getQualifiedModelInfo(); 662 if (parameterResult.length() > 0) 663 { 664 result.append(parameterResult); 665 result.append(' '); 666 } 667 } 668 669 appendAnnotationInfo(result, eOperation); 670 return result.toString().trim(); 671 } 672 673 public boolean reconcile(GenOperation oldGenOperationVersion) 674 { 675 if (getEcoreOperation().getName().equals(oldGenOperationVersion.getEcoreOperation().getName())) 676 { 677 for (Iterator i = getGenParameters().iterator(); i.hasNext(); ) 678 { 679 GenParameter genParameter = (GenParameter)i.next(); 680 for (Iterator j = oldGenOperationVersion.getGenParameters().iterator(); j.hasNext(); ) 681 { 682 GenParameter oldGenParameterVersion = (GenParameter)j.next(); 683 if (genParameter.reconcile(oldGenParameterVersion)) 684 { 685 break; 686 } 687 } 688 } 689 reconcileSettings(oldGenOperationVersion); 690 return true; 691 } 692 else 693 { 694 return false; 695 } 696 } 697 698 protected void reconcileSettings(GenOperation oldGenOperationVersion) 699 { 700 } 701 702 public boolean reconcile() 703 { 704 EOperation eOperation = getEcoreOperation(); 705 if (eOperation == null || eOperation.eIsProxy() || eOperation.eResource() == null) 706 { 707 return false; 708 } 709 else 710 { 711 for (Iterator i = getGenParameters().iterator(); i.hasNext(); ) 712 { 713 GenParameter genParameter = (GenParameter)i.next(); 714 if (!genParameter.reconcile()) 715 { 716 i.remove(); 717 } 718 } 719 720 return true; 721 } 722 } 723 724 protected String getBody() 725 { 726 EOperation eOperation = getEcoreOperation(); 727 EAnnotation eAnnotation = eOperation.getEAnnotation(GenModelPackage.eNS_URI); 728 return eAnnotation == null ? null : (String )eAnnotation.getDetails().get("body"); 729 } 730 731 public boolean hasBody() 732 { 733 return getBody() != null; 734 } 735 736 public String getBody(String indentation) 737 { 738 String body = getBody(); 739 if (body != null) 740 { 741 StringBuffer stringBuffer = new StringBuffer (indent(body, indentation)); 742 743 for (int i = 0; i < stringBuffer.length(); ) 744 { 745 String string = stringBuffer.toString(); 748 int start = string.indexOf("<%", i); 749 if (start == -1) 750 { 751 break; 752 } 753 else 754 { 755 int end = string.indexOf("%>", start + 2); 756 if (end == -1) 757 { 758 break; 759 } 760 else 761 { 762 String qualifiedName = stringBuffer.substring(start + 2, end); 763 String importedName = getGenModel().getImportedName(qualifiedName); 764 stringBuffer.replace(start, end + 2, importedName); 765 i += importedName.length(); 766 } 767 } 768 } 769 770 return stringBuffer.toString(); 771 } 772 else 773 { 774 return null; 775 } 776 } 777 778 public List getGenExceptions() 779 { 780 List result = new ArrayList (); 781 for (Iterator i = getEcoreOperation().getEExceptions().iterator(); i.hasNext(); ) 782 { 783 result.add(findGenClassifier((EClassifier)i.next())); 784 } 785 return result; 786 } 787 788 public String getThrows() 789 { 790 List genExceptions = getGenExceptions(); 791 if (genExceptions.isEmpty()) 792 { 793 return ""; 794 } 795 else 796 { 797 StringBuffer result = new StringBuffer (" throws "); 798 for (Iterator i = genExceptions.iterator(); i.hasNext(); ) 799 { 800 GenClassifier genClassifier = (GenClassifier)i.next(); 801 result.append(genClassifier.getImportedInstanceClassName()); 802 if (i.hasNext()) 803 { 804 result.append(", "); 805 } 806 } 807 return result.toString(); 808 } 809 } 810 811 public boolean isInvariant() 812 { 813 return 814 getReturnType() != null && 815 "boolean".equals(getReturnType()) && 816 getGenParameters().size() == 2 && 817 "org.eclipse.emf.common.util.DiagnosticChain".equals 818 (((GenParameter)getGenParameters().get(0)).getEcoreParameter().getEType().getInstanceClassName()) && 819 "java.util.Map".equals 820 (((GenParameter)getGenParameters().get(1)).getEcoreParameter().getEType().getInstanceClassName()); 821 } 822 823 public String getParameterNames(String separator) 824 { 825 StringBuffer parameterNames = new StringBuffer (); 826 for (Iterator genParameters = getGenParameters().iterator(); genParameters.hasNext();) 827 { 828 parameterNames.append(((GenParameter)genParameters.next()).getName()); 829 if (genParameters.hasNext()) 830 { 831 parameterNames.append(separator); 832 } 833 } 834 835 return parameterNames.toString(); 836 } 837 } 838 | Popular Tags |