Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-4767

PropertyModel fails to identifiy generic properties

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Cannot Reproduce
    • 1.5.8
    • None
    • wicket

    Description

      If I use e.g. PropertyModel<String>(new Bean<String>(), "value") on a generic bean Bean<T>

      { T value; }

      this will not work in Wicket by its property resolver. If I replace the generic type by its actual type, Wicket's property resolver works just fine for me.

      However, in my opinion Wicket should try to resolve this issue by using the property object's actual type instead of just giving up since the PropertyModel already compromizes type safety.

      PS: Fixed it, this works totally fine for me: (Using the Apache Commons PropertyUtils, why does Wicket use its own implementation anyways?)

      import java.lang.reflect.InvocationTargetException;

      import org.apache.commons.beanutils.PropertyUtils;
      import org.apache.wicket.model.IModel;

      @SuppressWarnings("serial")
      public class GenericCompatiblePropertyModel<T> implements IModel<T> {

      private String propertyExpression;
      private Object target;

      public GenericCompatiblePropertyModel(Object target, String propertyExpression)

      { this.propertyExpression = propertyExpression; this.target = target; if(target == null) throw new IllegalArgumentException("[target] must not be null"); }

      @SuppressWarnings("unchecked")
      @Override
      public T getObject() {
      try

      { return (T) PropertyUtils.getProperty(target, propertyExpression); }

      catch (IllegalAccessException e)

      { throw new RuntimeException("Property '" + propertyExpression + "' access is illegal for " + target.getClass().getSimpleName(), e); } catch (InvocationTargetException e) { throw new RuntimeException("Property '" + propertyExpression + "' cannot be resolved for " + target.getClass().getSimpleName(), e); } catch (NoSuchMethodException e) { throw new RuntimeException("Property '" + propertyExpression + "' cannot be found for " + target.getClass().getSimpleName(), e); }
      }

      @Override
      public void setObject(T object) {
      try { PropertyUtils.setProperty(target, propertyExpression, object); } catch (IllegalAccessException e) { throw new RuntimeException("Property '" + propertyExpression + "' access is illegal for " + target.getClass().getSimpleName(), e); }

      catch (InvocationTargetException e)

      { throw new RuntimeException("Property '" + propertyExpression + "' cannot be resolved for " + target.getClass().getSimpleName(), e); }

      catch (NoSuchMethodException e)

      { throw new RuntimeException("Property '" + propertyExpression + "' cannot be found for " + target.getClass().getSimpleName(), e); }

      }

      @Override
      public void detach()

      { /* empty */ }

      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            raphw Rafael Winterhalter
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: