Author: swd847
Date: 2010-01-16 17:13:59 -0500 (Sat, 16 Jan 2010)
New Revision: 11968
Added:
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/DirectFieldSetter.java
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/FieldValueSetter.java
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MethodFieldSetter.java
Modified:
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/ArrayFieldSet.java
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/CollectionFieldSet.java
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MapFieldSet.java
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/SimpleFieldValue.java
modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java
Log:
Setting field values now respects getters and setters
Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/ArrayFieldSet.java
===================================================================
---
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/ArrayFieldSet.java 2010-01-16
06:25:13 UTC (rev 11967)
+++
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/ArrayFieldSet.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -5,7 +5,6 @@
package org.jboss.seam.xml.fieldset;
import java.lang.reflect.Array;
-import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@@ -20,11 +19,11 @@
*/
public class ArrayFieldSet implements FieldValueObject
{
- Field field;
+ FieldValueSetter field;
List<AFS> values;
Class arrayType;
- public ArrayFieldSet(Field field, List<XmlItem> items)
+ public ArrayFieldSet(FieldValueSetter field, List<XmlItem> items)
{
this.field = field;
this.values = new ArrayList<AFS>();
Modified:
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/CollectionFieldSet.java
===================================================================
---
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/CollectionFieldSet.java 2010-01-16
06:25:13 UTC (rev 11967)
+++
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/CollectionFieldSet.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -4,7 +4,6 @@
*/
package org.jboss.seam.xml.fieldset;
-import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
@@ -31,12 +30,12 @@
*/
public class CollectionFieldSet implements FieldValueObject
{
- Field field;
+ FieldValueSetter field;
List<CFS> values;
Class elementType;
Class<? extends Collection> collectionType;
- public CollectionFieldSet(Field field, List<XmlItem> items)
+ public CollectionFieldSet(FieldValueSetter field, List<XmlItem> items)
{
this.field = field;
this.values = new ArrayList<CFS>();
Added: modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/DirectFieldSetter.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/DirectFieldSetter.java
(rev 0)
+++
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/DirectFieldSetter.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -0,0 +1,83 @@
+package org.jboss.seam.xml.fieldset;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Type;
+
+public class DirectFieldSetter implements FieldValueSetter
+{
+
+ Field field;
+
+ public DirectFieldSetter(Field field)
+ {
+ this.field = field;
+ field.setAccessible(true);
+ }
+
+ public void set(Object instance, Object value) throws IllegalAccessException
+ {
+ field.set(instance, value);
+ }
+
+ public Type getGenericType()
+ {
+ return field.getGenericType();
+ }
+
+ public Class getType()
+ {
+ return field.getType();
+ }
+
+ public String getName()
+ {
+ return field.getName();
+ }
+
+ public Class getDeclaringClass()
+ {
+ return field.getDeclaringClass();
+ }
+
+ public void setBoolean(Object instance, boolean value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setBoolean(instance, value);
+ }
+
+ public void setByte(Object instance, byte value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setByte(instance, value);
+ }
+
+ public void setChar(Object instance, char value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setChar(instance, value);
+ }
+
+ public void setDouble(Object instance, double value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setDouble(instance, value);
+ }
+
+ public void setFloat(Object instance, float value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setFloat(instance, value);
+ }
+
+ public void setInt(Object instance, int value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setInt(instance, value);
+ }
+
+ public void setLong(Object instance, long value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setLong(instance, value);
+ }
+
+ public void setShort(Object instance, short value) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setShort(instance, value);
+ }
+
+}
Added: modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/FieldValueSetter.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/FieldValueSetter.java
(rev 0)
+++
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/FieldValueSetter.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -0,0 +1,35 @@
+package org.jboss.seam.xml.fieldset;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Type;
+
+public interface FieldValueSetter
+{
+
+ public void set(Object instance, Object value) throws IllegalAccessException,
InvocationTargetException;
+
+ public Class getType();
+
+ public Type getGenericType();
+
+ public Class getDeclaringClass();
+
+ public String getName();
+
+ public void setInt(Object instance, int value) throws IllegalAccessException,
InvocationTargetException;
+
+ public void setShort(Object instance, short value) throws IllegalAccessException,
InvocationTargetException;
+
+ public void setLong(Object instance, long value) throws IllegalAccessException,
InvocationTargetException;
+
+ public void setByte(Object instance, byte value) throws IllegalAccessException,
InvocationTargetException;
+
+ public void setChar(Object instance, char value) throws IllegalAccessException,
InvocationTargetException;
+
+ public void setDouble(Object instance, double value) throws IllegalAccessException,
InvocationTargetException;
+
+ public void setFloat(Object instance, float value) throws IllegalAccessException,
InvocationTargetException;
+
+ public void setBoolean(Object instance, boolean value) throws IllegalAccessException,
InvocationTargetException;
+
+}
Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MapFieldSet.java
===================================================================
---
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MapFieldSet.java 2010-01-16
06:25:13 UTC (rev 11967)
+++
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MapFieldSet.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -4,7 +4,6 @@
*/
package org.jboss.seam.xml.fieldset;
-import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
@@ -27,13 +26,13 @@
*/
public class MapFieldSet implements FieldValueObject
{
- Field field;
+ FieldValueSetter field;
List<MFS> values;
Class keyType;
Class valueType;
Class<? extends Map> collectionType;
- public MapFieldSet(Field field, List<EntryXmlItem> items)
+ public MapFieldSet(FieldValueSetter field, List<EntryXmlItem> items)
{
this.field = field;
this.values = new ArrayList<MFS>();
Added: modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MethodFieldSetter.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MethodFieldSetter.java
(rev 0)
+++
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/MethodFieldSetter.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -0,0 +1,81 @@
+package org.jboss.seam.xml.fieldset;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+
+public class MethodFieldSetter implements FieldValueSetter
+{
+ Method method;
+
+ public MethodFieldSetter(Method method)
+ {
+ this.method = method;
+ }
+
+ public void set(Object instance, Object value) throws IllegalAccessException,
InvocationTargetException
+ {
+ method.invoke(instance, value);
+ }
+
+ public Type getGenericType()
+ {
+ return method.getGenericParameterTypes()[0];
+ }
+
+ public String getName()
+ {
+ return method.getName();
+ }
+
+ public Class getDeclaringClass()
+ {
+ return method.getDeclaringClass();
+ }
+
+ public Class getType()
+ {
+ return method.getParameterTypes()[0];
+ }
+
+ public void setBoolean(Object instance, boolean value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+ public void setByte(Object instance, byte value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+ public void setChar(Object instance, char value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+ public void setDouble(Object instance, double value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+ public void setFloat(Object instance, float value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+ public void setInt(Object instance, int value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+ public void setLong(Object instance, long value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+ public void setShort(Object instance, short value) throws IllegalAccessException,
InvocationTargetException
+ {
+ set(instance, value);
+ }
+
+}
Modified:
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/SimpleFieldValue.java
===================================================================
---
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/SimpleFieldValue.java 2010-01-16
06:25:13 UTC (rev 11967)
+++
modules/xml/trunk/src/main/java/org/jboss/seam/xml/fieldset/SimpleFieldValue.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -4,21 +4,20 @@
*/
package org.jboss.seam.xml.fieldset;
-import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import org.jboss.seam.xml.util.XmlObjectConverter;
public class SimpleFieldValue implements FieldValueObject
{
- final Field field;
+ final FieldValueSetter field;
FS setter;
- public SimpleFieldValue(Class javaObject, final Field f, final String value)
+ public SimpleFieldValue(Class javaObject, final FieldValueSetter f, final String
value)
{
this.field = f;
- field.setAccessible(true);
Object fv = XmlObjectConverter.convert(f.getType(), value);
if (field.getType() == char.class)
@@ -27,7 +26,7 @@
final char val = (Character) fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.setChar(o, val);
}
@@ -38,7 +37,7 @@
final int val = (Integer) fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.setInt(o, val);
}
@@ -49,7 +48,7 @@
final short val = (Short) fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.setShort(o, val);
}
@@ -60,7 +59,7 @@
final long val = (Long) fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.setLong(o, val);
}
@@ -71,7 +70,7 @@
final byte val = (Byte) fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.setByte(o, val);
}
@@ -82,7 +81,7 @@
final double val = (Double) fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.setDouble(o, val);
}
@@ -93,18 +92,29 @@
final float val = (Float) fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.setFloat(o, val);
}
};
}
+ else if (field.getType() == boolean.class)
+ {
+ final boolean val = (Boolean) fv;
+ setter = new FS()
+ {
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
+ {
+ field.setBoolean(o, val);
+ }
+ };
+ }
else
{
final Object val = fv;
setter = new FS()
{
- public void set(Object o) throws IllegalAccessException
+ public void set(Object o) throws IllegalAccessException,
InvocationTargetException
{
field.set(o, val);
}
@@ -114,7 +124,7 @@
interface FS
{
- void set(Object o) throws IllegalAccessException;
+ void set(Object o) throws IllegalAccessException, InvocationTargetException;
}
public void setValue(Object instance)
Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java 2010-01-16
06:25:13 UTC (rev 11967)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -5,6 +5,7 @@
package org.jboss.seam.xml.model;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -14,24 +15,29 @@
import org.jboss.seam.xml.fieldset.ArrayFieldSet;
import org.jboss.seam.xml.fieldset.CollectionFieldSet;
+import org.jboss.seam.xml.fieldset.DirectFieldSetter;
import org.jboss.seam.xml.fieldset.FieldValueObject;
+import org.jboss.seam.xml.fieldset.FieldValueSetter;
import org.jboss.seam.xml.fieldset.MapFieldSet;
+import org.jboss.seam.xml.fieldset.MethodFieldSetter;
import org.jboss.seam.xml.fieldset.SimpleFieldValue;
public class FieldXmlItem extends AbstractXmlItem
{
+ FieldValueSetter fieldSetter;
+ FieldValueObject fieldValue;
Field field;
- FieldValueObject fieldValue;
HashSet<XmlItemType> allowed = new HashSet<XmlItemType>();
public FieldXmlItem(XmlItem parent, Field c, String innerText)
{
super(XmlItemType.FIELD, parent, parent.getJavaClass(), innerText, null);
this.field = c;
+ this.fieldSetter = getFieldValueSetter(c);
if (innerText != null && innerText.length() > 0)
{
- fieldValue = new SimpleFieldValue(parent.getJavaClass(), c, innerText);
+ fieldValue = new SimpleFieldValue(parent.getJavaClass(), fieldSetter,
innerText);
}
allowed.add(XmlItemType.ANNOTATION);
allowed.add(XmlItemType.VALUE);
@@ -78,7 +84,7 @@
}
if (!mapEntries.isEmpty())
{
- fieldValue = new MapFieldSet(field, mapEntries);
+ fieldValue = new MapFieldSet(fieldSetter, mapEntries);
}
}
else if (Collection.class.isAssignableFrom(field.getType()) ||
field.getType().isArray())
@@ -91,11 +97,11 @@
{
if (field.getType().isArray())
{
- fieldValue = new ArrayFieldSet(field, valueEntries);
+ fieldValue = new ArrayFieldSet(fieldSetter, valueEntries);
}
else
{
- fieldValue = new CollectionFieldSet(field, valueEntries);
+ fieldValue = new CollectionFieldSet(fieldSetter, valueEntries);
}
}
}
@@ -109,7 +115,7 @@
{
throw new RuntimeException("Non collection fields can only have a
single <value> element Field:" + field.getDeclaringClass().getName() +
'.' + field.getName());
}
- fieldValue = new SimpleFieldValue(parent.getJavaClass(), field,
valueEntries.get(0).getInnerText());
+ fieldValue = new SimpleFieldValue(parent.getJavaClass(), fieldSetter,
valueEntries.get(0).getInnerText());
}
}
return true;
@@ -120,4 +126,27 @@
return allowed;
}
+ FieldValueSetter getFieldValueSetter(Field field)
+ {
+ String fieldName = field.getName();
+ String methodName = "set" + Character.toUpperCase(fieldName.charAt(0)) +
fieldName.substring(1);
+ Method setter = null;
+ try
+ {
+ setter = field.getDeclaringClass().getMethod(methodName, field.getType());
+ }
+ catch (SecurityException e)
+ {
+
+ }
+ catch (NoSuchMethodException e)
+ {
+
+ }
+ if (setter != null)
+ {
+ return new MethodFieldSetter(setter);
+ }
+ return new DirectFieldSetter(field);
+ }
}
Modified:
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java
===================================================================
---
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java 2010-01-16
06:25:13 UTC (rev 11967)
+++
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -11,7 +11,7 @@
public class FieldValueBean
{
- public int ivalue = 20;
+ private int ivalue = 20;
public String stringValue;
@@ -29,4 +29,14 @@
public long lvalue;
+ public void setIvalue(int value)
+ {
+ this.ivalue = value + 1;
+ }
+
+ public int getIvalue()
+ {
+ return ivalue;
+ }
+
}
Modified:
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java
===================================================================
---
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java 2010-01-16
06:25:13 UTC (rev 11967)
+++
modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java 2010-01-16
22:13:59 UTC (rev 11968)
@@ -28,7 +28,7 @@
assert x.dvalue == 0;
assert x.enumValue == QualifierEnum.A;
assert x.fvalue == 0;
- assert x.ivalue == 10;
+ assert x.getIvalue() == 11;
assert x.lvalue == 23;
assert x.svalue == 4;