henriqueolliveira / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

GenericData should have a generic type parameter #177

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?

http://javadoc.google-api-java-client.googlecode.com/hg/1.3.1-alpha/com/google/a
pi/client/util/GenericData.html

Java environments (e.g. Java 6, Android 2.3, App Engine 1.4.2, or All)?

All

Please describe the feature requested.

Currently GenericData extends Map<String, Object>.  Instead it should have a 
generic type parameter T so GenericData<T> extends Map<String, T>.  This can be 
very useful in case where the JSON values all match a specific type.  For 
example, the links in a Buzz entry could be represented as:

public class Entry {
  @Key GenericData<List<Link>> links;
}

Original issue reported on code.google.com by yan...@google.com on 13 Apr 2011 at 1:33

GoogleCodeExporter commented 9 years ago
In hindsight, this is a bad idea.  The only advantage of GenericData over Map 
is that it allows you to mix reflected fields with an arbitrary map.  But if we 
add a type parameter, there is no compile-time checking of field types matches 
the type parameter.  For example, even though this is clearly wrong, it would 
compile without warning and likely cause problems at runtime:

public class A extends GenericData<String> {
  @Key Integer num;
}

So really what we want developers to do for the above case:

public class Entry {
  @Key Map<String, List<Link>> links;
}

This should already be supported now.

Original comment by yan...@google.com on 13 Apr 2011 at 12:18