Exemplo n.º 1
0
  /** Return an entry from the property descrpitor cache for a class. */
  private PropertyDescriptorCacheEntry getPropertyDescriptorCacheEntry(Class c) {
    PropertyDescriptorCacheEntry pce =
        (PropertyDescriptorCacheEntry) propertyDescriptorCache.get(c);

    try {
      if (pce == null) {
        BeanInfo beanInfo = Introspector.getBeanInfo(c, stopClass);
        pce = new PropertyDescriptorCacheEntry();
        pce.propertyDescriptors = beanInfo.getPropertyDescriptors();
        pce.propertiesByName = createPropertiesByNameMap(pce.propertyDescriptors, c.getFields());
        if (cachePropertiesDescriptors) {
          synchronized (propertyDescriptorCache) {
            PropertyDescriptorCacheEntry pce2 =
                (PropertyDescriptorCacheEntry) propertyDescriptorCache.get(c);
            if (pce2 == null) propertyDescriptorCache.put(c, pce);
            else pce = pce2;
          }
        }
      }
    } catch (IntrospectionException ex) {
      // Log failed property set errors
      if (Log.isError()) {
        Logger log = Log.getLogger(LOG_CATEGORY);
        log.error(
            "Failed to introspect object of type: " + c + " error: " + ExceptionUtil.toString(ex));
      }

      // Return an empty descriptor rather than crashing
      pce = new PropertyDescriptorCacheEntry();
      pce.propertyDescriptors = new PropertyDescriptor[0];
      pce.propertiesByName = new TreeMap();
    }
    return pce;
  }
Exemplo n.º 2
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;
  }
Exemplo n.º 3
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;
      }
    }
  }