Package jmri.beans

Class BeanUtil


  • public class BeanUtil
    extends java.lang.Object
    JMRI-specific tools for the introspection of JavaBean properties.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean contains​(java.beans.PropertyChangeListener[] listeners, java.beans.PropertyChangeListener needle)
      Test that listeners contains needle even if listener is contained within a PropertyChangeListenerProxy.
      static java.lang.Object getIndexedProperty​(java.lang.Object bean, java.lang.String key, int index)
      Get the item at index index of property key of bean.
      static java.lang.Object getIntrospectedIndexedProperty​(java.lang.Object bean, java.lang.String key, int index)
      Get the item at index index of property key of bean.
      static java.lang.Object getIntrospectedProperty​(java.lang.Object bean, java.lang.String key)
      Get the property key of bean.
      static java.util.Set<java.lang.String> getIntrospectedPropertyNames​(java.lang.Object bean)
      Use an Introspector to get a set of the named properties of the bean.
      static java.lang.Object getProperty​(java.lang.Object bean, java.lang.String key)
      Get the property key of bean.
      static java.util.Set<java.lang.String> getPropertyNames​(java.lang.Object bean)  
      static boolean hasIndexedProperty​(java.lang.Object bean, java.lang.String key)
      Test if bean has the indexed property key.
      static boolean hasIntrospectedIndexedProperty​(java.lang.Object bean, java.lang.String key)
      Test that bean has the indexed property key.
      static boolean hasIntrospectedProperty​(java.lang.Object bean, java.lang.String key)
      Test that bean has the property key.
      static boolean hasProperty​(java.lang.Object bean, java.lang.String key)
      Test if bean has the property key.
      static boolean implementsBeanInterface​(java.lang.Object bean)
      Test that bean implements BeanInterface.
      static void setIndexedProperty​(java.lang.Object bean, java.lang.String key, int index, java.lang.Object value)
      Set element index of property key of bean to value.
      static void setIntrospectedIndexedProperty​(java.lang.Object bean, java.lang.String key, int index, java.lang.Object value)
      Set element index of property key of bean to value.
      static void setIntrospectedProperty​(java.lang.Object bean, java.lang.String key, java.lang.Object value)
      Set property key of bean to value.
      static void setProperty​(java.lang.Object bean, java.lang.String key, java.lang.Object value)
      Set property key of bean to value.
      • Methods inherited from class java.lang.Object

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

      • setIntrospectedIndexedProperty

        public static void setIntrospectedIndexedProperty​(java.lang.Object bean,
                                                          java.lang.String key,
                                                          int index,
                                                          java.lang.Object value)
        Set element index of property key of bean to value.

        This method relies on the standard JavaBeans coding patterns to get and invoke the setter for the property. Note that if key is not a String, this method will not attempt to set the property (JavaBeans introspection rules require that key be a String, while other JMRI coding patterns accept that key can be an Object). Note also that the setter must be public. This should only be called from outside this class in an implementation of BeanInterface.setIndexedProperty(java.lang.String, int, java.lang.Object), but is public so it can be accessed by any potential implementation of that method.

        Parameters:
        bean - The bean to update.
        key - The indexed property to set.
        index - The element to use.
        value - The value to set.
      • getIndexedProperty

        public static java.lang.Object getIndexedProperty​(java.lang.Object bean,
                                                          java.lang.String key,
                                                          int index)
        Get the item at index index of property key of bean. If the index index of property key does not exist, this method returns null instead of throwing ArrayIndexOutOfBoundsException do to the inability to get the size of the indexed property using introspection.
        Parameters:
        bean - The bean to inspect.
        key - The indexed property to get.
        index - The element to return.
        Returns:
        the value at index or null
      • getIntrospectedIndexedProperty

        public static java.lang.Object getIntrospectedIndexedProperty​(java.lang.Object bean,
                                                                      java.lang.String key,
                                                                      int index)
        Get the item at index index of property key of bean. This should only be called from outside this class in an implementation of BeanInterface.setProperty(java.lang.String, java.lang.Object), but is public so it can be accessed by any potential implementation of that method.
        Parameters:
        bean - The bean to inspect.
        key - The indexed property to get.
        index - The element to return.
        Returns:
        the value at index or null
      • setIntrospectedProperty

        public static void setIntrospectedProperty​(java.lang.Object bean,
                                                   java.lang.String key,
                                                   java.lang.Object value)
        Set property key of bean to value.

        This method relies on the standard JavaBeans coding patterns to get and invoke the property's write method. Note that if key is not a String, this method will not attempt to set the property (JavaBeans introspection rules require that key be a String, while other JMRI coding patterns accept that key can be an Object). This should only be called from outside this class in an implementation of BeanInterface.setProperty(java.lang.String, java.lang.Object), but is public so it can be accessed by any potential implementation of that method.

        Parameters:
        bean - The bean to update.
        key - The property to set.
        value - The value to set.
      • getIntrospectedProperty

        public static java.lang.Object getIntrospectedProperty​(java.lang.Object bean,
                                                               java.lang.String key)
        Get the property key of bean.

        If the property key cannot be found, this method returns null.

        This method relies on the standard JavaBeans coding patterns to get and invoke the property's read method. Note that if key is not a String, this method will not attempt to get the property (JavaBeans introspection rules require that key be a String, while other JMRI coding patterns accept that key can be an Object). This should only be called from outside this class in an implementation of BeanInterface.getProperty(java.lang.String), but is public so it can be accessed by any potential implementation of that method.

        Parameters:
        bean - The bean to inspect.
        key - The property to get.
        Returns:
        value of property key or null
      • hasIntrospectedProperty

        public static boolean hasIntrospectedProperty​(java.lang.Object bean,
                                                      java.lang.String key)
        Test that bean has the property key.

        This method relies on the standard JavaBeans coding patterns to find the property. Note that if key is not a String, this method will not attempt to find the property (JavaBeans introspection rules require that key be a String, while other JMRI coding patterns accept that key can be an Object). This should only be called from outside this class in an implementation of BeanInterface.hasProperty(java.lang.String), but is public so it can be accessed by any potential implementation of that method.

        Parameters:
        bean - The bean to inspect.
        key - The property to check for.
        Returns:
        true if bean has property key
      • hasIntrospectedIndexedProperty

        public static boolean hasIntrospectedIndexedProperty​(java.lang.Object bean,
                                                             java.lang.String key)
        Test that bean has the indexed property key.

        This method relies on the standard JavaBeans coding patterns to find the property. Note that if key is not a String, this method will not attempt to find the property (JavaBeans introspection rules require that key be a String, while other JMRI coding patterns accept that key can be an Object). This should only be called from outside this class in an implementation of BeanInterface.hasIndexedProperty(java.lang.String), but is public so it can be accessed by any potential implementation of that method.

        Parameters:
        bean - The bean to inspect.
        key - The indexed property to check for.
        Returns:
        true if bean has indexed property key
      • getPropertyNames

        public static java.util.Set<java.lang.String> getPropertyNames​(java.lang.Object bean)
      • getIntrospectedPropertyNames

        public static java.util.Set<java.lang.String> getIntrospectedPropertyNames​(java.lang.Object bean)
        Use an Introspector to get a set of the named properties of the bean. Note that properties discovered through this mechanism must have public accessors per the JavaBeans specification. This should only be called from outside this class in an implementation of BeanInterface.getPropertyNames(), but is public so it can be accessed by any potential implementation of that method.
        Parameters:
        bean - The bean to inspect.
        Returns:
        Set of property names
      • implementsBeanInterface

        public static boolean implementsBeanInterface​(java.lang.Object bean)
        Test that bean implements BeanInterface.
        Parameters:
        bean - The bean to inspect.
        Returns:
        true if bean implements BeanInterface.
      • contains

        public static boolean contains​(java.beans.PropertyChangeListener[] listeners,
                                       @Nonnull
                                       java.beans.PropertyChangeListener needle)
        Test that listeners contains needle even if listener is contained within a PropertyChangeListenerProxy.

        This is intended to be used where action needs to be taken (or not taken) if needle is (or is not) listening for property changes. Note that if a listener was registered to listen for changes in a single property, it is wrapped by a PropertyChangeListenerProxy such that using Arrays.toList(getPropertyChangeListeners()).contains(needle) may return false when needle is listening to a specific property.

        Parameters:
        listeners - the array of listeners to search through
        needle - the listener to search for
        Returns:
        true if needle is in listeners; false otherwise