Beispiel #1
0
  /**
   * Gets the value specified by the BeanProperty
   *
   * @param instance Object to get the value from
   * @param bp the property to get
   * @return the value of the property if it exists
   */
  protected final Object getBeanValue(Object instance, BeanProperty bp) {
    String propertyName = bp.getName();
    if (bp.isRead()) {
      try {
        Object value = bp.get(instance);
        if (value != null && descriptor != null) {
          SerializationDescriptor subDescriptor =
              (SerializationDescriptor) descriptor.get(propertyName);
          if (subDescriptor != null) {
            PropertyProxy subProxy = PropertyProxyRegistry.getProxyAndRegister(value);
            subProxy = (PropertyProxy) subProxy.clone();
            subProxy.setDescriptor(subDescriptor);
            subProxy.setDefaultInstance(value);
            value = subProxy;
          }
        }
        return value;
      } catch (Exception e) {
        SerializationContext context = getSerializationContext();

        // Log failed property set errors
        if (Log.isWarn() && logPropertyErrors(context)) {
          Logger log = Log.getLogger(LOG_CATEGORY);
          log.warn(
              "Failed to get 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_READ_ERROR, new Object[] {propertyName, getAlias(instance)});
          ex.setRootCause(e);
          throw ex;
        }
      }
    } else {
      SerializationContext context = getSerializationContext();
      if (!ignorePropertyErrors(context)) {
        // Property '{propertyName}' not readable from class '{alias}'.
        MessageException ex = new MessageException();
        ex.setMessage(NON_READABLE_PROPERTY_ERROR, new Object[] {propertyName, getAlias(instance)});
        throw ex;
      }
    }
    return null;
  }
Beispiel #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;
      }
    }
  }