sitebricks
sitebricks copied to clipboard
List conversion does not honor specified type
Conversion succeeds, but objects stored in ids
list are String instances "1", "2", "3" not 1L, 2L, 3L.
private Form {
List<Long> ids = Lists.newArrayList();
.. setters, getters
}
<form>
<input type="text" name="ids" value="1" />
<input type="text" name="ids" value="2" />
<input type="text" name="ids" value="3" />
</form>
There is a type cast exception when use ArrayList Long
When I replace List<Long>
with ArrayList<Long>
, no exception gets raised
I'm using sitebricks 0.8.5
I'm getting ClassCastException
only when I explicitly iterate over the posted collection (String cannot be Long), however when there is no usage of the ids
collection inside the Form instance, only in the the template, no exception is thrown.
for (Long ex : ids) {
System.out.println(ex);
}
I think following code from mvel org.mvel2.PropertyAccessor is the exception source:
if (curr instanceof Map) {
//noinspection unchecked
((Map) curr).put(eval(ex, this.ctx, this.variableFactory), value);
}
else if (curr instanceof List) {
//noinspection unchecked
((List) curr).set(eval(ex, this.ctx, this.variableFactory, Integer.class), value);
}
else if (hasPropertyHandler(curr.getClass())) {
getPropertyHandler(curr.getClass()).setProperty(ex, ctx, variableFactory, value);
}
else if (curr.getClass().isArray()) {
Array.set(curr, eval(ex, this.ctx, this.variableFactory, Integer.class), convert(value, getBaseComponentType(curr.getClass())));
}
But I'm not sure yet
I just add a test to PropertyAccessTests in MVEL
public class Datas {
private List<Long> ids = new ArrayList<Long>();
public void setIds(List<Long> ids) {
this.ids = ids;
}
public List<Long> getIds() {
return this.ids;
}
}
public void testBindingConvert() {
Datas datas = new Datas();
List values = new ArrayList();
values.add("1");
values.add("2");
values.add("3");
MVEL.setProperty(datas, "ids", values);
assertEquals(new Long(1),datas.getIds().get(0));
}
and got this:
testBindingConvert(org.mvel2.tests.core.PropertyAccessTests): expected:<1> but was:<1>
Have you tried registering a converter? Is this issue still happening?