rharter / auto-value-gson

AutoValue Extension to add Gson De/Serializer support
Apache License 2.0
607 stars 103 forks source link

Fix factory method with generics #227

Closed bryanstern closed 5 years ago

bryanstern commented 5 years ago

Fixes #226.

Invalid java code was being generated when trying to instantiate a builder with generics via its factory method inside the TypeAdapter#read.

The fix is to use the builder's type rather than class when declaring the builder field inside of TypeAdapter#read.

I've also updated GenericsExample to show that it works and to serve as a test. The read method generated now looks like.

public GenericsExample<A, B, C> read(JsonReader jsonReader) throws IOException {
  if (jsonReader.peek() == JsonToken.NULL) {
    jsonReader.nextNull();
    return null;
   }
   jsonReader.beginObject();
   GenericsExample.Builder<A, B, C> builder = GenericsExample.builder();
   ...
}