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

Generated TypeAdapterFactory and null safety #50

Closed
muddin1 opened this issue Jun 10, 2016 · 8 comments
Closed

Generated TypeAdapterFactory and null safety #50

muddin1 opened this issue Jun 10, 2016 · 8 comments

Comments

@muddin1
Copy link

muddin1 commented Jun 10, 2016

As of 0.2.5, My custom AutoValueTypeAdapterFactory would call nullSafe() on every type adapter I return in the create() method. Refer #28 (comment).

Class<? super T> rawType = type.getRawType();
if (rawType.equals(User.class)) {
    return (TypeAdapter<T>) User.typeAdapter(gson).nullSafe();
}

Beginning from 0.3.0, the generated AutoValueGsonTypeAdapterFactory does not call nullSafe() by default, which is the appropriate way for sure.

So, how'd I go about ensuring null-safety when using the generated TypeAdapterFactory.

@rharter
Copy link
Owner

rharter commented Jun 10, 2016

You could submit a PR if you've got the code. Otherwise I'll have to add that.

@muddin1
Copy link
Author

muddin1 commented Jun 10, 2016

Are you saying that we should attach nullSafe() to every AutoValue typeAdapter mentioned in the generated Factory i.e. in the annotation processor?

Also, there's this PR #23 that presumably eliminates the need to use nullSafe() completely.

@jachenry
Copy link

Below is a quick solution for anyone else that hits this before #23 is completed.

@GsonTypeAdapterFactory
public abstract class AutoValueAdapterFactory implements TypeAdapterFactory {

  public static TypeAdapterFactory create() {
    final TypeAdapterFactory factory = new AutoValueGson_AutoValueAdapterFactory();

    return new TypeAdapterFactory() {
      @Override
      public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        TypeAdapter<T> typeAdapter = factory.create(gson, type);
        return typeAdapter != null ? typeAdapter.nullSafe() : null;
      }
    };
  }
}

@muddin1
Copy link
Author

muddin1 commented Aug 23, 2016

@jachenry Thanks, I've been doing the same.

@jjimenez0611
Copy link

Hi @jachenry

Please can you explain to me with more details..

I have my factory like this:

public class AutoValueGsonTyeAdapterFactory implements TypeAdapterFactory {

public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<? super T> rawType = type.getRawType();

    if (rawType.equals(MGuest.class)) {
        return (TypeAdapter<T>) MGuest.typeAdapter(gson);
    }

    if(rawType.equals(MTicket.class)){
        return (TypeAdapter<T>) MTicket.typeAdapter(gson);
    }

    return null;
}

}

Then I have your abstract class that contains my class

@GsonTypeAdapterFactory
public abstract class AutoValueAdapterFactory implements TypeAdapterFactory {

public static TypeAdapterFactory create() {
    final TypeAdapterFactory factory = new **AutoValueGsonTyeAdapterFactory();**

    return new TypeAdapterFactory() {
        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            TypeAdapter<T> typeAdapter = factory.create(gson, type);
            return typeAdapter != null ? typeAdapter.nullSafe() : null;
        }
    };
}

}

And finally I need to call this classes in my GsonConverterFactory, but in this point I really don't now how to call the abstract class...

public static GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(
new GsonBuilder()
.registerTypeAdapterFactory(MyTypeAdapterFactory)
.create());

Can you help me in this last step?

@jachenry
Copy link

@jjimenez0611 You'll want to call the static create() method on AutoValueAdapterFactory.

@GsonTypeAdapterFactory
public abstract class AutoValueAdapterFactory implements TypeAdapterFactory {

public static TypeAdapterFactory create() {
    final TypeAdapterFactory factory = new AutoValueGson_AutoValueAdapterFactory();

    return new TypeAdapterFactory() {
        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            TypeAdapter<T> typeAdapter = factory.create(gson, type);
            return typeAdapter != null ? typeAdapter.nullSafe() : null;
        }
    };
}
new GsonBuilder()
    .registerTypeAdapterFactory(AutoValueAdapterFactory.create())
    .create()

@jjimenez0611
Copy link

@jachenry, thanks I really appreciate your help, now I understand.

ansman pushed a commit to ansman/auto-value-gson that referenced this issue Oct 24, 2016
ansman added a commit to ansman/auto-value-gson that referenced this issue Oct 24, 2016
ansman added a commit to ansman/auto-value-gson that referenced this issue Oct 24, 2016
rharter pushed a commit that referenced this issue Oct 25, 2016
@ZacSweers
Copy link
Collaborator

Closed by #78

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

No branches or pull requests

5 participants