Ejemplo n.º 1
0
  private void setValueUsingSetter(Object value) throws ComponentConfigurationException {
    if (setterParamType == null || setter == null) {
      throw new ComponentConfigurationException("No setter found");
    }

    String exceptionInfo =
        object.getClass().getName()
            + "."
            + setter.getName()
            + "( "
            + setterParamType.getClass().getName()
            + " )";

    if (listener != null) {
      listener.notifyFieldChangeUsingSetter(fieldName, value, object);
    }

    try {
      setter.invoke(object, new Object[] {value});
    } catch (IllegalAccessException e) {
      throw new ComponentConfigurationException("Cannot access method: " + exceptionInfo, e);
    } catch (IllegalArgumentException e) {
      throw new ComponentConfigurationException(
          "Invalid parameter supplied while setting '" + value + "' to " + exceptionInfo, e);
    } catch (InvocationTargetException e) {
      throw new ComponentConfigurationException(
          "Setter "
              + exceptionInfo
              + " threw exception when called with parameter '"
              + value
              + "': "
              + e.getTargetException().getMessage(),
          e);
    }
  }
Ejemplo n.º 2
0
  private void setValueUsingField(Object value) throws ComponentConfigurationException {
    try {
      boolean wasAccessible = field.isAccessible();

      if (!wasAccessible) {
        field.setAccessible(true);
      }

      if (listener != null) {
        listener.notifyFieldChangeUsingReflection(fieldName, value, object);
      }

      field.set(object, value);

      if (!wasAccessible) {
        field.setAccessible(false);
      }
    } catch (IllegalAccessException e) {
      throw new ComponentConfigurationException("Cannot access field: " + field, e);
    } catch (IllegalArgumentException e) {
      throw new ComponentConfigurationException(
          "Cannot assign value '" + value + "' (type: " + value.getClass() + ") to " + field, e);
    }
  }