Esempio n. 1
0
  /**
   * Return true if this property is write only, which means we cannot get a value for it.
   *
   * @param instance the instance
   * @param propertyName the property name
   * @return true if there is a way to write but not read the property
   */
  public boolean isWriteOnly(Object instance, String propertyName) {
    if (instance == null || propertyName == null) return false;

    BeanProperty bp = getBeanProperty(instance, propertyName);
    return bp != null && bp.isWrite() && !bp.isRead();
  }
Esempio n. 2
0
  /** {@inheritDoc} */
  public void setValue(Object instance, String propertyName, Object value) {
    BeanProperty bp = getBeanProperty(instance, propertyName);

    if (bp != null) {
      if (bp.isWrite()) {
        try {
          Class desiredPropClass = bp.getType();
          TypeMarshaller marshaller = TypeMarshallingContext.getTypeMarshaller();
          value = marshaller.convert(value, desiredPropClass);
          ClassUtil.validateAssignment(instance, propertyName, value);
          bp.set(instance, value);
        } catch (Exception e) {
          SerializationContext context = getSerializationContext();

          // Log ignore failed property set errors
          if (Log.isWarn() && logPropertyErrors(context)) {
            Logger log = Log.getLogger(LOG_CATEGORY);
            log.warn(
                "Failed to set property {0} on type {1}.",
                new Object[] {propertyName, getAlias(instance)}, e);
          }

          if (!ignorePropertyErrors(context)) {
            // Failed to get property '{propertyName}' on type '{className}'.
            MessageException ex = new MessageException();
            ex.setMessage(
                FAILED_PROPERTY_WRITE_ERROR, new Object[] {propertyName, getAlias(instance)});
            ex.setRootCause(e);
            throw ex;
          }
        }
      } else {
        SerializationContext context = getSerializationContext();

        if (Log.isWarn() && logPropertyErrors(context)) {
          Logger log = Log.getLogger(LOG_CATEGORY);
          log.warn(
              "Property {0} not writable on class {1}",
              new Object[] {propertyName, getAlias(instance)});
        }

        if (!ignorePropertyErrors(context)) {
          // Property '{propertyName}' not writable on class '{alias}'.
          MessageException ex = new MessageException();
          ex.setMessage(
              NON_WRITABLE_PROPERTY_ERROR, new Object[] {propertyName, getAlias(instance)});
          throw ex;
        }
      }
    } else {
      SerializationContext context = getSerializationContext();

      if (Log.isWarn() && logPropertyErrors(context)) {
        Logger log = Log.getLogger(LOG_CATEGORY);
        log.warn(
            "Ignoring set property {0} for type {1} as a setter could not be found.",
            new Object[] {propertyName, getAlias(instance)});
      }

      if (!ignorePropertyErrors(context)) {
        // Property '{propertyName}' not found on class '{alias}'.
        MessageException ex = new MessageException();
        ex.setMessage(UNKNOWN_PROPERTY_ERROR, new Object[] {propertyName, getAlias(instance)});
        throw ex;
      }
    }
  }