|
Commons Beanutils example source code file (LazyDynaBean.java)
The Commons Beanutils LazyDynaBean.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.beanutils; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import java.util.Date; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.io.Serializable; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <p>DynaBean which automatically adds properties to the ${myDynaBean.map.fooProperty} if the mapped property contains a value for
* the specified key, otherwise <code>false
*
* @exception IllegalArgumentException if no property name is specified
*/
public boolean contains(String name, String key) {
if (name == null) {
throw new IllegalArgumentException("No property name specified");
}
Object value = values.get(name);
if (value == null) {
return false;
}
if (value instanceof Map) {
return (((Map) value).containsKey(key));
}
return false;
}
/**
* <p>Return the value of a simple property with the specified name.
*
* <p>N.B. Returns null if there is no property
* of the specified name.</p>
*
* @param name Name of the property whose value is to be retrieved.
* @return The property's value
* @exception IllegalArgumentException if no property name is specified
*/
public Object get(String name) {
if (name == null) {
throw new IllegalArgumentException("No property name specified");
}
// Value found
Object value = values.get(name);
if (value != null) {
return value;
}
// Property doesn't exist
if (!isDynaProperty(name)) {
return null;
}
// Property doesn't exist
value = createProperty(name, dynaClass.getDynaProperty(name).getType());
if (value != null) {
set(name, value);
}
return value;
}
/**
* <p>Return the value of an indexed property with the specified name.
*
* <p>N.B. Returns null if there is no 'indexed'
* property of the specified name.</p>
*
* @param name Name of the property whose value is to be retrieved
* @param index Index of the value to be retrieved
* @return The indexed property's value
*
* @exception IllegalArgumentException if the specified property
* exists, but is not indexed
* @exception IndexOutOfBoundsException if the specified index
* is outside the range of the underlying property
*/
public Object get(String name, int index) {
// If its not a property, then create default indexed property
if (!isDynaProperty(name)) {
set(name, defaultIndexedProperty(name));
}
// Get the indexed property
Object indexedProperty = get(name);
// Check that the property is indexed
if (!dynaClass.getDynaProperty(name).isIndexed()) {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]' "
+ dynaClass.getDynaProperty(name).getName());
}
// Grow indexed property to appropriate size
indexedProperty = growIndexedProperty(name, indexedProperty, index);
// Return the indexed value
if (indexedProperty.getClass().isArray()) {
return Array.get(indexedProperty, index);
} else if (indexedProperty instanceof List) {
return ((List)indexedProperty).get(index);
} else {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]' "
+ indexedProperty.getClass().getName());
}
}
/**
* <p>Return the value of a mapped property with the specified name.
*
* <p>N.B. Returns null if there is no 'mapped'
* property of the specified name.</p>
*
* @param name Name of the property whose value is to be retrieved
* @param key Key of the value to be retrieved
* @return The mapped property's value
*
* @exception IllegalArgumentException if the specified property
* exists, but is not mapped
*/
public Object get(String name, String key) {
// If its not a property, then create default mapped property
if (!isDynaProperty(name)) {
set(name, defaultMappedProperty(name));
}
// Get the mapped property
Object mappedProperty = get(name);
// Check that the property is mapped
if (!dynaClass.getDynaProperty(name).isMapped()) {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")' "
+ dynaClass.getDynaProperty(name).getType().getName());
}
// Get the value from the Map
if (mappedProperty instanceof Map) {
return (((Map) mappedProperty).get(key));
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'"
+ mappedProperty.getClass().getName());
}
}
/**
* Return the <code>DynaClass instance that describes the set of
* properties available for this DynaBean.
*
* @return The associated DynaClass
*/
public DynaClass getDynaClass() {
return dynaClass;
}
/**
* Remove any existing value for the specified key on the
* specified mapped property.
*
* @param name Name of the property for which a value is to
* be removed
* @param key Key of the value to be removed
*
* @exception IllegalArgumentException if there is no property
* of the specified name
*/
public void remove(String name, String key) {
if (name == null) {
throw new IllegalArgumentException("No property name specified");
}
Object value = values.get(name);
if (value == null) {
return;
}
if (value instanceof Map) {
((Map) value).remove(key);
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'"
+ value.getClass().getName());
}
}
/**
* Set the value of a simple property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param value Value to which this property is to be set
*
* @exception IllegalArgumentException if this is not an existing property
* name for our DynaClass and the MutableDynaClass is restricted
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception NullPointerException if an attempt is made to set a
* primitive property to null
*/
public void set(String name, Object value) {
// If the property doesn't exist, then add it
if (!isDynaProperty(name)) {
if (dynaClass.isRestricted()) {
throw new IllegalArgumentException
("Invalid property name '" + name + "' (DynaClass is restricted)");
}
if (value == null) {
dynaClass.add(name);
} else {
dynaClass.add(name, value.getClass());
}
}
DynaProperty descriptor = dynaClass.getDynaProperty(name);
if (value == null) {
if (descriptor.getType().isPrimitive()) {
throw new NullPointerException
("Primitive value for '" + name + "'");
}
} else if (!isAssignable(descriptor.getType(), value.getClass())) {
throw new ConversionException
("Cannot assign value of type '" +
value.getClass().getName() +
"' to property '" + name + "' of type '" +
descriptor.getType().getName() + "'");
}
// Set the property's value
values.put(name, value);
}
/**
* Set the value of an indexed property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param index Index of the property to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not indexed
* @exception IndexOutOfBoundsException if the specified index
* is outside the range of the underlying property
*/
public void set(String name, int index, Object value) {
// If its not a property, then create default indexed property
if (!isDynaProperty(name)) {
set(name, defaultIndexedProperty(name));
}
// Get the indexed property
Object indexedProperty = get(name);
// Check that the property is indexed
if (!dynaClass.getDynaProperty(name).isIndexed()) {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]'"
+ dynaClass.getDynaProperty(name).getType().getName());
}
// Grow indexed property to appropriate size
indexedProperty = growIndexedProperty(name, indexedProperty, index);
// Set the value in an array
if (indexedProperty.getClass().isArray()) {
Array.set(indexedProperty, index, value);
} else if (indexedProperty instanceof List) {
((List)indexedProperty).set(index, value);
} else {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]' "
+ indexedProperty.getClass().getName());
}
}
/**
* Set the value of a mapped property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param key Key of the property to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not mapped
*/
public void set(String name, String key, Object value) {
// If the 'mapped' property doesn't exist, then add it
if (!isDynaProperty(name)) {
set(name, defaultMappedProperty(name));
}
// Get the mapped property
Object mappedProperty = get(name);
// Check that the property is mapped
if (!dynaClass.getDynaProperty(name).isMapped()) {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'"
+ dynaClass.getDynaProperty(name).getType().getName());
}
// Set the value in the Map
((Map)mappedProperty).put(key, value);
}
// ------------------- protected Methods ----------------------------------
/**
* Grow the size of an indexed property
* @param name The name of the property
* @param indexedProperty The current property value
* @param index The indexed value to grow the property to (i.e. one less than
* the required size)
* @return The new property value (grown to the appropriate size)
*/
protected Object growIndexedProperty(String name, Object indexedProperty, int index) {
// Grow a List to the appropriate size
if (indexedProperty instanceof List) {
List list = (List)indexedProperty;
while (index >= list.size()) {
Class contentType = getDynaClass().getDynaProperty(name).getContentType();
Object value = null;
if (contentType != null) {
value = createProperty(name+"["+list.size()+"]", contentType);
}
list.add(value);
}
}
// Grow an Array to the appropriate size
if ((indexedProperty.getClass().isArray())) {
int length = Array.getLength(indexedProperty);
if (index >= length) {
Class componentType = indexedProperty.getClass().getComponentType();
Object newArray = Array.newInstance(componentType, (index + 1));
System.arraycopy(indexedProperty, 0, newArray, 0, length);
indexedProperty = newArray;
set(name, indexedProperty);
int newLength = Array.getLength(indexedProperty);
for (int i = length; i < newLength; i++) {
Array.set(indexedProperty, i, createProperty(name+"["+i+"]", componentType));
}
}
}
return indexedProperty;
}
/**
* Create a new Instance of a Property
* @param name The name of the property
* @param type The class of the property
* @return The new value
*/
protected Object createProperty(String name, Class type) {
if (type == null) {
return null;
}
// Create Lists, arrays or DynaBeans
if (type.isArray() || List.class.isAssignableFrom(type)) {
return createIndexedProperty(name, type);
}
if (Map.class.isAssignableFrom(type)) {
return createMappedProperty(name, type);
}
if (DynaBean.class.isAssignableFrom(type)) {
return createDynaBeanProperty(name, type);
}
if (type.isPrimitive()) {
return createPrimitiveProperty(name, type);
}
if (Number.class.isAssignableFrom(type)) {
return createNumberProperty(name, type);
}
return createOtherProperty(name, type);
}
/**
* Create a new Instance of an 'Indexed' Property
* @param name The name of the property
* @param type The class of the property
* @return The new value
*/
protected Object createIndexedProperty(String name, Class type) {
// Create the indexed object
Object indexedProperty = null;
if (type == null) {
indexedProperty = defaultIndexedProperty(name);
} else if (type.isArray()) {
indexedProperty = Array.newInstance(type.getComponentType(), 0);
} else if (List.class.isAssignableFrom(type)) {
if (type.isInterface()) {
indexedProperty = defaultIndexedProperty(name);
} else {
try {
indexedProperty = type.newInstance();
}
catch (Exception ex) {
throw new IllegalArgumentException
("Error instantiating indexed property of type '" +
type.getName() + "' for '" + name + "' " + ex);
}
}
} else {
throw new IllegalArgumentException
("Non-indexed property of type '" + type.getName() + "' for '" + name + "'");
}
return indexedProperty;
}
/**
* Create a new Instance of a 'Mapped' Property
* @param name The name of the property
* @param type The class of the property
* @return The new value
*/
protected Object createMappedProperty(String name, Class type) {
// Create the mapped object
Object mappedProperty = null;
if (type == null) {
mappedProperty = defaultMappedProperty(name);
} else if (type.isInterface()) {
mappedProperty = defaultMappedProperty(name);
} else if (Map.class.isAssignableFrom(type)) {
try {
mappedProperty = type.newInstance();
}
catch (Exception ex) {
throw new IllegalArgumentException
("Error instantiating mapped property of type '" +
type.getName() + "' for '" + name + "' " + ex);
}
} else {
throw new IllegalArgumentException
("Non-mapped property of type '" + type.getName() + "' for '" + name + "'");
}
return mappedProperty;
}
/**
* Create a new Instance of a 'DynaBean' Property.
* @param name The name of the property
* @param type The class of the property
* @return The new value
*/
protected Object createDynaBeanProperty(String name, Class type) {
try {
return type.newInstance();
}
catch (Exception ex) {
if (logger().isWarnEnabled()) {
logger().warn("Error instantiating DynaBean property of type '" +
type.getName() + "' for '" + name + "' " + ex);
}
return null;
}
}
/**
* Create a new Instance of a 'Primitive' Property.
* @param name The name of the property
* @param type The class of the property
* @return The new value
*/
protected Object createPrimitiveProperty(String name, Class type) {
if (type == Boolean.TYPE) {
return Boolean.FALSE;
} else if (type == Integer.TYPE) {
return Integer_ZERO;
} else if (type == Long.TYPE) {
return Long_ZERO;
} else if (type == Double.TYPE) {
return Double_ZERO;
} else if (type == Float.TYPE) {
return Float_ZERO;
} else if (type == Byte.TYPE) {
return Byte_ZERO;
} else if (type == Short.TYPE) {
return Short_ZERO;
} else if (type == Character.TYPE) {
return Character_SPACE;
} else {
return null;
}
}
/**
* Create a new Instance of a <code>java.lang.Number Property.
* @param name The name of the property
* @param type The class of the property
* @return The new value
*/
protected Object createNumberProperty(String name, Class type) {
return null;
}
/**
* Create a new Instance of other Property types
* @param name The name of the property
* @param type The class of the property
* @return The new value
*/
protected Object createOtherProperty(String name, Class type) {
if (type == Object.class ||
type == String.class ||
type == Boolean.class ||
type == Character.class ||
Date.class.isAssignableFrom(type)) {
return null;
}
try {
return type.newInstance();
}
catch (Exception ex) {
if (logger().isWarnEnabled()) {
logger().warn("Error instantiating property of type '" + type.getName() + "' for '" + name + "' " + ex);
}
return null;
}
}
/**
* <p>Creates a new ArrayList for an 'indexed' property
* which doesn't exist.</p>
*
* <p>This method shouls be overriden if an alternative List
* or <code>Array implementation is required for 'indexed' properties.
*
* @param name Name of the 'indexed property.
* @return The default value for an indexed property (java.util.ArrayList)
*/
protected Object defaultIndexedProperty(String name) {
return new ArrayList();
}
/**
* <p>Creates a new HashMap for a 'mapped' property
* which doesn't exist.</p>
*
* <p>This method can be overriden if an alternative Map
* implementation is required for 'mapped' properties.</p>
*
* @param name Name of the 'mapped property.
* @return The default value for a mapped property (java.util.HashMap)
*/
protected Map defaultMappedProperty(String name) {
return new HashMap();
}
/**
* Indicates if there is a property with the specified name.
* @param name The name of the property to check
* @return <code>true if there is a property of the
* specified name, otherwise <code>false
*/
protected boolean isDynaProperty(String name) {
if (name == null) {
throw new IllegalArgumentException("No property name specified");
}
// Handle LazyDynaClasses
if (dynaClass instanceof LazyDynaClass) {
return ((LazyDynaClass)dynaClass).isDynaProperty(name);
}
// Handle other MutableDynaClass
return dynaClass.getDynaProperty(name) == null ? false : true;
}
/**
* Is an object of the source class assignable to the destination class?
*
* @param dest Destination class
* @param source Source class
* @return <code>true if the source class is assignable to the
* destination class, otherwise <code>false
*/
protected boolean isAssignable(Class dest, Class source) {
if (dest.isAssignableFrom(source) ||
((dest == Boolean.TYPE) && (source == Boolean.class)) ||
((dest == Byte.TYPE) && (source == Byte.class)) ||
((dest == Character.TYPE) && (source == Character.class)) ||
((dest == Double.TYPE) && (source == Double.class)) ||
((dest == Float.TYPE) && (source == Float.class)) ||
((dest == Integer.TYPE) && (source == Integer.class)) ||
((dest == Long.TYPE) && (source == Long.class)) ||
((dest == Short.TYPE) && (source == Short.class))) {
return (true);
} else {
return (false);
}
}
/**
* <p>Creates a new instance of the Map .
* @return a new Map instance
*/
protected Map newMap() {
return new HashMap();
}
/**
* <p>Returns the Log .
*/
private Log logger() {
if (logger == null) {
logger = LogFactory.getLog(LazyDynaBean.class);
}
return logger;
}
}
Other Commons Beanutils examples (source code examples)Here is a short list of links related to this Commons Beanutils LazyDynaBean.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.