Class PropertyAccess


  • public class PropertyAccess
    extends Object
    Accesses properties of java classes by reflection.
    Version:
    $Id: $
    • Constructor Detail

      • PropertyAccess

        public PropertyAccess​(Object target,
                              String propertyName)
                       throws SourceTransformerException
        Constructs a reflection access to a property of an object. The property is assumed to be a public field, or if no such field exists on the target object, by getters and setters using the java beans naming conventions. The searched field and method suffixes are "s", "Array", "List", the searched field prefixes are "_". It is not an error to call this constructor on fields which do not exist.
        Parameters:
        target - the target object, not null.
        propertyName - the name of the property, not null.
        Throws:
        SourceTransformerException - if reflection access to fields or methods fails.
        NullPointerException - if target or propertyName are null.
      • PropertyAccess

        public PropertyAccess​(Object target,
                              String propertyName,
                              List<String> prefixList,
                              List<String> suffixList)
                       throws SourceTransformerException
        Constructs a reflection access to a property of an object. The property is assumed to be a public field, or if no such field exists on the target object, by getters and setters using the java beans naming conventions. As second order preference, the suffix list is worked first, if no match is found there the prefix list is tried. It is not an error to call this constructor on fields which do not exist.
        Parameters:
        target - the target object, not null.
        propertyName - the name of the property, not null.
        prefixList - List of all possible prefixes for field names. I.e. if the property name is "name", "prefix" + "name" is also tried as field name if "prefix" is contained in this list.
        suffixList - List of all possible suffixes for field and method names. I.e. if the property name is "name", "name" + "suffix" is also tried as field name and base method name if "suffix" is contained in this list.
        Throws:
        SourceTransformerException - if reflection access to fields or methods fails.
        NullPointerException - if target or propertyName are null.
    • Method Detail

      • setProperty

        public void setProperty​(Object value)
                         throws SourceTransformerException
        Sets the property on the target object to a given value. If the property is an array and value is not an array, value is set as a member of the array. If the property is a collection and value is not a collection, value is added to the collection. In all other cases, the field is set to the value.
        Parameters:
        value - the value to set the property to.
        Throws:
        SourceTransformerException - if the property cannot be set. Common reasons for such an exception are that the property does not exist at all, or is not writeable, or value has the wrong class.
      • setPropertyStrict

        public void setPropertyStrict​(Object value)
                               throws SourceTransformerException
        Sets the property on the target object to a given value. Arrays and Lists are not treated specially.
        Parameters:
        value - the value to set the property to.
        Throws:
        SourceTransformerException - if the property cannot be set. Common reasons for such an exception are that the property does not exist at all, or is not writeable, or value has the wrong class.
      • getProperty

        public Object getProperty()
                           throws SourceTransformerException
        Returns the value of the property.
        Returns:
        the value of the property.
        Throws:
        SourceTransformerException - if the property is not readable. A common reason for this is that no public field and no getter exists with the given name.
      • isPropertyAccessible

        public boolean isPropertyAccessible()
      • getPropertyType

        public Class<?> getPropertyType()
        Returns the class of the property.
        Returns:
        the class of the property, or null if the property does not exist.
      • getPropertyGenericType

        public Type getPropertyGenericType()
        Returns the generic type of the property.
        Returns:
        the generic type of the property, or null if the property does not exist.
      • getFirstGenericTypeArgument

        public Class<?> getFirstGenericTypeArgument()
        Gets the class of the first generic argument of the property. E.g. if the property is a collection, this method will return the class of the members of the collection.
        Returns:
        the class of the first generic argument of the property, or null if the property is not generified.
      • getPrefixList

        public List<String> getPrefixList()
        Returns the list of all possible prefixes for field names. I.e. if the property name is "name", "prefix" + "name" is also tried as field name if "prefix" is contained in this list.
        Returns:
        the prefix list, not null.
      • getSuffixList

        public List<String> getSuffixList()
        Returns the list of all possible suffixes for field and method names. I.e. if the property name is "name", "name" + "suffix" is also tried as field name and base method name if "suffix" is contained in this list.
        Returns:
        the suffix list with added "" as first entry, not null.