2

Currently I have a class setup to be processed as an autobean:

public interface Asset extends Hit {
    String getGuid();
    String getHitType();
    Map<String,Serializable> getMetadata();
}

I tried using Object instead of Serializable:

Map<String,Object>  getMetadata()

but this seems to blow up when trying to access data (because it's not 'reified').

The Metadata map may contain other maps, strings, ints, etc. How do I retrieve data from an inner map of that metadata object?

Currently, if I call asset.getMetadata().get("title"); this returns a SerializableAutoBean and performing toString() or String.valueOf(obj) on that object returns the in memory object information and not the actually string value.

Can an AutoBean object be this dynamic, or do you specifically have to define every field?

Matt Traynham
  • 205
  • 3
  • 11
  • Well after asking, I found out that I could map my Metadata to String, Splittable. Wherein Splittable allows me to retrieve the data driving an autobean and I can recurse on it as much as I want. Not very clean though, especially when Lists are involved. – Matt Traynham Mar 02 '12 at 21:18
  • 2
    As your comment notes, `Splittable` works for this, and can even allow the serialized object to be encoded in another `AutoBeanFactory`. (More on this: http://stackoverflow.com/questions/9234601/parsing-json-objects-of-unknown-type-with-autobean-on-gwt) It does add an extra line of parsing, where you can specify the type of the to-be-decoded object, but in a loop that can even be an advantage - you can have several different types of objects in the same list, and use Splittable's methods to ask a given property for data on the type of each. – Colin Alworth Mar 02 '12 at 21:47

1 Answers1

0

AutoBeans aren't "dynamic" in the Java generics or RTTI sense.

In GWT, all types have to be known at compile time for anything which is auto-generated (which includes AutoBeans). This places restrictions on your designs which don't allow you to take full advantage of Java's language features (specifically, generics and other RTTI features). So, AutoBeans are not dynamic in the RTTI or Java generic sense. However, AutoBeans are simply a low-level way of wrapping your data, and you still have access to the data by using Splittables!

As stated in the previous comments, you can use Splittables for the parts of your JSON object whose type is not known at serialization/decode time. Sure, it would be nice to have everything happen at once, but nothing is stopping you from performing some post-processing on your data objects to get them into your desired state.

A really good way for someone to "Grok" what is going on with AutoBeans (and anything else which is autogenerated) is to look at the resulting generated code. The default location for maven is: ${project.build.directory}/.generated.

If you look in there after you've compiled, you should find the code which the GWT compiler produces for your AutoBeans.

Jonathan
  • 705
  • 5
  • 16