Class BeanUtil
- java.lang.Object
-
- jmri.beans.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 aPropertyChangeListenerProxy
.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 anIntrospector
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 implementsBeanInterface
.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.
-
-
-
Method Detail
-
setIndexedProperty
public 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.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:
BeanInterface.setIndexedProperty(java.lang.String, int, java.lang.Object)
-
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 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
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 throwingArrayIndexOutOfBoundsException
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 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
public static void setProperty(java.lang.Object bean, java.lang.String key, java.lang.Object value)
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:
BeanInterface.setProperty(java.lang.String, java.lang.Object)
-
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 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
public static java.lang.Object getProperty(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.
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:
BeanInterface.getProperty(java.lang.String)
-
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 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
public static boolean hasProperty(java.lang.Object bean, java.lang.String key)
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
public static boolean hasIndexedProperty(java.lang.Object bean, java.lang.String key)
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
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 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
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 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
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 anIntrospector
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 ofBeanInterface.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 implementsBeanInterface
.- 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 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
-
-