first commit

This commit is contained in:
2025-07-28 13:56:49 +05:30
commit e9eb805edb
3438 changed files with 520990 additions and 0 deletions

View File

@@ -0,0 +1,445 @@
package org.apache.commons.beanutils;
import java.beans.IndexedPropertyDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.collections.FastHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class BeanUtils {
private static FastHashMap dummy = new FastHashMap();
private static Log log = LogFactory.getLog(BeanUtils.class);
private static int debug = 0;
public static int getDebug() {
return debug;
}
public static void setDebug(int newDebug) {
debug = newDebug;
}
public static Object cloneBean(Object bean) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
if (log.isDebugEnabled())
log.debug("Cloning bean: " + bean.getClass().getName());
Class clazz = bean.getClass();
Object newBean = clazz.newInstance();
PropertyUtils.copyProperties(newBean, bean);
return newBean;
}
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
if (dest == null)
throw new IllegalArgumentException("No destination bean specified");
if (orig == null)
throw new IllegalArgumentException("No origin bean specified");
if (log.isDebugEnabled())
log.debug("BeanUtils.copyProperties(" + dest + ", " + orig + ")");
if (orig instanceof DynaBean) {
DynaProperty[] origDescriptors = ((DynaBean)orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ((DynaBean)orig).get(name);
copyProperty(dest, name, value);
}
}
} else if (orig instanceof Map) {
Iterator names = ((Map)orig).keySet().iterator();
while (names.hasNext()) {
String name = names.next();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ((Map)orig).get(name);
copyProperty(dest, name, value);
}
}
} else {
PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (!"class".equals(name))
if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name))
try {
Object value = PropertyUtils.getSimpleProperty(orig, name);
copyProperty(dest, name, value);
} catch (NoSuchMethodException e) {}
}
}
}
public static void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException {
if (log.isTraceEnabled()) {
StringBuffer sb = new StringBuffer(" copyProperty(");
sb.append(bean);
sb.append(", ");
sb.append(name);
sb.append(", ");
if (value == null) {
sb.append("<NULL>");
} else if (value instanceof String) {
sb.append((String)value);
} else if (value instanceof String[]) {
String[] values = (String[])value;
sb.append('[');
for (int k = 0; k < values.length; k++) {
if (k > 0)
sb.append(',');
sb.append(values[k]);
}
sb.append(']');
} else {
sb.append(value.toString());
}
sb.append(')');
log.trace(sb.toString());
}
Object target = bean;
int delim = name.lastIndexOf('.');
if (delim >= 0) {
try {
target = PropertyUtils.getProperty(bean, name.substring(0, delim));
} catch (NoSuchMethodException e) {
return;
}
name = name.substring(delim + 1);
if (log.isTraceEnabled()) {
log.trace(" Target bean = " + target);
log.trace(" Target name = " + name);
}
}
String propName = null;
Class type = null;
int index = -1;
String key = null;
propName = name;
int i = propName.indexOf('[');
if (i >= 0) {
int k = propName.indexOf(']');
try {
index = Integer.parseInt(propName.substring(i + 1, k));
} catch (NumberFormatException e) {}
propName = propName.substring(0, i);
}
int j = propName.indexOf('(');
if (j >= 0) {
int k = propName.indexOf(')');
try {
key = propName.substring(j + 1, k);
} catch (IndexOutOfBoundsException e) {}
propName = propName.substring(0, j);
}
if (target instanceof DynaBean) {
DynaClass dynaClass = ((DynaBean)target).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
if (dynaProperty == null)
return;
type = dynaProperty.getType();
} else {
PropertyDescriptor descriptor = null;
try {
descriptor = PropertyUtils.getPropertyDescriptor(target, name);
if (descriptor == null)
return;
} catch (NoSuchMethodException e) {
return;
}
type = descriptor.getPropertyType();
if (type == null) {
if (log.isTraceEnabled())
log.trace(" target type for property '" + propName + "' is null, so skipping ths setter");
return;
}
}
if (log.isTraceEnabled())
log.trace(" target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key);
if (index >= 0) {
Converter converter = ConvertUtils.lookup(type.getComponentType());
if (converter != null) {
log.trace(" USING CONVERTER " + converter);
value = converter.convert(type, value);
}
try {
PropertyUtils.setIndexedProperty(target, propName, index, value);
} catch (NoSuchMethodException e) {
throw new InvocationTargetException(e, "Cannot set " + propName);
}
} else if (key != null) {
try {
PropertyUtils.setMappedProperty(target, propName, key, value);
} catch (NoSuchMethodException e) {
throw new InvocationTargetException(e, "Cannot set " + propName);
}
} else {
Converter converter = ConvertUtils.lookup(type);
if (converter != null) {
log.trace(" USING CONVERTER " + converter);
value = converter.convert(type, value);
}
try {
PropertyUtils.setSimpleProperty(target, propName, value);
} catch (NoSuchMethodException e) {
throw new InvocationTargetException(e, "Cannot set " + propName);
}
}
}
public static Map describe(Object bean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
return new HashMap();
if (log.isDebugEnabled())
log.debug("Describing bean: " + bean.getClass().getName());
Map description = new HashMap();
if (bean instanceof DynaBean) {
DynaProperty[] descriptors = ((DynaBean)bean).getDynaClass().getDynaProperties();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
description.put(name, getProperty(bean, name));
}
} else {
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean);
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (descriptors[i].getReadMethod() != null)
description.put(name, getProperty(bean, name));
}
}
return description;
}
public static String[] getArrayProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object value = PropertyUtils.getProperty(bean, name);
if (value == null)
return null;
if (value instanceof Collection) {
ArrayList values = new ArrayList();
Iterator items = ((Collection)value).iterator();
while (items.hasNext()) {
Object item = items.next();
if (item == null) {
values.add((String)null);
continue;
}
values.add(item.toString());
}
return values.<String>toArray(new String[values.size()]);
}
if (value.getClass().isArray()) {
int n = Array.getLength(value);
String[] arrayOfString = new String[n];
for (int i = 0; i < n; i++) {
Object item = Array.get(value, i);
if (item == null) {
arrayOfString[i] = null;
} else {
arrayOfString[i] = item.toString();
}
}
return arrayOfString;
}
String[] results = new String[1];
results[0] = value.toString();
return results;
}
public static String getIndexedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object value = PropertyUtils.getIndexedProperty(bean, name);
return ConvertUtils.convert(value);
}
public static String getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object value = PropertyUtils.getIndexedProperty(bean, name, index);
return ConvertUtils.convert(value);
}
public static String getMappedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object value = PropertyUtils.getMappedProperty(bean, name);
return ConvertUtils.convert(value);
}
public static String getMappedProperty(Object bean, String name, String key) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object value = PropertyUtils.getMappedProperty(bean, name, key);
return ConvertUtils.convert(value);
}
public static String getNestedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object value = PropertyUtils.getNestedProperty(bean, name);
return ConvertUtils.convert(value);
}
public static String getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
return getNestedProperty(bean, name);
}
public static String getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object value = PropertyUtils.getSimpleProperty(bean, name);
return ConvertUtils.convert(value);
}
public static void populate(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException {
if (bean == null || properties == null)
return;
if (log.isDebugEnabled())
log.debug("BeanUtils.populate(" + bean + ", " + properties + ")");
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = names.next();
if (name == null)
continue;
Object value = properties.get(name);
setProperty(bean, name, value);
}
}
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException {
if (log.isTraceEnabled()) {
StringBuffer sb = new StringBuffer(" setProperty(");
sb.append(bean);
sb.append(", ");
sb.append(name);
sb.append(", ");
if (value == null) {
sb.append("<NULL>");
} else if (value instanceof String) {
sb.append((String)value);
} else if (value instanceof String[]) {
String[] values = (String[])value;
sb.append('[');
for (int k = 0; k < values.length; k++) {
if (k > 0)
sb.append(',');
sb.append(values[k]);
}
sb.append(']');
} else {
sb.append(value.toString());
}
sb.append(')');
log.trace(sb.toString());
}
Object target = bean;
int delim = name.lastIndexOf('.');
if (delim >= 0) {
try {
target = PropertyUtils.getProperty(bean, name.substring(0, delim));
} catch (NoSuchMethodException e) {
return;
}
name = name.substring(delim + 1);
if (log.isTraceEnabled()) {
log.trace(" Target bean = " + target);
log.trace(" Target name = " + name);
}
}
String propName = null;
Class type = null;
int index = -1;
String key = null;
propName = name;
int i = propName.indexOf('[');
if (i >= 0) {
int k = propName.indexOf(']');
try {
index = Integer.parseInt(propName.substring(i + 1, k));
} catch (NumberFormatException e) {}
propName = propName.substring(0, i);
}
int j = propName.indexOf('(');
if (j >= 0) {
int k = propName.indexOf(')');
try {
key = propName.substring(j + 1, k);
} catch (IndexOutOfBoundsException e) {}
propName = propName.substring(0, j);
}
if (target instanceof DynaBean) {
DynaClass dynaClass = ((DynaBean)target).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
if (dynaProperty == null)
return;
type = dynaProperty.getType();
} else {
PropertyDescriptor descriptor = null;
try {
descriptor = PropertyUtils.getPropertyDescriptor(target, name);
if (descriptor == null)
return;
} catch (NoSuchMethodException e) {
return;
}
if (descriptor instanceof MappedPropertyDescriptor) {
if (((MappedPropertyDescriptor)descriptor).getMappedWriteMethod() == null) {
if (log.isDebugEnabled())
log.debug("Skipping read-only property");
return;
}
type = ((MappedPropertyDescriptor)descriptor).getMappedPropertyType();
} else if (descriptor instanceof IndexedPropertyDescriptor) {
if (((IndexedPropertyDescriptor)descriptor).getIndexedWriteMethod() == null) {
if (log.isDebugEnabled())
log.debug("Skipping read-only property");
return;
}
type = ((IndexedPropertyDescriptor)descriptor).getIndexedPropertyType();
} else {
if (descriptor.getWriteMethod() == null) {
if (log.isDebugEnabled())
log.debug("Skipping read-only property");
return;
}
type = descriptor.getPropertyType();
}
}
Object newValue = null;
if (type.isArray() && index < 0) {
if (value == null) {
String[] values = new String[1];
values[0] = (String)value;
newValue = ConvertUtils.convert(values, type);
} else if (value instanceof String) {
String[] values = new String[1];
values[0] = (String)value;
newValue = ConvertUtils.convert(values, type);
} else if (value instanceof String[]) {
newValue = ConvertUtils.convert((String[])value, type);
} else {
newValue = value;
}
} else if (type.isArray()) {
if (value instanceof String) {
newValue = ConvertUtils.convert((String)value, type.getComponentType());
} else if (value instanceof String[]) {
newValue = ConvertUtils.convert(((String[])value)[0], type.getComponentType());
} else {
newValue = value;
}
} else if (value instanceof String || value == null) {
newValue = ConvertUtils.convert((String)value, type);
} else if (value instanceof String[]) {
newValue = ConvertUtils.convert(((String[])value)[0], type);
} else if (ConvertUtils.lookup(value.getClass()) != null) {
newValue = ConvertUtils.convert(value.toString(), type);
} else {
newValue = value;
}
try {
if (index >= 0) {
PropertyUtils.setIndexedProperty(target, propName, index, newValue);
} else if (key != null) {
PropertyUtils.setMappedProperty(target, propName, key, newValue);
} else {
PropertyUtils.setProperty(target, propName, newValue);
}
} catch (NoSuchMethodException e) {
throw new InvocationTargetException(e, "Cannot set " + propName);
}
}
}

