简体   繁体   中英

Java Type Erasure, Generic Lists with Unchecked Assignment

Currently I'm writing a program where I have the following statement.

List<BaseballStatistic> q = BaseballStatistic.FIND.where().eq("teamID", "CHN").query();

Here, it complains

Unchecked assignment: 'java.util.List' to 'java.util.List'. Reason: 'BaseballStatistic.FIND.where().eq("teamID", "CHN")' has raw type, so result of query is erased more...

I have an interface which looks like this

public interface Query<T> {
  ...
  List<T> execute();
}

then an abstract class that implements this interface

public abstract class AbstractQuery<T> implements Query<T> {
  Statement _statement = null;
  String _tableName;
  List<Clause> _clauses;
  Class<T> _type;

AbstractQuery(Class<T> type) {
  _type = type;
  _clauses = new ArrayList<>();
  _tableName = type.getAnnotation(Table.class).name();
}

...
public abstract List<T> execute();
}

and finally a concrete implementation:

public class SimpleQuery<T> extends AbstractQuery<T> {

public SimpleQuery(Class<T> type) {
  super(type);
}

which houses the following .query function which looks like:

@Override
public List<T> execute() {
try {
  JSONObject jsonObject = Peanut.getClient().listStatistics(buildQuery());

  if (jsonObject == null || !jsonObject.has("results")) {
    return Collections.emptyList();
  }

  JSONArray columnNames = jsonObject.getJSONArray("columns");

  Map<String, Integer> columnNameMap = new HashMap<>();
  for (int i = 0; i < columnNames.length(); i++) {
    columnNameMap.put((String) columnNames.get(i), i);
  }

  JSONArray results = jsonObject.getJSONArray("results");

  List<T> ts = new ArrayList<>();
  for (int i = 0; i < results.length(); i++) {
    JSONArray result = results.getJSONArray(i);
    T t = _type.newInstance();
    for (Field field : ObjectUtils.getFieldsUpTo(t.getClass(), PinotModel.class)) {
      if (field.getAnnotation(Column.class) == null) {
        continue;
      }

      Object obj = ObjectUtils.getDefaultValue(field.getType());
      String columnName = field.getAnnotation(Column.class).name();

      if (columnNameMap.containsKey(columnName)) {
        int idx = columnNameMap.get(columnName);
        field.setAccessible(true);
        field.set(t, ObjectUtils.convertObject(obj, result.get(idx)));
      }
    }
    ts.add(t);
  }
  return ts;
} catch (Exception e) {
  // TODO: Throw Peanut specific error.
  Peanut.LOG.error(e);
  return Collections.emptyList();
}
}

It seems like here, at compilation, the returned list has lost it's type leading to the warning. If I change the original variable declaration to List the warning will leave, which makes sense.

Is there anyway around this or is there a larger fundamental issue at play?

EDIT:

Query Function that calls execute is here

 public List<T> query() {
   return _query.execute();
 }

And the relationship between SimpleQuery and BaseballStatistic.Find is as follows.

@Table(name = "baseballStats")
public class BaseballStatistic extends PinotModel {

  public static final Find FIND = new Find<BaseballStatistic (BaseballStatistic.class) { };
...

and PinotModel looks like

public class PinotModel {
  public static class Find<T> {
    private final Class<T> type;

  protected Find(Class<T> type) {
    this.type = type;
  }

  public Query select(String... s) {
    return new SimpleQuery<T>(type).select(s);
  }

  public Clause where() {
    return new SimpleQuery<T>(type).where();
  }

  public Clause limit(Integer n) {
    return new SimpleQuery<T>(type).limit(n);
  }

  public Clause top(Integer n) {
    return new SimpleQuery<T>(type).top(n);
  }

  public Clause orderBy(String columnName, Order o) {
    return new SimpleQuery<T>(type).orderBy(columnName, o);
  }

  public String tableName() {
    return new SimpleQuery<T>(type).getTableName();
  }
 }
}

There are 2 places that you're missing generic type parameters.

  1. BaseballStatistic.FIND :

    public static final Find<BaseballStatistic> FIND = new Find<BaseballStatistic> (BaseballStatistic.class) { };

  2. PinotModel.select :

    public Query<T> select(String... s) { return new SimpleQuery<T>(type).select(s); }

You're also missing type parameters on PinotModel.where() . Clause would also need a type parameter, including on the AbstractQuery._clauses field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM