Exemple #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;
  }
Exemple #2
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();
  }