/**
     * Process the end of this element.
     *
     * @param namespace the namespace URI of the matching element, or an empty string if the parser
     *     is not namespace aware or the element has no namespace
     * @param name the local name if the parser is namespace aware, or just the element name
     *     otherwise
     * @exception NoSuchMethodException if the bean does not have a writeable property of the
     *     specified name
     */
    public void end(String namespace, String name) throws Exception {

      String property = propertyName;

      if (property == null) {
        // If we don't have a specific property name,
        // use the element name.
        property = name;
      }

      // Get a reference to the top object
      Object top = this.digester.peek();

      // log some debugging information
      if (getDigester().getLogger().isDebugEnabled()) {
        getDigester()
            .getLogger()
            .debug(
                "[BeanPropertySetterRule]{"
                    + getDigester().getMatch()
                    + "} Set "
                    + top.getClass().getName()
                    + " property "
                    + property
                    + " with text "
                    + bodyText);
      }

      // Force an exception if the property does not exist
      // (BeanUtils.setProperty() silently returns in this case)
      if (top instanceof DynaBean) {
        DynaProperty desc = ((DynaBean) top).getDynaClass().getDynaProperty(property);
        if (desc == null) {
          throw new NoSuchMethodException("Bean has no property named " + property);
        }
      } else /* this is a standard JavaBean */ {
        PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(top, property);
        if (desc == null) {
          throw new NoSuchMethodException("Bean has no property named " + property);
        }
      }

      // Set the property only using this converter
      Object value = converter.convert(clazz, bodyText);
      PropertyUtils.setProperty(top, property, value);
    }
Exemplo n.º 2
0
  /**
   * Helper method for {@link #parse(StaplerRequest, Annotation, Class, String)} to convert to the
   * right type from String.
   */
  protected final Object convert(Class targetType, String value) {
    Converter converter = Stapler.lookupConverter(targetType);
    if (converter == null) throw new IllegalArgumentException("Unable to convert to " + targetType);

    return converter.convert(targetType, value);
  }
Exemplo n.º 3
0
 public static <S, D> D convertType(Object obj, Class<S> srcClass, Class<D> destClass) {
   Converter converter = convertUtils.lookup(srcClass, destClass);
   return converter.convert(destClass, obj);
 }