Class PropertySetter


  • public class PropertySetter
    extends java.lang.Object
    General purpose Object property setter. Clients repeatedly invokes setProperty(name,value) in order to invoke setters on the Object specified in the constructor. This class relies on the JavaBeans Introspector to analyze the given Object Class using reflection.

    Usage:

     PropertySetter ps = new PropertySetter(anObject);
     ps.set("name", "Joe");
     ps.set("age", "32");
     ps.set("isMale", "true");
     
    will cause the invocations anObject.setName("Joe"), anObject.setAge(32), and setMale(true) if such methods exist with those signatures. Otherwise an IntrospectionException are thrown.
    • Constructor Summary

      Constructors 
      Constructor Description
      PropertySetter​(java.lang.Object obj)
      Create a new PropertySetter for the specified Object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activate()  
      protected java.lang.Object convertArg​(java.lang.String val, java.lang.Class<?> type)
      Convert val a String parameter to an object of a given type.
      protected java.beans.PropertyDescriptor getPropertyDescriptor​(java.lang.String name)  
      protected void introspect()
      Uses JavaBeans Introspector to computer setters of object to be configured.
      static void setProperties​(java.lang.Object obj, java.util.Properties properties, java.lang.String prefix)
      Set the properties of an object passed as a parameter in one go.
      void setProperties​(java.util.Properties properties, java.lang.String prefix)
      Set the properites for the object that match the prefix passed as parameter.
      void setProperty​(java.beans.PropertyDescriptor prop, java.lang.String name, java.lang.String value)
      Set the named property given a PropertyDescriptor.
      void setProperty​(java.lang.String name, java.lang.String value)
      Set a property on this PropertySetter's Object.
      • Methods inherited from class java.lang.Object

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

      • EMPTY_PROPERTY_DESCRIPTOR_ARRAY

        private static final java.beans.PropertyDescriptor[] EMPTY_PROPERTY_DESCRIPTOR_ARRAY
      • LOGGER

        private static Logger LOGGER
      • obj

        protected java.lang.Object obj
      • props

        protected java.beans.PropertyDescriptor[] props
    • Constructor Detail

      • PropertySetter

        public PropertySetter​(java.lang.Object obj)
        Create a new PropertySetter for the specified Object. This is done in prepartion for invoking setProperty(java.lang.String, java.lang.String) one or more times.
        Parameters:
        obj - the object for which to set properties
    • Method Detail

      • setProperties

        public static void setProperties​(java.lang.Object obj,
                                         java.util.Properties properties,
                                         java.lang.String prefix)
        Set the properties of an object passed as a parameter in one go. The properties are parsed relative to a prefix.
        Parameters:
        obj - The object to configure.
        properties - A java.util.Properties containing keys and values.
        prefix - Only keys having the specified prefix will be set.
      • introspect

        protected void introspect()
        Uses JavaBeans Introspector to computer setters of object to be configured.
      • setProperties

        public void setProperties​(java.util.Properties properties,
                                  java.lang.String prefix)
        Set the properites for the object that match the prefix passed as parameter.
        Parameters:
        properties - The properties.
        prefix - The prefix of the properties to use.
      • setProperty

        public void setProperty​(java.lang.String name,
                                java.lang.String value)
        Set a property on this PropertySetter's Object. If successful, this method will invoke a setter method on the underlying Object. The setter is the one for the specified property name and the value is determined partly from the setter argument type and partly from the value specified in the call to this method.

        If the setter expects a String no conversion is necessary. If it expects an int, then an attempt is made to convert 'value' to an int using new Integer(value). If the setter expects a boolean, the conversion is by new Boolean(value).

        Parameters:
        name - name of the property
        value - String value of the property
      • setProperty

        public void setProperty​(java.beans.PropertyDescriptor prop,
                                java.lang.String name,
                                java.lang.String value)
                         throws PropertySetterException
        Set the named property given a PropertyDescriptor.
        Parameters:
        prop - A PropertyDescriptor describing the characteristics of the property to set.
        name - The named of the property to set.
        value - The value of the property.
        Throws:
        PropertySetterException - if no setter is available.
      • convertArg

        protected java.lang.Object convertArg​(java.lang.String val,
                                              java.lang.Class<?> type)
        Convert val a String parameter to an object of a given type.
        Parameters:
        val - The value to convert.
        type - The type of the value to convert to.
        Returns:
        The result of the conversion.
      • getPropertyDescriptor

        protected java.beans.PropertyDescriptor getPropertyDescriptor​(java.lang.String name)
      • activate

        public void activate()