View File

@@ -0,0 +1,26 @@
package org.apache.commons.beanutils;
public class ConversionException extends RuntimeException {
protected Throwable cause;
public ConversionException(String message) {
super(message);
this.cause = null;
}
public ConversionException(String message, Throwable cause) {
super(message);
this.cause = null;
this.cause = cause;
}
public ConversionException(Throwable cause) {
super(cause.getMessage());
this.cause = null;
this.cause = cause;
}
public Throwable getCause() {
return this.cause;
}
}

View File

@@ -0,0 +1,244 @@
package org.apache.commons.beanutils;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanArrayConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteArrayConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterArrayConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.ClassConverter;
import org.apache.commons.beanutils.converters.DoubleArrayConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatArrayConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerArrayConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongArrayConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortArrayConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimeConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.apache.commons.beanutils.converters.StringArrayConverter;
import org.apache.commons.beanutils.converters.StringConverter;
import org.apache.commons.collections.FastHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ConvertUtils {
private static Boolean defaultBoolean = Boolean.FALSE;
public static boolean getDefaultBoolean() {
return defaultBoolean.booleanValue();
}
public static void setDefaultBoolean(boolean newDefaultBoolean) {
defaultBoolean = new Boolean(newDefaultBoolean);
converters.put(boolean.class, new BooleanConverter(defaultBoolean));
converters.put(Boolean.class, new BooleanConverter(defaultBoolean));
}
private static Byte defaultByte = new Byte((byte)0);
public static byte getDefaultByte() {
return defaultByte.byteValue();
}
public static void setDefaultByte(byte newDefaultByte) {
defaultByte = new Byte(newDefaultByte);
converters.put(byte.class, new ByteConverter(defaultByte));
converters.put(Byte.class, new ByteConverter(defaultByte));
}
private static Character defaultCharacter = new Character(' ');
public static char getDefaultCharacter() {
return defaultCharacter.charValue();
}
public static void setDefaultCharacter(char newDefaultCharacter) {
defaultCharacter = new Character(newDefaultCharacter);
converters.put(char.class, new CharacterConverter(defaultCharacter));
converters.put(Character.class, new CharacterConverter(defaultCharacter));
}
private static Double defaultDouble = new Double(0.0D);
public static double getDefaultDouble() {
return defaultDouble.doubleValue();
}
public static void setDefaultDouble(double newDefaultDouble) {
defaultDouble = new Double(newDefaultDouble);
converters.put(double.class, new DoubleConverter(defaultDouble));
converters.put(Double.class, new DoubleConverter(defaultDouble));
}
private static Float defaultFloat = new Float(0.0F);
public static float getDefaultFloat() {
return defaultFloat.floatValue();
}
public static void setDefaultFloat(float newDefaultFloat) {
defaultFloat = new Float(newDefaultFloat);
converters.put(float.class, new FloatConverter(defaultFloat));
converters.put(Float.class, new FloatConverter(defaultFloat));
}
private static Integer defaultInteger = new Integer(0);
public static int getDefaultInteger() {
return defaultInteger.intValue();
}
public static void setDefaultInteger(int newDefaultInteger) {
defaultInteger = new Integer(newDefaultInteger);
converters.put(int.class, new IntegerConverter(defaultInteger));
converters.put(Integer.class, new IntegerConverter(defaultInteger));
}
private static Long defaultLong = new Long(0L);
public static long getDefaultLong() {
return defaultLong.longValue();
}
public static void setDefaultLong(long newDefaultLong) {
defaultLong = new Long(newDefaultLong);
converters.put(long.class, new LongConverter(defaultLong));
converters.put(Long.class, new LongConverter(defaultLong));
}
private static Short defaultShort = new Short((short)0);
public static short getDefaultShort() {
return defaultShort.shortValue();
}
public static void setDefaultShort(short newDefaultShort) {
defaultShort = new Short(newDefaultShort);
converters.put(short.class, new ShortConverter(defaultShort));
converters.put(Short.class, new ShortConverter(defaultShort));
}
private static FastHashMap converters = new FastHashMap();
static {
converters.setFast(false);
deregister();
converters.setFast(true);
}
private static Log log = LogFactory.getLog(ConvertUtils.class);
public static String convert(Object value) {
if (value == null)
return (String)null;
if (value.getClass().isArray()) {
if (Array.getLength(value) < 1)
return null;
value = Array.get(value, 0);
if (value == null)
return (String)null;
Converter converter1 = (Converter)converters.get(String.class);
return (String)converter1.convert(String.class, value);
}
Converter converter = (Converter)converters.get(String.class);
return (String)converter.convert(String.class, value);
}
public static Object convert(String value, Class clazz) {
if (log.isDebugEnabled())
log.debug("Convert string '" + value + "' to class '" + clazz.getName() + "'");
Converter converter = (Converter)converters.get(clazz);
if (converter == null)
converter = (Converter)converters.get(String.class);
if (log.isTraceEnabled())
log.trace(" Using converter " + converter);
return converter.convert(clazz, value);
}
public static Object convert(String[] values, Class clazz) {
Class type = clazz;
if (clazz.isArray())
type = clazz.getComponentType();
if (log.isDebugEnabled())
log.debug("Convert String[" + values.length + "] to class '" + type.getName() + "[]'");
Converter converter = (Converter)converters.get(type);
if (converter == null)
converter = (Converter)converters.get(String.class);
if (log.isTraceEnabled())
log.trace(" Using converter " + converter);
Object array = Array.newInstance(type, values.length);
for (int i = 0; i < values.length; i++)
Array.set(array, i, converter.convert(type, values[i]));
return array;
}
public static void deregister() {
boolean[] booleanArray = new boolean[0];
byte[] byteArray = new byte[0];
char[] charArray = new char[0];
double[] doubleArray = new double[0];
float[] floatArray = new float[0];
int[] intArray = new int[0];
long[] longArray = new long[0];
short[] shortArray = new short[0];
String[] stringArray = new String[0];
converters.clear();
converters.put(BigDecimal.class, new BigDecimalConverter());
converters.put(BigInteger.class, new BigIntegerConverter());
converters.put(boolean.class, new BooleanConverter(defaultBoolean));
converters.put(Boolean.class, new BooleanConverter(defaultBoolean));
converters.put(booleanArray.getClass(), new BooleanArrayConverter(booleanArray));
converters.put(byte.class, new ByteConverter(defaultByte));
converters.put(Byte.class, new ByteConverter(defaultByte));
converters.put(byteArray.getClass(), new ByteArrayConverter(byteArray));
converters.put(char.class, new CharacterConverter(defaultCharacter));
converters.put(Character.class, new CharacterConverter(defaultCharacter));
converters.put(charArray.getClass(), new CharacterArrayConverter(charArray));
converters.put(Class.class, new ClassConverter());
converters.put(double.class, new DoubleConverter(defaultDouble));
converters.put(Double.class, new DoubleConverter(defaultDouble));
converters.put(doubleArray.getClass(), new DoubleArrayConverter(doubleArray));
converters.put(float.class, new FloatConverter(defaultFloat));
converters.put(Float.class, new FloatConverter(defaultFloat));
converters.put(floatArray.getClass(), new FloatArrayConverter(floatArray));
converters.put(int.class, new IntegerConverter(defaultInteger));
converters.put(Integer.class, new IntegerConverter(defaultInteger));
converters.put(intArray.getClass(), new IntegerArrayConverter(intArray));
converters.put(long.class, new LongConverter(defaultLong));
converters.put(Long.class, new LongConverter(defaultLong));
converters.put(longArray.getClass(), new LongArrayConverter(longArray));
converters.put(short.class, new ShortConverter(defaultShort));
converters.put(Short.class, new ShortConverter(defaultShort));
converters.put(shortArray.getClass(), new ShortArrayConverter(shortArray));
converters.put(String.class, new StringConverter());
converters.put(stringArray.getClass(), new StringArrayConverter(stringArray));
converters.put(Date.class, new SqlDateConverter());
converters.put(Time.class, new SqlTimeConverter());
converters.put(Timestamp.class, new SqlTimestampConverter());
}
public static void deregister(Class clazz) {
converters.remove(clazz);
}
public static Converter lookup(Class clazz) {
return (Converter)converters.get(clazz);
}
public static void register(Converter converter, Class clazz) {
converters.put(clazz, converter);
}
}

