/**
   * Creates a {@link hudson.model.StringParameterValue} and adds it to the provided list. If the
   * parameter with the same name already exists in the list it will be replaced by the new
   * parameter, but its description will be used, unless the parameter type is something else than a
   * StringParameterValue.
   *
   * @param parameters the list of existing parameters.
   * @param value the value.
   * @param escapeQuotes if quote characters should be escaped.
   */
  public void setOrCreateStringParameterValue(
      List<ParameterValue> parameters, String value, boolean escapeQuotes) {
    ParameterValue parameter = null;
    for (ParameterValue p : parameters) {
      if (p.getName().toUpperCase().equals(this.name())) {
        parameter = p;
        break;
      }
    }
    String description = null;
    if (parameter != null) {
      if (parameter instanceof StringParameterValue) {
        // Perhaps it is manually added to remind the user of what it is for.
        description = parameter.getDescription();
      }
      parameters.remove(parameter);
    }
    String stringValue;
    if (escapeQuotes) {
      stringValue = StringUtil.escapeQuotes(value);
    } else {
      stringValue = value;
    }
    if (stringValue == null) {
      stringValue = "";
    }

    parameter = new StringParameterValue(this.name(), stringValue, description);
    parameters.add(parameter);
  }
  /**
   * Creates a {@link hudson.model.ParameterValue} and adds it to the provided list. If the
   * parameter with the same name already exists in the list it will be replaced by the new
   * parameter, but its description will be used, unless the parameter type is something else than a
   * StringParameterValue.
   *
   * @param parameters the list of existing parameters.
   * @param value the value.
   * @param escapeQuotes if quote characters should be escaped.
   * @param clazz the class which extends {@link hudson.model.ParameterValue}.
   */
  private void setOrCreateParameterValue(
      List<ParameterValue> parameters,
      String value,
      boolean escapeQuotes,
      Class<? extends StringParameterValue> clazz) {
    ParameterValue parameter = null;
    for (ParameterValue p : parameters) {
      if (p.getName().toUpperCase().equals(this.name())) {
        parameter = p;
        break;
      }
    }
    String description = null;
    if (parameter != null) {
      if (parameter instanceof StringParameterValue) {
        // Perhaps it is manually added to remind the user of what it is for.
        description = parameter.getDescription();
      }
      parameters.remove(parameter);
    }
    String stringValue;
    if (escapeQuotes) {
      stringValue = StringUtil.escapeQuotes(value);
    } else {
      stringValue = value;
    }
    if (stringValue == null) {
      stringValue = "";
    }

    Class<?>[] types = {String.class, String.class, String.class};
    Object[] args = {this.name(), stringValue, description};
    Constructor<? extends StringParameterValue> constructor;
    try {
      constructor = clazz.getConstructor(types);
      parameter = constructor.newInstance(args);
      parameters.add(parameter);
    } catch (Exception ex) {
      parameter = null;
    }
  }