Class PropertyResolver


  • public final class PropertyResolver
    extends java.lang.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 get and set method. Or if a map is given as an object it will be lookup with the property as a key when there is not get method for that property.
    "property1.property2": Both properties are looked up as described above. If property1 evaluates to null then if there is a setMethod (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.
    "property.index": If the property is a List or Array then the second property can be a index on that list like: 'mylist.0' this expression will also map on a getProperty(index) or setProperty(index,value) methods. If the object is a List then the list will grow automatically if the index is greater than the size
    Index or map properties can also be written as: "property[index]" or "property[key]"
    Note that the property resolver 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 Model then the expression should use '.object' to work with its value.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void destroy​(java.lang.Object application)
      Clean up cache for this app.
      static <T> java.lang.Class<T> getPropertyClass​(java.lang.String expression, java.lang.Class<?> clz)
      Gets the property class.
      static java.lang.Class<?> getPropertyClass​(java.lang.String expression, java.lang.Object object)
      Gets the property class.
      static java.lang.reflect.Field getPropertyField​(java.lang.String expression, java.lang.Object object)
      Gets the property field.
      static java.lang.reflect.Method getPropertyGetter​(java.lang.String expression, java.lang.Object object)
      Gets the property getter.
      static java.lang.reflect.Method getPropertySetter​(java.lang.String expression, java.lang.Object object)
      Gets the property setter.
      static java.lang.Object getValue​(java.lang.String expression, java.lang.Object object)
      Looks up the value from the object with the given expression.
      static java.lang.CharSequence replaceAll​(java.lang.CharSequence s, java.lang.CharSequence searchFor, java.lang.CharSequence replaceWith)
      Replace all occurrences of one string replaceWith another string.
      static void setClassCache​(java.lang.Object application, de.alpharogroup.model.api.ClassCache classCache)
      Sets the ClassCache for the given application.
      static void setValue​(java.lang.String expression, java.lang.Object object, java.lang.Object value)
      Set the value on the object with the given expression.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • destroy

        public static void destroy​(java.lang.Object application)
        Clean up cache for this app.
        Parameters:
        application - the application
      • getPropertyClass

        public static <T> java.lang.Class<T> getPropertyClass​(java.lang.String expression,
                                                              java.lang.Class<?> clz)
        Gets the property class.
        Type Parameters:
        T - the generic type
        Parameters:
        expression - the expression
        clz - the class
        Returns:
        class of the target Class property expression
        Throws:
        java.lang.RuntimeException - if class cannot be resolved
      • getPropertyClass

        public static final java.lang.Class<?> getPropertyClass​(java.lang.String expression,
                                                                java.lang.Object object)
        Gets the property class.
        Parameters:
        expression - the expression
        object - the object
        Returns:
        class of the target property object
        Throws:
        java.lang.RuntimeException - if the cannot be resolved
      • getPropertyField

        public static final java.lang.reflect.Field getPropertyField​(java.lang.String expression,
                                                                     java.lang.Object object)
        Gets the property field.
        Parameters:
        expression - the expression
        object - the object
        Returns:
        Field for the property expression
        Throws:
        java.lang.RuntimeException - if there is no such field
      • getPropertyGetter

        public static final java.lang.reflect.Method getPropertyGetter​(java.lang.String expression,
                                                                       java.lang.Object object)
        Gets the property getter.
        Parameters:
        expression - the expression
        object - the object
        Returns:
        Getter method for the property expression
        Throws:
        java.lang.RuntimeException - if there is no getter method
      • getPropertySetter

        public static final java.lang.reflect.Method getPropertySetter​(java.lang.String expression,
                                                                       java.lang.Object object)
        Gets the property setter.
        Parameters:
        expression - the expression
        object - the object
        Returns:
        Setter method for the property expression
        Throws:
        java.lang.RuntimeException - if there is no setter method
      • getValue

        public static final java.lang.Object getValue​(java.lang.String expression,
                                                      java.lang.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.
      • replaceAll

        public static java.lang.CharSequence replaceAll​(java.lang.CharSequence s,
                                                        java.lang.CharSequence searchFor,
                                                        java.lang.CharSequence replaceWith)
        Replace all occurrences of one string replaceWith another string.
        Parameters:
        s - The string to process
        searchFor - The value to search for
        replaceWith - The value to searchFor replaceWith
        Returns:
        The resulting string with searchFor replaced with replaceWith
      • setClassCache

        public static void setClassCache​(java.lang.Object application,
                                         de.alpharogroup.model.api.ClassCache classCache)
        Sets the ClassCache for the given application. If the Application is null then it will be the default if no application is found. So if you want to be sure that your ClassCache is handled in all situations then call this method twice with your implementations. One time for the application and the second time with null.
        Parameters:
        application - to use or null if the default must be set.
        classCache - the class cache
      • setValue

        public static final void setValue​(java.lang.String expression,
                                          java.lang.Object object,
                                          java.lang.Object value)
        Set the value on the object with the given expression. If the expression can't be evaluated then a RuntimeException 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.
        Throws:
        java.lang.RuntimeException - is thrown if expression is empty or the object is null or the setter could not be found