View File

@@ -0,0 +1,5 @@
package org.apache.commons.beanutils;
public interface Converter {
Object convert(Class paramClass, Object paramObject);
}

View File

@@ -0,0 +1,21 @@
package org.apache.commons.beanutils;
public interface DynaBean {
boolean contains(String paramString1, String paramString2);
Object get(String paramString);
Object get(String paramString, int paramInt);
Object get(String paramString1, String paramString2);
DynaClass getDynaClass();
void remove(String paramString1, String paramString2);
void set(String paramString, Object paramObject);
void set(String paramString, int paramInt, Object paramObject);
void set(String paramString1, String paramString2, Object paramObject);
}

View File

@@ -0,0 +1,11 @@
package org.apache.commons.beanutils;
public interface DynaClass {
String getName();
DynaProperty getDynaProperty(String paramString);
DynaProperty[] getDynaProperties();
DynaBean newInstance() throws IllegalAccessException, InstantiationException;
}

View File

@@ -0,0 +1,140 @@
package org.apache.commons.beanutils;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.List;
import java.util.Map;
public class DynaProperty implements Serializable {
private static final int BOOLEAN_TYPE = 1;
private static final int BYTE_TYPE = 2;
private static final int CHAR_TYPE = 3;
private static final int DOUBLE_TYPE = 4;
private static final int FLOAT_TYPE = 5;
private static final int INT_TYPE = 6;
private static final int LONG_TYPE = 7;
private static final int SHORT_TYPE = 8;
protected String name;
protected transient Class type;
public DynaProperty(String name) {
this(name, Object.class);
}
public DynaProperty(String name, Class type) {
this.name = null;
this.type = null;
this.name = name;
this.type = type;
}
public String getName() {
return this.name;
}
public Class getType() {
return this.type;
}
public boolean isIndexed() {
if (this.type == null)
return false;
if (this.type.isArray())
return true;
if (List.class.isAssignableFrom(this.type))
return true;
return false;
}
public boolean isMapped() {
if (this.type == null)
return false;
return Map.class.isAssignableFrom(this.type);
}
public String toString() {
StringBuffer sb = new StringBuffer("DynaProperty[name=");
sb.append(this.name);
sb.append(",type=");
sb.append(this.type);
sb.append("]");
return sb.toString();
}
private void writeObject(ObjectOutputStream out) throws IOException {
int primitiveType = 0;
if (boolean.class.equals(this.type)) {
primitiveType = 1;
} else if (byte.class.equals(this.type)) {
primitiveType = 2;
} else if (char.class.equals(this.type)) {
primitiveType = 3;
} else if (double.class.equals(this.type)) {
primitiveType = 4;
} else if (float.class.equals(this.type)) {
primitiveType = 5;
} else if (int.class.equals(this.type)) {
primitiveType = 6;
} else if (long.class.equals(this.type)) {
primitiveType = 7;
} else if (short.class.equals(this.type)) {
primitiveType = 8;
}
if (primitiveType == 0) {
out.writeBoolean(false);
out.writeObject(this.type);
} else {
out.writeBoolean(true);
out.writeInt(primitiveType);
}
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
if (in.readBoolean()) {
switch (in.readInt()) {
case 1:
this.type = boolean.class;
break;
case 2:
this.type = byte.class;
break;
case 3:
this.type = char.class;
break;
case 4:
this.type = double.class;
break;
case 5:
this.type = float.class;
break;
case 6:
this.type = int.class;
break;
case 7:
this.type = long.class;
break;
case 8:
this.type = short.class;
break;
default:
throw new StreamCorruptedException("Invalid primitive type. Check version of beanutils used to serialize is compatible.");
}
} else {
this.type = (Class)in.readObject();
}
in.defaultReadObject();
}
}

View File

@@ -0,0 +1,242 @@
package org.apache.commons.beanutils;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Hashtable;
public class MappedPropertyDescriptor extends PropertyDescriptor {
private Class mappedPropertyType;
private Method mappedReadMethod;
private Method mappedWriteMethod;
private static final Class[] stringClassArray = new Class[] { String.class };
public MappedPropertyDescriptor(String propertyName, Class beanClass) throws IntrospectionException {
super(propertyName, (Method)null, (Method)null);
if (propertyName == null || propertyName.length() == 0)
throw new IntrospectionException("bad property name: " + propertyName + " on class: " + beanClass.getClass().getName());
setName(propertyName);
String base = capitalizePropertyName(propertyName);
try {
this.mappedReadMethod = findMethod(beanClass, "get" + base, 1, stringClassArray);
Class[] params = { String.class, this.mappedReadMethod.getReturnType() };
this.mappedWriteMethod = findMethod(beanClass, "set" + base, 2, params);
} catch (IntrospectionException e) {}
if (this.mappedReadMethod == null)
this.mappedWriteMethod = findMethod(beanClass, "set" + base, 2);
if (this.mappedReadMethod == null && this.mappedWriteMethod == null)
throw new IntrospectionException("Property '" + propertyName + "' not found on " + beanClass.getName());
findMappedPropertyType();
}
public MappedPropertyDescriptor(String propertyName, Class beanClass, String mappedGetterName, String mappedSetterName) throws IntrospectionException {
super(propertyName, (Method)null, (Method)null);
if (propertyName == null || propertyName.length() == 0)
throw new IntrospectionException("bad property name: " + propertyName);
setName(propertyName);
this.mappedReadMethod = findMethod(beanClass, mappedGetterName, 1, stringClassArray);
if (this.mappedReadMethod != null) {
Class[] params = { String.class, this.mappedReadMethod.getReturnType() };
this.mappedWriteMethod = findMethod(beanClass, mappedSetterName, 2, params);
} else {
this.mappedWriteMethod = findMethod(beanClass, mappedSetterName, 2);
}
findMappedPropertyType();
}
public MappedPropertyDescriptor(String propertyName, Method mappedGetter, Method mappedSetter) throws IntrospectionException {
super(propertyName, mappedGetter, mappedSetter);
if (propertyName == null || propertyName.length() == 0)
throw new IntrospectionException("bad property name: " + propertyName);
setName(propertyName);
this.mappedReadMethod = mappedGetter;
this.mappedWriteMethod = mappedSetter;
findMappedPropertyType();
}
public Class getMappedPropertyType() {
return this.mappedPropertyType;
}
public Method getMappedReadMethod() {
return this.mappedReadMethod;
}
public void setMappedReadMethod(Method mappedGetter) throws IntrospectionException {
this.mappedReadMethod = mappedGetter;
findMappedPropertyType();
}
public Method getMappedWriteMethod() {
return this.mappedWriteMethod;
}
public void setMappedWriteMethod(Method mappedSetter) throws IntrospectionException {
this.mappedWriteMethod = mappedSetter;
findMappedPropertyType();
}
private void findMappedPropertyType() throws IntrospectionException {
try {
this.mappedPropertyType = null;
if (this.mappedReadMethod != null) {
if ((this.mappedReadMethod.getParameterTypes()).length != 1)
throw new IntrospectionException("bad mapped read method arg count");
this.mappedPropertyType = this.mappedReadMethod.getReturnType();
if (this.mappedPropertyType == void.class)
throw new IntrospectionException("mapped read method " + this.mappedReadMethod.getName() + " returns void");
}
if (this.mappedWriteMethod != null) {
Class[] params = this.mappedWriteMethod.getParameterTypes();
if (params.length != 2)
throw new IntrospectionException("bad mapped write method arg count");
if (this.mappedPropertyType != null && this.mappedPropertyType != params[1])
throw new IntrospectionException("type mismatch between mapped read and write methods");
this.mappedPropertyType = params[1];
}
} catch (IntrospectionException ex) {
throw ex;
}
}
private static String capitalizePropertyName(String s) {
if (s.length() == 0)
return s;
char[] chars = s.toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
return new String(chars);
}
private static Hashtable declaredMethodCache = new Hashtable();
private static synchronized Method[] getPublicDeclaredMethods(Class clz) {
Class fclz = clz;
Method[] result = (Method[])declaredMethodCache.get(fclz);
if (result != null)
return result;
result = AccessController.<Method[]>doPrivileged(new PrivilegedAction(fclz) {
private final Class val$fclz;
public Object run() {
return this.val$fclz.getDeclaredMethods();
}
});
for (int i = 0; i < result.length; i++) {
Method method = result[i];
int mods = method.getModifiers();
if (!Modifier.isPublic(mods))
result[i] = null;
}
declaredMethodCache.put(clz, result);
return result;
}
private static Method internalFindMethod(Class start, String methodName, int argCount) {
for (Class cl = start; cl != null; cl = cl.getSuperclass()) {
Method[] methods = getPublicDeclaredMethods(cl);
for (int j = 0; j < methods.length; j++) {
Method method = methods[j];
if (method != null) {
int mods = method.getModifiers();
if (!Modifier.isStatic(mods))
if (method.getName().equals(methodName) && (method.getParameterTypes()).length == argCount)
return method;
}
}
}
Class[] ifcs = start.getInterfaces();
for (int i = 0; i < ifcs.length; i++) {
Method m = internalFindMethod(ifcs[i], methodName, argCount);
if (m != null)
return m;
}
return null;
}
private static Method internalFindMethod(Class start, String methodName, int argCount, Class[] args) {
for (Class cl = start; cl != null; cl = cl.getSuperclass()) {
Method[] methods = getPublicDeclaredMethods(cl);
for (int j = 0; j < methods.length; j++) {
Method method = methods[j];
if (method == null)
continue;
int mods = method.getModifiers();
if (Modifier.isStatic(mods))
continue;
Class[] params = method.getParameterTypes();
if (method.getName().equals(methodName) && params.length == argCount) {
boolean different = false;
if (argCount > 0) {
for (int k = 0; k < argCount; k++) {
if (params[k] != args[k])
different = true;
}
if (different)
continue;
}
return method;
}
continue;
}
}
Class[] ifcs = start.getInterfaces();
for (int i = 0; i < ifcs.length; i++) {
Method m = internalFindMethod(ifcs[i], methodName, argCount);
if (m != null)
return m;
}
return null;
}
static Method findMethod(Class cls, String methodName, int argCount) throws IntrospectionException {
if (methodName == null)
return null;
Method m = internalFindMethod(cls, methodName, argCount);
if (m != null)
return m;
throw new IntrospectionException("No method \"" + methodName + "\" with " + argCount + " arg(s)");
}
static Method findMethod(Class cls, String methodName, int argCount, Class[] args) throws IntrospectionException {
if (methodName == null)
return null;
Method m = internalFindMethod(cls, methodName, argCount, args);
if (m != null)
return m;
throw new IntrospectionException("No method \"" + methodName + "\" with " + argCount + " arg(s) of matching types.");
}
static boolean isSubclass(Class a, Class b) {
if (a == b)
return true;
if (a == null || b == null)
return false;
for (Class x = a; x != null; x = x.getSuperclass()) {
if (x == b)
return true;
if (b.isInterface()) {
Class[] interfaces = x.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (isSubclass(interfaces[i], b))
return true;
}
}
}
return false;
}
private boolean throwsException(Method method, Class exception) {
Class[] exs = method.getExceptionTypes();
for (int i = 0; i < exs.length; i++) {
if (exs[i] == exception)
return true;
}
return false;
}
}

View File

@@ -0,0 +1,204 @@
package org.apache.commons.beanutils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MethodUtils {
private static Log log = LogFactory.getLog(MethodUtils.class);
private static boolean loggedAccessibleWarning = false;
private static final Class[] emptyClassArray = new Class[0];
private static final Object[] emptyObjectArray = new Object[0];
public static Object invokeMethod(Object object, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Object[] args = { arg };
return invokeMethod(object, methodName, args);
}
public static Object invokeMethod(Object object, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (args == null)
args = emptyObjectArray;
int arguments = args.length;
Class[] parameterTypes = new Class[arguments];
for (int i = 0; i < arguments; i++)
parameterTypes[i] = args[i].getClass();
return invokeMethod(object, methodName, args, parameterTypes);
}
public static Object invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (parameterTypes == null)
parameterTypes = emptyClassArray;
if (args == null)
args = emptyObjectArray;
Method method = getMatchingAccessibleMethod(object.getClass(), methodName, parameterTypes);
if (method == null)
throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + object.getClass().getName());
return method.invoke(object, args);
}
public static Object invokeExactMethod(Object object, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Object[] args = { arg };
return invokeExactMethod(object, methodName, args);
}
public static Object invokeExactMethod(Object object, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (args == null)
args = emptyObjectArray;
int arguments = args.length;
Class[] parameterTypes = new Class[arguments];
for (int i = 0; i < arguments; i++)
parameterTypes[i] = args[i].getClass();
return invokeExactMethod(object, methodName, args, parameterTypes);
}
public static Object invokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (args == null)
args = emptyObjectArray;
if (parameterTypes == null)
parameterTypes = emptyClassArray;
Method method = getAccessibleMethod(object.getClass(), methodName, parameterTypes);
if (method == null)
throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + object.getClass().getName());
return method.invoke(object, args);
}
public static Method getAccessibleMethod(Class clazz, String methodName, Class parameterType) {
Class[] parameterTypes = { parameterType };
return getAccessibleMethod(clazz, methodName, parameterTypes);
}
public static Method getAccessibleMethod(Class clazz, String methodName, Class[] parameterTypes) {
try {
return getAccessibleMethod(clazz.getMethod(methodName, parameterTypes));
} catch (NoSuchMethodException e) {
return null;
}
}
public static Method getAccessibleMethod(Method method) {
if (method == null)
return null;
if (!Modifier.isPublic(method.getModifiers()))
return null;
Class clazz = method.getDeclaringClass();
if (Modifier.isPublic(clazz.getModifiers()))
return method;
method = getAccessibleMethodFromInterfaceNest(clazz, method.getName(), method.getParameterTypes());
return method;
}
private static Method getAccessibleMethodFromInterfaceNest(Class clazz, String methodName, Class[] parameterTypes) {
Method method = null;
for (; clazz != null; clazz = clazz.getSuperclass()) {
Class[] interfaces = clazz.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (Modifier.isPublic(interfaces[i].getModifiers())) {
try {
method = interfaces[i].getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {}
if (method != null)
break;
method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes);
if (method != null)
break;
}
}
}
if (method != null)
return method;
return null;
}
public static Method getMatchingAccessibleMethod(Class clazz, String methodName, Class[] parameterTypes) {
if (log.isTraceEnabled())
log.trace("Matching name=" + methodName + " on " + clazz);
try {
Method method = clazz.getMethod(methodName, parameterTypes);
if (log.isTraceEnabled()) {
log.trace("Found straight match: " + method);
log.trace("isPublic:" + Modifier.isPublic(method.getModifiers()));
}
try {
method.setAccessible(true);
} catch (SecurityException se) {
if (!loggedAccessibleWarning) {
log.warn("Cannot use JVM pre-1.4 access bug workaround die to restrictive security manager.");
loggedAccessibleWarning = true;
}
log.debug("Cannot setAccessible on method. Therefore cannot use jvm access bug workaround.", se);
}
return method;
} catch (NoSuchMethodException e) {
int paramSize = parameterTypes.length;
Method[] methods = clazz.getMethods();
for (int i = 0, size = methods.length; i < size; i++) {
if (methods[i].getName().equals(methodName)) {
if (log.isTraceEnabled()) {
log.trace("Found matching name:");
log.trace(methods[i]);
}
Class[] methodsParams = methods[i].getParameterTypes();
int methodParamSize = methodsParams.length;
if (methodParamSize == paramSize) {
boolean match = true;
for (int n = 0; n < methodParamSize; n++) {
if (log.isTraceEnabled()) {
log.trace("Param=" + parameterTypes[n].getName());
log.trace("Method=" + methodsParams[n].getName());
}
if (!isAssignmentCompatible(methodsParams[n], parameterTypes[n])) {
if (log.isTraceEnabled())
log.trace(methodsParams[n] + " is not assignable from " + parameterTypes[n]);
match = false;
break;
}
}
if (match) {
Method method = getAccessibleMethod(methods[i]);
if (method != null) {
if (log.isTraceEnabled())
log.trace(method + " accessible version of " + methods[i]);
try {
method.setAccessible(true);
} catch (SecurityException se) {
if (!loggedAccessibleWarning) {
log.warn("Cannot use JVM pre-1.4 access bug workaround die to restrictive security manager.");
loggedAccessibleWarning = true;
}
log.debug("Cannot setAccessible on method. Therefore cannot use jvm access bug workaround.", se);
}
return method;
}
log.trace("Couldn't find accessible method.");
}
}
}
}
log.trace("No match found.");
return null;
}
}
protected static final boolean isAssignmentCompatible(Class parameterType, Class parameterization) {
if (parameterType.isAssignableFrom(parameterization))
return true;
if (parameterType.isPrimitive()) {
if (boolean.class.equals(parameterType))
return Boolean.class.equals(parameterization);
if (float.class.equals(parameterType))
return Float.class.equals(parameterization);
if (long.class.equals(parameterType))
return Long.class.equals(parameterization);
if (int.class.equals(parameterType))
return Integer.class.equals(parameterization);
if (double.class.equals(parameterType))
return Double.class.equals(parameterization);
}
return false;
}
}

View File

@@ -0,0 +1,707 @@
package org.apache.commons.beanutils;
import java.beans.BeanInfo;
import java.beans.IndexedPropertyDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.FastHashMap;
public class PropertyUtils {
public static final char INDEXED_DELIM = '[';
public static final char INDEXED_DELIM2 = ']';
public static final char MAPPED_DELIM = '(';
public static final char MAPPED_DELIM2 = ')';
public static final char NESTED_DELIM = '.';
private static int debug = 0;
public static int getDebug() {
return debug;
}
public static void setDebug(int newDebug) {
debug = newDebug;
}
private static FastHashMap descriptorsCache = null;
private static FastHashMap mappedDescriptorsCache = null;
static {
descriptorsCache = new FastHashMap();
descriptorsCache.setFast(true);
mappedDescriptorsCache = new FastHashMap();
mappedDescriptorsCache.setFast(true);
}
public static void clearDescriptors() {
descriptorsCache.clear();
mappedDescriptorsCache.clear();
Introspector.flushCaches();
}
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (dest == null)
throw new IllegalArgumentException("No destination bean specified");
if (orig == null)
throw new IllegalArgumentException("No origin bean specified");
if (orig instanceof DynaBean) {
DynaProperty[] origDescriptors = ((DynaBean)orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (dest instanceof DynaBean) {
if (isWriteable(dest, name)) {
Object value = ((DynaBean)orig).get(name);
((DynaBean)dest).set(name, value);
}
} else if (isWriteable(dest, name)) {
Object value = ((DynaBean)orig).get(name);
setSimpleProperty(dest, name, value);
}
}
} else if (orig instanceof Map) {
Iterator names = ((Map)orig).keySet().iterator();
while (names.hasNext()) {
String name = names.next();
if (dest instanceof DynaBean) {
if (isWriteable(dest, name)) {
Object value = ((Map)orig).get(name);
((DynaBean)dest).set(name, value);
}
continue;
}
if (isWriteable(dest, name)) {
Object value = ((Map)orig).get(name);
setSimpleProperty(dest, name, value);
}
}
} else {
PropertyDescriptor[] origDescriptors = getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (isReadable(orig, name))
if (dest instanceof DynaBean) {
if (isWriteable(dest, name)) {
Object value = getSimpleProperty(orig, name);
((DynaBean)dest).set(name, value);
}
} else if (isWriteable(dest, name)) {
Object value = getSimpleProperty(orig, name);
setSimpleProperty(dest, name, value);
}
}
}
}
public static Map describe(Object bean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
Map description = new HashMap();
if (bean instanceof DynaBean) {
DynaProperty[] descriptors = ((DynaBean)bean).getDynaClass().getDynaProperties();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
description.put(name, getProperty(bean, name));
}
} else {
PropertyDescriptor[] descriptors = getPropertyDescriptors(bean);
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (descriptors[i].getReadMethod() != null)
description.put(name, getProperty(bean, name));
}
}
return description;
}
public static Object getIndexedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('[');
int delim2 = name.indexOf(']');
if (delim < 0 || delim2 <= delim)
throw new IllegalArgumentException("Invalid indexed property '" + name + "'");
int index = -1;
try {
String subscript = name.substring(delim + 1, delim2);
index = Integer.parseInt(subscript);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid indexed property '" + name + "'");
}
name = name.substring(0, delim);
return getIndexedProperty(bean, name, index);
}
public static Object getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (bean instanceof DynaBean) {
DynaProperty dynaProperty = ((DynaBean)bean).getDynaClass().getDynaProperty(name);
if (dynaProperty == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
return ((DynaBean)bean).get(name, index);
}
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
if (descriptor instanceof IndexedPropertyDescriptor) {
Method method = ((IndexedPropertyDescriptor)descriptor).getIndexedReadMethod();
if (method != null) {
Object[] subscript = new Object[1];
subscript[0] = new Integer(index);
try {
return method.invoke(bean, subscript);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof ArrayIndexOutOfBoundsException)
throw (ArrayIndexOutOfBoundsException)e.getTargetException();
throw e;
}
}
}
Method readMethod = getReadMethod(descriptor);
if (readMethod == null)
throw new NoSuchMethodException("Property '" + name + "' has no getter method");
Object value = readMethod.invoke(bean, new Object[0]);
if (!value.getClass().isArray()) {
if (!(value instanceof List))
throw new IllegalArgumentException("Property '" + name + "' is not indexed");
return ((List)value).get(index);
}
return Array.get(value, index);
}
public static Object getMappedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('(');
int delim2 = name.indexOf(')');
if (delim < 0 || delim2 <= delim)
throw new IllegalArgumentException("Invalid mapped property '" + name + "'");
String key = name.substring(delim + 1, delim2);
name = name.substring(0, delim);
return getMappedProperty(bean, name, key);
}
public static Object getMappedProperty(Object bean, String name, String key) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (key == null)
throw new IllegalArgumentException("No key specified");
if (bean instanceof DynaBean) {
DynaProperty dynaProperty = ((DynaBean)bean).getDynaClass().getDynaProperty(name);
if (dynaProperty == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
return ((DynaBean)bean).get(name, key);
}
Object result = null;
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
if (descriptor instanceof MappedPropertyDescriptor) {
Method readMethod = ((MappedPropertyDescriptor)descriptor).getMappedReadMethod();
if (readMethod != null) {
Object[] keyArray = new Object[1];
keyArray[0] = key;
result = readMethod.invoke(bean, keyArray);
} else {
throw new NoSuchMethodException("Property '" + name + "' has no mapped getter method");
}
} else {
Method readMethod = descriptor.getReadMethod();
if (readMethod != null) {
Object invokeResult = readMethod.invoke(bean, new Object[0]);
if (invokeResult instanceof Map)
result = ((Map)invokeResult).get(key);
} else {
throw new NoSuchMethodException("Property '" + name + "' has no mapped getter method");
}
}
return result;
}
public static FastHashMap getMappedPropertyDescriptors(Class beanClass) {
if (beanClass == null)
return null;
return (FastHashMap)mappedDescriptorsCache.get(beanClass);
}
public static FastHashMap getMappedPropertyDescriptors(Object bean) {
if (bean == null)
return null;
return getMappedPropertyDescriptors(bean.getClass());
}
public static Object getNestedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int indexOfINDEXED_DELIM = -1;
int indexOfMAPPED_DELIM = -1;
int indexOfMAPPED_DELIM2 = -1;
int indexOfNESTED_DELIM = -1;
while (true) {
indexOfNESTED_DELIM = name.indexOf('.');
indexOfMAPPED_DELIM = name.indexOf('(');
indexOfMAPPED_DELIM2 = name.indexOf(')');
if (indexOfMAPPED_DELIM2 >= 0 && indexOfMAPPED_DELIM >= 0 && (indexOfNESTED_DELIM < 0 || indexOfNESTED_DELIM > indexOfMAPPED_DELIM)) {
indexOfNESTED_DELIM = name.indexOf('.', indexOfMAPPED_DELIM2);
} else {
indexOfNESTED_DELIM = name.indexOf('.');
}
if (indexOfNESTED_DELIM < 0)
break;
String next = name.substring(0, indexOfNESTED_DELIM);
indexOfINDEXED_DELIM = next.indexOf('[');
indexOfMAPPED_DELIM = next.indexOf('(');
if (bean instanceof Map) {
bean = ((Map)bean).get(next);
} else if (indexOfMAPPED_DELIM >= 0) {
bean = getMappedProperty(bean, next);
} else if (indexOfINDEXED_DELIM >= 0) {
bean = getIndexedProperty(bean, next);
} else {
bean = getSimpleProperty(bean, next);
}
if (bean == null)
throw new IllegalArgumentException("Null property value for '" + name.substring(0, indexOfNESTED_DELIM) + "'");
name = name.substring(indexOfNESTED_DELIM + 1);
}
indexOfINDEXED_DELIM = name.indexOf('[');
indexOfMAPPED_DELIM = name.indexOf('(');
if (bean instanceof Map) {
bean = ((Map)bean).get(name);
} else if (indexOfMAPPED_DELIM >= 0) {
bean = getMappedProperty(bean, name);
} else if (indexOfINDEXED_DELIM >= 0) {
bean = getIndexedProperty(bean, name);
} else {
bean = getSimpleProperty(bean, name);
}
return bean;
}
public static Object getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
return getNestedProperty(bean, name);
}
public static PropertyDescriptor getPropertyDescriptor(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
while (true) {
int period = name.indexOf('.');
if (period < 0)
break;
String next = name.substring(0, period);
int indexOfINDEXED_DELIM = next.indexOf('[');
int indexOfMAPPED_DELIM = next.indexOf('(');
if (indexOfMAPPED_DELIM >= 0 && (indexOfINDEXED_DELIM < 0 || indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
bean = getMappedProperty(bean, next);
} else if (indexOfINDEXED_DELIM >= 0) {
bean = getIndexedProperty(bean, next);
} else {
bean = getSimpleProperty(bean, next);
}
if (bean == null)
throw new IllegalArgumentException("Null property value for '" + name.substring(0, period) + "'");
name = name.substring(period + 1);
}
int left = name.indexOf('[');
if (left >= 0)
name = name.substring(0, left);
left = name.indexOf('(');
if (left >= 0)
name = name.substring(0, left);
if (bean == null || name == null)
return null;
PropertyDescriptor[] descriptors = getPropertyDescriptors(bean);
if (descriptors != null)
for (int i = 0; i < descriptors.length; i++) {
if (name.equals(descriptors[i].getName()))
return descriptors[i];
}
PropertyDescriptor result = null;
FastHashMap mappedDescriptors = getMappedPropertyDescriptors(bean);
if (mappedDescriptors == null) {
mappedDescriptors = new FastHashMap();
mappedDescriptors.setFast(true);
mappedDescriptorsCache.put(bean.getClass(), mappedDescriptors);
}
result = (PropertyDescriptor)mappedDescriptors.get(name);
if (result == null) {
try {
result = new MappedPropertyDescriptor(name, bean.getClass());
} catch (IntrospectionException ie) {}
if (result != null)
mappedDescriptors.put(name, result);
}
return result;
}
public static PropertyDescriptor[] getPropertyDescriptors(Class beanClass) {
if (beanClass == null)
throw new IllegalArgumentException("No bean class specified");
PropertyDescriptor[] descriptors = null;
descriptors = (PropertyDescriptor[])descriptorsCache.get(beanClass);
if (descriptors != null)
return descriptors;
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(beanClass);
} catch (IntrospectionException e) {
return new PropertyDescriptor[0];
}
descriptors = beanInfo.getPropertyDescriptors();
if (descriptors == null)
descriptors = new PropertyDescriptor[0];
descriptorsCache.put(beanClass, descriptors);
return descriptors;
}
public static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
return getPropertyDescriptors(bean.getClass());
}
public static Class getPropertyEditorClass(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor != null)
return descriptor.getPropertyEditorClass();
return null;
}
public static Class getPropertyType(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (bean instanceof DynaBean) {
DynaProperty dynaProperty = ((DynaBean)bean).getDynaClass().getDynaProperty(name);
if (dynaProperty == null)
return null;
Class type = dynaProperty.getType();
if (type == null)
return null;
if (type.isArray())
return type.getComponentType();
return type;
}
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
return null;
if (descriptor instanceof IndexedPropertyDescriptor)
return ((IndexedPropertyDescriptor)descriptor).getIndexedPropertyType();
if (descriptor instanceof MappedPropertyDescriptor)
return ((MappedPropertyDescriptor)descriptor).getMappedPropertyType();
return descriptor.getPropertyType();
}
public static Method getReadMethod(PropertyDescriptor descriptor) {
return MethodUtils.getAccessibleMethod(descriptor.getReadMethod());
}
public static Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (name.indexOf('.') >= 0)
throw new IllegalArgumentException("Nested property names are not allowed");
if (name.indexOf('[') >= 0)
throw new IllegalArgumentException("Indexed property names are not allowed");
if (name.indexOf('(') >= 0)
throw new IllegalArgumentException("Mapped property names are not allowed");
if (bean instanceof DynaBean) {
DynaProperty dynaProperty = ((DynaBean)bean).getDynaClass().getDynaProperty(name);
if (dynaProperty == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
return ((DynaBean)bean).get(name);
}
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
Method readMethod = getReadMethod(descriptor);
if (readMethod == null)
throw new NoSuchMethodException("Property '" + name + "' has no getter method");
Object value = readMethod.invoke(bean, new Object[0]);
return value;
}
public static Method getWriteMethod(PropertyDescriptor descriptor) {
return MethodUtils.getAccessibleMethod(descriptor.getWriteMethod());
}
public static boolean isReadable(Object bean, String name) {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (bean instanceof DynaBean)
return (((DynaBean)bean).getDynaClass().getDynaProperty(name) != null);
try {
PropertyDescriptor desc = getPropertyDescriptor(bean, name);
if (desc != null) {
Method readMethod = desc.getReadMethod();
if (readMethod == null && desc instanceof IndexedPropertyDescriptor)
readMethod = ((IndexedPropertyDescriptor)desc).getIndexedReadMethod();
return (readMethod != null);
}
return false;
} catch (IllegalAccessException e) {
return false;
} catch (InvocationTargetException e) {
return false;
} catch (NoSuchMethodException e) {
return false;
}
}
public static boolean isWriteable(Object bean, String name) {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (bean instanceof DynaBean)
return (((DynaBean)bean).getDynaClass().getDynaProperty(name) != null);
try {
PropertyDescriptor desc = getPropertyDescriptor(bean, name);
if (desc != null) {
Method writeMethod = desc.getWriteMethod();
if (writeMethod == null && desc instanceof IndexedPropertyDescriptor)
writeMethod = ((IndexedPropertyDescriptor)desc).getIndexedWriteMethod();
return (writeMethod != null);
}
return false;
} catch (IllegalAccessException e) {
return false;
} catch (InvocationTargetException e) {
return false;
} catch (NoSuchMethodException e) {
return false;
}
}
public static void setIndexedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('[');
int delim2 = name.indexOf(']');
if (delim < 0 || delim2 <= delim)
throw new IllegalArgumentException("Invalid indexed property '" + name + "'");
int index = -1;
try {
String subscript = name.substring(delim + 1, delim2);
index = Integer.parseInt(subscript);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid indexed property '" + name + "'");
}
name = name.substring(0, delim);
setIndexedProperty(bean, name, index, value);
}
public static void setIndexedProperty(Object bean, String name, int index, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (bean instanceof DynaBean) {
DynaProperty dynaProperty = ((DynaBean)bean).getDynaClass().getDynaProperty(name);
if (dynaProperty == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
((DynaBean)bean).set(name, index, value);
return;
}
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
if (descriptor instanceof IndexedPropertyDescriptor) {
Method writeMethod = ((IndexedPropertyDescriptor)descriptor).getIndexedWriteMethod();
if (writeMethod != null) {
Object[] subscript = new Object[2];
subscript[0] = new Integer(index);
subscript[1] = value;
try {
writeMethod.invoke(bean, subscript);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof ArrayIndexOutOfBoundsException)
throw (ArrayIndexOutOfBoundsException)e.getTargetException();
throw e;
}
return;
}
}
Method readMethod = descriptor.getReadMethod();
if (readMethod == null)
throw new NoSuchMethodException("Property '" + name + "' has no getter method");
Object array = readMethod.invoke(bean, new Object[0]);
if (!array.getClass().isArray()) {
if (array instanceof List) {
((List)array).set(index, value);
} else {
throw new IllegalArgumentException("Property '" + name + "' is not indexed");
}
} else {
Array.set(array, index, value);
}
}
public static void setMappedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('(');
int delim2 = name.indexOf(')');
if (delim < 0 || delim2 <= delim)
throw new IllegalArgumentException("Invalid mapped property '" + name + "'");
String key = name.substring(delim + 1, delim2);
name = name.substring(0, delim);
setMappedProperty(bean, name, key, value);
}
public static void setMappedProperty(Object bean, String name, String key, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (key == null)
throw new IllegalArgumentException("No key specified");
if (bean instanceof DynaBean) {
DynaProperty dynaProperty = ((DynaBean)bean).getDynaClass().getDynaProperty(name);
if (dynaProperty == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
((DynaBean)bean).set(name, key, value);
return;
}
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
if (descriptor instanceof MappedPropertyDescriptor) {
Method mappedWriteMethod = ((MappedPropertyDescriptor)descriptor).getMappedWriteMethod();
if (mappedWriteMethod != null) {
Object[] params = new Object[2];
params[0] = key;
params[1] = value;
mappedWriteMethod.invoke(bean, params);
} else {
throw new NoSuchMethodException("Property '" + name + "' has no mapped setter method");
}
} else {
Method readMethod = descriptor.getReadMethod();
if (readMethod != null) {
Object invokeResult = readMethod.invoke(bean, new Object[0]);
if (invokeResult instanceof Map)
((Map)invokeResult).put(key, value);
} else {
throw new NoSuchMethodException("Property '" + name + "' has no mapped getter method");
}
}
}
public static void setNestedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int indexOfINDEXED_DELIM = -1;
int indexOfMAPPED_DELIM = -1;
while (true) {
int delim = name.indexOf('.');
if (delim < 0)
break;
String next = name.substring(0, delim);
indexOfINDEXED_DELIM = next.indexOf('[');
indexOfMAPPED_DELIM = next.indexOf('(');
if (bean instanceof Map) {
bean = ((Map)bean).get(next);
} else if (indexOfMAPPED_DELIM >= 0) {
bean = getMappedProperty(bean, next);
} else if (indexOfINDEXED_DELIM >= 0) {
bean = getIndexedProperty(bean, next);
} else {
bean = getSimpleProperty(bean, next);
}
if (bean == null)
throw new IllegalArgumentException("Null property value for '" + name.substring(0, delim) + "'");
name = name.substring(delim + 1);
}
indexOfINDEXED_DELIM = name.indexOf('[');
indexOfMAPPED_DELIM = name.indexOf('(');
if (bean instanceof Map) {
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null) {
((Map)bean).put(name, value);
} else {
setSimpleProperty(bean, name, value);
}
} else if (indexOfMAPPED_DELIM >= 0) {
setMappedProperty(bean, name, value);
} else if (indexOfINDEXED_DELIM >= 0) {
setIndexedProperty(bean, name, value);
} else {
setSimpleProperty(bean, name, value);
}
}
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
setNestedProperty(bean, name, value);
}
public static void setSimpleProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (name.indexOf('.') >= 0)
throw new IllegalArgumentException("Nested property names are not allowed");
if (name.indexOf('[') >= 0)
throw new IllegalArgumentException("Indexed property names are not allowed");
if (name.indexOf('(') >= 0)
throw new IllegalArgumentException("Mapped property names are not allowed");
if (bean instanceof DynaBean) {
DynaProperty dynaProperty = ((DynaBean)bean).getDynaClass().getDynaProperty(name);
if (dynaProperty == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
((DynaBean)bean).set(name, value);
return;
}
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '" + name + "'");
Method writeMethod = getWriteMethod(descriptor);
if (writeMethod == null)
throw new NoSuchMethodException("Property '" + name + "' has no setter method");
Object[] values = new Object[1];
values[0] = value;
writeMethod.invoke(bean, values);
}
}

View File

@@ -0,0 +1,52 @@
package org.apache.commons.beanutils.converters;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public abstract class AbstractArrayConverter implements Converter {
protected Object defaultValue = null;
protected static String[] strings = new String[0];
protected boolean useDefault = true;
public abstract Object convert(Class paramClass, Object paramObject);
protected List parseElements(String svalue) {
if (svalue == null)
throw new NullPointerException();
svalue = svalue.trim();
if (svalue.startsWith("{") && svalue.endsWith("}"))
svalue = svalue.substring(1, svalue.length() - 1);
try {
int ttype;
StreamTokenizer st = new StreamTokenizer(new StringReader(svalue));
st.whitespaceChars(44, 44);
st.ordinaryChars(48, 57);
st.ordinaryChars(46, 46);
st.ordinaryChars(45, 45);
st.wordChars(48, 57);
st.wordChars(46, 46);
st.wordChars(45, 45);
ArrayList list = new ArrayList();
while (true) {
ttype = st.nextToken();
if (ttype == -3 || ttype > 0) {
list.add(st.sval);
continue;
}
break;
}
if (ttype == -1)
return list;
throw new ConversionException("Encountered token of type " + ttype);
} catch (IOException e) {
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,42 @@
package org.apache.commons.beanutils.converters;
import java.math.BigDecimal;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class BigDecimalConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public BigDecimalConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public BigDecimalConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof BigDecimal)
return value;
try {
return new BigDecimal(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,42 @@
package org.apache.commons.beanutils.converters;
import java.math.BigInteger;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class BigIntegerConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public BigIntegerConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public BigIntegerConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof BigInteger)
return value;
try {
return new BigInteger(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,71 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class BooleanArrayConverter extends AbstractArrayConverter {
public BooleanArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public BooleanArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static boolean[] model = new boolean[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
boolean[] results = new boolean[values.length];
for (int i = 0; i < values.length; i++) {
String stringValue = values[i];
if (stringValue.equalsIgnoreCase("yes") || stringValue.equalsIgnoreCase("y") || stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("on") || stringValue.equalsIgnoreCase("1")) {
results[i] = true;
} else if (stringValue.equalsIgnoreCase("no") || stringValue.equalsIgnoreCase("n") || stringValue.equalsIgnoreCase("false") || stringValue.equalsIgnoreCase("off") || stringValue.equalsIgnoreCase("0")) {
results[i] = false;
} else {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString());
}
}
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
boolean[] results = new boolean[list.size()];
for (int i = 0; i < results.length; i++) {
String stringValue = list.get(i);
if (stringValue.equalsIgnoreCase("yes") || stringValue.equalsIgnoreCase("y") || stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("on") || stringValue.equalsIgnoreCase("1")) {
results[i] = true;
} else if (stringValue.equalsIgnoreCase("no") || stringValue.equalsIgnoreCase("n") || stringValue.equalsIgnoreCase("false") || stringValue.equalsIgnoreCase("off") || stringValue.equalsIgnoreCase("0")) {
results[i] = false;
} else {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString());
}
}
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,48 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class BooleanConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public BooleanConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public BooleanConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Boolean)
return value;
try {
String stringValue = value.toString();
if (stringValue.equalsIgnoreCase("yes") || stringValue.equalsIgnoreCase("y") || stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("on") || stringValue.equalsIgnoreCase("1"))
return Boolean.TRUE;
if (stringValue.equalsIgnoreCase("no") || stringValue.equalsIgnoreCase("n") || stringValue.equalsIgnoreCase("false") || stringValue.equalsIgnoreCase("off") || stringValue.equalsIgnoreCase("0"))
return Boolean.FALSE;
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(stringValue);
} catch (ClassCastException e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,51 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class ByteArrayConverter extends AbstractArrayConverter {
public ByteArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public ByteArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static byte[] model = new byte[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
byte[] results = new byte[values.length];
for (int i = 0; i < values.length; i++)
results[i] = Byte.parseByte(values[i]);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
byte[] results = new byte[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = Byte.parseByte((String)list.get(i));
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,43 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class ByteConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public ByteConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public ByteConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Byte)
return value;
if (value instanceof Number)
return new Byte(((Number)value).byteValue());
try {
return new Byte(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,51 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class CharacterArrayConverter extends AbstractArrayConverter {
public CharacterArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public CharacterArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static char[] model = new char[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
char[] results = new char[values.length];
for (int i = 0; i < values.length; i++)
results[i] = values[i].charAt(0);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
char[] results = new char[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = ((String)list.get(i)).charAt(0);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,41 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class CharacterConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public CharacterConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public CharacterConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Character)
return value;
try {
return new Character(value.toString().charAt(0));
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,44 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class ClassConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public ClassConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public ClassConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Class)
return value;
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null)
classLoader = ClassConverter.class.getClassLoader();
return classLoader.loadClass(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,51 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class DoubleArrayConverter extends AbstractArrayConverter {
public DoubleArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public DoubleArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static double[] model = new double[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
double[] results = new double[values.length];
for (int i = 0; i < values.length; i++)
results[i] = Double.parseDouble(values[i]);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
double[] results = new double[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = Double.parseDouble((String)list.get(i));
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,43 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class DoubleConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public DoubleConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public DoubleConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Double)
return value;
if (value instanceof Number)
return new Double(((Number)value).doubleValue());
try {
return new Double(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,51 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class FloatArrayConverter extends AbstractArrayConverter {
public FloatArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public FloatArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static float[] model = new float[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
float[] results = new float[values.length];
for (int i = 0; i < values.length; i++)
results[i] = Float.parseFloat(values[i]);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
float[] results = new float[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = Float.parseFloat((String)list.get(i));
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,43 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class FloatConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public FloatConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public FloatConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Float)
return value;
if (value instanceof Number)
return new Float(((Number)value).floatValue());
try {
return new Float(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,51 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class IntegerArrayConverter extends AbstractArrayConverter {
public IntegerArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public IntegerArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static int[] model = new int[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
int[] results = new int[values.length];
for (int i = 0; i < values.length; i++)
results[i] = Integer.parseInt(values[i]);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
int[] results = new int[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = Integer.parseInt((String)list.get(i));
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,43 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class IntegerConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public IntegerConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public IntegerConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Integer)
return value;
if (value instanceof Number)
return new Integer(((Number)value).intValue());
try {
return new Integer(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,51 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class LongArrayConverter extends AbstractArrayConverter {
public LongArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public LongArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static long[] model = new long[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
long[] results = new long[values.length];
for (int i = 0; i < values.length; i++)
results[i] = Long.parseLong(values[i]);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
long[] results = new long[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = Long.parseLong((String)list.get(i));
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,43 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class LongConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public LongConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public LongConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Long)
return value;
if (value instanceof Number)
return new Long(((Number)value).longValue());
try {
return new Long(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,51 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class ShortArrayConverter extends AbstractArrayConverter {
public ShortArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public ShortArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static short[] model = new short[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
if (AbstractArrayConverter.strings.getClass() == value.getClass())
try {
String[] values = (String[])value;
short[] results = new short[values.length];
for (int i = 0; i < values.length; i++)
results[i] = Short.parseShort(values[i]);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
try {
List list = parseElements(value.toString());
short[] results = new short[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = Short.parseShort((String)list.get(i));
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,43 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class ShortConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public ShortConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public ShortConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Short)
return value;
if (value instanceof Number)
return new Short(((Number)value).shortValue());
try {
return new Short(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,42 @@
package org.apache.commons.beanutils.converters;
import java.sql.Date;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class SqlDateConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public SqlDateConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public SqlDateConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Date)
return value;
try {
return Date.valueOf(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,42 @@
package org.apache.commons.beanutils.converters;
import java.sql.Time;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class SqlTimeConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public SqlTimeConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public SqlTimeConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Time)
return value;
try {
return Time.valueOf(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,42 @@
package org.apache.commons.beanutils.converters;
import java.sql.Timestamp;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
public final class SqlTimestampConverter implements Converter {
private Object defaultValue;
private boolean useDefault;
public SqlTimestampConverter() {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = null;
this.useDefault = false;
}
public SqlTimestampConverter(Object defaultValue) {
this.defaultValue = null;
this.useDefault = true;
this.defaultValue = defaultValue;
this.useDefault = true;
}
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (value instanceof Timestamp)
return value;
try {
return Timestamp.valueOf(value.toString());
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(e);
}
}
}

View File

@@ -0,0 +1,39 @@
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
public final class StringArrayConverter extends AbstractArrayConverter {
public StringArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
public StringArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
private static String[] model = new String[0];
public Object convert(Class type, Object value) {
if (value == null) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException("No value specified");
}
if (model.getClass() == value.getClass())
return value;
try {
List list = parseElements(value.toString());
String[] results = new String[list.size()];
for (int i = 0; i < results.length; i++)
results[i] = list.get(i);
return results;
} catch (Exception e) {
if (this.useDefault)
return this.defaultValue;
throw new ConversionException(value.toString(), e);
}
}
}

View File

@@ -0,0 +1,11 @@
package org.apache.commons.beanutils.converters;
import org.apache.commons.beanutils.Converter;
public final class StringConverter implements Converter {
public Object convert(Class type, Object value) {
if (value == null)
return null;
return value.toString();
}
}