Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Generics and AdapterFactory #79

Closed
BraisGabin opened this issue Oct 26, 2016 · 1 comment
Closed

Problem with Generics and AdapterFactory #79

BraisGabin opened this issue Oct 26, 2016 · 1 comment
Labels
Milestone

Comments

@BraisGabin
Copy link

If I add the example with generics to my code:

@AutoValue
public abstract class Foo<A, B, C> {
    // properties...

    public static <A, B, C> TypeAdapter<Foo<A, B, C>> typeAdapter(Gson gson,
                                                                  TypeToken<? extends Foo<A, B, C>> typeToken) {
        return new AutoValue_Foo.GsonTypeAdapter(gson, typeToken);
    }
}

I have this error on my AdapterFactory:

/path/app/build/generated/source/apt/staging/debug/package/AutoValueGson_MyAdapterFactory.java
Error:(33, 78) error: cannot find symbol class A
Error:(33, 81) error: cannot find symbol class B
Error:(33, 84) error: cannot find symbol class C

To fix it I must change TypeToken<? extends Foo<A, B, C>> typeToken to TypeToken<? extends Foo> typeToken:

@AutoValue
public abstract class Foo<A, B, C> {
    // properties...

    public static <A, B, C> TypeAdapter<Foo<A, B, C>> typeAdapter(Gson gson,
                                                                  TypeToken<? extends Foo> typeToken) {
        return new AutoValue_Foo.GsonTypeAdapter(gson, typeToken);
    }
}

What's the problem? The example? Am I missing anything? Is my "fix" correct?

@ZacSweers
Copy link
Collaborator

Just documenting for clarity after taking a peek, the issue is in the generated factory itself, not in the actual class definition.

public final class AutoValueGson_SampleAdapterFactory extends SampleAdapterFactory {
  @Override
  @SuppressWarnings("unchecked")
  public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<?> rawType = type.getRawType();
    if (Foo.class.isAssignableFrom(rawType)) {
      return (TypeAdapter<T>) Foo.typeAdapter(gson, (TypeToken<? extends Foo<A, B, C>>) type);
    } else {
      return null;
    }
  }
}

ZacSweers added a commit that referenced this issue Jan 13, 2019
…handling

Resolves #79 and simplifies the way generics are handled. Before this we were always going through and trying to pass around or unpack a typetoken and mess with the generics, only to raw them out to `Type` under the hood anyway. This basically matches how we do it in `auto-value-moshi` and switches to just accept that `Type[]` directly and also handle that in the generated factory.
ZacSweers added a commit that referenced this issue Jan 13, 2019
…handling (#193)

Simplifies the way generics are handled. Before this we were always going through and trying to pass around or unpack a typetoken and mess with the generics, only to raw them out to `Type` under the hood anyway. This basically matches how we do it in `auto-value-moshi` and switches to just accept that `Type[]` directly and also handle that in the generated factory.

Resolves #79 
Resolves #115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants