📄 lazydynabean.java
字号:
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</code> 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 <code>ArrayList</code> for an 'indexed' property
* which doesn't exist.</p>
*
* <p>This method shouls be overriden if an alternative <code>List</code>
* or <code>Array</code> implementation is required for 'indexed' properties.</p>
*
* @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 <code>HashMap</code> for a 'mapped' property
* which doesn't exist.</p>
*
* <p>This method can be overriden if an alternative <code>Map</code>
* 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<code> if there is a property of the
* specified name, otherwise <code>false</code>
*/
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<code> if the source class is assignable to the
* destination class, otherwise <code>false</code>
*/
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 <code>Map</code>.</p>
* @return a new Map instance
*/
protected Map newMap() {
return new HashMap();
}
/**
* <p>Returns the <code>Log</code>.
*/
private Log logger() {
if (logger == null) {
logger = LogFactory.getLog(LazyDynaBean.class);
}
return logger;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -