craigmcc 01/04/16 09:30:14
Modified: beanutils/src/java/org/apache/commons/beanutils
BeanUtils.java PropertyUtils.java
Log:
Add a describe() method to both BeanUtils and PropertyUtils that returns a
Map of all properties of the specified bean that have a property reader
method. The BeanUtils version converts property values to Strings
(consistent with the rest of the BeanUtils methods), while the
PropertyUtils version returns them unmodified.
Submitted by: Michael L. Heuer <[EMAIL PROTECTED]>
Revision Changes Path
1.3 +41 -4
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java
Index: BeanUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BeanUtils.java 2001/04/16 13:26:46 1.2
+++ BeanUtils.java 2001/04/16 16:30:12 1.3
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
1.2 2001/04/16 13:26:46 geirm Exp $
- * $Revision: 1.2 $
- * $Date: 2001/04/16 13:26:46 $
+ * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
1.3 2001/04/16 16:30:12 craigmcc Exp $
+ * $Revision: 1.3 $
+ * $Date: 2001/04/16 16:30:12 $
*
* ====================================================================
*
@@ -73,6 +73,8 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -83,7 +85,7 @@
* @author Craig R. McClanahan
* @author Ralph Schaer
* @author Chris Audley
- * @version $Revision: 1.2 $ $Date: 2001/04/16 13:26:46 $
+ * @version $Revision: 1.3 $ $Date: 2001/04/16 16:30:12 $
*/
public class BeanUtils {
@@ -132,6 +134,41 @@
Object newBean = clazz.newInstance();
PropertyUtils.copyProperties(newBean, bean);
return (newBean);
+
+ }
+
+
+ /**
+ * Return the entire set of properties for which the specified bean
+ * provides a read method. This map can be fed back to a call to
+ * <code>BeanUtils.populate()</code> to reconsitute the same set of
+ * properties, modulo differences for read-only and write-only
+ * properties.
+ *
+ * @param bean Bean whose properties are to be extracted
+ *
+ * @exception IllegalAccessException if the caller does not have
+ * access to the property accessor method
+ * @exception InvocationTargetException if the property accessor method
+ * throws an exception
+ * @exception NoSuchMethodException if an accessor method for this
+ * propety cannot be found
+ */
+ public static Map describe(Object bean)
+ throws IllegalAccessException, InvocationTargetException,
+ NoSuchMethodException {
+
+ if (bean == null)
+ return (Collections.EMPTY_MAP);
+ PropertyDescriptor descriptors[] =
+ PropertyUtils.getPropertyDescriptors(bean);
+ Map description = new HashMap(descriptors.length);
+ 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);
}
1.2 +39 -3
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java
Index: PropertyUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PropertyUtils.java 2001/04/14 18:29:56 1.1
+++ PropertyUtils.java 2001/04/16 16:30:13 1.2
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
1.1 2001/04/14 18:29:56 craigmcc Exp $
- * $Revision: 1.1 $
- * $Date: 2001/04/14 18:29:56 $
+ * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
1.2 2001/04/16 16:30:13 craigmcc Exp $
+ * $Revision: 1.2 $
+ * $Date: 2001/04/16 16:30:13 $
*
* ====================================================================
*
@@ -72,7 +72,9 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.Map;
/**
@@ -118,7 +120,7 @@
* @author Craig R. McClanahan
* @author Ralph Schaer
* @author Chris Audley
- * @version $Revision: 1.1 $ $Date: 2001/04/14 18:29:56 $
+ * @version $Revision: 1.2 $ $Date: 2001/04/16 16:30:13 $
*/
public class PropertyUtils {
@@ -209,6 +211,40 @@
}
}
}
+
+ }
+
+
+ /**
+ * Return the entire set of properties for which the specified bean
+ * provides a read method. This map contains the unconverted property
+ * values for all properties for which a read method is provided
+ * (i.e. where the <code>getReadMethod()</code> returns non-null).
+ *
+ * @param bean Bean whose properties are to be extracted
+ *
+ * @exception IllegalAccessException if the caller does not have
+ * access to the property accessor method
+ * @exception InvocationTargetException if the property accessor method
+ * throws an exception
+ * @exception NoSuchMethodException if an accessor method for this
+ * propety cannot be found
+ */
+ public static Map describe(Object bean)
+ throws IllegalAccessException, InvocationTargetException,
+ NoSuchMethodException {
+
+ if (bean == null)
+ return (Collections.EMPTY_MAP);
+ PropertyDescriptor descriptors[] =
+ PropertyUtils.getPropertyDescriptors(bean);
+ Map description = new HashMap(descriptors.length);
+ 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);
}