Class PropertyResolver

java.lang.Object
org.apache.wicket.core.util.lang.PropertyResolver

public final class PropertyResolver extends Object
This class parses expressions to lookup or set a value on the object that is given.
The supported expressions are:
"property"
This could be a bean property with getter and setter. Or if a map is given as an object it will be lookup with the expression as a key when there is not getter for that property.
"property1.property2"
Both properties are looked up as described above. If property1 evaluates to null then if there is a setter (or if it is a map) and the Class of the property has a default constructor then the object will be constructed and set on the object.
"method()"
The corresponding method is invoked.
"property.index" or "property[index]"
If the property is a List or Array then the following expression can be a index on that list like: 'mylist.0'. The list will grow automatically if the index is greater than the size.

This expression will also map on indexed properties, i.e. getProperty(index) and setProperty(index,value) methods.

"property.key" or "property[key]"
If the property is a Map then the following expression can be a key in that map like: 'myMap.key'.
Note that the PropertyResolver.DefaultPropertyLocator by default provides access to private members and methods. If guaranteeing encapsulation of the target objects is a big concern, you should consider using an alternative implementation.

Note: If a property evaluates to an instance of IModel then the expression should use '.object' to work with its value.

Author:
jcompagner, svenmeier
  • Method Details

    • getValue

      public static Object getValue(String expression, Object object)
      Looks up the value from the object with the given expression. If the expression, the object itself or one property evaluates to null then a null will be returned.
      Parameters:
      expression - The expression string with the property to be lookup.
      object - The object which is evaluated.
      Returns:
      The value that is evaluated. Null something in the expression evaluated to null.
    • setValue

      public static void setValue(String expression, Object object, Object value, PropertyResolverConverter converter)
      Set the value on the object with the given expression. If the expression can't be evaluated then a WicketRuntimeException will be thrown. If a null object is encountered then it will try to generate it by calling the default constructor and set it on the object. The value will be tried to convert to the right type with the given converter.
      Parameters:
      expression - The expression string with the property to be set.
      object - The object which is evaluated to set the value on.
      value - The value to set.
      converter - The converter to convert the value if needed to the right type.
      Throws:
      WicketRuntimeException
    • getPropertyClass

      public static Class<?> getPropertyClass(String expression, Object object)
      Parameters:
      expression -
      object -
      Returns:
      class of the target property object
      Throws:
      WicketRuntimeException - if the cannot be resolved
    • getPropertyClass

      public static <T> Class<T> getPropertyClass(String expression, Class<?> clz)
      Type Parameters:
      T -
      Parameters:
      expression -
      clz -
      Returns:
      class of the target Class property expression
      Throws:
      WicketRuntimeException - if class cannot be resolved
    • getPropertyField

      public static Field getPropertyField(String expression, Object object)
      Parameters:
      expression -
      object -
      Returns:
      Field for the property expression
      Throws:
      WicketRuntimeException - if there is no such field
    • getPropertyGetter

      public static Method getPropertyGetter(String expression, Object object)
      Parameters:
      expression -
      object -
      Returns:
      Getter method for the property expression
      Throws:
      WicketRuntimeException - if there is no getter method
    • getPropertySetter

      public static Method getPropertySetter(String expression, Object object)
      Parameters:
      expression -
      object -
      Returns:
      Setter method for the property expression
      Throws:
      WicketRuntimeException - if there is no setter method
    • destroy

      public static void destroy(Application application)
      Clean up cache for this app.
      Parameters:
      application -
    • getLocator

      Returns:
      locator for the current Application or a general one if no current application is present
      See Also:
    • setLocator

      public static void setLocator(Application application, PropertyResolver.IPropertyLocator locator)
      Set a locator for the given application.
      Parameters:
      application - application, may be null
      locator - locator