webwork/src/main/webwork/util BeanUtil.java,1.20,1.21
[email protected] Sun, 30 Jun 2002 22:21:45 -0700
| Newsgroups | gmane.comp.java.webwork.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/webwork/webwork/src/main/webwork/util
In directory usw-pr-cvs1:/tmp/cvs-serv14433
Modified Files:
BeanUtil.java
Log Message:
added support for primitive and object arrays
Index: BeanUtil.java
===================================================================
RCS file: /cvsroot/webwork/webwork/src/main/webwork/util/BeanUtil.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- BeanUtil.java 30 Jun 2002 01:27:09 -0000 1.20
+++ BeanUtil.java 1 Jul 2002 05:21:42 -0000 1.21
@@ -13,6 +13,7 @@
import java.beans.*;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Array;
import java.util.*;
import java.text.DateFormat;
@@ -37,14 +38,29 @@
static
{
PropertyEditorManager.registerEditor(Integer.class, IntegerEditor.class);
+ PropertyEditorManager.registerEditor(Integer[].class, IntegerEditor.class);
PropertyEditorManager.registerEditor(Double.class, DoubleEditor.class);
+ PropertyEditorManager.registerEditor(Double[].class, DoubleEditor.class);
PropertyEditorManager.registerEditor(Byte.class, ByteEditor.class);
+ PropertyEditorManager.registerEditor(Byte[].class, ByteEditor.class);
PropertyEditorManager.registerEditor(Short.class, ShortEditor.class);
+ PropertyEditorManager.registerEditor(Short[].class, ShortEditor.class);
PropertyEditorManager.registerEditor(Long.class, LongEditor.class);
+ PropertyEditorManager.registerEditor(Long[].class, LongEditor.class);
PropertyEditorManager.registerEditor(Float.class, FloatEditor.class);
+ PropertyEditorManager.registerEditor(Float[].class, FloatEditor.class);
PropertyEditorManager.registerEditor(Date.class, DateEditor.class);
+ PropertyEditorManager.registerEditor(Date[].class, DateEditor.class);
PropertyEditorManager.registerEditor(Boolean.class, BooleanEditor.class);
+ PropertyEditorManager.registerEditor(Boolean[].class, BooleanEditor.class);
PropertyEditorManager.registerEditor(DateFormat.class, DateFormatEditor.class);
+ PropertyEditorManager.registerEditor(int[].class, sun.beans.editors.IntEditor.class);
+ PropertyEditorManager.registerEditor(double[].class, sun.beans.editors.DoubleEditor.class);
+ PropertyEditorManager.registerEditor(short[].class, sun.beans.editors.ShortEditor.class);
+ PropertyEditorManager.registerEditor(long[].class, sun.beans.editors.LongEditor.class);
+ PropertyEditorManager.registerEditor(float[].class, sun.beans.editors.FloatEditor.class);
+ PropertyEditorManager.registerEditor(boolean[].class, sun.beans.editors.BoolEditor.class);
+ PropertyEditorManager.registerEditor(byte[].class, sun.beans.editors.ByteEditor.class);
}
/**
@@ -171,7 +187,7 @@
// Map
if(Map.class.isAssignableFrom(curObject.getClass())) {
- curObject = ((Map)curObject).get(key.toString());
+ curObject = ((Map)curObject).get(key);
}
// Resource Bundle
else if(ResourceBundle.class.isAssignableFrom(curObject.getClass())) {
@@ -194,7 +210,7 @@
{
try {
if(descriptor instanceof IndexedPropertyDescriptor) {
- throw new IllegalArgumentException("Attempting to set a indexed field " + curSegment.getId() + "as an non-indexed field");
+ throw new IllegalArgumentException("Attempting to set a indexed field " + curSegment.getId() + " as an non-indexed field");
}
//if here then we are setting the property directly
@@ -372,7 +388,7 @@
}
/**
- * Set an indexed field. Will delegate the actuall setting of the field to
+ * Set an indexed field. Will delegate the actual setting of the field to
* one of three methods. Sections were taken from BeanUtil.setProprty (rev.1.4)
*
* @param obj the object to set the property on.
@@ -401,14 +417,14 @@
if(value != null) {
if(descriptor.getPropertyEditorClass() == null) // No property editor given -> try default conversions
{
- if(setIndexedStringValueDirectly(obj, descriptor, value, index)) {
+ if (setIndexedStringValueDirectly(obj, descriptor, value, index))
return true;
- }
}
setIndexedStringValueWithPropertyEditor(obj, descriptor, value, index);
return true;
+ } else {
+ return false;
}
- return false;
}
/**
@@ -545,9 +561,45 @@
}
try {
try {
- pe.setAsText(values[0]);
- Object realValue = pe.getValue();
- m.invoke(obj, new Object[]{realValue});
+ Class[] paramClasses = m.getParameterTypes();
+ Class paramClass = paramClasses[0];
+ Class compType = paramClass.getComponentType();
+ if (paramClass.getComponentType() != null)
+ {
+ Object a = Array.newInstance(paramClass.getComponentType(), values.length);
+ for (int i=0; i<values.length; i++) {
+ pe.setAsText(values[i]);
+ Array.set(a, i, pe.getValue());
+ }
+ if (compType.getName().compareTo("int")==0) {
+ m.invoke(obj, new Object[]{(int[])a});
+ } else
+ if (compType.getName().compareTo("double")==0) {
+ m.invoke(obj, new Object[]{(double[])a});
+ } else
+ if (compType.getName().compareTo("short")==0) {
+ m.invoke(obj, new Object[]{(short[])a});
+ } else
+ if (compType.getName().compareTo("long")==0) {
+ m.invoke(obj, new Object[]{(long[])a});
+ } else
+ if (compType.getName().compareTo("float")==0) {
+ m.invoke(obj, new Object[]{(float[])a});
+ } else
+ if (compType.getName().compareTo("boolean")==0) {
+ m.invoke(obj, new Object[]{(boolean[])a});
+ } else
+ if (compType.getName().compareTo("byte")==0) {
+ m.invoke(obj, new Object[]{(byte[])a});
+ }
+ else {
+ m.invoke(obj, new Object[]{(Object[])a});
+ }
+ } else {
+ pe.setAsText(values[0]);
+ Object realValue = pe.getValue();
+ m.invoke(obj, new Object[]{realValue});
+ }
} catch(NumberFormatException nfe) { //need this for Sun's Editor's
throw new IllegalArgumentException("Unable to set value for " + values[0]);
}
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf