1

I would like to have a data class using AutoValue and one of the property is generic, wondering what I am doing wrong ?

public abstract class Data<T> {

    public static <T> Data createData(T value, Integer index) {
        return new AutoValue_Data<T>(value, index);
    }

    @NotNull
    public abstract T value();

    @NotNull
    public abstract Integer index();
}
Artur A
  • 257
  • 3
  • 20

2 Answers2

2

Your code looks like it should work, with one line that should get a warning fixed:

public static <T> Data createData(T value, Integer index) {

should be

public static <T> Data<T> createData(T value, Integer index) {
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

You forgot generic T after Data, your code should be:

public abstract class Data<T> {

    public static <T> Data <T> createData(T value, Integer index) {
        return new AutoValue_Data<T>(value, index);
    }

    @NotNull
    public abstract T value();

    @NotNull
    public abstract Integer index();
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197