JSONSplittable#setSize() fails if new size is bigger than existing size

45 views
Skip to first unread message

Andreas Kohn

unread,
Feb 5, 2014, 6:14:17 AM2/5/14
to google-we...@googlegroups.com
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

Thomas Broyer

unread,
Feb 5, 2014, 8:33:01 AM2/5/14
to google-we...@googlegroups.com
Splittable is supposed to represent any JavaScript value (except undefined, which is seen the same as null), and Array.length in JS is settable to any value (note that in some (edge?) cases, the length property is not set to the exact value assigned to it), extending the array with undefined items.
So I'd say it's a bug, and setSize should behave as you describe (in JsonSplittable; in JsoSplittable it just sets the length property of the underlying JS array)

Andreas Kohn

unread,
Feb 7, 2014, 10:14:28 AM2/7/14
to google-we...@googlegroups.com


--
You received this message because you are subscribed to a topic in the Google Groups "Google Web Toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/YE5HRc3x-H8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages