/**
   * Bind an operation by copying the signature from the actual.
   *
   * @param copy the copier
   * @param actual the actual. If an operation, its signature is copied to the template
   * @param operation The operation template
   * @return
   */
  public static Operation instantiateOperation(
      LazyCopier copy, Element actual, Operation operation) {
    try {
      Operation newOperation = copy.getCopy(operation);
      if (actual instanceof Operation) {
        for (Parameter parameter : ((Operation) actual).getOwnedParameters()) {
          Parameter newParam = EcoreUtil.copy(parameter); // copy parameter via EcoreUtil
          newParam.setType(copy.getCopy(parameter.getType()));
          newOperation.getOwnedParameters().add(newParam);
        }
      }
      TransformationContext.classifier = newOperation.getClass_();
      if (actual instanceof Classifier) {
        bindOperation(newOperation, (Classifier) actual);
      }
      String newName = AcceleoDriverWrapper.evaluate(operation.getName(), actual, null);
      newOperation.setName(newName);

      return newOperation;
    } catch (TransformationException e) {
      // throw runtime exception
      throw new RuntimeException(
          String.format(Messages.TemplateInstantiationListener_TrafoException, e.getMessage()));
    }
  }
  /**
   * Returns the string used to represent this parameter
   *
   * @param parameter a parameter
   * @return the string used to represent this parameter
   */
  public static String getLabel(Parameter parameter) {
    StringBuffer buffer = new StringBuffer();
    // visibility
    buffer.append(" "); // $NON-NLS-1$
    buffer.append(NamedElementUtil.getVisibilityAsSign(parameter));

    // direction
    buffer.append(" "); // $NON-NLS-1$
    buffer.append(parameter.getDirection().getLiteral());

    // name
    buffer.append(" "); // $NON-NLS-1$
    if (parameter.getName() != null) {
      buffer.append(parameter.getName());
    }

    // type
    if (parameter.getType() != null) {
      EList<Namespace> namespaces = parameter.allNamespaces();
      buffer.append(
          " : "
              + getTypeLabel(
                  parameter.getType(), namespaces.get(namespaces.size() - 1))); // $NON-NLS-1$
    } else {
      buffer.append(" : " + TypeUtil.UNDEFINED_TYPE_NAME); // $NON-NLS-1$
    }

    // multiplicity -> do not display [1]
    String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(parameter);
    if (!multiplicity.trim().equals("[1]")) { // $NON-NLS-1$
      buffer.append(multiplicity);
    }

    // default value
    if (parameter.getDefault() != null) {
      buffer.append(" = "); // $NON-NLS-1$
      buffer.append(parameter.getDefault());
    }

    // property modifiers
    buffer.append(ParameterUtil.getModifiersAsString(parameter, false));
    buffer.append(getEffectAsString(parameter));
    return buffer.toString();
  }
  /**
   * Returns the string representing the import block for a given classifier.
   *
   * @param aClassifier The classifier
   * @return The string representing the import block for a given classifier.
   */
  public String reqImport(Classifier aClassifier) {
    Set<String> importedTypes = new LinkedHashSet<String>();

    List<Property> attributes = aClassifier.getAttributes();
    for (Property property : attributes) {
      String qualifiedName = this.qualifiedName(property.getType());
      if (qualifiedName != null) {
        importedTypes.add(qualifiedName);
      }
      if (property.getUpper() != 1) {
        String collectionQualifiedName =
            this.collectionQualifiedName(aClassifier, property.isOrdered(), property.isUnique());
        if (collectionQualifiedName != null) {
          importedTypes.add(collectionQualifiedName);
        }
      }
    }

    List<Operation> operations = aClassifier.getOperations();
    for (Operation operation : operations) {
      String qualifiedName = this.qualifiedName(operation.getType());
      if (qualifiedName != null) {
        importedTypes.add(qualifiedName);
      }
      if (operation.getUpper() != 1) {
        String collectionQualifiedName =
            this.collectionQualifiedName(aClassifier, operation.isOrdered(), operation.isUnique());
        if (collectionQualifiedName != null) {
          importedTypes.add(collectionQualifiedName);
        }
      }

      List<Parameter> ownedParameters = operation.getOwnedParameters();
      for (Parameter parameter : ownedParameters) {
        qualifiedName = this.qualifiedName(parameter.getType());
        if (qualifiedName != null) {
          importedTypes.add(qualifiedName);
        }
        if (parameter.getUpper() != 1) {
          String collectionQualifiedName =
              this.collectionQualifiedName(
                  aClassifier, parameter.isOrdered(), parameter.isUnique());
          if (collectionQualifiedName != null) {
            importedTypes.add(collectionQualifiedName);
          }
        }
      }

      List<Type> raisedExceptions = operation.getRaisedExceptions();
      for (Type type : raisedExceptions) {
        String exceptionQualifiedName = this.qualifiedName(type);
        if (exceptionQualifiedName != null) {
          importedTypes.add(exceptionQualifiedName);
        }
      }
    }

    List<String> sortedImportedTypes = new ArrayList<String>(importedTypes);
    Collections.sort(sortedImportedTypes);

    StringBuilder stringBuilder = new StringBuilder();
    for (String importedType : sortedImportedTypes) {
      stringBuilder.append(IMPORT + importedType + END_IMPORT);
    }

    return stringBuilder.toString();
  }
Example #4
0
 public static String getCustomLabel(
     Message e, int paramIndex, Parameter parameter, Collection<String> displayValue) {
   StringBuffer buffer = new StringBuffer();
   // visibility
   buffer.append(" ");
   if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) {
     buffer.append(NamedElementUtil.getVisibilityAsSign(parameter));
   }
   // direction property
   if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DIRECTION)) {
     buffer.append(" ");
     buffer.append(parameter.getDirection().getLiteral());
   }
   boolean showEqualMark = false;
   // name
   if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_NAME)) {
     buffer.append(" ");
     String name = StringHelper.trimToEmpty(parameter.getName());
     if (name.trim().length() > 0) {
       showEqualMark = true;
     }
     buffer.append(name);
   }
   if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_TYPE)) {
     // type
     if (parameter.getType() != null) {
       buffer.append(": " + StringHelper.trimToEmpty(parameter.getType().getName()));
     } else {
       buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME);
     }
     showEqualMark = true;
   }
   if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_MULTIPLICITY)) {
     // multiplicity -> do not display [1]
     String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(parameter);
     buffer.append(multiplicity);
   }
   if (displayValue.contains(ICustomAppearance.DISP_DERIVE)) {
     String value = getValue(e, paramIndex, parameter);
     if (value != null) {
       if (showEqualMark) {
         buffer.append(" = ");
       }
       buffer.append(value);
     }
   } else if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DEFAULT)) {
     // default value
     if (parameter.getDefault() != null) {
       if (showEqualMark) {
         buffer.append(" = ");
       }
       buffer.append(parameter.getDefault());
     }
   }
   if (displayValue.contains(ICustomAppearance.DISP_MODIFIERS)) {
     boolean multiLine = displayValue.contains(ICustomAppearance.DISP_MULTI_LINE);
     // property modifiers
     String modifiers = ParameterUtil.getModifiersAsString(parameter, multiLine);
     if (!modifiers.equals("")) {
       if (multiLine) {
         buffer.append("\n");
       }
       buffer.append(modifiers);
     }
   }
   return buffer.toString();
 }