Hi,
I was working today with autobeans in a JSON format that is partially fixed, and partially open. The fixed parts we handle with regular autobean get/set methods, the open parts we handle with a single get/set pair that produces a Map<String, Splittable>.
One of the splittables I needed was an array of strings, and probably because of premature optimization I used #setSize() before filling the array:
----
List<String> values = (coming from the caller)
Splittable valuesSplittable = StringQuoter.createIndexed();
valuesSplittable.setSize(values.size()); /// here.
for (int i = 0; i < values.size(); i++) {
Splittable valueSplittable = StringQuoter.create(values.get(i));
valueSplittable.assign(valuesSplittable, i);
}
----
This leads to an exception:
java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy18.setValues(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.web.bindery.autobean.vm.impl.ShimHandler.invoke(ShimHandler.java:85)
at com.sun.proxy.$Proxy18.setValues(Unknown Source)
at com.company.ClassTest.testSetValues(Unknown Source)
...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.web.bindery.autobean.vm.impl.BeanMethod$5.invoke(BeanMethod.java:155)
at com.google.web.bindery.autobean.vm.impl.SimpleBeanHandler.invoke(SimpleBeanHandler.java:43)
... 32 more
Caused by: java.lang.RuntimeException: org.json.JSONException: JSONArray[0] not found.
at com.google.web.bindery.autobean.vm.impl.JsonSplittable.setSize(JsonSplittable.java:278)
at com.company.Class.setValues(Unknown Source)
... 38 more
Caused by: org.json.JSONException: JSONArray[0] not found.
at org.json.JSONArray.get(JSONArray.java:234)
at com.google.web.bindery.autobean.vm.impl.JsonSplittable.setSize(JsonSplittable.java:276)
... 39 more
It seems that #setSize() works fine when reducing the size of the array, but not when trying to increase it: #setSize() assumes that all indices from 0..newSize-1 are valid.
1. Is this intentional? Or should the #setSize() method only copy to Min(newSize, oldSize), and fill up the array with 'null' if newSize > oldSize, so that the assumption #size() returns the size set with #setSize() is retained?
2. Should a note be added to the javadoc to better specify the #setSize() method behavior to avoid such issues?
Regards,
Andreas