Class BeanMap
- java.lang.Object
-
- java.util.AbstractMap<java.lang.Object,java.lang.Object>
-
- org.apache.commons.beanutils.BeanMap
-
- All Implemented Interfaces:
java.lang.Cloneable,java.util.Map<java.lang.Object,java.lang.Object>
public class BeanMap extends java.util.AbstractMap<java.lang.Object,java.lang.Object> implements java.lang.CloneableAn implementation of Map for JavaBeans which uses introspection to get and put properties in the bean.If an exception occurs during attempts to get or set a property then the property is considered non existent in the Map
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classBeanMap.EntryMap entry used byBeanMap.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Objectbeanstatic java.util.HashMapdefaultTransformersDeprecated.UsegetTypeTransformer(Class)methodstatic java.lang.Object[]NULL_ARGUMENTSAn empty array.private java.util.HashMap<java.lang.String,java.lang.reflect.Method>readMethodsprivate java.util.HashMap<java.lang.String,java.lang.Class<? extends java.lang.Object>>typesprivate static java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer>typeTransformersMaps primitive Class types to transformers.private java.util.HashMap<java.lang.String,java.lang.reflect.Method>writeMethods
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()This method reinitializes the bean map to have default values for the bean's properties.java.lang.Objectclone()Clone this bean map using the following process: If there is no underlying bean, return a cloned BeanMap without a bean. Since there is an underlying bean, try to instantiate a new bean of the same type using Class.newInstance(). If the instantiation fails, throw a CloneNotSupportedException Clone the bean map and set the newly instantiated bean as the underlying bean for the bean map. Copy each property that is both readable and writable from the existing object to a cloned bean map. If anything fails along the way, throw a CloneNotSupportedException.booleancontainsKey(java.lang.Object name)Returns true if the bean defines a property with the given name.booleancontainsValue(java.lang.Object value)Returns true if the bean defines a property whose current value is the given object.protected java.lang.ObjectconvertType(java.lang.Class<?> newType, java.lang.Object value)Converts the given value to the given type.private static java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer>createTypeTransformers()protected java.lang.Object[]createWriteMethodArguments(java.lang.reflect.Method method, java.lang.Object value)Creates an array of parameters to pass to the given mutator method.java.util.Iterator<java.util.Map.Entry<java.lang.Object,java.lang.Object>>entryIterator()Convenience method for getting an iterator over the entries.java.util.Set<java.util.Map.Entry<java.lang.Object,java.lang.Object>>entrySet()Gets a Set of MapEntry objects that are the mappings for this BeanMap.protected voidfirePropertyChange(java.lang.Object key, java.lang.Object oldValue, java.lang.Object newValue)Called during a successfulput(Object,Object)operation.java.lang.Objectget(java.lang.Object name)Returns the value of the bean's property with the given name.java.lang.ObjectgetBean()Returns the bean currently being operated on.protected java.lang.reflect.MethodgetReadMethod(java.lang.Object name)Returns the accessor for the property with the given name.java.lang.reflect.MethodgetReadMethod(java.lang.String name)Returns the accessor for the property with the given name.java.lang.Class<?>getType(java.lang.String name)Returns the type of the property with the given name.protected org.apache.commons.collections.TransformergetTypeTransformer(java.lang.Class<?> aType)Returns a transformer for the given primitive type.protected java.lang.reflect.MethodgetWriteMethod(java.lang.Object name)Returns the mutator for the property with the given name.java.lang.reflect.MethodgetWriteMethod(java.lang.String name)Returns the mutator for the property with the given name.private voidinitialise()java.util.Iterator<java.lang.String>keyIterator()Convenience method for getting an iterator over the keys.java.util.Set<java.lang.Object>keySet()Get the keys for this BeanMap.protected voidlogInfo(java.lang.Exception ex)Logs the given exception toSystem.out.protected voidlogWarn(java.lang.Exception ex)Logs the given exception toSystem.err.java.lang.Objectput(java.lang.Object name, java.lang.Object value)Sets the bean property with the given name to the given value.voidputAllWriteable(BeanMap map)Puts all of the writable properties from the given BeanMap into this BeanMap.protected voidreinitialise()Reinitializes this bean.voidsetBean(java.lang.Object newBean)Sets the bean to be operated on by this map.intsize()Returns the number of properties defined by the bean.java.lang.StringtoString()Renders a string representation of this object.java.util.Iterator<java.lang.Object>valueIterator()Convenience method for getting an iterator over the values.java.util.Collection<java.lang.Object>values()Returns the values for the BeanMap.
-
-
-
Field Detail
-
NULL_ARGUMENTS
public static final java.lang.Object[] NULL_ARGUMENTS
An empty array. Used to invoke accessors via reflection.
-
typeTransformers
private static final java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer> typeTransformers
Maps primitive Class types to transformers. The transformer transform strings into the appropriate primitive wrapper. Private & unmodifiable replacement for the (public & static) defaultTransformers instance.
-
defaultTransformers
@Deprecated public static java.util.HashMap defaultTransformers
Deprecated.UsegetTypeTransformer(Class)methodThis HashMap has been made unmodifiable to prevent issues when loaded in a shared ClassLoader enviroment.- See Also:
- "http://issues.apache.org/jira/browse/BEANUTILS-112"
-
bean
private transient java.lang.Object bean
-
readMethods
private transient java.util.HashMap<java.lang.String,java.lang.reflect.Method> readMethods
-
writeMethods
private transient java.util.HashMap<java.lang.String,java.lang.reflect.Method> writeMethods
-
types
private transient java.util.HashMap<java.lang.String,java.lang.Class<? extends java.lang.Object>> types
-
-
Method Detail
-
createTypeTransformers
private static java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer> createTypeTransformers()
-
clear
public void clear()
This method reinitializes the bean map to have default values for the bean's properties. This is accomplished by constructing a new instance of the bean which the map uses as its underlying data source. This behavior forclear()differs from the Map contract in that the mappings are not actually removed from the map (the mappings for a BeanMap are fixed).- Specified by:
clearin interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
clearin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionClone this bean map using the following process:- If there is no underlying bean, return a cloned BeanMap without a bean.
- Since there is an underlying bean, try to instantiate a new bean of the same type using Class.newInstance().
- If the instantiation fails, throw a CloneNotSupportedException
- Clone the bean map and set the newly instantiated bean as the underlying bean for the bean map.
- Copy each property that is both readable and writable from the existing object to a cloned bean map.
- If anything fails along the way, throw a CloneNotSupportedException.
- Overrides:
clonein classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Returns:
- a cloned instance of this bean map
- Throws:
java.lang.CloneNotSupportedException- if the underlying bean cannot be cloned
-
containsKey
public boolean containsKey(java.lang.Object name)
Returns true if the bean defines a property with the given name.The given name must be a
String; if not, this method returns false. This method will also return false if the bean does not define a property with that name.Write-only properties will not be matched as the test operates against property read methods.
- Specified by:
containsKeyin interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
containsKeyin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Parameters:
name- the name of the property to check- Returns:
- false if the given name is null or is not a
String; false if the bean does not define a property with that name; or true if the bean does define a property with that name
-
containsValue
public boolean containsValue(java.lang.Object value)
Returns true if the bean defines a property whose current value is the given object.- Specified by:
containsValuein interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
containsValuein classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Parameters:
value- the value to check- Returns:
- false true if the bean has at least one property whose current value is that object, false otherwise
-
convertType
protected java.lang.Object convertType(java.lang.Class<?> newType, java.lang.Object value) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetExceptionConverts the given value to the given type. First, reflection is is used to find a public constructor declared by the given class that takes one argument, which must be the precise type of the given value. If such a constructor is found, a new object is created by passing the given value to that constructor, and the newly constructed object is returned.If no such constructor exists, and the given type is a primitive type, then the given value is converted to a string using its
toString()method, and that string is parsed into the correct primitive type using, for instance,Integer.valueOf(String)to convert the string into anint.If no special constructor exists and the given type is not a primitive type, this method returns the original value.
- Parameters:
newType- the type to convert the value tovalue- the value to convert- Returns:
- the converted value
- Throws:
java.lang.NumberFormatException- if newType is a primitive type, and the string representation of the given value cannot be converted to that typejava.lang.InstantiationException- if the constructor found with reflection raises itjava.lang.reflect.InvocationTargetException- if the constructor found with reflection raises itjava.lang.IllegalAccessException- neverjava.lang.IllegalArgumentException- never
-
createWriteMethodArguments
protected java.lang.Object[] createWriteMethodArguments(java.lang.reflect.Method method, java.lang.Object value) throws java.lang.IllegalAccessException, java.lang.ClassCastExceptionCreates an array of parameters to pass to the given mutator method. If the given object is not the right type to pass to the method directly, it will be converted usingconvertType(Class,Object).- Parameters:
method- the mutator methodvalue- the value to pass to the mutator method- Returns:
- an array containing one object that is either the given value or a transformed value
- Throws:
java.lang.IllegalAccessException- ifconvertType(Class,Object)raises itjava.lang.IllegalArgumentException- if any other exception is raised byconvertType(Class,Object)java.lang.ClassCastException- if an error occurs creating the method args
-
entryIterator
public java.util.Iterator<java.util.Map.Entry<java.lang.Object,java.lang.Object>> entryIterator()
Convenience method for getting an iterator over the entries.- Returns:
- an iterator over the entries
-
entrySet
public java.util.Set<java.util.Map.Entry<java.lang.Object,java.lang.Object>> entrySet()
Gets a Set of MapEntry objects that are the mappings for this BeanMap.Each MapEntry can be set but not removed.
- Specified by:
entrySetin interfacejava.util.Map<java.lang.Object,java.lang.Object>- Specified by:
entrySetin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Returns:
- the unmodifiable set of mappings
-
firePropertyChange
protected void firePropertyChange(java.lang.Object key, java.lang.Object oldValue, java.lang.Object newValue)Called during a successfulput(Object,Object)operation. Default implementation does nothing. Override to be notified of property changes in the bean caused by this map.- Parameters:
key- the name of the property that changedoldValue- the old value for that propertynewValue- the new value for that property
-
get
public java.lang.Object get(java.lang.Object name)
Returns the value of the bean's property with the given name.The given name must be a
Stringand must not be null; otherwise, this method returnsnull. If the bean defines a property with the given name, the value of that property is returned. Otherwise,nullis returned.Write-only properties will not be matched as the test operates against property read methods.
- Specified by:
getin interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
getin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Parameters:
name- the name of the property whose value to return- Returns:
- the value of the property with that name
-
getBean
public java.lang.Object getBean()
Returns the bean currently being operated on. The return value may be null if this map is empty.- Returns:
- the bean being operated on by this map
-
getReadMethod
protected java.lang.reflect.Method getReadMethod(java.lang.Object name)
Returns the accessor for the property with the given name.- Parameters:
name- the name of the property- Returns:
- null if the name is null; null if the name is not a
String; null if no such property exists; or the accessor method for that property
-
getReadMethod
public java.lang.reflect.Method getReadMethod(java.lang.String name)
Returns the accessor for the property with the given name.- Parameters:
name- the name of the property- Returns:
- the accessor method for the property, or null
-
getType
public java.lang.Class<?> getType(java.lang.String name)
Returns the type of the property with the given name.- Parameters:
name- the name of the property- Returns:
- the type of the property, or
nullif no such property exists
-
getTypeTransformer
protected org.apache.commons.collections.Transformer getTypeTransformer(java.lang.Class<?> aType)
Returns a transformer for the given primitive type.- Parameters:
aType- the primitive type whose transformer to return- Returns:
- a transformer that will convert strings into that type, or null if the given type is not a primitive type
-
getWriteMethod
protected java.lang.reflect.Method getWriteMethod(java.lang.Object name)
Returns the mutator for the property with the given name.- Parameters:
name- the name of the- Returns:
- null if the name is null; null if the name is not a
String; null if no such property exists; null if the property is read-only; or the mutator method for that property
-
getWriteMethod
public java.lang.reflect.Method getWriteMethod(java.lang.String name)
Returns the mutator for the property with the given name.- Parameters:
name- the name of the property- Returns:
- the mutator method for the property, or null
-
initialise
private void initialise()
-
keyIterator
public java.util.Iterator<java.lang.String> keyIterator()
Convenience method for getting an iterator over the keys.Write-only properties will not be returned in the iterator.
- Returns:
- an iterator over the keys
-
keySet
public java.util.Set<java.lang.Object> keySet()
Get the keys for this BeanMap.Write-only properties are not included in the returned set of property names, although it is possible to set their value and to get their type.
- Specified by:
keySetin interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
keySetin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Returns:
- BeanMap keys. The Set returned by this method is not modifiable.
-
logInfo
protected void logInfo(java.lang.Exception ex)
Logs the given exception toSystem.out. Used to display warnings while accessing/mutating the bean.- Parameters:
ex- the exception to log
-
logWarn
protected void logWarn(java.lang.Exception ex)
Logs the given exception toSystem.err. Used to display errors while accessing/mutating the bean.- Parameters:
ex- the exception to log
-
put
public java.lang.Object put(java.lang.Object name, java.lang.Object value) throws java.lang.IllegalArgumentException, java.lang.ClassCastExceptionSets the bean property with the given name to the given value.- Specified by:
putin interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
putin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Parameters:
name- the name of the property to setvalue- the value to set that property to- Returns:
- the previous value of that property
- Throws:
java.lang.IllegalArgumentException- if the given name is null; if the given name is not aString; if the bean doesn't define a property with that name; or if the bean property with that name is read-onlyjava.lang.ClassCastException- if an error occurs creating the method args
-
putAllWriteable
public void putAllWriteable(BeanMap map)
Puts all of the writable properties from the given BeanMap into this BeanMap. Read-only and Write-only properties will be ignored.- Parameters:
map- the BeanMap whose properties to put
-
reinitialise
protected void reinitialise()
Reinitializes this bean. Called duringsetBean(Object). Does introspection to find properties.
-
setBean
public void setBean(java.lang.Object newBean)
Sets the bean to be operated on by this map. The given value may be null, in which case this map will be empty.- Parameters:
newBean- the new bean to operate on
-
size
public int size()
Returns the number of properties defined by the bean.- Specified by:
sizein interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
sizein classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Returns:
- the number of properties defined by the bean
-
toString
public java.lang.String toString()
Renders a string representation of this object.- Overrides:
toStringin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Returns:
- a
Stringrepresentation of this object
-
valueIterator
public java.util.Iterator<java.lang.Object> valueIterator()
Convenience method for getting an iterator over the values.- Returns:
- an iterator over the values
-
values
public java.util.Collection<java.lang.Object> values()
Returns the values for the BeanMap.- Specified by:
valuesin interfacejava.util.Map<java.lang.Object,java.lang.Object>- Overrides:
valuesin classjava.util.AbstractMap<java.lang.Object,java.lang.Object>- Returns:
- values for the BeanMap. The returned collection is not modifiable.
-
-