Class BeanUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontains(PropertyChangeListener[] listeners, PropertyChangeListener needle) Test that listeners contains needle even if listener is contained within aPropertyChangeListenerProxy.static ObjectgetIndexedProperty(Object bean, String key, int index) Get the item at index index of property key of bean.static ObjectgetIntrospectedIndexedProperty(Object bean, String key, int index) Get the item at index index of property key of bean.static ObjectgetIntrospectedProperty(Object bean, String key) Get the property key of bean.Use anIntrospectorto get a set of the named properties of the bean.static ObjectgetProperty(Object bean, String key) Get the property key of bean.getPropertyNames(Object bean) static booleanhasIndexedProperty(Object bean, String key) Test if bean has the indexed property key.static booleanhasIntrospectedIndexedProperty(Object bean, String key) Test that bean has the indexed property key.static booleanhasIntrospectedProperty(Object bean, String key) Test that bean has the property key.static booleanhasProperty(Object bean, String key) Test if bean has the property key.static booleanTest that bean implementsBeanInterface.static voidsetIndexedProperty(Object bean, String key, int index, Object value) Set element index of property key of bean to value.static voidsetIntrospectedIndexedProperty(Object bean, String key, int index, Object value) Set element index of property key of bean to value.static voidsetIntrospectedProperty(Object bean, String key, Object value) Set property key of bean to value.static voidsetProperty(Object bean, String key, Object value) Set property key of bean to value.
-
Method Details
-
setIndexedProperty
Set element index of property key of bean to value.If bean implements
BeanInterface, this method callsBeanInterface.setIndexedProperty(java.lang.String, int, java.lang.Object)otherwise it callssetIntrospectedIndexedProperty(java.lang.Object, java.lang.String, int, java.lang.Object)- Parameters:
bean- The bean to update.key- The indexed property to set.index- The element to use.value- The value to set.- See Also:
-
setIntrospectedIndexedProperty
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 ofBeanInterface.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
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 throwingArrayIndexOutOfBoundsExceptiondo 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
Get the item at index index of property key of bean. This should only be called from outside this class in an implementation ofBeanInterface.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
-
setProperty
Set property key of bean to value.If bean implements
BeanInterface, this method callsBeanInterface.setProperty(java.lang.String, java.lang.Object), otherwise it callssetIntrospectedProperty(java.lang.Object, java.lang.String, java.lang.Object).- Parameters:
bean- The bean to update.key- The property to set.value- The value to set.- See Also:
-
setIntrospectedProperty
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 ofBeanInterface.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.
-
getProperty
Get the property key of bean.If the property key cannot be found, this method returns null.
If bean implements
BeanInterface, this method callsBeanInterface.getProperty(java.lang.String), otherwise it callsgetIntrospectedProperty(java.lang.Object, java.lang.String).- Parameters:
bean- The bean to inspect.key- The property to get.- Returns:
- value of property key
- See Also:
-
getIntrospectedProperty
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 ofBeanInterface.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
-
hasProperty
Test if bean has the property key.If bean implements
BeanInterface, this method callsBeanInterface.hasProperty(java.lang.String), otherwise it callshasIntrospectedProperty(java.lang.Object, java.lang.String).- Parameters:
bean- The bean to inspect.key- The property key to check for.- Returns:
- true if bean has property key
-
hasIndexedProperty
Test if bean has the indexed property key.If bean implements
BeanInterface, this method callsBeanInterface.hasIndexedProperty(java.lang.String), otherwise it callshasIntrospectedIndexedProperty(java.lang.Object, java.lang.String).- Parameters:
bean- The bean to inspect.key- The indexed property to check for.- Returns:
- true if bean has indexed property key
-
hasIntrospectedProperty
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 ofBeanInterface.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
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 ofBeanInterface.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
-
getIntrospectedPropertyNames
Use anIntrospectorto 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 ofBeanInterface.getPropertyNames(), but is public so it can be accessed by any potential implementation of that method.- Parameters:
bean- The bean to inspect.- Returns:
Setof property names
-
implementsBeanInterface
Test that bean implementsBeanInterface.- Parameters:
bean- The bean to inspect.- Returns:
- true if bean implements BeanInterface.
-
contains
public static boolean contains(PropertyChangeListener[] listeners, @Nonnull PropertyChangeListener needle) Test that listeners contains needle even if listener is contained within aPropertyChangeListenerProxy.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 throughneedle- the listener to search for- Returns:
- true if needle is in listeners; false otherwise
-